1
0

Refactor map rendering to happen in the map class

This commit is contained in:
Timothy Warren 2022-01-07 14:35:47 -05:00
parent 7a3614e963
commit 12dd66727b
4 changed files with 17 additions and 13 deletions

View File

@ -1,4 +1,4 @@
from typing import Set, Iterable, Any from typing import Iterable, Any
from tcod.context import Context from tcod.context import Context
from tcod.console import Console from tcod.console import Console
@ -12,12 +12,10 @@ from input_handlers import EventHandler
class Engine: class Engine:
def __init__( def __init__(
self, self,
entities: Set[Entity],
event_handler: EventHandler, event_handler: EventHandler,
game_map: GameMap, game_map: GameMap,
player: Entity player: Entity
): ):
self.entities = entities
self.event_handler = event_handler self.event_handler = event_handler
self.game_map = game_map self.game_map = game_map
self.player = player self.player = player
@ -49,11 +47,6 @@ class Engine:
def render(self, console: Console, context: Context) -> None: def render(self, console: Console, context: Context) -> None:
self.game_map.render(console) self.game_map.render(console)
for entity in self.entities:
# Only print entities that are in FOV
if self.game_map.visible[entity.x, entity.y]:
console.print(entity.x, entity.y, entity.char, fg=entity.color)
# Actually output to screen # Actually output to screen
context.present(console) context.present(console)
console.clear() console.clear()

View File

@ -1,12 +1,20 @@
from __future__ import annotations
from typing import Iterable, TYPE_CHECKING
import numpy as np # type: ignore import numpy as np # type: ignore
from tcod.console import Console from tcod.console import Console
import tile_types import tile_types
if TYPE_CHECKING:
from entity import Entity
class GameMap: class GameMap:
def __init__(self, width: int, height: int): def __init__(self, width: int, height: int, entities: Iterable[Entity] = ()):
self.width, self.height = width, height self.width, self.height = width, height
self.entities = entities
self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F") self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F")
self.visible = np.full((width, height), fill_value=False, order="F") # Tiles the player can currently see self.visible = np.full((width, height), fill_value=False, order="F") # Tiles the player can currently see
@ -31,3 +39,8 @@ class GameMap:
choicelist=[self.tiles["light"], self.tiles["dark"]], choicelist=[self.tiles["light"], self.tiles["dark"]],
default=tile_types.SHROUD default=tile_types.SHROUD
) )
for entity in self.entities:
# Only print entities that are in the FOV
if self.visible[entity.x, entity.y]:
console.print(x=entity.x, y=entity.y, string=entity.char, fg=entity.color)

View File

@ -28,8 +28,6 @@ def main() -> None:
event_handler = EventHandler() event_handler = EventHandler()
player = Entity(int(screen_width / 2), int(screen_height / 2), "@", (255, 255, 255)) player = Entity(int(screen_width / 2), int(screen_height / 2), "@", (255, 255, 255))
npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", (255, 255, 0))
entities = {npc, player}
game_map = generate_dungeon( game_map = generate_dungeon(
max_rooms, max_rooms,
@ -40,7 +38,7 @@ def main() -> None:
player, player,
) )
engine = Engine(entities, event_handler, game_map, player) engine = Engine(event_handler, game_map, player)
with tcod.context.new_terminal( with tcod.context.new_terminal(
screen_width, screen_width,

View File

@ -69,7 +69,7 @@ def generate_dungeon(
player: Entity, player: Entity,
) -> GameMap: ) -> GameMap:
"""Generate a new dungeon map.""" """Generate a new dungeon map."""
dungeon = GameMap(map_width, map_height) dungeon = GameMap(map_width, map_height, entities=[player])
rooms: List[RectangularRoom] = [] rooms: List[RectangularRoom] = []