Refactor seating a bit.
[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     _vehicle_types = {}
22
23     @classmethod
24     def by_type(cls, vehicle_type):
25         return cls._vehicle_types.get(vehicle_type)()
26
27     @classmethod
28     def register(cls, vehicle_cls):
29         cls._vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls
30
31     @classmethod
32     def register_all(cls):
33         from .walking import Walking
34         cls.register(Walking)
35
36
37 class Seat:
38     """ A space in a vehicle for a roach.
39
40     * pos -- (x, y) position of the seat relative to the centre of the vehicle.
41       x and y may be numbers from approximately -1.0 to 1.0. They will be
42       multiplied by the approximate_radius of the vehicle.
43     * roach -- name of the roach occupying the seat, if any.
44     * allowed -- f(roach) for checking whether a roach may occupy the
45       seat.
46     """
47
48     def __init__(self, vehicle, pos, roach=None, allowed=None):
49         self.vehicle = vehicle
50         self.pos = pos
51         self.roach = roach
52         self.allowed = allowed or (lambda roach: True)
53         vrad = vehicle.approximate_radius
54         self.vehicle_pos = (pos[0] * vrad, pos[1] * vrad)
55
56     def actor(self):
57         return Actor("vehicles/%s/seat" % (self.vehicle.vehicle_type,))
58
59
60 Vehicle.register_all()