Refactor map rendering to happen in the map class
This commit is contained in:
parent
7a3614e963
commit
12dd66727b
@ -1,4 +1,4 @@
|
||||
from typing import Set, Iterable, Any
|
||||
from typing import Iterable, Any
|
||||
|
||||
from tcod.context import Context
|
||||
from tcod.console import Console
|
||||
@ -12,12 +12,10 @@ from input_handlers import EventHandler
|
||||
class Engine:
|
||||
def __init__(
|
||||
self,
|
||||
entities: Set[Entity],
|
||||
event_handler: EventHandler,
|
||||
game_map: GameMap,
|
||||
player: Entity
|
||||
):
|
||||
self.entities = entities
|
||||
self.event_handler = event_handler
|
||||
self.game_map = game_map
|
||||
self.player = player
|
||||
@ -49,11 +47,6 @@ class Engine:
|
||||
def render(self, console: Console, context: Context) -> None:
|
||||
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
|
||||
context.present(console)
|
||||
console.clear()
|
||||
|
15
game_map.py
15
game_map.py
@ -1,12 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable, TYPE_CHECKING
|
||||
|
||||
import numpy as np # type: ignore
|
||||
from tcod.console import Console
|
||||
|
||||
import tile_types
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from entity import Entity
|
||||
|
||||
|
||||
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.entities = entities
|
||||
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
|
||||
@ -31,3 +39,8 @@ class GameMap:
|
||||
choicelist=[self.tiles["light"], self.tiles["dark"]],
|
||||
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)
|
||||
|
4
main.py
4
main.py
@ -28,8 +28,6 @@ def main() -> None:
|
||||
event_handler = EventHandler()
|
||||
|
||||
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(
|
||||
max_rooms,
|
||||
@ -40,7 +38,7 @@ def main() -> None:
|
||||
player,
|
||||
)
|
||||
|
||||
engine = Engine(entities, event_handler, game_map, player)
|
||||
engine = Engine(event_handler, game_map, player)
|
||||
|
||||
with tcod.context.new_terminal(
|
||||
screen_width,
|
||||
|
@ -69,7 +69,7 @@ def generate_dungeon(
|
||||
player: Entity,
|
||||
) -> GameMap:
|
||||
"""Generate a new dungeon map."""
|
||||
dungeon = GameMap(map_width, map_height)
|
||||
dungeon = GameMap(map_width, map_height, entities=[player])
|
||||
|
||||
rooms: List[RectangularRoom] = []
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user