Seatable roaches.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 12:49:46 +0000 (14:49 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 12:49:46 +0000 (14:49 +0200)
koperkapel/scenes/roach_management.py
koperkapel/vehicles/base.py
koperkapel/vehicles/walking.py

index e7dcd7c66b8c188b81cfd585ad6181fe1c7fe0d8..d5fc67ffe749bcb3039778b2f7142e29c96d47ec 100644 (file)
@@ -48,6 +48,7 @@ class RoachesScene(Scene):
 
     def enter(self, world):
         self._vehicle = Vehicle.current(world)
+        self._update_calls = []
         self._init_bg()
         self._init_seats()
         self._init_roaches(world)
@@ -91,7 +92,7 @@ class RoachesScene(Scene):
     def _update_roaches(self, world):
         self._roach_layer.clear()
         seating = self._vehicle.seating(world)
-        outside_roaches = []
+        self._outside_roaches = []
         for roach in world.roaches:
             seat_pos = seating.get(roach.name)
             if seat_pos is not None:
@@ -99,16 +100,18 @@ class RoachesScene(Scene):
                 roach_actor.pos = self._seat_layer[seat_pos].pos
                 self._roach_layer.add(roach_actor)
             else:
-                outside_roaches.append(roach.name)
-        self._outside_roach_pos %= len(outside_roaches)
-        if outside_roaches:
+                self._outside_roaches.append(roach.name)
+        if self._outside_roaches:
+            self._outside_roach_pos %= len(self._outside_roaches)
             roach_actor = self._roach_actors[
-                outside_roaches[self._outside_roach_pos]]
+                self._outside_roaches[self._outside_roach_pos]]
             roach_pad_center = self._roach_pad.center
             roach_actor.pos = (
                 roach_pad_center[0] + ROACH_PAD_OFFSET[0],
                 roach_pad_center[1] + ROACH_PAD_OFFSET[1])
             self._roach_layer.add(roach_actor)
+        else:
+            self._outside_roach_pos = 0
 
     def _init_pads(self):
         self._roach_pad = self._pad_layer.add(
@@ -163,12 +166,33 @@ class RoachesScene(Scene):
         self._seat_pos = seat_pos
         self._seat_layer[self._seat_pos].selected = True
 
-    def _eject_roach(self):
-        print("Eject roach.")
+    def _eject_roach(self, world=None):
+        if world is None:
+            self._update_calls.append(self._eject_roach)
+            return
+        self._vehicle.seat_roach(world, None, self._seat_pos)
+
+    def _click_roach_pad(self, world=None):
+        if world is None:
+            self._update_calls.append(self._click_roach_pad)
+            return
+        if self._outside_roaches:
+            roach = self._outside_roaches[self._outside_roach_pos]
+            self._vehicle.seat_roach(world, roach, self._seat_pos)
+
+    def _click_inventory_pad(self, world=None):
+        if world is None:
+            self._update_calls.append(self._click_inventory_pad)
+            return
 
     def update(self, world, engine, dt):
+        while self._update_calls:
+            f = self._update_calls.pop()
+            f(world)
+        events = world.pop_events()
         self._update_inventory(world)
         self._update_roaches(world)
+        return events
 
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE:
@@ -183,3 +207,7 @@ class RoachesScene(Scene):
             for i, actor in enumerate(self.actors.seats):
                 if actor.collidepoint(pos):
                     return self._select_seat(i)
+            if self._roach_pad.collidepoint(pos):
+                return self._click_roach_pad()
+            if self._inventory_pad.collidepoint(pos):
+                return self._click_inventory_pad()
index 9aff447f1fdbb0134635059671773734a9a6bd9a..31f7eeaaa4f9ced6ac2285ab336038afd7975841 100644 (file)
@@ -1,5 +1,6 @@
 """ Base class for vehicles.  """
 
+from itertools import chain, islice, repeat
 from pygame.constants import BLEND_RGBA_MULT
 from pgzero.actor import Actor
 from pgzero.loaders import images
@@ -30,6 +31,16 @@ class Vehicle:
             for seat_pos, (roach, _) in roach_seating_numbers if roach
         }
 
+    def seat_roach(self, world, roach, seat_pos):
+        vehicle = world.vehicles[self.vehicle_type]
+        seats = len(self.seats)
+        seating = list(vehicle.seating)
+        seating = list(islice(
+            chain(seating, repeat(None, seats)), 0, seats))
+        seating[seat_pos] = roach
+        # line below records new seating on the world proxy
+        vehicle.seating = seating
+
     _vehicle_types = {}
 
     @classmethod
index 91cf02df66372b8f33c22771fd43a7dfd4ad6006..9c19aa2264e7f730e0a4ea303b4c7e5310944848 100644 (file)
@@ -1,7 +1,6 @@
 """ A vehicle to represent roaches on foot. """
 
 import math
-from pgzero.actor import Actor
 from .base import Vehicle, Seat