Moar vehicles!
[koperkapel.git] / koperkapel / vehicles / base.py
1 """ Base class for vehicles.  """
2
3 import math
4 from itertools import chain, islice, repeat
5 from pygame.constants import BLEND_RGBA_MULT
6 from pgzero.actor import Actor
7 from pgzero.loaders import images
8 from ..actors.orientatedsurf import OrientatedSurfActor
9 from ..actors.animsurf import AnimatedSurfActor
10
11
12 class Vehicle:
13     """ Vehicle base class. """
14
15     vehicle_type = None
16     approximate_radius = 200
17     selected_seat_overlay_color = (255, 0, 0, 255)
18
19     def __init__(self):
20         self.seats = self.init_seats()
21         self.game_pos = (0, 0)
22
23     def roach_management_overlay(self):
24         return Actor("vehicles/%s/background" % (self.vehicle_type,))
25
26     def init_seats(self):
27         raise NotImplementedError("Vehicles should specify a list of seats")
28
29     def seating(self, world):
30         roach_seating = world.vehicles[self.vehicle_type].seating
31         roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
32         return {
33             roach: seat_pos
34             for seat_pos, (roach, _) in roach_seating_numbers if roach
35         }
36
37     def seat_roach(self, world, roach, seat_pos):
38         vehicle = world.vehicles[self.vehicle_type]
39         seats = len(self.seats)
40         seating = list(vehicle.seating)
41         seating = list(islice(
42             chain(seating, repeat(None, seats)), 0, seats))
43         seating[seat_pos] = roach
44         # line below records new seating on the world proxy
45         vehicle.seating = seating
46
47     def roach_at(self, world, seat_pos):
48         roach_seating = world.vehicles[self.vehicle_type].seating
49         if seat_pos >= len(roach_seating):
50             return None
51         return roach_seating[seat_pos]
52
53     def changed(self):
54         return False  # TODO: remove this
55
56     _vehicle_types = {}
57
58     @classmethod
59     def current(cls, world):
60         return cls.by_type(world.vehicles.current)
61
62     @classmethod
63     def by_type(cls, vehicle_type):
64         return cls._vehicle_types.get(vehicle_type)()
65
66     @classmethod
67     def register(cls, vehicle_cls):
68         cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
69
70     @classmethod
71     def register_all(cls):
72         from .walking import Walking
73         from .quadcopter import Quadcopter
74         from .robot import Robot
75         from .roomba import Roomba
76         cls.register(Walking)
77         cls.register(Quadcopter)
78         cls.register(Robot)
79         cls.register(Roomba)
80
81     def _avatar_frame(self, i, suffix="_tiles"):
82         vehicle = images.load("vehicle%s/%s_%d" % (
83             suffix, self.vehicle_type, i + 1))
84         frame = vehicle.copy()
85         return frame
86
87     def get_avatar(self, world):
88         frames = [self._avatar_frame(i) for i in range(4)]
89         return AnimatedSurfActor(frames)
90
91
92 class Seat:
93     """ A space in a vehicle for a roach.
94
95     * pos -- (x, y) position of the seat relative to the centre of the vehicle.
96       x and y may be numbers from approximately -1.0 to 1.0. They will be
97       multiplied by the approximate_radius of the vehicle.
98     * roach -- name of the roach occupying the seat, if any.
99     * allowed -- f(roach) for checking whether a roach may occupy the
100       seat.
101     """
102
103     def __init__(self, vehicle, pos, roach=None, allowed=None):
104         self.vehicle = vehicle
105         self.pos = pos
106         self.roach = roach
107         self.allowed = allowed or (lambda roach: True)
108         vrad = vehicle.approximate_radius
109         self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
110
111     def actor(self):
112         seat = images.load(
113             "vehicles/%s/seat" % (self.vehicle.vehicle_type,))
114         selected_seat = seat.copy()
115         selected_seat.fill(
116             self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
117         return SeatActor(seat, selected_seat)
118
119
120 class SeatActor(OrientatedSurfActor):
121     def __init__(self, seat, selected_seat):
122         self._selected = False
123         self._seat = seat
124         self._selected_seat = selected_seat
125         super().__init__(surf=self._seat, angle=0)
126
127     @property
128     def selected(self):
129         return self._selected
130
131     @selected.setter
132     def selected(self, value):
133         self._selected = value
134         self.surf = self._selected_seat if value else self._seat
135
136
137 def circle_of_seats(n_seats, **kw):
138     d_theta = 2 * math.pi / n_seats
139     return [
140         Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
141         for i in range(n_seats)]
142
143
144 Vehicle.register_all()