think of the poor windows users
[koperkapel.git] / koperkapel / vehicles / base.py
index 27373a5f1eb881c658b2f00021d4e21e5482e65e..3d7765c11f53094273bfc310b1f6816d118e956a 100644 (file)
 """ Base class for vehicles.  """
 
+import math
+import random
+from itertools import chain, islice, repeat
+from pygame.constants import BLEND_RGBA_MULT
+from pgzero.loaders import images
+from ..actors.orientatedsurf import OrientatedSurfActor
+from ..actors.animsurf import AnimatedSurfActor
+from ..weapons import default_weapons
+from ..util import safepath
+
 
 class Vehicle:
-    """ Vehicle base class.
+    """ Vehicle base class. """
 
-    A vehicle should have the following attributes:
+    vehicle_type = None
+    overlay_frame_no = None
+    approximate_radius = 200
+    weapons_taped_on = True
+    selected_seat_overlay_color = (255, 0, 0, 255)
 
-    * seats -- list of roach seats.
-    * background -- actor representing background for management scene
-    """
+    def __init__(self):
+        self.seats = self.init_seats()
+        self.game_pos = (0, 0)
 
-    vehicle_types = {}
-    approximate_radius = 200
+    def roach_management_overlay(self):
+        return images.load(safepath("vehicles/walking/background"))
+
+    def roach_management_frame(self):
+        if self.overlay_frame_no is None:
+            return None
+        return images.load(safepath("vehicle_big/%s_%d") % (
+            self.vehicle_type, self.overlay_frame_no))
+
+    def init_seats(self):
+        raise NotImplementedError("Vehicles should specify a list of seats")
+
+    def seating(self, world):
+        roach_seating = world.vehicles[self.vehicle_type].seating
+        roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
+        return {
+            roach: seat_pos
+            for seat_pos, (roach, _) in roach_seating_numbers if roach
+        }
+
+    def seat_roach(self, world, roach, seat_pos):
+        vehicle = world.vehicles[self.vehicle_type]
+        seats = len(self.seats)
+        seating = list(vehicle.seating)
+        seating = list(islice(
+            chain(seating, repeat(None, seats)), 0, seats))
+        seating[seat_pos] = roach
+        # line below records new seating on the world proxy
+        vehicle.seating = seating
+
+    def roach_at(self, world, seat_pos):
+        roach_seating = world.vehicles[self.vehicle_type].seating
+        if seat_pos >= len(roach_seating):
+            return None
+        return roach_seating[seat_pos]
+
+    def changed(self):
+        return False  # TODO: remove this
+
+    _vehicle_types = {}
+
+    @classmethod
+    def current(cls, world):
+        return cls.by_type(world.vehicles.current)
 
     @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 random(cls):
+        return random.choice(list(cls._vehicle_types.keys()))
 
     @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, weapon, suffix="_tiles"):
+        vehicle = images.load(safepath("vehicle%s/%s_%d") % (
+            suffix, self.vehicle_type, i + 1))
+        frame = vehicle.copy()
+        frame.blit(weapon.surf, (0, 0))
+        return frame
+
+    def get_avatar(self, world):
+        weapon = default_weapons.assemble(
+            world.weapons.current, tape=self.weapons_taped_on)
+        frames = [self._avatar_frame(i, weapon) for i in range(4)]
+        return AnimatedSurfActor(frames, anchor=(0, 0))
 
 
 class Seat:
@@ -33,13 +112,49 @@ 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):
+        seat = images.load(safepath("vehicles/walking/seat"))
+        selected_seat = seat.copy()
+        selected_seat.fill(
+            self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
+        return SeatActor(seat, selected_seat)
+
+
+class SeatActor(OrientatedSurfActor):
+    def __init__(self, seat, selected_seat):
+        self._selected = False
+        self._seat = seat
+        self._selected_seat = selected_seat
+        super().__init__(surf=self._seat, angle=0)
+
+    @property
+    def selected(self):
+        return self._selected
+
+    @selected.setter
+    def selected(self, value):
+        self._selected = value
+        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()