1 """ Base class for vehicles. """
4 from itertools import chain, islice, repeat
5 from pygame.constants import BLEND_RGBA_MULT
6 from pgzero.loaders import images
7 from ..actors.orientatedsurf import OrientatedSurfActor
8 from ..actors.animsurf import AnimatedSurfActor
12 """ Vehicle base class. """
15 overlay_frame_no = None
16 approximate_radius = 200
17 selected_seat_overlay_color = (255, 0, 0, 255)
20 self.seats = self.init_seats()
21 self.game_pos = (0, 0)
23 def roach_management_overlay(self):
24 return images.load("vehicles/walking/background")
26 def roach_management_frame(self):
27 if self.overlay_frame_no is None:
29 return images.load("vehicle_big/%s_%d" % (
30 self.vehicle_type, self.overlay_frame_no))
33 raise NotImplementedError("Vehicles should specify a list of seats")
35 def seating(self, world):
36 roach_seating = world.vehicles[self.vehicle_type].seating
37 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
40 for seat_pos, (roach, _) in roach_seating_numbers if roach
43 def seat_roach(self, world, roach, seat_pos):
44 vehicle = world.vehicles[self.vehicle_type]
45 seats = len(self.seats)
46 seating = list(vehicle.seating)
47 seating = list(islice(
48 chain(seating, repeat(None, seats)), 0, seats))
49 seating[seat_pos] = roach
50 # line below records new seating on the world proxy
51 vehicle.seating = seating
53 def roach_at(self, world, seat_pos):
54 roach_seating = world.vehicles[self.vehicle_type].seating
55 if seat_pos >= len(roach_seating):
57 return roach_seating[seat_pos]
60 return False # TODO: remove this
65 def current(cls, world):
66 return cls.by_type(world.vehicles.current)
69 def by_type(cls, vehicle_type):
70 return cls._vehicle_types.get(vehicle_type)()
73 def register(cls, vehicle_cls):
74 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
77 def register_all(cls):
78 from .walking import Walking
79 from .quadcopter import Quadcopter
80 from .robot import Robot
81 from .roomba import Roomba
83 cls.register(Quadcopter)
87 def _avatar_frame(self, i, suffix="_tiles"):
88 vehicle = images.load("vehicle%s/%s_%d" % (
89 suffix, self.vehicle_type, i + 1))
90 frame = vehicle.copy()
93 def get_avatar(self, world):
94 frames = [self._avatar_frame(i) for i in range(4)]
95 return AnimatedSurfActor(frames, anchor=(0, 0))
99 """ A space in a vehicle for a roach.
101 * pos -- (x, y) position of the seat relative to the centre of the vehicle.
102 x and y may be numbers from approximately -1.0 to 1.0. They will be
103 multiplied by the approximate_radius of the vehicle.
104 * roach -- name of the roach occupying the seat, if any.
105 * allowed -- f(roach) for checking whether a roach may occupy the
109 def __init__(self, vehicle, pos, roach=None, allowed=None):
110 self.vehicle = vehicle
113 self.allowed = allowed or (lambda roach: True)
114 vrad = vehicle.approximate_radius
115 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
118 seat = images.load("vehicles/walking/seat")
119 selected_seat = seat.copy()
121 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
122 return SeatActor(seat, selected_seat)
125 class SeatActor(OrientatedSurfActor):
126 def __init__(self, seat, selected_seat):
127 self._selected = False
129 self._selected_seat = selected_seat
130 super().__init__(surf=self._seat, angle=0)
134 return self._selected
137 def selected(self, value):
138 self._selected = value
139 self.surf = self._selected_seat if value else self._seat
142 def circle_of_seats(n_seats, **kw):
143 d_theta = 2 * math.pi / n_seats
145 Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
146 for i in range(n_seats)]
149 Vehicle.register_all()