Stepwise/source/engine.py

76 lines
1.9 KiB
Python

from tcod.context import Context
from tcod.console import Console
from tcod.map import compute_fov
from message_log import Message, MessageLog
from render_functions import render_hp_bar
import entity_types
from floor_map import FloorMap #TODO: replace with "DungeonMap"
class Engine:
def __init__(self, floor_map: FloorMap, intro_msg: Message = None):
from event_handler import InGameEventHandler
self.event_handler = InGameEventHandler(self)
self.floor_map = floor_map
self.floor_map.engine = self #references everywhere!
self.message_log = MessageLog()
if intro_msg:
self.message_log.push_message(intro_msg)
#grab the player object
self.player = self.floor_map.player
#kick off the render
self.update_fov()
def run_loop(self, context: Context, console: Console) -> None:
while True:
if self.event_handler.handle_events():
self.handle_entities()
self.handle_rendering(context, console)
def handle_entities(self) -> None:
self.update_fov() #knowing the FOV lets entities mess with it
#all *actors* in the level
for actor in set(self.floor_map.actors) - {self.player}:
if actor.ai:
actor.ai.apply()
def handle_rendering(self, context: Context, console: Console) -> None:
#map and all entities within
self.floor_map.render(console)
#UI
render_hp_bar(
console = console,
current_value = self.player.fighter.current_hp,
max_value = self.player.fighter.maximum_hp,
total_width = 20
)
self.message_log.render(
console=console,
x=21,
y=45 - 5,
width = 40,
height = 5
)
#send to the screen
context.present(console)
console.clear()
#utils
def update_fov(self):
self.floor_map.visible[:] = compute_fov(
self.floor_map.tiles["transparent"],
(self.player.x, self.player.y),
radius = 8,
)
#add the visible tiles to the explored list
self.floor_map.explored |= self.floor_map.visible