Let there be roaches.
authorSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 15:41:06 +0000 (17:41 +0200)
committerSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 15:41:06 +0000 (17:41 +0200)
koperkapel/pgzapp.py
koperkapel/scenes/base.py
koperkapel/world.py [new file with mode: 0644]

index ca9a23f6d11faeb364cb208ff3becbb26a34ca74..712bae7e9793256e716edf50d086f16bcf3155c8 100644 (file)
@@ -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):
index dae87e387f69280330f4edaa296c40cacfe089ea..aa30be492baa518327975e8d5a96377dc88e3935 100644 (file)
@@ -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 (file)
index 0000000..efb6403
--- /dev/null
@@ -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