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