Load sounds on weapon init
[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         frame.blit(weapon.surf, (0, 0))
100         return frame
101
102     def get_avatar(self, world):
103         weapon = default_weapons.assemble(
104             world.weapons.current, tape=self.weapons_taped_on)
105         frames = [self._avatar_frame(i, weapon) for i in range(4)]
106         return AnimatedSurfActor(frames, anchor=(0, 0))
107
108
109 class Seat:
110     """ A space in a vehicle for a roach.
111
112     * pos -- (x, y) position of the seat relative to the centre of the vehicle.
113       x and y may be numbers from approximately -1.0 to 1.0. They will be
114       multiplied by the approximate_radius of the vehicle.
115     * roach -- name of the roach occupying the seat, if any.
116     * allowed -- f(roach) for checking whether a roach may occupy the
117       seat.
118     """
119
120     def __init__(self, vehicle, pos, roach=None, allowed=None):
121         self.vehicle = vehicle
122         self.pos = pos
123         self.roach = roach
124         self.allowed = allowed or (lambda roach: True)
125         vrad = vehicle.approximate_radius
126         self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
127
128     def actor(self):
129         seat = images.load(safepath("vehicles/walking/seat"))
130         selected_seat = seat.copy()
131         selected_seat.fill(
132             self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
133         return SelectableSurfActor(seat, selected_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()