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