commit 36c3c710d55d8bb4af3dfdff3bb2b0b36e7d86aa
parent 0f9dcfcb095d750fe5cb77e13990011fe934c46b
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Mon, 7 Jun 2021 19:24:24 -0500
added menus
Diffstat:
M | src/game.py | | | 3 | ++- |
A | src/menu.py | | | 155 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 157 insertions(+), 1 deletion(-)
diff --git a/src/game.py b/src/game.py
@@ -1,5 +1,5 @@
import pygame
-from . import interface, bus, camera, sound, images, board
+from . import interface, bus, camera, sound, images, board, menu
from .envvars import *
###########
@@ -40,6 +40,7 @@ class Game(object):
# Managers
self.board_manager = board.BoardManager(self, "board_manager", self.manager_bus, self.subsystem_bus)
+ self.menu_manager = menu.MenuManager(self, "menu_manager", self.manager_bus, self.subsystem_bus)
# Setup (This is WIP)
#self.sheet_system.load_animations_from_json("anims.json")
diff --git a/src/menu.py b/src/menu.py
@@ -0,0 +1,155 @@
+import pygame, json, os
+from . import images, manager, entity
+from .envvars import *
+
+###########
+# menu.py #
+###########
+
+# This file contains:
+# 1. The MenuManager class, which handles Menu objects and switches between them
+# 2. The Menu class, which consists of a background, 0 or more decorative Entities, and an ordered series of Button objects
+# 3. Various Menu-specific entity subclasses
+
+##################################
+# Section 1 - MenuManager Object #
+##################################
+
+class MenuManager(manager.Manager):
+ """
+ Manager for Menu objects. Mostly responsible for switching
+ between Menus and for updating their contents. The Menu
+ object itself controls sprite groups, but MenuManager will
+ access those groups to draw them.
+ """
+
+ def __init__(self, game, name, bus, system_bus):
+
+ super().__init__(game, name, bus, system_bus)
+
+ # Fix properties
+ self.effectual = True
+
+ # Important values
+ self.current_menu = None
+
+ def load_menu_from_file(self, menufile):
+ """
+ Load a Menu object from a provided menu definition
+ file.
+ """
+ self.current_menu = Menu(self, menufile)
+
+ def trigger_button_at_pos(self, pos):
+ """
+ Trigger a button if pos is within that button's
+ rect.
+ """
+ if self.current_menu != None:
+ for b in self.current_menu.button_group:
+ if b.rect.collidepoint(pos):
+ self.trigger_effects(b.effects)
+
+ def effect_extension(self, effect):
+ """
+ MenuManager-only effects for trigger_effects.
+ """
+ pass
+
+ def expose(self):
+ """
+ Expose menu info to the ManagerBus.
+ """
+ pass
+
+ def update_managed(self, surface = None):
+ """
+ Update the current Menu object and all of its
+ sub-entities.
+ """
+ if surface != None and self.current_menu != None:
+ self.current_menu.update_menu(surface)
+
+###########################
+# Section 2 - Menu Object #
+###########################
+
+class Menu(object):
+ """
+ Class that represents the menus that are interacted with in
+ menu mode. Contains a background sprite object as well as a
+ group buttons.
+ """
+
+ def __init__(self, manager, menufile):
+
+ # Saved values
+ self.manager = manager
+ self.menu_file = menufile
+ self.background = None
+ self.button_group = pygame.sprite.LayeredDirty()
+ self.entities_group = pygame.sprite.LayeredDirty()
+ with open(os.path.join(MENU_JSON_PATH, menufile)) as df: self.definition = json.load(df)
+
+ # Load/setup menu
+ self.load_menu()
+
+ def load_menu(self):
+ """
+ Load Menu from definition, if it exists. This is called in
+ the __init__ of the Menu object, and should not be called
+ anywhere else.
+ """
+ if self.definition != None:
+
+ # Load background, if any
+ if self.definition["bg_sheet"] != None:
+ self.background = entity.Entity(self.manager.bus.fetch("sheet_manager", "sheets")[self.definition["bg_sheet"]], tuple(self.definition["bg_sprite"]), None, False)
+
+ # Load buttons, if any
+ for b in self.definition["buttons"]:
+ nmb = MenuButton(self.manager.bus.fetch("sheet_manager", "sheets")[self.definition["buttons"][b]["sheet"]],
+ tuple(self.definition["buttons"][b]["sprite"]), None, False, self.definition["buttons"][b]["effects"])
+ nmb.rect.topleft = tuple(self.definition["buttons"][b]["pos"])
+ self.button_group.add(nmb)
+
+ # TODO: add generic ent loader and some way to enumerate sublayouts.
+
+ def update_menu(self, surface = None):
+ """
+ Draw and update menu elements.
+ """
+
+ if surface != None:
+ # Draw background first, if it exists
+ if self.background != None:
+ self.background.update(surface)
+ # Next, update buttons
+ self.button_group.update(surface)
+ # TODO: Add the other groups when they get made
+
+#############################
+# Section 3 - Menu Entities #
+#############################
+
+class MenuButton(entity.Entity):
+ """
+ Object that represents pushable menu buttons in menu mode. Created
+ and managed by Menu objects.
+ """
+ # TODO: MenuButton should probably eventually be passed info on its draw layer.
+
+ def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, effects = []):
+
+ # Parent initialization
+ super().__init__(sheet, sprite, animation, animated)
+
+ # Saved values
+ self.effects = effects # A list of effects. Each effect goes with a method in MenuManager, and each one is triggered on a press.
+
+ # Entity settings
+ self.custom_flags = None
+
+ # DirtySprite settings
+ self.layer = 1
+