def enter(self, world):
self._vehicle = Vehicle.current(world)
+ self._update_calls = []
self._init_bg()
self._init_seats()
self._init_roaches(world)
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:
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(
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:
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()
""" 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
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