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