roguepy/floor.py

75 lines
2.2 KiB
Python

from enum import Enum
from typing import Tuple
from entity import Creature, Item
class TileType(Enum):
"""
An enum who has the different types of tiles
"""
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
"""
def __init__(self):
self.items = {} # K: Position V: List(Item)
self.pos_creatures = {} # K: Position V: Creature
self.creatures_pos = {} # K: Creature V: Position
def get_item(self, x, y) -> Item | None:
return self.items.get((x, y))
def get_creature(self, x, y) -> Item | None:
return self.pos_creatures.get((x, y))
def get_creature_position(self, creature: Creature) -> Tuple[int, int]:
return self.creatures_pos[creature]
def add_item(self, x, y, item: Item) -> None:
if (x, y) not in self.items:
self.items[(x, y)] = []
self.items[(x, y)].append(item)
def add_creature(self, x, y, creature: Creature) -> bool:
if (x, y) in self.pos_creatures:
return False
else:
self.pos_creatures[(x, y)] = creature
self.creatures_pos[creature] = (x, y)
return True
def pop_creature_pos(self, x, y) -> Creature | None:
if (x, y) in self.pos_creatures:
creature = self.pos_creatures.pop((x, y))
self.creatures_pos.pop(creature)
return creature
def pop_creature_ref(self, creature: Creature) -> Tuple[int, int] | None:
if creature in self.creatures_pos:
pos = self.creatures_pos.pop(creature)
self.pos_creatures.pop(pos)
return pos
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 = []
for row in range(width):
self.grid.append([])
for _ 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:
return self.grid[x][y]