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