Clean up inventory displaying.
authorSimon Cross <hodgestar@gmail.com>
Fri, 4 Mar 2016 22:58:03 +0000 (00:58 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 4 Mar 2016 22:58:03 +0000 (00:58 +0200)
koperkapel/scenes/roach_management.py

index e206e102d8a9d7563b174be0e513451aa76463c2..3ed1ab0f4ac88df5b976774aebb2bcb722cb70c1 100644 (file)
@@ -5,7 +5,7 @@ from pgzero.actor import Actor
 from ..actors.buttons import ImageButton
 from ..constants import WIDTH, HEIGHT
 from ..roaches import big_roaches
-from ..serums import big_serums
+from ..serums import big_serums, SERUMS
 from ..vehicles.base import Vehicle
 from .base import Scene, ChangeSceneEvent
 
@@ -32,20 +32,22 @@ class RoachesScene(Scene):
         super().__init__()
         self._outside_roach_pos = 0
         self._inventory_pos = 0
+        self._inventory_item = None
         self._roaches = self.actors.add_layer("roaches", level=10)
-        self._serums = self.actors.add_layer("serums", level=10)
+        self._inventory = self.actors.add_layer("inventory", level=10)
         self._pads = self.actors.add_layer("pads", level=5)
         self._seats = self.actors.add_layer("seats", level=5)
         self._buttons = self.actors.add_layer("buttons", level=6)
         self._init_pads()
         self._init_buttons()
+        self._init_serums()
 
     def enter(self, world):
         self._vehicle = Vehicle.by_type(world.vehicles.current)
         self._init_bg()
         self._init_seats()
         self._init_roaches(world.roaches)
-        self._update_serums(world.serums)
+        self._update_inventory(world)
 
     def _init_bg(self):
         self.actors.default.add(self._vehicle.background)
@@ -65,17 +67,22 @@ class RoachesScene(Scene):
             if i < len(self._seats):
                 roach_actor.pos = self._seats[i].pos
 
-    def _update_serums(self, serums):
-        self._serums.clear()
+    def _init_serums(self):
+        self._serum_actors = {
+            serum: big_serums.assemble(serum) for serum in SERUMS}
+
+    def _update_inventory(self, world):
+        self._inventory.clear()
+        serums = world.serums
         if not serums:
             return
         self._inventory_pos %= len(serums)
-        serum_actor = self._serums.add(
-            big_serums.assemble(serums[self._inventory_pos]))
+        serum_actor = self._serum_actors[serums[self._inventory_pos]]
         inv_pad_centre = self._inventory_pad.center
         serum_actor.pos = (
             inv_pad_centre[0] + SERUM_OFFSET[0],
             inv_pad_centre[1] + SERUM_OFFSET[1])
+        self._inventory.add(serum_actor)
 
     def _init_pads(self):
         self._roach_pad = self._pads.add(
@@ -128,7 +135,7 @@ class RoachesScene(Scene):
         print("Eject roach.")
 
     def update(self, world, dt):
-        self._update_serums(world.serums)
+        self._update_inventory(world)
 
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE: