diff --git a/floor.py b/floor.py index 9bc4b04..561c95e 100644 --- a/floor.py +++ b/floor.py @@ -9,6 +9,8 @@ class TileType(Enum): WALL = 1 AIR = 2 +COLLIDABLE = {TileType.WALL} + class EntityMap: """ A class that stores entities in an special way in order to be able to reach in O(1) the values @@ -57,10 +59,17 @@ class Floor: A class that represents a specific floor of the dungeon """ def __init__(self, width, height): + print(height, width) self.width = width self.height = height - self.grid = [[TileType.AIR] * height] * width + self.grid = [] + for row in range(width): + self.grid.append([]) + for tile in range(height): + self.grid[row].append(TileType.AIR) + self.grid[0][0] = TileType.WALL self.entities = EntityMap() def get_tile(self, x, y) -> TileType: + print(x,y) return self.grid[x][y] diff --git a/game.py b/game.py index a98ab43..2be6e9b 100644 --- a/game.py +++ b/game.py @@ -1,13 +1,14 @@ from terminal import Terminal from entity import Creature, Player, Enemy from actions import * -from floor import Floor, TileType +from floor import COLLIDABLE, Floor, TileType import sys TEXTURES = { TileType.WALL: '#', TileType.AIR: '.', - Player: '@' + Player: '@', + Enemy: 'Y' } class Game: @@ -42,6 +43,7 @@ class Game: Runs the game """ self.instance_creature(3, 3, self.player) + self.instance_creature(6, 4, Enemy()) self.render() while not self.should_exit: self.step() @@ -72,7 +74,7 @@ class Game: for x in range(self.floor.width): for y in range(self.floor.height): if (x, y) in self.floor.entities.pos_creatures: - self.term.put_char(x, y, TEXTURES[Player]) + self.term.put_char(x, y, TEXTURES[self.floor.entities.pos_creatures[(x,y)].__class__]) elif (x, y) in self.floor.entities.items: self.term.put_char(x, y, 'i') else: @@ -85,13 +87,13 @@ class Game: self.action_was_performed = False creatures = self.schedule.get(self.ticks) if creatures: - for creature in creatures: + while creature := creatures[0] if len(creatures) > 0 else None: if isinstance(creature, Player): self.input_event() elif isinstance(creature, Enemy): creature.calculate_action() - self.reschedule(creature) - self.perform(creature) + if self.perform(creature): + creatures.pop(0) self.schedule.pop(self.ticks) self.action_was_performed = True self.ticks += 1 @@ -103,7 +105,16 @@ class Game: if isinstance(creature.action, Move): pos = self.floor.entities.pop_creature_ref(creature) if pos: - self.floor.entities.add_creature(pos[0] + creature.action.x, pos[1] + creature.action.y, creature) + (dest_x, dest_y) = (pos[0] + creature.action.x, pos[1] + creature.action.y) + out_of_bounds = dest_x < 0 or dest_x > self.floor.width-1 or dest_y < 0 or dest_y > self.floor.height-1 + if out_of_bounds \ + or self.floor.get_tile(dest_x, dest_y) in COLLIDABLE \ + or not self.floor.entities.add_creature(dest_x, dest_y, creature): + self.floor.entities.add_creature(pos[0], pos[1], creature) + return False + + self.reschedule(creature) + return True def input_event(self): """