From 8795602b8b39d5dce884310b11fc9b0a21263ddb Mon Sep 17 00:00:00 2001 From: adrianna Date: Sat, 5 Mar 2016 23:41:54 +0200 Subject: [PATCH] get in and out of vehicles --- koperkapel/gamelib/items.py | 15 +++++++++++++++ koperkapel/levels/level1.json | 5 +++++ koperkapel/scenes/level.py | 29 +++++++++++++++++++++++------ koperkapel/vehicles/base.py | 12 ++++++++---- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/koperkapel/gamelib/items.py b/koperkapel/gamelib/items.py index b611250..14bf61d 100644 --- a/koperkapel/gamelib/items.py +++ b/koperkapel/gamelib/items.py @@ -2,6 +2,7 @@ from ..serums import default_serums from ..weapons import default_weapons +from ..vehicles.base import Vehicle def build_serum(item): @@ -13,10 +14,15 @@ def build_weapon(item): actor = default_weapons.assemble(item["weapon"]) return actor +def build_vehicle(item): + actor = Vehicle.by_type(item["vehicle"]).get_avatar(dead=True) + return actor + ITEM_BUILDERS = { "serum": build_serum, "weapon": build_weapon, + "vehicle": build_vehicle, } @@ -39,3 +45,12 @@ def clone_old_item(item, **new_data): new_item.update(item.item_data) new_item.update(new_data) return get_item(new_item) + + +def create_new_item(item_type, pos, **kwd): + new_item = { + "type": item_type, + "pos": pos, + } + new_item.update(kwd) + return get_item(new_item) diff --git a/koperkapel/levels/level1.json b/koperkapel/levels/level1.json index 7ec08b5..f5e920a 100644 --- a/koperkapel/levels/level1.json +++ b/koperkapel/levels/level1.json @@ -4348,6 +4348,11 @@ "serum": "fast", "pos": [2, 4] }, + { + "type": "vehicle", + "vehicle": "roomba", + "pos": [2, 7] + }, { "type": "serum", "serum": "smart", diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index b17beb6..c5a847b 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -7,7 +7,7 @@ from ..loaders.levelloader import levels from .base import ( Scene, ChangeSceneEvent, MoveViewportEvent, WorldEvent, defer_to_update) from ..constants import TILE_SIZE, WIDTH, HEIGHT -from ..gamelib.items import clone_old_item +from ..gamelib.items import clone_old_item, create_new_item from ..roaches import build_roach from ..vehicles.base import Vehicle from ..weapons import weapon_by_name @@ -231,8 +231,24 @@ class GameLevelScene(BaseLevelScene): @defer_to_update def _change_vehicle(self, world): - vehicle = Vehicle.random() - world.vehicles.current = vehicle + x, y = self._player_pos + + old_vehicle = world.vehicles.current + + item = self._level.item_at(x, y) + + if item and item.item_type == "vehicle": + world.vehicles.current = item.item_data["vehicle"] + self._level.remove_item(item) + self._items.remove(item) + else: + world.vehicles.current = "walking" + + if old_vehicle != "walking": + dropped_vehicle = create_new_item("vehicle", (x, y), vehicle=old_vehicle) + self._level.items.append(dropped_vehicle) + self._items.add(dropped_vehicle) + self._vehicle_changed() def update(self, world, engine, dt): @@ -316,9 +332,10 @@ class GameLevelScene(BaseLevelScene): self._add_roach() elif self._level.item_at(x, y): item = self._level.item_at(x, y) - self._level.remove_item(item) - self._items.remove(item) - self._gain_item(item) + if item.item_type != "vehicle": + self._level.remove_item(item) + self._items.remove(item) + self._gain_item(item) elif self._level.is_exit(x, y): next_level = self._level.get_exit_level() return [ diff --git a/koperkapel/vehicles/base.py b/koperkapel/vehicles/base.py index 2c1d9bf..258ffe0 100644 --- a/koperkapel/vehicles/base.py +++ b/koperkapel/vehicles/base.py @@ -96,13 +96,17 @@ class Vehicle: vehicle = images.load(safepath("vehicle%s/%s_%d") % ( suffix, self.vehicle_type, i + 1)) frame = vehicle.copy() - frame.blit(weapon.surf, (0, 0)) + if weapon: + frame.blit(weapon.surf, (0, 0)) return frame - def get_avatar(self, world): - weapon = default_weapons.assemble( + def get_avatar(self, world=None, dead=False): + if not dead: + weapon = default_weapons.assemble( world.weapons.current, tape=self.weapons_taped_on) - frames = [self._avatar_frame(i, weapon) for i in range(4)] + frames = [self._avatar_frame(i, weapon) for i in range(4)] + else: + frames = [self._avatar_frame(0, weapon=None)] return AnimatedSurfActor(frames, anchor=(0, 0)) -- 2.34.1