From 923be78dc52aa3334d658a8958672d0dfe23ac6a Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 5 Mar 2016 13:35:36 +0200 Subject: [PATCH] Door hacking --- koperkapel/gamelib/door.py | 22 ++++++++++++++ koperkapel/gamelib/keypad.py | 19 ++++++++++++ koperkapel/gamelib/level.py | 27 +++++++++++++++-- koperkapel/levels/level1.json | 48 +++++++++++++++++++++++++++++++ koperkapel/loaders/levelloader.py | 13 +++++++++ koperkapel/scenes/level.py | 8 ++++++ 6 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 koperkapel/gamelib/door.py create mode 100644 koperkapel/gamelib/keypad.py diff --git a/koperkapel/gamelib/door.py b/koperkapel/gamelib/door.py new file mode 100644 index 0000000..9deadca --- /dev/null +++ b/koperkapel/gamelib/door.py @@ -0,0 +1,22 @@ +# A doors / keypads set + +import os +from pgzero.loaders import images +from ..actors.orientatedsurf import OrientatedSurfActor + + +class Door(OrientatedSurfActor): + + def __init__(self, x, y, angle): + self._closed = True + self._open_surf = images.load(os.path.join('fixtures', 'door_open')) + self._closed_surf = images.load(os.path.join('fixtures', 'door_closed')) + self.game_pos = (x, y) + super().__init__(self._closed_surf, angle) + + def open(self): + self._closed = False + self.surf = self._open_surf + + def is_closed(self): + return self._closed diff --git a/koperkapel/gamelib/keypad.py b/koperkapel/gamelib/keypad.py new file mode 100644 index 0000000..6c6d1c8 --- /dev/null +++ b/koperkapel/gamelib/keypad.py @@ -0,0 +1,19 @@ +# A doors / keypads set + +import os +from pgzero.loaders import images +from ..actors.orientatedsurf import SurfActor + + +class Keypad(SurfActor): + + def __init__(self, x, y, doors): + self._doors = doors + self.game_pos = (x, y) + surf = images.load(os.path.join('fixtures', 'keypad')) + super().__init__(surf) + + def activate(self): + # FIXME: Check stats + for door in self._doors: + door.open() diff --git a/koperkapel/gamelib/level.py b/koperkapel/gamelib/level.py index 01c260e..4644e04 100644 --- a/koperkapel/gamelib/level.py +++ b/koperkapel/gamelib/level.py @@ -1,11 +1,16 @@ """ Class holding the level info """ +from .keypad import Keypad +from .door import Door + class Level(object): def __init__(self): self.width = self.height = 0 self.tiles = [] + self.keypads = [] + self.doors = [] self.tileset = None self.start_pos = (0, 0) @@ -18,10 +23,28 @@ class Level(object): ] def can_walk(self, x, y, layer): - return 'walk' in self.tiles[y][x][layer]['behaviour'] + if 'walk' in self.tiles[y][x][layer]['behaviour']: + # check doors + for door in self.doors: + if (x, y) == door.pos and door.closed(): + return False + return True + return False def can_fly(self, x, y, layer): - return 'fly' in self.tiles[y][x][layer]['behaviour'] + if 'fly' in self.tiles[y][x][layer]['behaviour']: + for door in self.doors: + if (x, y) == door.pos and door.closed(): + return False + return True + + return False def can_crawl(self, x, y, layer): return 'crawl' in self.tiles[y][x][layer]['behaviour'] + + def keypad_at(self, x, y): + for keypad in self.keypads: + if (x, y) == keypad.pos: + return keypad + return None diff --git a/koperkapel/levels/level1.json b/koperkapel/levels/level1.json index 38a872a..b4a4a19 100644 --- a/koperkapel/levels/level1.json +++ b/koperkapel/levels/level1.json @@ -4289,5 +4289,53 @@ "starting pos": [ 5, 6 + ], + "door_info": [ + { + "keypads": [ + [ + 6, + 7 + ] + ], + "doors": [ + [ + 7, + 8 + ] + ] + }, + { + "keypads": [ + [ + 12, + 5 + ] + ], + "doors": [ + [ + 12, + 7 + ], + [ + 13, + 4 + ] + ] + }, + { + "keypads": [ + [ + 18, + 9 + ] + ], + "doors": [ + [ + 19, + 8 + ] + ] + } ] } \ No newline at end of file diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index 0addecc..b5572ec 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -6,6 +6,8 @@ from pgzero.loaders import ResourceLoader from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate from ..gamelib.level import Level +from ..gamelib.door import Door +from ..gamelib.keypad import Keypad @@ -40,6 +42,17 @@ class LevelLoader(ResourceLoader): for tile in TILES.values(): tile.TILESET = self._level.tileset self._load_tile_images() + for door_info in level_data['door_info']: + # Create the doors first + doors = [] + for door in door_info['doors']: + # Sort out angle + new_door = Door(0, door[0], door[1]) + self._level.doors.append(new_door) + # Add the keypads + for keypad in door_info['keypads']: + new_keypad = Keypad(doors, keypad[0], keypad[1]) + self._level.keypads.append(new_keypad) return self._level def _load_tile_images(self): diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 2484119..a16e99f 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -21,6 +21,13 @@ class BaseLevelScene(Scene): for layer in ['floor', 'tunnels']: self._surfaces[layer] = self._render(layer) self._overlay = self._surfaces['floor'].copy() + self._doors = self.actors.add_layer("doors", level=15) + self._keypads = self.actors.add_layer("keypads", level=16) + # These are already Actors + for door in self._level.doors: + self._doors.append(door) + for keypad in self._level.keypads: + self._keypads.append(keypads) def _render(self, layer): # We cache the rendered surface to avoid doing a large number @@ -78,6 +85,7 @@ class GameLevelScene(BaseLevelScene): def _set_pos(self, x, y): self._player_pos = (x, y) + print('Now at ', x, y) def _can_move(self, x, y): if self._mode == 'walk': -- 2.34.1