From 6c6ebfbe8d8cbd476c94a5030bc1ab9e7d858f6d Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 4 Mar 2016 00:35:33 +0200 Subject: [PATCH] Roaches on foot. --- koperkapel/vehicles/base.py | 45 ++++++++++++++++++++++++++++++++++ koperkapel/vehicles/walking.py | 17 +++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 koperkapel/vehicles/base.py create mode 100644 koperkapel/vehicles/walking.py diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py new file mode 100644 index 0000000..27373a5 --- /dev/null +++ b/koperkapel/vehicles/base.py @@ -0,0 +1,45 @@ +""" Base class for vehicles. """ + + +class Vehicle: + """ Vehicle base class. + + A vehicle should have the following attributes: + + * seats -- list of roach seats. + * background -- actor representing background for management scene + """ + + vehicle_types = {} + approximate_radius = 200 + + @classmethod + def by_type(cls, vehicle_type): + return cls.vehicle_types.get(vehicle_type)() + + @classmethod + def register(cls, vehicle_cls): + cls.vehicle_types[vehicle_cls.__name__.lower()] = vehicle_cls + + @classmethod + def register_all(cls): + from .walking import Walking + cls.register(Walking) + + +class Seat: + """ A space in a vehicle for a roach. + + * pos -- (x, y) position of the seat relative to the centre of the vehicle. + x and y may be numbers from approximately -1.0 to 1.0. They will be + multiplied by the approximate_radius of the vehicle. + * allowed -- f(roach) for checking whether a roach may occupy the + seat + """ + + def __init__(self, pos, allowed=None): + self.pos = pos + self.allowed = allowed or (lambda roach: True) + + +Vehicle.register_all() diff --git a/koperkapel/vehicles/walking.py b/koperkapel/vehicles/walking.py new file mode 100644 index 0000000..f819892 --- /dev/null +++ b/koperkapel/vehicles/walking.py @@ -0,0 +1,17 @@ +""" A vehicle to represent roaches on foot. """ + +import math +from .base import Vehicle, Seat +from ..actors.buttons import TextButton + + +class Walking(Vehicle): + + def __init__(self): + n_seats = 6 + d_theta = 2 * math.pi / n_seats + self.seats = [ + Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta))) + for i in range(n_seats) + ] + self.background = TextButton("Walking Background") -- 2.34.1