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):
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):
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
@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)
ENGINE_METHOD = "change_scene"
+class WorldEvent(Event):
+ """ Be a hero. Change the world. """
+
+ ENGINE_METHOD = "change_world"
+
+
class QuitEvent(Event):
""" Quit the game. """
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):
--- /dev/null
+""" 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