1 """ Base class for vehicles. """
4 from itertools import chain, islice, repeat
5 from pygame.constants import BLEND_RGBA_MULT
6 from pgzero.actor import Actor
7 from pgzero.loaders import images
8 from ..actors.orientatedsurf import OrientatedSurfActor
9 from ..actors.animsurf import AnimatedSurfActor
13 """ Vehicle base class. """
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 Actor("vehicles/%s/background" % (self.vehicle_type,))
27 raise NotImplementedError("Vehicles should specify a list of seats")
29 def seating(self, world):
30 roach_seating = world.vehicles[self.vehicle_type].seating
31 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
34 for seat_pos, (roach, _) in roach_seating_numbers if roach
37 def seat_roach(self, world, roach, seat_pos):
38 vehicle = world.vehicles[self.vehicle_type]
39 seats = len(self.seats)
40 seating = list(vehicle.seating)
41 seating = list(islice(
42 chain(seating, repeat(None, seats)), 0, seats))
43 seating[seat_pos] = roach
44 # line below records new seating on the world proxy
45 vehicle.seating = seating
47 def roach_at(self, world, seat_pos):
48 roach_seating = world.vehicles[self.vehicle_type].seating
49 if seat_pos >= len(roach_seating):
51 return roach_seating[seat_pos]
54 return False # TODO: remove this
59 def current(cls, world):
60 return cls.by_type(world.vehicles.current)
63 def by_type(cls, vehicle_type):
64 return cls._vehicle_types.get(vehicle_type)()
67 def register(cls, vehicle_cls):
68 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
71 def register_all(cls):
72 from .walking import Walking
73 from .quadcopter import Quadcopter
74 from .robot import Robot
75 from .roomba import Roomba
77 cls.register(Quadcopter)
81 def _avatar_frame(self, i, suffix="_tiles"):
82 vehicle = images.load("vehicle%s/%s_%d" % (
83 suffix, self.vehicle_type, i + 1))
84 frame = vehicle.copy()
87 def get_avatar(self, world):
88 frames = [self._avatar_frame(i) for i in range(4)]
89 return AnimatedSurfActor(frames)
93 """ A space in a vehicle for a roach.
95 * pos -- (x, y) position of the seat relative to the centre of the vehicle.
96 x and y may be numbers from approximately -1.0 to 1.0. They will be
97 multiplied by the approximate_radius of the vehicle.
98 * roach -- name of the roach occupying the seat, if any.
99 * allowed -- f(roach) for checking whether a roach may occupy the
103 def __init__(self, vehicle, pos, roach=None, allowed=None):
104 self.vehicle = vehicle
107 self.allowed = allowed or (lambda roach: True)
108 vrad = vehicle.approximate_radius
109 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
113 "vehicles/%s/seat" % (self.vehicle.vehicle_type,))
114 selected_seat = seat.copy()
116 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
117 return SeatActor(seat, selected_seat)
120 class SeatActor(OrientatedSurfActor):
121 def __init__(self, seat, selected_seat):
122 self._selected = False
124 self._selected_seat = selected_seat
125 super().__init__(surf=self._seat, angle=0)
129 return self._selected
132 def selected(self, value):
133 self._selected = value
134 self.surf = self._selected_seat if value else self._seat
137 def circle_of_seats(n_seats, **kw):
138 d_theta = 2 * math.pi / n_seats
140 Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
141 for i in range(n_seats)]
144 Vehicle.register_all()