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