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)
24 self.seats = self.init_seats()
25 self.game_pos = (0, 0)
27 def roach_management_overlay(self):
28 return images.load(safepath("vehicles/walking/background"))
30 def roach_management_frame(self):
31 if self.overlay_frame_no is None:
33 return images.load(safepath("vehicle_big/%s_%d") % (
34 self.vehicle_type, self.overlay_frame_no))
37 raise NotImplementedError("Vehicles should specify a list of seats")
39 def seating(self, world):
40 roach_seating = world.vehicles[self.vehicle_type].seating
41 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
44 for seat_pos, (roach, _) in roach_seating_numbers if roach
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
57 def roach_at(self, world, seat_pos):
58 roach_seating = world.vehicles[self.vehicle_type].seating
59 if seat_pos >= len(roach_seating):
61 return roach_seating[seat_pos]
64 return False # TODO: remove this
69 def current(cls, world):
70 return cls.by_type(world.vehicles.current)
73 def by_type(cls, vehicle_type):
74 return cls._vehicle_types.get(vehicle_type)()
77 def register(cls, vehicle_cls):
78 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
82 return random.choice(list(cls._vehicle_types.keys()))
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
91 cls.register(Quadcopter)
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))
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))
110 """ A space in a vehicle for a roach.
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
120 def __init__(self, vehicle, pos, roach=None, allowed=None):
121 self.vehicle = vehicle
124 self.allowed = allowed or (lambda roach: True)
125 vrad = vehicle.approximate_radius
126 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
129 seat = images.load(safepath("vehicles/walking/seat"))
130 selected_seat = seat.copy()
132 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
133 return SelectableSurfActor(seat, selected_seat)
136 def circle_of_seats(n_seats, **kw):
137 d_theta = 2 * math.pi / n_seats
139 Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
140 for i in range(n_seats)]
143 Vehicle.register_all()