1 """ Base class for vehicles. """
3 from itertools import chain, islice, repeat
4 from pygame.constants import BLEND_RGBA_MULT
5 from pgzero.actor import Actor
6 from pgzero.loaders import images
7 from ..actors.orientatedsurf import OrientatedSurfActor
11 """ Vehicle base class. """
14 approximate_radius = 200
15 selected_seat_overlay_color = (255, 0, 0, 255)
18 self.seats = self.init_seats()
20 def roach_management_overlay(self):
21 return Actor("vehicles/%s/background" % (self.vehicle_type,))
24 raise NotImplementedError("Vehicles should specify a list of seats")
26 def seating(self, world):
27 roach_seating = world.vehicles[self.vehicle_type].seating
28 roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
31 for seat_pos, (roach, _) in roach_seating_numbers if roach
34 def seat_roach(self, world, roach, seat_pos):
35 vehicle = world.vehicles[self.vehicle_type]
36 seats = len(self.seats)
37 seating = list(vehicle.seating)
38 seating = list(islice(
39 chain(seating, repeat(None, seats)), 0, seats))
40 seating[seat_pos] = roach
41 # line below records new seating on the world proxy
42 vehicle.seating = seating
47 def current(cls, world):
48 return cls.by_type(world.vehicles.current)
51 def by_type(cls, vehicle_type):
52 return cls._vehicle_types.get(vehicle_type)()
55 def register(cls, vehicle_cls):
56 cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
59 def register_all(cls):
60 from .walking import Walking
65 """ A space in a vehicle for a roach.
67 * pos -- (x, y) position of the seat relative to the centre of the vehicle.
68 x and y may be numbers from approximately -1.0 to 1.0. They will be
69 multiplied by the approximate_radius of the vehicle.
70 * roach -- name of the roach occupying the seat, if any.
71 * allowed -- f(roach) for checking whether a roach may occupy the
75 def __init__(self, vehicle, pos, roach=None, allowed=None):
76 self.vehicle = vehicle
79 self.allowed = allowed or (lambda roach: True)
80 vrad = vehicle.approximate_radius
81 self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
85 "vehicles/%s/seat" % (self.vehicle.vehicle_type,))
86 selected_seat = seat.copy()
88 self.vehicle.selected_seat_overlay_color, None, BLEND_RGBA_MULT)
89 return SeatActor(seat, selected_seat)
92 class SeatActor(OrientatedSurfActor):
93 def __init__(self, seat, selected_seat):
94 self._selected = False
96 self._selected_seat = selected_seat
97 super().__init__(surf=self._seat, angle=0)
101 return self._selected
104 def selected(self, value):
105 self._selected = value
106 self.surf = self._selected_seat if value else self._seat
109 Vehicle.register_all()