Game class revamp

This commit is contained in:
Marcin Kostrzewski 2020-05-16 11:13:36 +02:00
parent 15babde728
commit 6ec1c0b2d2
2 changed files with 79 additions and 29 deletions

View File

@ -1,7 +1,7 @@
{ {
"window": { "window": {
"width": 1280, "width": 1920,
"height": 720, "height": 1080,
"name": "Adventure Survival" "name": "Adventure Survival"
} }
} }

View File

@ -4,6 +4,7 @@ from pathlib import Path
import pygame import pygame
from src.AI.Affinities import Affinities
from src.entities.Player import Player from src.entities.Player import Player
from src.game.EventManager import EventManager from src.game.EventManager import EventManager
from src.game.Map import Map from src.game.Map import Map
@ -13,17 +14,81 @@ from src.game.Timer import Timer
# Main Game class # Main Game class
class Game: class Game:
def __init__(self, filesPath): def __init__(self, filesPath, gamemode="test"):
""" """
Game script initialization. Loads all files, creates screen, map, tiles, entities and a player. Game script initialization. Loads all files, creates screen, map, tiles, entities and a player.
Starts the main game loop at the end. Starts the main game loop at the end.
:param filesPath: Absolute path to the root of the gamefiles :param filesPath: Absolute path to the root of the gamefiles
:param gamemode: Mode to run. Currently, there's only test gamemode.
""" """
self.running = True
print("Loading configuration...", end=" ")
# Load config params from file # If set to true, gameloop will run
self.running = False
# Config dict
self.config = None
# Container for all sprites
self.spritesList = None
# PyGame timer
self.pgTimer = None
# Custom in-game timer
self.ingameTimer = None
# Screen object
self.screen = None
# Map object
self.map = None
# The player
self.player = None
# EventManager object
self.eventManager = None
self.loadConfig(filesPath)
self.initializePygame()
# Runnable selection
if gamemode == "test":
self.testRun(filesPath)
# Start game loop
self.mainLoop()
def initializePygame(self):
"""
Initializes all pygame members.
"""
print("Initializing pygame...", end=" ")
pygame.init()
self.spritesList = pygame.sprite.Group()
print("OK")
# PyGame timer - precise timer, counts milliseconds every frame
self.pgTimer = pygame.time.Clock()
def initializeMap(self, filesPath):
"""
Initializes the map object.
:param filesPath:
"""
mapFile = None
try:
mapFile = Path(str(filesPath) + "/data/mapdata/")
except IOError:
print("Could not load map data. Exiting...")
exit(1)
self.map = Map(path.join(mapFile, 'map.txt'), self.screen)
def loadConfig(self, filesPath):
"""
Loads the configuration file.
:param filesPath: Absolute path to game folder
"""
print("Loading configuration...", end=" ")
try: try:
configFolder = Path(str(filesPath) + "/data/config/") configFolder = Path(str(filesPath) + "/data/config/")
configFile = configFolder / "mainConfig.json" configFile = configFolder / "mainConfig.json"
@ -35,11 +100,13 @@ class Game:
print("Error reading configuration file. Exiting...") print("Error reading configuration file. Exiting...")
exit(1) exit(1)
print("Initializing pygame...", end=" ") def testRun(self, filesPath):
pygame.init() """
self.spritesList = pygame.sprite.Group() Run the game in test mode. In this mode, you can manually move the player and test the environment.
print("OK") :param filesPath: Absolute path to root game directory
"""
self.running = True
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
# Vertical rotation is unsupported due to UI layout # Vertical rotation is unsupported due to UI layout
@ -48,8 +115,6 @@ class Game:
exit(1) exit(1)
# Initialize timers # Initialize timers
# PyGame timer - precise timer, counts milliseconds every frame
self.pgTimer = pygame.time.Clock()
# Virtual timer to track in-game time # Virtual timer to track in-game time
self.ingameTimer = Timer() self.ingameTimer = Timer()
self.ingameTimer.startClock() self.ingameTimer.startClock()
@ -58,28 +123,13 @@ class Game:
self.screen = Screen(self, self.config["window"]) self.screen = Screen(self, self.config["window"])
print("OK") print("OK")
self.moveTimer = 0 self.initializeMap(filesPath)
self.moveTime = 100
# Load map data from file
mapFile = None
try:
mapFile = Path(str(filesPath) + "/data/mapdata/")
except IOError:
print("Could not load map data. Exiting...")
exit(1)
# Initialize map object
self.map = Map(path.join(mapFile, 'map.txt'), self.screen)
# Initialize the player # Initialize the player
self.player = Player((6, 2), self.map.tileSize) self.player = Player((6, 2), self.map.tileSize, Affinities(0.3, 0.6, 0.1))
self.map.addEntity(self.player, DONTADD=True) self.map.addEntity(self.player, DONTADD=True)
self.eventManager = EventManager(self, self.player) self.eventManager = EventManager(self, self.player)
# Start game loop
self.mainLoop()
def mainLoop(self): def mainLoop(self):
""" """
Continuously running loop. Calls events, updates and draws everything. Continuously running loop. Calls events, updates and draws everything.