1
0

Create the game map, and finish Part 2

This commit is contained in:
Timothy Warren 2022-01-06 11:56:08 -05:00
parent 950578f9b8
commit 07dd70b25f
3 changed files with 50 additions and 9 deletions

View File

@ -1,9 +1,27 @@
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from engine import Engine
from entity import Entity
class Action: class Action:
pass def perform(self, engine: Engine, entity: Entity) -> None:
"""Perform this action with the objects needed to determine its scope.
`engine` is the scope this action is being performed in.
`entity` is the object performing the action.
This method must be overwritten by Action subclasses.
"""
raise NotImplementedError()
class EscapeAction(Action): class EscapeAction(Action):
pass def perform(self, engine: Engine, entity: Entity) -> None:
raise SystemExit()
class MovementAction(Action): class MovementAction(Action):
@ -12,3 +30,14 @@ class MovementAction(Action):
self.dx = dx self.dx = dx
self.dy = dy self.dy = dy
def perform(self, engine: Engine, entity: Entity) -> None:
dest_x = entity.x + self.dx
dest_y = entity.y + self.dy
if not engine.game_map.in_bounds(dest_x, dest_y):
return # Destination is out of bounds
if not engine.game_map.tiles["walkable"][dest_x, dest_y]:
return # Destination is blocked by a tile.
entity.move(self.dx, self.dy);

View File

@ -3,15 +3,22 @@ from typing import Set, Iterable, Any
from tcod.context import Context from tcod.context import Context
from tcod.console import Console from tcod.console import Console
from actions import EscapeAction, MovementAction
from entity import Entity from entity import Entity
from game_map import GameMap
from input_handlers import EventHandler from input_handlers import EventHandler
class Engine: class Engine:
def __init__(self, entities: Set[Entity], event_handler: EventHandler, player: Entity): def __init__(
self,
entities: Set[Entity],
event_handler: EventHandler,
game_map: GameMap,
player: Entity
):
self.entities = entities self.entities = entities
self.event_handler = event_handler self.event_handler = event_handler
self.game_map = game_map
self.player = player self.player = player
def handle_events(self, events: Iterable[Any]) -> None: def handle_events(self, events: Iterable[Any]) -> None:
@ -21,12 +28,11 @@ class Engine:
if action is None: if action is None:
continue continue
if isinstance(action, MovementAction): action.perform(self, self.player)
self.player.move(dx=action.dx, dy=action.dy)
elif isinstance(action, EscapeAction):
raise SystemExit()
def render(self, console: Console, context: Context) -> None: def render(self, console: Console, context: Context) -> None:
self.game_map.render(console)
for entity in self.entities: for entity in self.entities:
console.print(entity.x, entity.y, entity.char, fg=entity.color) console.print(entity.x, entity.y, entity.char, fg=entity.color)

View File

@ -3,6 +3,7 @@ import tcod
from engine import Engine from engine import Engine
from entity import Entity from entity import Entity
from game_map import GameMap
from input_handlers import EventHandler from input_handlers import EventHandler
@ -10,6 +11,9 @@ def main() -> None:
screen_width = 80 screen_width = 80
screen_height = 50 screen_height = 50
map_width = 80
map_height = 50
tileset = tcod.tileset.load_tilesheet( tileset = tcod.tileset.load_tilesheet(
"dejavu10x10_gs_tc.png", "dejavu10x10_gs_tc.png",
32, 32,
@ -23,7 +27,9 @@ def main() -> None:
npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", (255, 255, 0)) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", (255, 255, 0))
entities = {npc, player} entities = {npc, player}
engine = Engine(entities, event_handler, player) game_map = GameMap(map_width, map_height)
engine = Engine(entities, event_handler, game_map, player)
with tcod.context.new_terminal( with tcod.context.new_terminal(
screen_width, screen_width,