Implement 5s rule for seating.
[koperkapel.git] / koperkapel / vehicles / base.py
1 """ Base class for vehicles.  """
2
3 from pgzero.actor import Actor
4
5
6 class Vehicle:
7     """ Vehicle base class. """
8
9     vehicle_type = None
10     approximate_radius = 200
11
12     def __init__(self):
13         self.seats = self.init_seats()
14
15     def roach_management_overlay(self):
16         return Actor("vehicles/%s/background" % (self.vehicle_type,))
17
18     def init_seats(self):
19         raise NotImplementedError("Vehicles should specify a list of seats")
20
21     def seating(self, world):
22         roach_seating = world.vehicles[self.vehicle_type].seating
23         roach_seating_numbers = enumerate(zip(roach_seating, self.seats))
24         return {
25             roach: seat_pos
26             for seat_pos, (roach, _) in roach_seating_numbers if roach
27         }
28
29     _vehicle_types = {}
30
31     @classmethod
32     def current(cls, world):
33         return cls.by_type(world.vehicles.current)
34
35     @classmethod
36     def by_type(cls, vehicle_type):
37         return cls._vehicle_types.get(vehicle_type)()
38
39     @classmethod
40     def register(cls, vehicle_cls):
41         cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
42
43     @classmethod
44     def register_all(cls):
45         from .walking import Walking
46         cls.register(Walking)
47
48
49 class Seat:
50     """ A space in a vehicle for a roach.
51
52     * pos -- (x, y) position of the seat relative to the centre of the vehicle.
53       x and y may be numbers from approximately -1.0 to 1.0. They will be
54       multiplied by the approximate_radius of the vehicle.
55     * roach -- name of the roach occupying the seat, if any.
56     * allowed -- f(roach) for checking whether a roach may occupy the
57       seat.
58     """
59
60     def __init__(self, vehicle, pos, roach=None, allowed=None):
61         self.vehicle = vehicle
62         self.pos = pos
63         self.roach = roach
64         self.allowed = allowed or (lambda roach: True)
65         vrad = vehicle.approximate_radius
66         self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
67
68     def actor(self):
69         return Actor("vehicles/%s/seat" % (self.vehicle.vehicle_type,))
70
71
72 Vehicle.register_all()