From 4e59ab6a38bba2eccd59cee633dc4fd391c12100 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Wed, 2 Mar 2016 17:41:06 +0200 Subject: [PATCH] Let there be roaches. --- koperkapel/pgzapp.py | 3 ++- koperkapel/scenes/base.py | 27 +++++++++++++++++------- koperkapel/world.py | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 koperkapel/world.py diff --git a/koperkapel/pgzapp.py b/koperkapel/pgzapp.py index ca9a23f..712bae7 100644 --- a/koperkapel/pgzapp.py +++ b/koperkapel/pgzapp.py @@ -2,12 +2,13 @@ import sys +from .world import World from .scenes.base import Engine from .scenes.menu import MenuScene from .constants import WIDTH, HEIGHT, TITLE -engine = Engine(sys.modules[__name__], MenuScene()) +engine = Engine(sys.modules[__name__], MenuScene(), World()) def update(dt): diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index dae87e3..aa30be4 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -13,11 +13,13 @@ def apply_events(f): class Engine: - """ A holder for game state. """ + """ A holder for game state & scene management. + """ - def __init__(self, app, scene): + def __init__(self, app, scene, world): self._app = app self._scene = scene + self._world = world self._viewport = (0, 0) def _apply_events(self, events): @@ -27,9 +29,12 @@ class Engine: ev.apply(self) def change_scene(self, scene): - self._scene.exit() + self._apply_events(self._scene.exit(self._world)) self._scene = scene - self._scene.enter() + self._apply_events(self._scene.enter(self._world)) + + def change_world(self, *args, **kw): + self._world.apply_event(*args, **kw) def quit_game(self): from pgzero.game import exit @@ -41,7 +46,7 @@ class Engine: @apply_events def update(self, dt): - return self._scene.update(dt) + return self._scene.update(self._world, dt) def draw(self): self._scene.draw(self._app.screen, self._viewport) @@ -86,6 +91,12 @@ class ChangeSceneEvent(Event): ENGINE_METHOD = "change_scene" +class WorldEvent(Event): + """ Be a hero. Change the world. """ + + ENGINE_METHOD = "change_world" + + class QuitEvent(Event): """ Quit the game. """ @@ -123,13 +134,13 @@ class Scene: def __init__(self): self.actors = Actors() - def enter(self): + def enter(self, world): pass - def exit(self): + def exit(self, world): pass - def update(self, dt): + def update(self, world, dt): pass def draw(self, screen, viewport): diff --git a/koperkapel/world.py b/koperkapel/world.py new file mode 100644 index 0000000..efb6403 --- /dev/null +++ b/koperkapel/world.py @@ -0,0 +1,44 @@ +""" World and player state. """ + + +class World: + """ World and player state. """ + + def __init__(self): + self._state = self._build_initial_state() + + @property + def level(self): + return self._level["level"] + + @property + def roaches(self): + return self._state["roaches"] + + def _build_initial_state(self): + state = {} + state["roaches"] = [ + self._build_roach("roachel", intelligence=3), + self._build_roach("roeginald", strength=3), + self._build_roach("roichard", quickness=3), + ] + state["level"] = { + "file": "level1.json", + } + return state + + def _build_roach(self, name, **kw): + attributes = { + "intelligence": 1, + "strength": 1, + "quickness": 1, + "health": 5, + } + attributes.update(kw) + return { + "name": name, + "attributes": attributes, + } + + def apply_event(self, *args, **kw): + pass -- 2.34.1