From bc8a030300c8367c5412585ffe37c07ce2d7bcee Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 5 Mar 2016 15:24:18 +0200 Subject: [PATCH] Very crudely hack vehicles into the level --- koperkapel/roaches.py | 11 +++++++++++ koperkapel/scenes/level.py | 31 ++++++++++++++++++++----------- koperkapel/vehicles/base.py | 4 ++++ koperkapel/vehicles/walking.py | 19 ++++++++++++++++++- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/koperkapel/roaches.py b/koperkapel/roaches.py index 0d24482..b39d189 100644 --- a/koperkapel/roaches.py +++ b/koperkapel/roaches.py @@ -26,6 +26,15 @@ class RoachActor(OrientatedSurfActor): self.surf = self._frames[self._frame] +class WorldRoach(object): + """A roach proxy with no properties for display on the game level.""" + + def __init__(self): + self.smart = False + self.strong = False + self.fast = False + + class RoachFactory: def __init__(self, suffix, frames=4): @@ -52,3 +61,5 @@ default_roaches = RoachFactory("") t32_roaches = RoachFactory("_32") t21_roaches = RoachFactory("_21") big_roaches = RoachFactory("_big") +roaches_quartet = RoachFactory("_quartet") +roaches_nonet = RoachFactory("_nonet") diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index ea37e15..d2c7d16 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -6,7 +6,7 @@ import pygame.locals as pgl from ..loaders.levelloader import levels from .base import Scene, ChangeSceneEvent, MoveViewportEvent from ..constants import TILE_SIZE, WIDTH, HEIGHT -from ..roaches import default_roaches +from ..vehicles.base import Vehicle class BaseLevelScene(Scene): @@ -76,17 +76,18 @@ class GameLevelScene(BaseLevelScene): def enter(self, world): super().enter(world) self._roaches = self.actors.add_layer("roaches", level=10) + self._vehicle = Vehicle.current(world) self._mode = 'walk' - return self._init_roaches(world.roaches) + self._angle = 0 + return self._init_roaches(world) - def _init_roaches(self, roaches): - for roach in roaches: - roach_actor = self._roaches.add(default_roaches.assemble(roach)) - roach_actor.anchor = (0, 0) - roach_actor.pos = (WIDTH // 2, HEIGHT // 2) + def _init_roaches(self, world): x, y = self._level.start_pos - self._set_pos(x, y) self._level_layer = 'floor' + self._avatar = self._vehicle.get_avatar(world) + self._set_pos(x, y) + self._avatar.pos = (WIDTH // 2, HEIGHT // 2) + self._roaches.add(self._avatar) # Fix viewport offset return [MoveViewportEvent((x * TILE_SIZE - WIDTH // 2, y * TILE_SIZE - HEIGHT // 2))] @@ -103,8 +104,17 @@ class GameLevelScene(BaseLevelScene): return self._level.can_crawl(x, y, self._level_layer) def _set_angle(self, angle): - for roach in self._roaches: - roach.angle = angle + self._angle = angle + self._avatar.angle = angle + + def update(self, world, engine, dt): + if self._vehicle.changed(): + self._roaches.remove(self._avatar) + self._avatar = self._vehicle.get_avatar(world) + self._avatar.pos = (WIDTH // 2, HEIGHT // 2) + self._roaches.add(self._avatar) + self._avatar.set_angle(self._angle) + super().update(world, engine, dt) def on_key_down(self, key, mod, unicode): offset = None @@ -149,7 +159,6 @@ class GameLevelScene(BaseLevelScene): elif key == keys.V: # Leave vehicle print('Vehicle key pressed') - if offset: return [MoveViewportEvent(offset)] return super(GameLevelScene, self).on_key_down(key, mod, unicode) diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py index 31f7eea..ba4fbb7 100644 --- a/koperkapel/vehicles/base.py +++ b/koperkapel/vehicles/base.py @@ -16,6 +16,7 @@ class Vehicle: def __init__(self): self.seats = self.init_seats() + self.game_pos = (0, 0) def roach_management_overlay(self): return Actor("vehicles/%s/background" % (self.vehicle_type,)) @@ -60,6 +61,9 @@ class Vehicle: from .walking import Walking cls.register(Walking) + def get_avatar(self, world): + raise NotImplementedError("Vehicles should know how to create their own avatars.") + class Seat: """ A space in a vehicle for a roach. diff --git a/koperkapel/vehicles/walking.py b/koperkapel/vehicles/walking.py index 9c19aa2..aa88bc6 100644 --- a/koperkapel/vehicles/walking.py +++ b/koperkapel/vehicles/walking.py @@ -2,7 +2,7 @@ import math from .base import Vehicle, Seat - +from ..roaches import default_roaches, roaches_quartet, roaches_nonet, WorldRoach class Walking(Vehicle): @@ -17,3 +17,20 @@ class Walking(Vehicle): pos=(math.sin(i * d_theta), math.cos(i * d_theta))) for i in range(n_seats) ] + + def get_avatar(self, world): + num_roaches = len(world.roaches) + roach = WorldRoach() + if num_roaches == 1: + # Return a single large roach + avatar = default_roaches.assemble(roach) + avatar.anchor = (0, 0) + elif num_roaches < 6: + avatar = roaches_quartet.assemble(roach) + avatar.anchor = (-16, 0) + else: + avatar = roaches_nonet.assemble(roach) + return avatar + + def changed(self): + return False -- 2.34.1