Colisions

This commit is contained in:
Bizcochito 2023-03-20 21:12:05 +01:00
parent fa76489ed4
commit 3a49aa1dd4
2 changed files with 28 additions and 8 deletions

View File

@ -9,6 +9,8 @@ class TileType(Enum):
WALL = 1 WALL = 1
AIR = 2 AIR = 2
COLLIDABLE = {TileType.WALL}
class EntityMap: class EntityMap:
""" """
A class that stores entities in an special way in order to be able to reach in O(1) the values 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 A class that represents a specific floor of the dungeon
""" """
def __init__(self, width, height): def __init__(self, width, height):
print(height, width)
self.width = width self.width = width
self.height = height 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() self.entities = EntityMap()
def get_tile(self, x, y) -> TileType: def get_tile(self, x, y) -> TileType:
print(x,y)
return self.grid[x][y] return self.grid[x][y]

25
game.py
View File

@ -1,13 +1,14 @@
from terminal import Terminal from terminal import Terminal
from entity import Creature, Player, Enemy from entity import Creature, Player, Enemy
from actions import * from actions import *
from floor import Floor, TileType from floor import COLLIDABLE, Floor, TileType
import sys import sys
TEXTURES = { TEXTURES = {
TileType.WALL: '#', TileType.WALL: '#',
TileType.AIR: '.', TileType.AIR: '.',
Player: '@' Player: '@',
Enemy: 'Y'
} }
class Game: class Game:
@ -42,6 +43,7 @@ class Game:
Runs the game Runs the game
""" """
self.instance_creature(3, 3, self.player) self.instance_creature(3, 3, self.player)
self.instance_creature(6, 4, Enemy())
self.render() self.render()
while not self.should_exit: while not self.should_exit:
self.step() self.step()
@ -72,7 +74,7 @@ class Game:
for x in range(self.floor.width): for x in range(self.floor.width):
for y in range(self.floor.height): for y in range(self.floor.height):
if (x, y) in self.floor.entities.pos_creatures: 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: elif (x, y) in self.floor.entities.items:
self.term.put_char(x, y, 'i') self.term.put_char(x, y, 'i')
else: else:
@ -85,13 +87,13 @@ class Game:
self.action_was_performed = False self.action_was_performed = False
creatures = self.schedule.get(self.ticks) creatures = self.schedule.get(self.ticks)
if creatures: if creatures:
for creature in creatures: while creature := creatures[0] if len(creatures) > 0 else None:
if isinstance(creature, Player): if isinstance(creature, Player):
self.input_event() self.input_event()
elif isinstance(creature, Enemy): elif isinstance(creature, Enemy):
creature.calculate_action() creature.calculate_action()
self.reschedule(creature) if self.perform(creature):
self.perform(creature) creatures.pop(0)
self.schedule.pop(self.ticks) self.schedule.pop(self.ticks)
self.action_was_performed = True self.action_was_performed = True
self.ticks += 1 self.ticks += 1
@ -103,7 +105,16 @@ class Game:
if isinstance(creature.action, Move): if isinstance(creature.action, Move):
pos = self.floor.entities.pop_creature_ref(creature) pos = self.floor.entities.pop_creature_ref(creature)
if pos: 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): def input_event(self):
""" """