Merge branch 'master' of ctpug.org.za:koperkapel
authorSimon Cross <hodgestar@gmail.com>
Thu, 3 Mar 2016 22:37:26 +0000 (00:37 +0200)
committerSimon Cross <hodgestar@gmail.com>
Thu, 3 Mar 2016 22:37:26 +0000 (00:37 +0200)
koperkapel/scenes/roaches.py
koperkapel/vehicles/base.py [new file with mode: 0644]
koperkapel/vehicles/walking.py [new file with mode: 0644]
koperkapel/world.py

index 34f02679c5ed5807f6af13ef6e1d9a8e3490d520..342f23cd2a57a592cfe4c3600a33a631b83addb7 100644 (file)
@@ -4,6 +4,7 @@ from pgzero.constants import keys, mouse
 from pgzero.actor import Actor
 from ..actors.buttons import TextButton, ImageButton
 from ..constants import WIDTH, HEIGHT
+from ..vehicles.base import Vehicle
 from .base import Scene, ChangeSceneEvent
 
 
@@ -28,12 +29,21 @@ class RoachesScene(Scene):
         self._roaches = self.actors.add_layer("roaches", level=10)
         self._pads = self.actors.add_layer("pads", level=5)
         self._buttons = self.actors.add_layer("buttons", level=6)
-        self._init_bg()
         self._init_pads()
         self._init_buttons()
 
+    def enter(self, world):
+        self._vehicle = Vehicle.by_type(world.vehicles.current)
+        self._init_bg()
+        self._init_seats()
+
     def _init_bg(self):
         self.actors.default.add(Actor("vehicles/walking/background"))
+        self.actors.default.add(self._vehicle.background)
+
+    def _init_seats(self):
+        print("Seats:")
+        print([seat.pos for seat in self._vehicle.seats])
 
     def _init_pads(self):
         self._roach_pad = self._pads.add(
diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py
new file mode 100644 (file)
index 0000000..27373a5
--- /dev/null
@@ -0,0 +1,45 @@
+""" Base class for vehicles.  """
+
+
+class Vehicle:
+    """ Vehicle base class.
+
+    A vehicle should have the following attributes:
+
+    * seats -- list of roach seats.
+    * background -- actor representing background for management scene
+    """
+
+    vehicle_types = {}
+    approximate_radius = 200
+
+    @classmethod
+    def by_type(cls, vehicle_type):
+        return cls.vehicle_types.get(vehicle_type)()
+
+    @classmethod
+    def register(cls, vehicle_cls):
+        cls.vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
+
+    @classmethod
+    def register_all(cls):
+        from .walking import Walking
+        cls.register(Walking)
+
+
+class Seat:
+    """ A space in a vehicle for a roach.
+
+    * pos -- (x, y) position of the seat relative to the centre of the vehicle.
+      x and y may be numbers from approximately -1.0 to 1.0. They will be
+      multiplied by the approximate_radius of the vehicle.
+    * allowed -- f(roach) for checking whether a roach may occupy the
+      seat
+    """
+
+    def __init__(self, pos, allowed=None):
+        self.pos = pos
+        self.allowed = allowed or (lambda roach: True)
+
+
+Vehicle.register_all()
diff --git a/koperkapel/vehicles/walking.py b/koperkapel/vehicles/walking.py
new file mode 100644 (file)
index 0000000..f819892
--- /dev/null
@@ -0,0 +1,17 @@
+""" A vehicle to represent roaches on foot. """
+
+import math
+from .base import Vehicle, Seat
+from ..actors.buttons import TextButton
+
+
+class Walking(Vehicle):
+
+    def __init__(self):
+        n_seats = 6
+        d_theta = 2 * math.pi / n_seats
+        self.seats = [
+            Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)))
+            for i in range(n_seats)
+        ]
+        self.background = TextButton("Walking Background")
index 28f98b3da91ccad0365c2eb45ef0c12b3373ac23..f06d3bcbca1f4063e91dd9f300191c3f1f1639e1 100644 (file)
@@ -24,6 +24,10 @@ class World:
             self._build_roach("roeginald", strength=3),
             self._build_roach("roichard", quickness=3),
         ]
+        state["vehicles"] = {
+            "current": "walking",
+            "available": ["walking"],
+        }
         state["level"] = {
             "name": "level1",
         }