61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from enum import Enum
|
|
from typing import Tuple
|
|
from entity import Character, Entity, Item
|
|
|
|
class TileType(Enum):
|
|
WALL = 1
|
|
AIR = 2
|
|
|
|
class EntityMap:
|
|
"""
|
|
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):
|
|
return self.items.get((x, y))
|
|
|
|
def get_creature(self, x, y):
|
|
return self.pos_creatures.get((x, y))
|
|
|
|
def get_creature_position(self, creature: Character):
|
|
return self.creatures_pos.get(creature)
|
|
|
|
def add_item(self, x, y, item: Item):
|
|
if (x, y) not in self.items:
|
|
self.items[(x, y)] = []
|
|
self.items[(x, y)].append(item)
|
|
|
|
def add_creature(self, x, y, creature: Character) -> 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) -> Character | 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: Character) -> 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:
|
|
def __init__(self, width, height):
|
|
self.width = width
|
|
self.height = height
|
|
self.grid = [[TileType.AIR] * height] * width
|
|
self.entities = EntityMap()
|
|
|
|
def get_tile(self, x, y):
|
|
return self.grid[x][y]
|