Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 8f44b99515282e043d8eacec80b040115880825c
parent f2b9a44e4d23fb336b08918b25af09df3d1038a3
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 14 Oct 2020 19:47:50 -0500

enumify game state vals

Diffstat:
Msrc/constants.py | 6+++++-
Msrc/game.py | 45+++++++++++++++++++++------------------------
Msrc/menu.py | 6+++---
3 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/src/constants.py b/src/constants.py @@ -1,4 +1,4 @@ -import os +import os, enum ################ # constants.py # @@ -33,3 +33,7 @@ JSON_PATH = os.path.join(DATA_PATH, "json") ENTITY_JSON_PATH = os.path.join(JSON_PATH, "ents") STATUS_JSON_PATH = os.path.join(JSON_PATH, "stats") MENU_JSON_PATH = os.path.join(JSON_PATH, "menus") + +# Enums +STATE_MODES = enum.Enum('STATE_MODES', 'Main_Menu_Mode Battle_Mode') +CTRL_MODES = enum.Enum('CTRL_MODES', 'Main_Menu_Normal Turn_Normal Turn_Select_Move') diff --git a/src/game.py b/src/game.py @@ -1,6 +1,6 @@ import pygame from . import images, board, vgo, unit, menu -from .constants import SCREEN_WIDTH, SCREEN_HEIGHT, FRAMERATE, TILE_WIDTH, TILE_HEIGHT +from .constants import * ########### # game.py # @@ -36,9 +36,8 @@ class Game(object): self.frame_clock = pygame.time.Clock() # Mode management values - # TODO: enum - self.state_mode = "Main_Menu_Mode" - self.control_mode = "Main_Menu_Normal" + self.state_mode = STATE_MODES.Main_Menu_Mode + self.control_mode = CTRL_MODES.Main_Menu_Normal # Subsystems self.interface = GameInterface(self) @@ -55,7 +54,7 @@ class Game(object): self.sheet_manager.load_animations_from_json("anims.json") # Switch to game control - self.switch_mode("Main_Menu_Mode") + self.switch_mode(STATE_MODES.Main_Menu_Mode) def switch_mode(self, new_mode): """ @@ -67,12 +66,12 @@ class Game(object): """ self.state_mode = new_mode - if new_mode == "Main_Menu_Mode": - self.control_mode = "Main_Menu_Normal" + if new_mode == STATE_MODES.Main_Menu_Mode: + self.control_mode = CTRL_MODES.Main_Menu_Normal self.menu_manager.load_menu_from_file("mainmenu.json") self.menu_manager.switch_to_menu("mainmenu.json") - elif new_mode == "Battle_Mode": - self.control_mode = "Turn_Normal" + elif new_mode == STATE_MODES.Battle_Mode: + self.control_mode = CTRL_MODES.Turn_Normal # TODO: Obv this must be made more generic. Currently only # sources from the one map file. self.board_manager.load_board_from_file("testmap1.tmx") @@ -100,9 +99,9 @@ class Game(object): # TODO: This is WIP and will change self.interface.update_interface() - if self.state_mode == "Main_Menu_Mode": + if self.state_mode == STATE_MODES.Main_Menu_Mode: self.menu_manager.update_current_menu(self.screen) - elif self.state_mode == "Battle_Mode": + elif self.state_mode == STATE_MODES.Battle_Mode: self.board_manager.update_board(self.screen) self.entity_manager.update_entities(self.screen) # NOTE: MenuManager should not be an overlay for the @@ -171,9 +170,6 @@ class GameInterface(GameSubsystem): # Parent init super().__init__(game) - # Saved values - self.control_mode = "Turn_Normal" # TODO: This should be an enum - def handle_events(self, events): """ Handle any kind of PyGame event and react appropriately. @@ -192,9 +188,10 @@ class GameInterface(GameSubsystem): """ React to a key being pressed. """ - if self.control_mode == "Turn_Select_Move": + # TODO: This is irregular compared to mouseclick. Desireable??? + if self.game.control_mode == CTRL_MODES.Turn_Select_Move: if event.key == pygame.K_q: - self.control_mode = "Turn_Normal" + self.game.control_mode = CTRL_MODES.Turn_Normal # TODO: Should this really be done here??? self.game.board_manager.load_overlay() @@ -209,16 +206,16 @@ class GameInterface(GameSubsystem): React to a mousebutton being clicked. """ if event.button == 1: - if self.game.state_mode == "Main_Menu_Mode": - if self.game.control_mode == "Main_Menu_Normal": + if self.game.state_mode == STATE_MODES.Main_Menu_Mode: + if self.game.control_mode == CTRL_MODES.Main_Menu_Normal: self.game.menu_manager.trigger_button_at_pos(pygame.mouse.get_pos()) - elif self.game.state_mode == "Battle_Mode": - if self.game.control_mode == "Turn_Normal": + elif self.game.state_mode == STATE_MODES.Battle_Mode: + if self.game.control_mode == CTRL_MODES.Turn_Normal: if self.game.entity_manager.select_entities_with_tile_cursor(self.game.board_manager.get_tile_at_position(pygame.mouse.get_pos())): - self.game.control_mode = "Turn_Select_Move" - elif self.game.control_mode == "Turn_Select_Move": + self.game.control_mode = CTRL_MODES.Turn_Select_Move + elif self.game.control_mode == CTRL_MODES.Turn_Select_Move: if self.game.entity_manager.set_entity_move_to_tile_pos(self.game.entity_manager.selected_entity, self.game.board_manager.get_overlay_move_vgo_at_pos(pygame.mouse.get_pos())): - self.game.control_mode = "Turn_Normal" + self.game.control_mode = CTRL_MODES.Turn_Normal # TODO: Should this really be done here??? self.game.board_manager.load_overlay() @@ -235,5 +232,5 @@ class GameInterface(GameSubsystem): e.g. an Entity, and is logic-only. """ # Update cursor position - if self.game.state_mode == "Battle_Mode": + if self.game.state_mode == STATE_MODES.Battle_Mode: self.game.entity_manager.position_tile_cursor(self.game.board_manager.get_tile_at_position(pygame.mouse.get_pos())) diff --git a/src/menu.py b/src/menu.py @@ -1,6 +1,6 @@ import pygame, json, os from . import images, vgo, manager -from .constants import IMAGE_PATH, MENU_JSON_PATH +from .constants import * # TODO: EntityManager should be used to manage menu entities as well, eventually. @@ -54,9 +54,9 @@ class MenuManager(manager.Manager): """ for e in events: if e == "me_quit": # Quit the game - self.game.on = False + self.game.quit_game() elif e == "me_switch_mode": # Switch to another game mode - self.game.switch_mode(events[e]["mode"]) + self.game.switch_mode(STATE_MODES[events[e]["mode"]]) def trigger_button_at_pos(self, pos): """