Refactor seating a bit.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 10:39:01 +0000 (12:39 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 10:39:01 +0000 (12:39 +0200)
koperkapel/scenes/roach_management.py
koperkapel/vehicles/base.py
koperkapel/vehicles/walking.py

index 3ed1ab0f4ac88df5b976774aebb2bcb722cb70c1..0d10e21f5078e1f8b23a0fecc61dcbbf09811a00 100644 (file)
@@ -33,6 +33,7 @@ class RoachesScene(Scene):
         self._outside_roach_pos = 0
         self._inventory_pos = 0
         self._inventory_item = None
+        self._selected_seat = None
         self._roaches = self.actors.add_layer("roaches", level=10)
         self._inventory = self.actors.add_layer("inventory", level=10)
         self._pads = self.actors.add_layer("pads", level=5)
@@ -50,15 +51,14 @@ class RoachesScene(Scene):
         self._update_inventory(world)
 
     def _init_bg(self):
-        self.actors.default.add(self._vehicle.background)
+        self.actors.default.add(self._vehicle.roach_management_overlay())
 
     def _init_seats(self):
-        vrad = self._vehicle.approximate_radius
         for seat in self._vehicle.seats:
-            seat_actor = self._seats.add(seat.actor)
+            seat_actor = self._seats.add(seat.actor())
             seat_actor.pos = (
-                seat.pos[0] * vrad + VEHICLE_MID_X,
-                seat.pos[1] * vrad + VEHICLE_MID_Y)
+                seat.vehicle_pos[0] + VEHICLE_MID_X,
+                seat.vehicle_pos[1] + VEHICLE_MID_Y)
 
     def _init_roaches(self, roaches):
         for i, roach in enumerate(roaches):
index 09090048d5991b449f7a8f2a7c6a0599ae0dbac1..fb4640dd73be2a47e217bd0c1f66ac27565188ec 100644 (file)
@@ -4,24 +4,29 @@ 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
 
-    * background -- actor representing background for management scene
-    * seats -- list of roach seats.
-    """
+    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")
+
+    _vehicle_types = {}
 
     @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):
@@ -32,18 +37,24 @@ class Vehicle:
 class Seat:
     """ A space in a vehicle for a roach.
 
-    * background -- actor representing the 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, actor, pos, allowed=None):
-        self.actor = actor
+    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()
index adcdfc931f87b276e7d3be3e79ad54069076ad52..91cf02df66372b8f33c22771fd43a7dfd4ad6006 100644 (file)
@@ -7,13 +7,14 @@ from .base import Vehicle, Seat
 
 class Walking(Vehicle):
 
-    def __init__(self):
-        self.background = Actor("vehicles/walking/background")
+    vehicle_type = "walking"
+
+    def init_seats(self):
         n_seats = 6
         d_theta = 2 * math.pi / n_seats
-        self.seats = [
+        return [
             Seat(
-                actor=Actor("vehicles/walking/seat"),
+                vehicle=self,
                 pos=(math.sin(i * d_theta), math.cos(i * d_theta)))
             for i in range(n_seats)
         ]