revamped a whole bunch of stuff
This commit is contained in:
parent
d4b977abf6
commit
f498618f5a
12
actions.py
12
actions.py
|
@ -9,4 +9,14 @@ class Move:
|
||||||
class Idle:
|
class Idle:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
Action = Move | Idle
|
@dataclass
|
||||||
|
class NA:
|
||||||
|
pass
|
||||||
|
|
||||||
|
Action = Move | Idle | NA
|
||||||
|
|
||||||
|
action_timing = {
|
||||||
|
Move: 500,
|
||||||
|
Idle: 100,
|
||||||
|
NA: 1
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from actions import Action, Idle
|
from actions import Action, Idle, NA
|
||||||
|
|
||||||
class Entity():
|
class Entity():
|
||||||
pass
|
pass
|
||||||
|
@ -9,7 +9,8 @@ class Item(Entity):
|
||||||
|
|
||||||
class Character(Entity):
|
class Character(Entity):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.action: Action = Idle()
|
self.speed = 1
|
||||||
|
self.action: Action = NA()
|
||||||
|
|
||||||
class Player(Character):
|
class Player(Character):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -18,3 +19,6 @@ class Player(Character):
|
||||||
class Enemy(Character):
|
class Enemy(Character):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
def calculate_action(self):
|
||||||
|
self.action: Action = Idle()
|
68
floor.py
68
floor.py
|
@ -1,38 +1,60 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import Tuple
|
||||||
from entity import Character, Entity, Item
|
from entity import Character, Entity, Item
|
||||||
|
|
||||||
class TileType(Enum):
|
class TileType(Enum):
|
||||||
WALL = 1
|
WALL = 1
|
||||||
AIR = 2
|
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:
|
class Floor:
|
||||||
def __init__(self, width, height):
|
def __init__(self, width, height):
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.grid = [[TileType.AIR] * height] * width
|
self.grid = [[TileType.AIR] * height] * width
|
||||||
self.entities = {
|
self.entities = EntityMap()
|
||||||
Character: {},
|
|
||||||
Item: {}
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_tile(self, x, y):
|
def get_tile(self, x, y):
|
||||||
return self.grid[x][y]
|
return self.grid[x][y]
|
||||||
|
|
||||||
def add_entity(self, x, y, entity: Entity) -> bool:
|
|
||||||
if isinstance(entity, Character):
|
|
||||||
if (x, y) in self.entities[Character]:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
self.entities[Character][(x, y)] = entity
|
|
||||||
return True
|
|
||||||
elif isinstance(entity, Item):
|
|
||||||
if (x, y) not in self.entities[Item]:
|
|
||||||
self.entities[Item][(x,y)] = []
|
|
||||||
self.entities[Item][(x,y)].append(entity)
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def pop_character(self, x, y) -> Character | None:
|
|
||||||
if (x, y) in self.entities[Character]:
|
|
||||||
return self.entities[Character].pop((x, y))
|
|
47
game.py
47
game.py
|
@ -1,5 +1,5 @@
|
||||||
from terminal import Terminal
|
from terminal import Terminal
|
||||||
from entity import Character, Player, Item
|
from entity import Character, Player, Item, Enemy
|
||||||
from actions import *
|
from actions import *
|
||||||
from floor import Floor, TileType
|
from floor import Floor, TileType
|
||||||
|
|
||||||
|
@ -15,16 +15,32 @@ class Game:
|
||||||
self.player = Player()
|
self.player = Player()
|
||||||
self.floor = Floor(20, 10)
|
self.floor = Floor(20, 10)
|
||||||
self.should_exit = False
|
self.should_exit = False
|
||||||
|
self.ticks = 0
|
||||||
|
self.schedule = {}
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""
|
"""
|
||||||
Runs the game
|
Runs the game
|
||||||
"""
|
"""
|
||||||
self.floor.add_entity(3, 3, self.player)
|
self.instance_character(3, 3, self.player)
|
||||||
while not self.should_exit:
|
while not self.should_exit:
|
||||||
self.render()
|
self.render()
|
||||||
self.get_event()
|
|
||||||
self.step()
|
self.step()
|
||||||
|
self.ticks += 1
|
||||||
|
|
||||||
|
def instance_character(self, x, y, character: Character):
|
||||||
|
self.floor.entities.add_creature(x, y, character)
|
||||||
|
self.reschedule(character)
|
||||||
|
|
||||||
|
def reschedule(self, character: Character):
|
||||||
|
"""
|
||||||
|
Calculates turns for the next avaliable ticks untill player turn
|
||||||
|
"""
|
||||||
|
time = (action_timing[character.action.__class__] // character.speed) + self.ticks
|
||||||
|
if self.schedule.get(time):
|
||||||
|
self.schedule[time].append(character)
|
||||||
|
else:
|
||||||
|
self.schedule[time] = [character]
|
||||||
|
|
||||||
def render(self):
|
def render(self):
|
||||||
"""
|
"""
|
||||||
|
@ -32,21 +48,34 @@ 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[Character]:
|
if (x, y) in self.floor.entities.pos_creatures:
|
||||||
self.term.put_char(x, y, TEXTURES[Player])
|
self.term.put_char(x, y, TEXTURES[Player])
|
||||||
elif (x, y) in self.floor.entities[Item]:
|
elif (x, y) in self.floor.entities.items:
|
||||||
self.term.put_char(x, y, 'i')
|
self.term.put_char(x, y, 'i')
|
||||||
else:
|
else:
|
||||||
self.term.put_char(x, y, TEXTURES[self.floor.get_tile(x, y)])
|
self.term.put_char(x, y, TEXTURES[self.floor.get_tile(x, y)])
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
|
creaturas = self.schedule.get(self.ticks)
|
||||||
|
if creaturas:
|
||||||
|
for creatura in creaturas:
|
||||||
|
if isinstance(creatura, Player):
|
||||||
|
self.get_event()
|
||||||
|
elif isinstance(creatura, Enemy):
|
||||||
|
creatura.calculate_action()
|
||||||
|
self.reschedule(creatura)
|
||||||
|
self.perform(creatura)
|
||||||
|
self.schedule.pop(self.ticks)
|
||||||
|
|
||||||
|
def perform(self, character: Character):
|
||||||
"""
|
"""
|
||||||
Performs a turn in the game
|
Performs an action for a creature
|
||||||
"""
|
"""
|
||||||
for (pos, character) in dict(self.floor.entities[Character]).items():
|
|
||||||
if isinstance(character.action, Move):
|
if isinstance(character.action, Move):
|
||||||
character = self.floor.entities[Character].pop(pos)
|
pos = self.floor.entities.pop_creature_ref(character)
|
||||||
self.floor.add_entity(pos[0] + character.action.x, pos[1] + character.action.y, character)
|
if pos:
|
||||||
|
self.floor.entities.add_creature(pos[0] + character.action.x, pos[1] + character.action.y, character)
|
||||||
|
|
||||||
|
|
||||||
def get_event(self):
|
def get_event(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue