From: Simon Cross Date: Sat, 5 Mar 2016 15:10:42 +0000 (+0200) Subject: Moar vehicles! X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=350438a376706e28ab1d8de5ad78875bd99815bb;p=koperkapel.git Moar vehicles! --- diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py index 47588a5..551826f 100644 --- a/koperkapel/vehicles/base.py +++ b/koperkapel/vehicles/base.py @@ -1,10 +1,12 @@ """ Base class for vehicles. """ +import math from itertools import chain, islice, repeat from pygame.constants import BLEND_RGBA_MULT from pgzero.actor import Actor from pgzero.loaders import images from ..actors.orientatedsurf import OrientatedSurfActor +from ..actors.animsurf import AnimatedSurfActor class Vehicle: @@ -48,6 +50,9 @@ class Vehicle: return None return roach_seating[seat_pos] + def changed(self): + return False # TODO: remove this + _vehicle_types = {} @classmethod @@ -65,10 +70,23 @@ class Vehicle: @classmethod def register_all(cls): from .walking import Walking + from .quadcopter import Quadcopter + from .robot import Robot + from .roomba import Roomba cls.register(Walking) + cls.register(Quadcopter) + cls.register(Robot) + cls.register(Roomba) + + def _avatar_frame(self, i, suffix="_tiles"): + vehicle = images.load("vehicle%s/%s_%d" % ( + suffix, self.vehicle_type, i + 1)) + frame = vehicle.copy() + return frame def get_avatar(self, world): - raise NotImplementedError("Vehicles should know how to create their own avatars.") + frames = [self._avatar_frame(i) for i in range(4)] + return AnimatedSurfActor(frames) class Seat: @@ -116,4 +134,11 @@ class SeatActor(OrientatedSurfActor): self.surf = self._selected_seat if value else self._seat +def circle_of_seats(n_seats, **kw): + d_theta = 2 * math.pi / n_seats + return [ + Seat(pos=(math.sin(i * d_theta), math.cos(i * d_theta)), **kw) + for i in range(n_seats)] + + Vehicle.register_all() diff --git a/koperkapel/vehicles/quadcopter.py b/koperkapel/vehicles/quadcopter.py new file mode 100644 index 0000000..ed5024f --- /dev/null +++ b/koperkapel/vehicles/quadcopter.py @@ -0,0 +1,11 @@ +""" Flying roaches! """ + +from .base import Vehicle, circle_of_seats + + +class Quadcopter(Vehicle): + + vehicle_type = "quadcopter" + + def init_seats(self): + return circle_of_seats(4, vehicle=self) diff --git a/koperkapel/vehicles/robot.py b/koperkapel/vehicles/robot.py new file mode 100644 index 0000000..50ebc13 --- /dev/null +++ b/koperkapel/vehicles/robot.py @@ -0,0 +1,11 @@ +""" Roach Robot! """ + +from .base import Vehicle, circle_of_seats + + +class Robot(Vehicle): + + vehicle_type = "robot" + + def init_seats(self): + return circle_of_seats(8, vehicle=self) diff --git a/koperkapel/vehicles/roomba.py b/koperkapel/vehicles/roomba.py new file mode 100644 index 0000000..ff93f8a --- /dev/null +++ b/koperkapel/vehicles/roomba.py @@ -0,0 +1,11 @@ +""" Roaches do not abhor a vacuum. """ + +from .base import Vehicle, circle_of_seats + + +class Roomba(Vehicle): + + vehicle_type = "roomba" + + def init_seats(self): + return circle_of_seats(6, vehicle=self) diff --git a/koperkapel/vehicles/walking.py b/koperkapel/vehicles/walking.py index 6b57dff..f7b73aa 100644 --- a/koperkapel/vehicles/walking.py +++ b/koperkapel/vehicles/walking.py @@ -1,22 +1,16 @@ """ A vehicle to represent roaches on foot. """ -import math -from .base import Vehicle, Seat -from ..roaches import default_roaches, roaches_quartet, roaches_nonet, WorldRoach +from .base import Vehicle, circle_of_seats +from ..roaches import ( + default_roaches, roaches_quartet, roaches_nonet, WorldRoach) + class Walking(Vehicle): vehicle_type = "walking" def init_seats(self): - n_seats = 6 - d_theta = 2 * math.pi / n_seats - return [ - Seat( - vehicle=self, - pos=(math.sin(i * d_theta), math.cos(i * d_theta))) - for i in range(n_seats) - ] + return circle_of_seats(6, vehicle=self) def get_avatar(self, world): num_roaches = len(world.roaches) @@ -32,6 +26,3 @@ class Walking(Vehicle): avatar = roaches_nonet.assemble(roach) avatar.anchor = (0, 0) return avatar - - def changed(self): - return False