1 """ Base class for vehicles. """
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
15 """ Vehicle base class. """
18 overlay_frame_no = None
19 approximate_radius = 200
20 weapons_taped_on = True
21 selected_seat_overlay_color = (255, 0, 0, 255)
25 self.seats = self.init_seats()
26 self.game_pos = (0, 0)
28 def roach_management_overlay(self):
29 return images.load(safepath("vehicles/walking/background"))
31 def roach_management_frame(self):
32 if self.overlay_frame_no is None:
34 return images.load(safepath("vehicle_big/%s_%d") % (
35 self.vehicle_type, self.overlay_frame_no))
38 raise NotImplementedError("Vehicles should specify a list of seats")
40 def seating(self, world):
41 roach_seating = world.vehicles[self.vehicle_type].seating
42 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
45 for seat_pos, (roach, _) in roach_seating_numbers if roach
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
58 def roach_at(self, world, seat_pos):
59 roach_seating = world.vehicles[self.vehicle_type].seating
60 if seat_pos >= len(roach_seating):
62 return roach_seating[seat_pos]
65 return False # TODO: remove this
70 def current(cls, world):
71 return cls.by_type(world.vehicles.current)
74 def by_type(cls, vehicle_type):
75 return cls._vehicle_types.get(vehicle_type)()
78 def register(cls, vehicle_cls):
79 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
83 return random.choice(list(cls._vehicle_types.keys()))
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
92 cls.register(Quadcopter)
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()
101 frame.blit(weapon.surf, (0, 0))
104 def get_avatar(self, world=None, dead=False):
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)]
110 frames = [self._avatar_frame(0, weapon=None)]
111 return AnimatedSurfActor(frames, anchor=(0, 0))
115 """ A space in a vehicle for a roach.
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
125 def __init__(self, vehicle, pos, roach=None, allowed=None):
126 self.vehicle = vehicle
129 self.allowed = allowed or (lambda roach: True)
130 vrad = vehicle.approximate_radius
131 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
134 seat = images.load(safepath("vehicles/walking/seat"))
135 selected_seat = seat.copy()
137 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
138 return SelectableSurfActor(seat, selected_seat)
141 def circle_of_seats(n_seats, **kw):
142 d_theta = 2 * math.pi / n_seats
144 Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
145 for i in range(n_seats)]
148 Vehicle.register_all()