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