Moar vehicles!
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 15:10:42 +0000 (17:10 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 15:10:42 +0000 (17:10 +0200)
koperkapel/vehicles/base.py
koperkapel/vehicles/quadcopter.py [new file with mode: 0644]
koperkapel/vehicles/robot.py [new file with mode: 0644]
koperkapel/vehicles/roomba.py [new file with mode: 0644]
koperkapel/vehicles/walking.py

index 47588a59c5450aaaabd5f74ac120302c65037f50..551826f5053dee9d56c8fd489af54db8abd218f3 100644 (file)
@@ -1,10 +1,12 @@
 """ Base class for vehicles.  """
 
+import math
 from itertools import chain, islice, repeat
 from pygame.constants import BLEND_RGBA_MULT
 from pgzero.actor import Actor
 from pgzero.loaders import images
 from ..actors.orientatedsurf import OrientatedSurfActor
+from ..actors.animsurf import AnimatedSurfActor
 
 
 class Vehicle:
@@ -48,6 +50,9 @@ class Vehicle:
             return None
         return roach_seating[seat_pos]
 
+    def changed(self):
+        return False  # TODO: remove this
+
     _vehicle_types = {}
 
     @classmethod
@@ -65,10 +70,23 @@ class Vehicle:
     @classmethod
     def register_all(cls):
         from .walking import Walking
+        from .quadcopter import Quadcopter
+        from .robot import Robot
+        from .roomba import Roomba
         cls.register(Walking)
+        cls.register(Quadcopter)
+        cls.register(Robot)
+        cls.register(Roomba)
+
+    def _avatar_frame(self, i, suffix="_tiles"):
+        vehicle = images.load("vehicle%s/%s_%d" % (
+            suffix, self.vehicle_type, i + 1))
+        frame = vehicle.copy()
+        return frame
 
     def get_avatar(self, world):
-        raise NotImplementedError("Vehicles should know how to create their own avatars.")
+        frames = [self._avatar_frame(i) for i in range(4)]
+        return AnimatedSurfActor(frames)
 
 
 class Seat:
@@ -116,4 +134,11 @@ class SeatActor(OrientatedSurfActor):
         self.surf = self._selected_seat if value else self._seat
 
 
+def circle_of_seats(n_seats, **kw):
+    d_theta = 2 * math.pi / n_seats
+    return [
+        Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
+        for i in range(n_seats)]
+
+
 Vehicle.register_all()
diff --git a/koperkapel/vehicles/quadcopter.py b/koperkapel/vehicles/quadcopter.py
new file mode 100644 (file)
index 0000000..ed5024f
--- /dev/null
@@ -0,0 +1,11 @@
+""" Flying roaches! """
+
+from .base import Vehicle, circle_of_seats
+
+
+class Quadcopter(Vehicle):
+
+    vehicle_type = "quadcopter"
+
+    def init_seats(self):
+        return circle_of_seats(4, vehicle=self)
diff --git a/koperkapel/vehicles/robot.py b/koperkapel/vehicles/robot.py
new file mode 100644 (file)
index 0000000..50ebc13
--- /dev/null
@@ -0,0 +1,11 @@
+""" Roach Robot! """
+
+from .base import Vehicle, circle_of_seats
+
+
+class Robot(Vehicle):
+
+    vehicle_type = "robot"
+
+    def init_seats(self):
+        return circle_of_seats(8, vehicle=self)
diff --git a/koperkapel/vehicles/roomba.py b/koperkapel/vehicles/roomba.py
new file mode 100644 (file)
index 0000000..ff93f8a
--- /dev/null
@@ -0,0 +1,11 @@
+""" Roaches do not abhor a vacuum. """
+
+from .base import Vehicle, circle_of_seats
+
+
+class Roomba(Vehicle):
+
+    vehicle_type = "roomba"
+
+    def init_seats(self):
+        return circle_of_seats(6, vehicle=self)
index 6b57dff2c22fee29c0d80d42dfaddb9677216a6d..f7b73aa398cbf31ca6451f0b117bd0c980e00139 100644 (file)
@@ -1,22 +1,16 @@
 """ A vehicle to represent roaches on foot. """
 
-import math
-from .base import Vehicle, Seat
-from ..roaches import default_roaches, roaches_quartet, roaches_nonet, WorldRoach
+from .base import Vehicle, circle_of_seats
+from ..roaches import (
+    default_roaches, roaches_quartet, roaches_nonet, WorldRoach)
+
 
 class Walking(Vehicle):
 
     vehicle_type = "walking"
 
     def init_seats(self):
-        n_seats = 6
-        d_theta = 2 * math.pi / n_seats
-        return [
-            Seat(
-                vehicle=self,
-                pos=(math.sin(i * d_theta), math.cos(i * d_theta)))
-            for i in range(n_seats)
-        ]
+        return circle_of_seats(6, vehicle=self)
 
     def get_avatar(self, world):
         num_roaches = len(world.roaches)
@@ -32,6 +26,3 @@ class Walking(Vehicle):
             avatar = roaches_nonet.assemble(roach)
             avatar.anchor = (0, 0)
         return avatar
-
-    def changed(self):
-        return False