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 approximate_radius = 200
16 selected_seat_overlay_color = (255, 0, 0, 255)
19 self.seats = self.init_seats()
20 self.game_pos = (0, 0)
22 def roach_management_overlay(self):
23 return images.load("vehicles/%s/background" % (self.vehicle_type,))
26 raise NotImplementedError("Vehicles should specify a list of seats")
28 def seating(self, world):
29 roach_seating = world.vehicles[self.vehicle_type].seating
30 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
33 for seat_pos, (roach, _) in roach_seating_numbers if roach
36 def seat_roach(self, world, roach, seat_pos):
37 vehicle = world.vehicles[self.vehicle_type]
38 seats = len(self.seats)
39 seating = list(vehicle.seating)
40 seating = list(islice(
41 chain(seating, repeat(None, seats)), 0, seats))
42 seating[seat_pos] = roach
43 # line below records new seating on the world proxy
44 vehicle.seating = seating
46 def roach_at(self, world, seat_pos):
47 roach_seating = world.vehicles[self.vehicle_type].seating
48 if seat_pos >= len(roach_seating):
50 return roach_seating[seat_pos]
53 return False # TODO: remove this
58 def current(cls, world):
59 return cls.by_type(world.vehicles.current)
62 def by_type(cls, vehicle_type):
63 return cls._vehicle_types.get(vehicle_type)()
66 def register(cls, vehicle_cls):
67 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
70 def register_all(cls):
71 from .walking import Walking
72 from .quadcopter import Quadcopter
73 from .robot import Robot
74 from .roomba import Roomba
76 cls.register(Quadcopter)
80 def _avatar_frame(self, i, suffix="_tiles"):
81 vehicle = images.load("vehicle%s/%s_%d" % (
82 suffix, self.vehicle_type, i + 1))
83 frame = vehicle.copy()
86 def get_avatar(self, world):
87 frames = [self._avatar_frame(i) for i in range(4)]
88 return AnimatedSurfActor(frames)
92 """ A space in a vehicle for a roach.
94 * pos -- (x, y) position of the seat relative to the centre of the vehicle.
95 x and y may be numbers from approximately -1.0 to 1.0. They will be
96 multiplied by the approximate_radius of the vehicle.
97 * roach -- name of the roach occupying the seat, if any.
98 * allowed -- f(roach) for checking whether a roach may occupy the
102 def __init__(self, vehicle, pos, roach=None, allowed=None):
103 self.vehicle = vehicle
106 self.allowed = allowed or (lambda roach: True)
107 vrad = vehicle.approximate_radius
108 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
112 "vehicles/%s/seat" % (self.vehicle.vehicle_type,))
113 selected_seat = seat.copy()
115 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
116 return SeatActor(seat, selected_seat)
119 class SeatActor(OrientatedSurfActor):
120 def __init__(self, seat, selected_seat):
121 self._selected = False
123 self._selected_seat = selected_seat
124 super().__init__(surf=self._seat, angle=0)
128 return self._selected
131 def selected(self, value):
132 self._selected = value
133 self.surf = self._selected_seat if value else self._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()