Random vehicle swapping.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 16:15:20 +0000 (18:15 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 16:15:20 +0000 (18:15 +0200)
koperkapel/scenes/level.py
koperkapel/scenes/roach_management.py
koperkapel/vehicles/base.py

index 856f4cec8e9ead960cc666a1866d48fdd38363b1..54449dcc8513f7d2d91b5768d7549307f54e1e94 100644 (file)
@@ -4,7 +4,7 @@ from pgzero.constants import keys
 from pygame import Surface
 import pygame.locals as pgl
 from ..loaders.levelloader import levels
-from .base import Scene, ChangeSceneEvent, MoveViewportEvent
+from .base import Scene, ChangeSceneEvent, MoveViewportEvent, defer_to_update
 from ..constants import TILE_SIZE, WIDTH, HEIGHT
 from ..vehicles.base import Vehicle
 
@@ -51,6 +51,7 @@ class BaseLevelScene(Scene):
 
     def update(self, world, engine, dt):
         """Fix the door and keypad positions"""
+        super().update(world, engine, dt)
         for door in self._doors:
             door.pos = self.calc_offset(
                 door.game_pos[0] * TILE_SIZE, door.game_pos[1] * TILE_SIZE)
@@ -119,14 +120,25 @@ class GameLevelScene(BaseLevelScene):
         self._angle = angle
         self._avatar.angle = angle
 
+    @defer_to_update
+    def _vehicle_changed(self, world):
+        self._roaches.remove(self._avatar)
+        self._vehicle = Vehicle.current(world)
+        self._avatar = self._vehicle.get_avatar(world)
+        self._avatar.pos = (WIDTH // 2, HEIGHT // 2)
+        self._roaches.add(self._avatar)
+        self._set_angle(self._angle)
+
+    @defer_to_update
+    def _change_vehicle(self, world):
+        vehicle = Vehicle.random()
+        world.vehicles.current = vehicle
+        self._vehicle_changed()
+
     def update(self, world, engine, dt):
-        if self._vehicle.changed():
-            self._roaches.remove(self._avatar)
-            self._avatar = self._vehicle.get_avatar(world)
-            self._avatar.pos = (WIDTH // 2, HEIGHT // 2)
-            self._roaches.add(self._avatar)
-            self._avatar.set_angle(self._angle)
         super().update(world, engine, dt)
+        events = world.pop_events()
+        return events
 
     def on_key_down(self, key, mod, unicode):
         offset = None
@@ -170,7 +182,7 @@ class GameLevelScene(BaseLevelScene):
             print('Boom')
         elif key == keys.V:
             # Leave vehicle
-            print('Vehicle key pressed')
+            self._change_vehicle()
         elif key == keys.Z:
             # Vehicle management
             from .roach_management import RoachesScene
index 6c867fb4f196142b424a7508c063b7bca751026f..4e41b9c538608d0dd7df2097335a772556d1331a 100644 (file)
@@ -10,7 +10,7 @@ from ..constants import WIDTH, HEIGHT
 from ..roaches import big_roaches, roach_by_name
 from ..serums import big_serums, roach_is_serumless, SERUMS
 from ..vehicles.base import Vehicle
-from .base import Scene, ChangeSceneEvent
+from .base import Scene, ChangeSceneEvent, defer_to_update
 
 
 TOOLBAR_LEFT_X = WIDTH * 3 // 4
@@ -52,7 +52,6 @@ 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)
@@ -187,24 +186,18 @@ class RoachesScene(Scene):
         self._seat_pos = seat_pos
         self._seat_layer[self._seat_pos].selected = True
 
-    def _eject_roach(self, world=None):
-        if world is None:
-            self._update_calls.append(self._eject_roach)
-            return
+    @defer_to_update
+    def _eject_roach(self, world):
         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
+    @defer_to_update
+    def _click_roach_pad(self, world):
         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
+    @defer_to_update
+    def _click_inventory_pad(self, world):
         roach_name = self._vehicle.roach_at(world, self._seat_pos)
         if roach_name is None:
             return
@@ -218,19 +211,15 @@ class RoachesScene(Scene):
         if roach_is_serumless(roach):
             roach[serum] = True
             world.serums = serums
-            self._update_calls.append((self._update_roach_actor, roach_name))
+            self._update_roach_actor(roach_name)
 
+    @defer_to_update
     def _update_roach_actor(self, world, roach_name):
         roach = roach_by_name(world, roach_name)
         self._roach_actors[roach_name] = big_roaches.assemble(roach)
 
     def update(self, world, engine, dt):
-        update_calls, self._update_calls = self._update_calls, []
-        while update_calls:
-            f, args = update_calls.pop(), ()
-            if type(f) is tuple:
-                f, args = f[0], f[1:]
-            f(world, *args)
+        super().update(world, engine, dt)
         events = world.pop_events()
         self._update_inventory(world)
         self._update_roaches(world)
index 2ae347cd54e541187b46cd39729da98a7cc86f2b..9eb3eb0cfe714f74f9517d934da6f8e73ba5c7d6 100644 (file)
@@ -1,6 +1,7 @@
 """ Base class for vehicles.  """
 
 import math
+import random
 from itertools import chain, islice, repeat
 from pygame.constants import BLEND_RGBA_MULT
 from pgzero.loaders import images
@@ -73,6 +74,10 @@ class Vehicle:
     def register(cls, vehicle_cls):
         cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
 
+    @classmethod
+    def random(cls):
+        return random.choice(list(cls._vehicle_types.keys()))
+
     @classmethod
     def register_all(cls):
         from .walking import Walking