Move the event loop to Engine.run_event_loop()

This commit is contained in:
Eryn Wells 2022-05-14 23:38:29 -07:00
parent bd5e1bc3c1
commit c44c4e7bc6
2 changed files with 17 additions and 9 deletions

View file

@ -6,7 +6,6 @@ import sys
import tcod import tcod
from . import log from . import log
from .engine import Configuration, Engine from .engine import Configuration, Engine
from .events import EventHandler
from .geometry import Size from .geometry import Size
CONSOLE_WIDTH, CONSOLE_HEIGHT = 80, 50 CONSOLE_WIDTH, CONSOLE_HEIGHT = 80, 50
@ -64,15 +63,9 @@ def main(argv):
configuration = Configuration(map_size=Size(MAP_WIDTH, MAP_HEIGHT)) configuration = Configuration(map_size=Size(MAP_WIDTH, MAP_HEIGHT))
engine = Engine(configuration) engine = Engine(configuration)
event_handler = EventHandler(engine)
with tcod.context.new(columns=console.width, rows=console.height, tileset=tileset) as context: with tcod.context.new(columns=console.width, rows=console.height, tileset=tileset) as context:
while True: engine.run_event_loop(context, console)
console.clear()
engine.print_to_console(console)
context.present(console)
event_handler.wait_for_events()
def run_until_exit(): def run_until_exit():
''' '''

View file

@ -4,17 +4,21 @@
import random import random
from dataclasses import dataclass from dataclasses import dataclass
from typing import MutableSet from typing import TYPE_CHECKING, MutableSet, NoReturn
import tcod import tcod
from . import log from . import log
from . import monsters from . import monsters
from .ai import HostileEnemy from .ai import HostileEnemy
from .events import MainGameEventHandler
from .geometry import Size from .geometry import Size
from .map import Map from .map import Map
from .object import Entity, Hero, Monster from .object import Entity, Hero, Monster
if TYPE_CHECKING:
from .events import EventHandler
@dataclass @dataclass
class Configuration: class Configuration:
map_size: Size map_size: Size
@ -45,6 +49,8 @@ class Engine:
self.map = Map(configuration.map_size) self.map = Map(configuration.map_size)
self.hero = Hero(position=self.map.generator.rooms[0].center) self.hero = Hero(position=self.map.generator.rooms[0].center)
self.event_handler: 'EventHandler' = MainGameEventHandler(self)
self.entities: MutableSet[Entity] = {self.hero} self.entities: MutableSet[Entity] = {self.hero}
for room in self.map.rooms: for room in self.map.rooms:
should_spawn_monster_chance = random.random() should_spawn_monster_chance = random.random()
@ -82,6 +88,15 @@ class Engine:
continue continue
ent.print_to_console(console) ent.print_to_console(console)
def run_event_loop(self, context: tcod.context.Context, console: tcod.Console) -> NoReturn:
'''Run the event loop forever. This method never returns.'''
while True:
console.clear()
self.print_to_console(console)
context.present(console)
self.event_handler.wait_for_events()
def update_field_of_view(self) -> None: def update_field_of_view(self) -> None:
'''Compute visible area of the map based on the player's position and point of view.''' '''Compute visible area of the map based on the player's position and point of view.'''
# FIXME: Move this to the Map class # FIXME: Move this to the Map class