finished part 5 http://rogueliketutorials.com/tutorials/tcod/v2/part-5/
This commit is contained in:
parent
ceb000f229
commit
ea986af048
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
venv/
|
||||
__pycache__/
|
32
actions.py
32
actions.py
@ -23,14 +23,36 @@ class EscapeAction(Action):
|
||||
def perform(self, engine: Engine, entity: Entity) -> None:
|
||||
raise SystemExit()
|
||||
|
||||
|
||||
class MovementAction(Action):
|
||||
class ActionWithDirection(Action):
|
||||
def __init__(self, dx: int, dy: int):
|
||||
super().__init__()
|
||||
|
||||
self.dx = dx
|
||||
self.dy = dy
|
||||
|
||||
def perform(self, engine: Engine, entity: Entity) -> None:
|
||||
raise NotImplementedError()
|
||||
|
||||
class MeleeAction(ActionWithDirection):
|
||||
def perform(self, engine: Engine, entity: Entity) -> None:
|
||||
dest_x = entity.x + self.dx
|
||||
dest_y = entity.y + self.dy
|
||||
target = engine.game_map.get_blocking_entity_at_location(dest_x, dest_y)
|
||||
if not target:
|
||||
return # no entity to blockj
|
||||
print(f"You kick {target.name}, much to its annoyance")
|
||||
|
||||
|
||||
class BumpAction(ActionWithDirection):
|
||||
def perform(self, engine: Engine, entity: Entity) -> None:
|
||||
dest_x = entity.x + self.dx
|
||||
dest_y = entity.y + self.dy
|
||||
if engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
||||
return MeleeAction(self.dx, self.dy).perform(engine, entity)
|
||||
else:
|
||||
return MovementAction(self.dx, self.dy).perform(engine, entity)
|
||||
|
||||
|
||||
class MovementAction(ActionWithDirection):
|
||||
def perform(self, engine: Engine, entity: Entity) -> None:
|
||||
dest_x = entity.x + self.dx
|
||||
dest_y = entity.y + self.dy
|
||||
@ -38,6 +60,8 @@ class MovementAction(Action):
|
||||
if not engine.game_map.in_bounds(dest_x, dest_y):
|
||||
return # OOB
|
||||
if not engine.game_map.tiles["walkable"][dest_x, dest_y]:
|
||||
return # can't walk
|
||||
return # can't walk
|
||||
if engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
||||
return # Blocked
|
||||
|
||||
entity.move(self.dx, self.dy)
|
||||
|
13
engine.py
13
engine.py
@ -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
|
||||
@ -10,19 +10,23 @@ from input_handlers import EventHandler
|
||||
|
||||
|
||||
class Engine:
|
||||
def __init__(self, entities: Set[Entity], event_handler: EventHandler, game_map: GameMap, player: Entity):
|
||||
self.entities = entities
|
||||
def __init__(self, event_handler: EventHandler, game_map: GameMap, player: Entity):
|
||||
self.event_handler = event_handler
|
||||
self.player = player
|
||||
self.game_map = game_map
|
||||
self.update_fov()
|
||||
|
||||
def handle_enemy_turns(self) -> None:
|
||||
for entity in self.game_map.entities - {self.player}:
|
||||
print(f'the {entity.name} wonders when it will move')
|
||||
|
||||
def handle_events(self, events: Iterable[Any]) -> None:
|
||||
for event in events:
|
||||
action = self.event_handler.dispatch(event)
|
||||
if action is None:
|
||||
continue
|
||||
action.perform(self, self.player)
|
||||
self.handle_enemy_turns()
|
||||
self.update_fov()
|
||||
|
||||
def update_fov(self) -> None:
|
||||
@ -35,9 +39,6 @@ class Engine:
|
||||
|
||||
def render(self, console: Console, context: Context) -> None:
|
||||
self.game_map.render(console)
|
||||
for entity in self.entities:
|
||||
if self.game_map.visible[entity.x, entity.y]:
|
||||
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|
||||
|
||||
context.present(console)
|
||||
console.clear()
|
31
entity.py
31
entity.py
@ -1,14 +1,41 @@
|
||||
from typing import Tuple
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from typing import Tuple, TypeVar, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game_map import GameMap
|
||||
|
||||
T = TypeVar("T", bound="Entity")
|
||||
|
||||
|
||||
class Entity:
|
||||
"""
|
||||
A generic object to represent players, enemies, items, etc.
|
||||
"""
|
||||
def __init__(self, x: int, y: int, char: str, color: Tuple[int, int, int]):
|
||||
|
||||
def __init__(self,
|
||||
x: int = 0,
|
||||
y: int = 0,
|
||||
char: str = "?",
|
||||
color: Tuple[int, int, int] = (255,255,255),
|
||||
name: str = "<Unnamed>",
|
||||
blocks_movement: bool = False,
|
||||
):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.char = char
|
||||
self.color = color
|
||||
self.name = name
|
||||
self.blocks_movement = blocks_movement
|
||||
|
||||
def spawn(self: T, gamemap: GameMap, x: int, y: int) -> T:
|
||||
"""Spawns a copy of this instance at the given location"""
|
||||
clone = copy.deepcopy(self)
|
||||
clone.x = x
|
||||
clone.y = y
|
||||
gamemap.entities.add(clone)
|
||||
return clone
|
||||
|
||||
def move(self, dx: int, dy: int) -> None:
|
||||
self.x += dx
|
||||
|
7
entity_factories.py
Normal file
7
entity_factories.py
Normal file
@ -0,0 +1,7 @@
|
||||
from entity import Entity
|
||||
|
||||
|
||||
player = Entity(char="@", color=(255, 255, 255), name="Player", blocks_movement=True)
|
||||
|
||||
orc = Entity(char="o", color=(63, 127, 63), name="Orc", blocks_movement=True)
|
||||
troll = Entity(char="T", color=(0, 127, 0), name="Troll", blocks_movement=True)
|
25
game_map.py
25
game_map.py
@ -1,23 +1,40 @@
|
||||
from __future__ import annotations
|
||||
from typing import Iterable, TYPE_CHECKING, Optional
|
||||
|
||||
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.tiles = np.full((width,height), fill_value=tile_types.wall, order="F")
|
||||
self.entities = set(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")
|
||||
self.explored = np.full((width, height), fill_value=False, order="F")
|
||||
|
||||
def get_blocking_entity_at_location(self, location_x: int, location_y: int) -> Optiona9[Entity]:
|
||||
for entity in self.entities:
|
||||
if entity.blocks_movement and entity.x == location_x and entity.y == location_y:
|
||||
return entity
|
||||
return None
|
||||
|
||||
def in_bounds(self, x: int, y: int) -> bool:
|
||||
return 0 <= x < self.width and 0 <= y < self.height
|
||||
|
||||
def render(self, console: Console) -> None:
|
||||
console.tiles_rgb[0:self.width, 0:self.height] = np.select(
|
||||
console.tiles_rgb[0 : self.width, 0 : self.height] = np.select(
|
||||
condlist=[self.visible, self.explored],
|
||||
choicelist=[self.tiles["light"], self.tiles["dark"]],
|
||||
default=tile_types.SHROUD
|
||||
default=tile_types.SHROUD,
|
||||
)
|
||||
|
||||
for entity in self.entities:
|
||||
if self.visible[entity.x, entity.y]:
|
||||
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|
||||
|
@ -2,7 +2,7 @@ from typing import Optional
|
||||
|
||||
import tcod.event
|
||||
|
||||
from actions import Action, EscapeAction, MovementAction
|
||||
from actions import Action, EscapeAction, BumpAction
|
||||
|
||||
|
||||
class EventHandler(tcod.event.EventDispatch[Action]):
|
||||
@ -15,13 +15,13 @@ class EventHandler(tcod.event.EventDispatch[Action]):
|
||||
key = event.sym
|
||||
|
||||
if key == tcod.event.K_UP:
|
||||
action = MovementAction(dx=0, dy=-1)
|
||||
action = BumpAction(dx=0, dy=-1)
|
||||
elif key == tcod.event.K_DOWN:
|
||||
action = MovementAction(dx=0, dy=1)
|
||||
action = BumpAction(dx=0, dy=1)
|
||||
elif key == tcod.event.K_LEFT:
|
||||
action = MovementAction(dx=-1, dy=0)
|
||||
action = BumpAction(dx=-1, dy=0)
|
||||
elif key == tcod.event.K_RIGHT:
|
||||
action = MovementAction(dx=1, dy=0)
|
||||
action = BumpAction(dx=1, dy=0)
|
||||
|
||||
elif key == tcod.event.K_ESCAPE:
|
||||
action = EscapeAction()
|
||||
|
24
main.py
24
main.py
@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
import tcod
|
||||
import copy
|
||||
import entity_factories
|
||||
|
||||
from entity import Entity
|
||||
from engine import Engine
|
||||
from input_handlers import EventHandler
|
||||
from procgen import generate_dungeon
|
||||
@ -18,15 +19,15 @@ def main():
|
||||
room_min_size = 6
|
||||
max_rooms = 30
|
||||
|
||||
max_monsters_per_room = 2
|
||||
|
||||
tileset = tcod.tileset.load_tilesheet(
|
||||
"dejavu10x10_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD
|
||||
)
|
||||
|
||||
event_handler = EventHandler()
|
||||
|
||||
player = Entity(screen_width // 2, screen_height // 2, "@", (255, 255, 255))
|
||||
npc = Entity(screen_width // 2 - 5, screen_height // 2, "@", (255, 255, 0))
|
||||
entities = {npc, player}
|
||||
player = copy.deepcopy(entity_factories.player)
|
||||
|
||||
game_map = generate_dungeon(
|
||||
max_rooms=max_rooms,
|
||||
@ -34,17 +35,18 @@ def main():
|
||||
room_max_size=room_max_size,
|
||||
map_width=map_width,
|
||||
map_height=map_height,
|
||||
player=player
|
||||
max_monsters_per_room=max_monsters_per_room,
|
||||
player=player,
|
||||
)
|
||||
|
||||
engine = Engine(entities=entities, event_handler=event_handler, game_map=game_map, player=player)
|
||||
engine = Engine(event_handler=event_handler, game_map=game_map, player=player)
|
||||
|
||||
with tcod.context.new_terminal(
|
||||
screen_width,
|
||||
screen_height,
|
||||
tileset=tileset,
|
||||
title="Yet Another Roguelike Tutorial",
|
||||
vsync=True
|
||||
screen_width,
|
||||
screen_height,
|
||||
tileset=tileset,
|
||||
title="Yet Another Roguelike Tutorial",
|
||||
vsync=True,
|
||||
) as context:
|
||||
root_console = tcod.Console(screen_width, screen_height, order="F")
|
||||
while True:
|
||||
|
49
procgen.py
49
procgen.py
@ -6,6 +6,7 @@ from typing import Tuple, Iterator, TYPE_CHECKING, List
|
||||
import tcod.los
|
||||
|
||||
import tile_types
|
||||
import entity_factories
|
||||
from game_map import GameMap
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -30,17 +31,17 @@ class RectangularRoom:
|
||||
def intersects(self, other: RectangularRoom) -> bool:
|
||||
"""Return True if this room overlaps with another RectangularRoom."""
|
||||
return (
|
||||
self.x1 <= other.x2
|
||||
and self.x2 >= other.x1
|
||||
and self.y1 <= other.y2
|
||||
and self.y2 >= other.y1
|
||||
self.x1 <= other.x2
|
||||
and self.x2 >= other.x1
|
||||
and self.y1 <= other.y2
|
||||
and self.y2 >= other.y1
|
||||
)
|
||||
|
||||
|
||||
def tunnel_between(
|
||||
start: Tuple[int, int], end: Tuple[int, int]
|
||||
start: Tuple[int, int], end: Tuple[int, int]
|
||||
) -> Iterator[Tuple[int, int]]:
|
||||
""" return an L shape tunel between points"""
|
||||
"""return an L shape tunel between points"""
|
||||
x1, y1 = start
|
||||
x2, y2 = end
|
||||
if random.random() < 0.5:
|
||||
@ -54,16 +55,30 @@ def tunnel_between(
|
||||
for x, y in tcod.los.bresenham((corner_x, corner_y), (x2, y2)).tolist():
|
||||
yield x, y
|
||||
|
||||
def place_entities(
|
||||
room: RectangularRoom, dungeon: GameMap, maximum_monsters: int
|
||||
) -> None:
|
||||
number_of_monsters = random.randint(0, maximum_monsters)
|
||||
|
||||
for i in range(number_of_monsters):
|
||||
x = random.randint(room.x1 + 1, room.x2 -1)
|
||||
y = random.randint(room.y1 + 1, room.y2 -1)
|
||||
if not any(entity.x == x and entity.y == y for entity in dungeon.entities):
|
||||
if random.random() <= 0.8:
|
||||
entity_factories.orc.spawn(dungeon, x,y)
|
||||
else:
|
||||
entity_factories.troll.spawn(dungeon, x,y)
|
||||
|
||||
def generate_dungeon(
|
||||
max_rooms: int,
|
||||
room_min_size: int,
|
||||
room_max_size: int,
|
||||
map_width: int,
|
||||
map_height: int,
|
||||
player: Entity
|
||||
max_rooms: int,
|
||||
room_min_size: int,
|
||||
room_max_size: int,
|
||||
map_width: int,
|
||||
map_height: int,
|
||||
max_monsters_per_room: int,
|
||||
player: Entity,
|
||||
) -> GameMap:
|
||||
dungeon = GameMap(map_width, map_height)
|
||||
dungeon = GameMap(map_width, map_height, entities=[player])
|
||||
rooms: List[RectangularRoom] = []
|
||||
|
||||
for room in range(max_rooms):
|
||||
@ -72,7 +87,7 @@ def generate_dungeon(
|
||||
x = random.randint(0, dungeon.width - room_width - 1)
|
||||
y = random.randint(0, dungeon.height - room_height - 1)
|
||||
|
||||
new_room = RectangularRoom(x,y, room_width,room_height)
|
||||
new_room = RectangularRoom(x, y, room_width, room_height)
|
||||
|
||||
if any(new_room.intersects(other_room) for other_room in rooms):
|
||||
continue
|
||||
@ -83,8 +98,10 @@ def generate_dungeon(
|
||||
# first room
|
||||
player.x, player.y = new_room.center
|
||||
else:
|
||||
for x,y in tunnel_between(rooms[-1].center, new_room.center):
|
||||
dungeon.tiles[x,y] = tile_types.floor
|
||||
for x, y in tunnel_between(rooms[-1].center, new_room.center):
|
||||
dungeon.tiles[x, y] = tile_types.floor
|
||||
|
||||
place_entities(new_room, dungeon, max_monsters_per_room)
|
||||
|
||||
rooms.append(new_room)
|
||||
|
||||
|
@ -21,11 +21,11 @@ tile_dt = np.dtype(
|
||||
|
||||
|
||||
def new_tile(
|
||||
*, # keywords only
|
||||
walkable: int,
|
||||
transparent: int,
|
||||
dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
|
||||
light: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
|
||||
*, # keywords only
|
||||
walkable: int,
|
||||
transparent: int,
|
||||
dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
|
||||
light: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
|
||||
) -> np.ndarray:
|
||||
"""helper function for making tiles"""
|
||||
return np.array((walkable, transparent, dark, light), dtype=tile_dt)
|
||||
|
Loading…
Reference in New Issue
Block a user