--- /dev/null
+# A doors / keypads set
+
+import os
+from pgzero.loaders import images
+from ..actors.orientatedsurf import OrientatedSurfActor
+from ..constants import TILE_SIZE
+
+
+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'))
+ self.game_pos = (x, y)
+ super().__init__(self._closed_surf, angle=angle, anchor=(0, 0))
+
+ def open(self):
+ self._closed = False
+ self.surf = self._open_surf
+
+ def is_closed(self):
+ return self._closed
--- /dev/null
+# A doors / keypads set
+
+import os
+from pgzero.loaders import images
+from ..actors.orientatedsurf import SurfActor
+from ..constants import TILE_SIZE
+
+
+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, anchor=(0, 0))
+
+ def activate(self):
+ # FIXME: Check stats
+ for door in self._doors:
+ door.open()
""" 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)
]
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.game_pos and door.is_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.game_pos and door.is_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.game_pos:
+ return keypad
+ return None
"starting pos": [
5,
6
+ ],
+ "door_info": [
+ {
+ "keypads": [
+ [
+ 6,
+ 7
+ ]
+ ],
+ "doors": [
+ [
+ 7,
+ 8
+ ]
+ ]
+ },
+ {
+ "keypads": [
+ [
+ 5,
+ 12
+ ]
+ ],
+ "doors": [
+ [
+ 7,
+ 12
+ ],
+ [
+ 4,
+ 13
+ ]
+ ]
+ },
+ {
+ "keypads": [
+ [
+ 18,
+ 9
+ ]
+ ],
+ "doors": [
+ [
+ 19,
+ 8
+ ]
+ ]
+ }
]
-}
\ No newline at end of file
+}
from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate
from ..gamelib.level import Level
+from ..gamelib.door import Door
+from ..gamelib.keypad import Keypad
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']:
+ x, y = door
+ # is this the right check
+ if self._level.can_walk(x + 1, y, 'floor') and self._level.can_walk(x - 1, y, 'floor'):
+ # vertical door
+ angle = 90
+ else:
+ angle = 0
+ new_door = Door(x, y, angle)
+ self._level.doors.append(new_door)
+ # Add the keypads
+ for keypad in door_info['keypads']:
+ new_keypad = Keypad(keypad[0], keypad[1], doors)
+ self._level.keypads.append(new_keypad)
return self._level
def _load_tile_images(self):
self._viewport = (self._viewport[0] + offset[0],
self._viewport[1] + offset[1])
+ def calc_offset(self, x, y):
+ """Return a position offset by the viewport"""
+ return x - self._viewport[0], y - self._viewport[1]
+
@apply_events
def update(self, dt):
- return self._scene.update(self._world.proxy(), dt)
+ return self._scene.update(self._world.proxy(), self, dt)
def draw(self):
self._scene.draw(self._app.screen, self._viewport)
def exit(self, world):
pass
- def update(self, world, dt):
+ def update(self, world, engine, dt):
pass
def draw(self, screen, viewport):
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=9)
+ self._keypads = self.actors.add_layer("keypads", level=8)
+ # These are already Actors
+ for door in self._level.doors:
+ self._doors.add(door)
+ for keypad in self._level.keypads:
+ self._keypads.add(keypad)
def _render(self, layer):
# We cache the rendered surface to avoid doing a large number
surface.blit(tile[layer_key], pos)
return surface.convert_alpha()
+ def update(self, world, engine, dt):
+ """Fix the door and keypad positions"""
+ for door in self._doors:
+ door.pos = engine.calc_offset(door.game_pos[0] * TILE_SIZE, door.game_pos[1] * TILE_SIZE)
+ for keypad in self._keypads:
+ keypad.pos = engine.calc_offset(keypad.game_pos[0] * TILE_SIZE, keypad.game_pos[1] * TILE_SIZE)
+
def draw(self, screen, viewport):
screen.clear()
# Viewport is the position of the screen relative to the
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':
def _eject_roach(self):
print("Eject roach.")
- def update(self, world, dt):
+ def update(self, world, engine, dt):
self._update_inventory(world)
self._update_roaches(world)