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