Very crudely hack vehicles into the level
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 13:24:18 +0000 (15:24 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 13:24:27 +0000 (15:24 +0200)
koperkapel/roaches.py
koperkapel/scenes/level.py
koperkapel/vehicles/base.py
koperkapel/vehicles/walking.py

index 0d2448216c16a00900759736ed66e32fe975dbcb..b39d189fe669c6571047973feb33f8b7b6e4eecc 100644 (file)
@@ -26,6 +26,15 @@ class RoachActor(OrientatedSurfActor):
         self.surf = self._frames[self._frame]
 
 
+class WorldRoach(object):
+    """A roach proxy with no properties for display on the game level."""
+
+    def __init__(self):
+        self.smart = False
+        self.strong = False
+        self.fast = False
+
+
 class RoachFactory:
 
     def __init__(self, suffix, frames=4):
@@ -52,3 +61,5 @@ default_roaches = RoachFactory("")
 t32_roaches = RoachFactory("_32")
 t21_roaches = RoachFactory("_21")
 big_roaches = RoachFactory("_big")
+roaches_quartet = RoachFactory("_quartet")
+roaches_nonet = RoachFactory("_nonet")
index ea37e154ed643a39f166a1fcd8ada72553fd8af3..d2c7d162ef28b99007850086b1a1f45ee02a74bc 100644 (file)
@@ -6,7 +6,7 @@ import pygame.locals as pgl
 from ..loaders.levelloader import levels
 from .base import Scene, ChangeSceneEvent, MoveViewportEvent
 from ..constants import TILE_SIZE, WIDTH, HEIGHT
-from ..roaches import default_roaches
+from ..vehicles.base import Vehicle
 
 
 class BaseLevelScene(Scene):
@@ -76,17 +76,18 @@ class GameLevelScene(BaseLevelScene):
     def enter(self, world):
         super().enter(world)
         self._roaches = self.actors.add_layer("roaches", level=10)
+        self._vehicle = Vehicle.current(world)
         self._mode = 'walk'
-        return self._init_roaches(world.roaches)
+        self._angle = 0
+        return self._init_roaches(world)
 
-    def _init_roaches(self, roaches):
-        for roach in roaches:
-            roach_actor = self._roaches.add(default_roaches.assemble(roach))
-            roach_actor.anchor = (0, 0)
-            roach_actor.pos = (WIDTH // 2, HEIGHT // 2)
+    def _init_roaches(self, world):
         x, y = self._level.start_pos
-        self._set_pos(x, y)
         self._level_layer = 'floor'
+        self._avatar = self._vehicle.get_avatar(world)
+        self._set_pos(x, y)
+        self._avatar.pos = (WIDTH // 2, HEIGHT // 2)
+        self._roaches.add(self._avatar)
         # Fix viewport offset
         return [MoveViewportEvent((x * TILE_SIZE - WIDTH // 2, y * TILE_SIZE - HEIGHT // 2))]
 
@@ -103,8 +104,17 @@ class GameLevelScene(BaseLevelScene):
             return self._level.can_crawl(x, y, self._level_layer)
 
     def _set_angle(self, angle):
-        for roach in self._roaches:
-            roach.angle = angle
+        self._angle = angle
+        self._avatar.angle = angle
+
+    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)
 
     def on_key_down(self, key, mod, unicode):
         offset = None
@@ -149,7 +159,6 @@ class GameLevelScene(BaseLevelScene):
         elif key == keys.V:
             # Leave vehicle
             print('Vehicle key pressed')
-            
         if offset:
             return [MoveViewportEvent(offset)]
         return super(GameLevelScene, self).on_key_down(key, mod, unicode)
index 31f7eeaaa4f9ced6ac2285ab336038afd7975841..ba4fbb77325b0542d32d7cb35e0d0d8a302be57f 100644 (file)
@@ -16,6 +16,7 @@ class Vehicle:
 
     def __init__(self):
         self.seats = self.init_seats()
+        self.game_pos = (0, 0)
 
     def roach_management_overlay(self):
         return Actor("vehicles/%s/background" % (self.vehicle_type,))
@@ -60,6 +61,9 @@ class Vehicle:
         from .walking import Walking
         cls.register(Walking)
 
+    def get_avatar(self, world):
+        raise NotImplementedError("Vehicles should know how to create their own avatars.")
+
 
 class Seat:
     """ A space in a vehicle for a roach.
index 9c19aa2264e7f730e0a4ea303b4c7e5310944848..aa88bc65a97230d5048f01927e43b4cddcdfbc1c 100644 (file)
@@ -2,7 +2,7 @@
 
 import math
 from .base import Vehicle, Seat
-
+from ..roaches import default_roaches, roaches_quartet, roaches_nonet, WorldRoach
 
 class Walking(Vehicle):
 
@@ -17,3 +17,20 @@ class Walking(Vehicle):
                 pos=(math.sin(i * d_theta), math.cos(i * d_theta)))
             for i in range(n_seats)
         ]
+
+    def get_avatar(self, world):
+        num_roaches = len(world.roaches)
+        roach = WorldRoach()
+        if num_roaches == 1:
+            # Return a single large roach
+            avatar = default_roaches.assemble(roach)
+            avatar.anchor = (0, 0)
+        elif num_roaches < 6:
+            avatar = roaches_quartet.assemble(roach)
+            avatar.anchor = (-16, 0)
+        else:
+            avatar = roaches_nonet.assemble(roach)
+        return avatar
+
+    def changed(self):
+        return False