2023-03-01 19:47:24 +00:00
|
|
|
from enum import Enum
|
2023-03-02 16:51:17 +00:00
|
|
|
from typing import Tuple
|
2023-03-13 18:46:29 +00:00
|
|
|
from entity import Creature, Item
|
2023-03-01 19:47:24 +00:00
|
|
|
|
|
|
|
class TileType(Enum):
|
2023-03-13 18:46:29 +00:00
|
|
|
"""
|
|
|
|
An enum who has the different types of tiles
|
|
|
|
"""
|
2023-03-01 19:47:24 +00:00
|
|
|
WALL = 1
|
|
|
|
AIR = 2
|
|
|
|
|
2023-03-20 20:12:05 +00:00
|
|
|
COLLIDABLE = {TileType.WALL}
|
|
|
|
|
2023-03-02 16:51:17 +00:00
|
|
|
class EntityMap:
|
|
|
|
"""
|
2023-03-13 18:46:29 +00:00
|
|
|
A class that stores entities in an special way in order to be able to reach in O(1) the values
|
2023-03-02 16:51:17 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.items = {} # K: Position V: List(Item)
|
|
|
|
self.pos_creatures = {} # K: Position V: Creature
|
|
|
|
self.creatures_pos = {} # K: Creature V: Position
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def get_item(self, x, y) -> Item | None:
|
2023-03-02 16:51:17 +00:00
|
|
|
return self.items.get((x, y))
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def get_creature(self, x, y) -> Item | None:
|
2023-03-02 16:51:17 +00:00
|
|
|
return self.pos_creatures.get((x, y))
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def get_creature_position(self, creature: Creature) -> Tuple[int, int]:
|
|
|
|
return self.creatures_pos[creature]
|
2023-03-02 16:51:17 +00:00
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def add_item(self, x, y, item: Item) -> None:
|
2023-03-02 16:51:17 +00:00
|
|
|
if (x, y) not in self.items:
|
|
|
|
self.items[(x, y)] = []
|
|
|
|
self.items[(x, y)].append(item)
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def add_creature(self, x, y, creature: Creature) -> bool:
|
2023-03-02 16:51:17 +00:00
|
|
|
if (x, y) in self.pos_creatures:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.pos_creatures[(x, y)] = creature
|
|
|
|
self.creatures_pos[creature] = (x, y)
|
|
|
|
return True
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def pop_creature_pos(self, x, y) -> Creature | None:
|
2023-03-02 16:51:17 +00:00
|
|
|
if (x, y) in self.pos_creatures:
|
|
|
|
creature = self.pos_creatures.pop((x, y))
|
|
|
|
self.creatures_pos.pop(creature)
|
|
|
|
return creature
|
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def pop_creature_ref(self, creature: Creature) -> Tuple[int, int] | None:
|
2023-03-02 16:51:17 +00:00
|
|
|
if creature in self.creatures_pos:
|
|
|
|
pos = self.creatures_pos.pop(creature)
|
|
|
|
self.pos_creatures.pop(pos)
|
|
|
|
return pos
|
|
|
|
|
2023-03-01 19:47:24 +00:00
|
|
|
class Floor:
|
2023-03-13 18:46:29 +00:00
|
|
|
"""
|
|
|
|
A class that represents a specific floor of the dungeon
|
|
|
|
"""
|
2023-03-01 19:47:24 +00:00
|
|
|
def __init__(self, width, height):
|
2023-03-20 20:12:05 +00:00
|
|
|
print(height, width)
|
2023-03-01 19:47:24 +00:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2023-03-20 20:12:05 +00:00
|
|
|
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
|
2023-03-02 16:51:17 +00:00
|
|
|
self.entities = EntityMap()
|
2023-03-01 19:47:24 +00:00
|
|
|
|
2023-03-13 18:46:29 +00:00
|
|
|
def get_tile(self, x, y) -> TileType:
|
2023-03-01 19:47:24 +00:00
|
|
|
return self.grid[x][y]
|