From: Simon Cross Date: Sat, 5 Mar 2016 12:49:46 +0000 (+0200) Subject: Seatable roaches. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=8792e5a86456400f818a71bbd501a1a5128f0122;p=koperkapel.git Seatable roaches. --- diff --git a/koperkapel/scenes/roach_management.py b/koperkapel/scenes/roach_management.py index e7dcd7c..d5fc67f 100644 --- a/koperkapel/scenes/roach_management.py +++ b/koperkapel/scenes/roach_management.py @@ -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() diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py index 9aff447..31f7eea 100644 --- a/koperkapel/vehicles/base.py +++ b/koperkapel/vehicles/base.py @@ -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 diff --git a/koperkapel/vehicles/walking.py b/koperkapel/vehicles/walking.py index 91cf02d..9c19aa2 100644 --- a/koperkapel/vehicles/walking.py +++ b/koperkapel/vehicles/walking.py @@ -1,7 +1,6 @@ """ A vehicle to represent roaches on foot. """ import math -from pgzero.actor import Actor from .base import Vehicle, Seat