Slightly more advanced roach seating.
[koperkapel.git] / koperkapel / vehicles / base.py
index 27373a5f1eb881c658b2f00021d4e21e5482e65e..1b2f3ed279ff471ff9d151d9c5a4ca93e7e3ba24 100644 (file)
@@ -1,25 +1,43 @@
 """ Base class for vehicles.  """
 
+from pgzero.actor import Actor
+
 
 class Vehicle:
-    """ Vehicle base class.
+    """ Vehicle base class. """
 
-    A vehicle should have the following attributes:
+    vehicle_type = None
+    approximate_radius = 200
 
-    * seats -- list of roach seats.
-    * background -- actor representing background for management scene
-    """
+    def __init__(self):
+        self.seats = self.init_seats()
 
-    vehicle_types = {}
-    approximate_radius = 200
+    def roach_management_overlay(self):
+        return Actor("vehicles/%s/background" % (self.vehicle_type,))
+
+    def init_seats(self):
+        raise NotImplementedError("Vehicles should specify a list of seats")
+
+    def assign_seats(self, seating):
+        for roach_name, seat in zip(seating, self.seats):
+            seat.roach = roach_name
+
+    _vehicle_types = {}
+
+    @classmethod
+    def current(cls, world):
+        vehicle = cls.by_type(world.vehicles.current)
+        vehicle.assign_seats(
+            world.vehicles[vehicle.vehicle_type].seating)
+        return vehicle
 
     @classmethod
     def by_type(cls, vehicle_type):
-        return cls.vehicle_types.get(vehicle_type)()
+        return cls._vehicle_types.get(vehicle_type)()
 
     @classmethod
     def register(cls, vehicle_cls):
-        cls.vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
+        cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
 
     @classmethod
     def register_all(cls):
@@ -33,13 +51,21 @@ class Seat:
     * 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.
+    * roach -- name of the roach occupying the seat, if any.
     * allowed -- f(roach) for checking whether a roach may occupy the
-      seat
+      seat.
     """
 
-    def __init__(self, pos, allowed=None):
+    def __init__(self, vehicle, pos, roach=None, allowed=None):
+        self.vehicle = vehicle
         self.pos = pos
+        self.roach = roach
         self.allowed = allowed or (lambda roach: True)
+        vrad = vehicle.approximate_radius
+        self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
+
+    def actor(self):
+        return Actor("vehicles/%s/seat" % (self.vehicle.vehicle_type,))
 
 
 Vehicle.register_all()