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 OrientatedSurfActor
9 from ..actors.animsurf import AnimatedSurfActor
13 """ Vehicle base class. """
16 overlay_frame_no = None
17 approximate_radius = 200
18 selected_seat_overlay_color = (255, 0, 0, 255)
21 self.seats = self.init_seats()
22 self.game_pos = (0, 0)
24 def roach_management_overlay(self):
25 return images.load("vehicles/walking/background")
27 def roach_management_frame(self):
28 if self.overlay_frame_no is None:
30 return images.load("vehicle_big/%s_%d" % (
31 self.vehicle_type, self.overlay_frame_no))
34 raise NotImplementedError("Vehicles should specify a list of seats")
36 def seating(self, world):
37 roach_seating = world.vehicles[self.vehicle_type].seating
38 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
41 for seat_pos, (roach, _) in roach_seating_numbers if roach
44 def seat_roach(self, world, roach, seat_pos):
45 vehicle = world.vehicles[self.vehicle_type]
46 seats = len(self.seats)
47 seating = list(vehicle.seating)
48 seating = list(islice(
49 chain(seating, repeat(None, seats)), 0, seats))
50 seating[seat_pos] = roach
51 # line below records new seating on the world proxy
52 vehicle.seating = seating
54 def roach_at(self, world, seat_pos):
55 roach_seating = world.vehicles[self.vehicle_type].seating
56 if seat_pos >= len(roach_seating):
58 return roach_seating[seat_pos]
61 return False # TODO: remove this
66 def current(cls, world):
67 return cls.by_type(world.vehicles.current)
70 def by_type(cls, vehicle_type):
71 return cls._vehicle_types.get(vehicle_type)()
74 def register(cls, vehicle_cls):
75 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
79 return random.choice(list(cls._vehicle_types.keys()))
82 def register_all(cls):
83 from .walking import Walking
84 from .quadcopter import Quadcopter
85 from .robot import Robot
86 from .roomba import Roomba
88 cls.register(Quadcopter)
92 def _avatar_frame(self, i, suffix="_tiles"):
93 vehicle = images.load("vehicle%s/%s_%d" % (
94 suffix, self.vehicle_type, i + 1))
95 frame = vehicle.copy()
98 def get_avatar(self, world):
99 frames = [self._avatar_frame(i) for i in range(4)]
100 return AnimatedSurfActor(frames, anchor=(0, 0))
104 """ A space in a vehicle for a roach.
106 * pos -- (x, y) position of the seat relative to the centre of the vehicle.
107 x and y may be numbers from approximately -1.0 to 1.0. They will be
108 multiplied by the approximate_radius of the vehicle.
109 * roach -- name of the roach occupying the seat, if any.
110 * allowed -- f(roach) for checking whether a roach may occupy the
114 def __init__(self, vehicle, pos, roach=None, allowed=None):
115 self.vehicle = vehicle
118 self.allowed = allowed or (lambda roach: True)
119 vrad = vehicle.approximate_radius
120 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
123 seat = images.load("vehicles/walking/seat")
124 selected_seat = seat.copy()
126 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
127 return SeatActor(seat, selected_seat)
130 class SeatActor(OrientatedSurfActor):
131 def __init__(self, seat, selected_seat):
132 self._selected = False
134 self._selected_seat = selected_seat
135 super().__init__(surf=self._seat, angle=0)
139 return self._selected
142 def selected(self, value):
143 self._selected = value
144 self.surf = self._selected_seat if value else self._seat
147 def circle_of_seats(n_seats, **kw):
148 d_theta = 2 * math.pi / n_seats
150 Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw)
151 for i in range(n_seats)]
154 Vehicle.register_all()