completed moving walls to map

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 09:56:13 +02:00
parent 9b85305481
commit 91485c0f6b
4 changed files with 14 additions and 15 deletions

3
g.py
View file

@ -7,6 +7,7 @@ import tcod.context
import tcod.ecs import tcod.ecs
import game.state import game.state
from game.components import Position
context: tcod.context.Context context: tcod.context.Context
"""The window managed by tcod.""" """The window managed by tcod."""
@ -17,7 +18,7 @@ world: tcod.ecs.Registry
world_map: tcod.map.Map world_map: tcod.map.Map
"""Wall Map of current World""" """Wall Map of current World"""
world_center: tuple[int,int] = (50, 50) world_center: tuple[int,int] = Position(50, 50)
states: list[game.state.State] = [] states: list[game.state.State] = []
"""A stack of states with the last item being the active state.""" """A stack of states with the last item being the active state."""

View file

@ -15,7 +15,7 @@ import game.world_tools
from game.components import Gold, Graphic, Position from game.components import Gold, Graphic, Position
from game.constants import DIRECTION_KEYS, ACTION_KEYS from game.constants import DIRECTION_KEYS, ACTION_KEYS
from game.state import Push, Reset, State, StateResult from game.state import Push, Reset, State, StateResult
from game.tags import IsItem, IsPlayer, IsWall, IsDoor, IsActor from game.tags import IsItem, IsPlayer, IsDoor, IsActor
from game.constants import WALL_CHAR from game.constants import WALL_CHAR
@attrs.define() @attrs.define()
@ -34,7 +34,7 @@ class InGame(State):
raise SystemExit() raise SystemExit()
case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS: case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
new_pos = player.components[Position] + DIRECTION_KEYS[sym] new_pos = player.components[Position] + DIRECTION_KEYS[sym]
if not g.world_map.walkable[g.world_center[1]+new_pos.y][g.world_center[0]+new_pos.x]: if not g.world_map.walkable[g.world_center.y+new_pos.y][g.world_center.x+new_pos.x]:
return None return None
player.components[Position] = new_pos player.components[Position] = new_pos
# Auto pickup gold # Auto pickup gold

View file

@ -13,8 +13,5 @@ IsActor: Final = "IsActor"
IsItem: Final = "IsItem" IsItem: Final = "IsItem"
"""Entity is an item.""" """Entity is an item."""
IsWall: Final = "IsWall"
"""Entity is a wall."""
IsDoor: Final = "IsDoor" IsDoor: Final = "IsDoor"
"""Entiy is a door.""" """Entiy is a door."""

View file

@ -10,17 +10,18 @@ from tcod.ecs import Registry, Entity
from tcod.map import Map from tcod.map import Map
from game.components import Gold, Graphic, Position from game.components import Gold, Graphic, Position
from game.tags import IsActor, IsItem, IsPlayer, IsWall, IsDoor from game.tags import IsActor, IsItem, IsPlayer, IsDoor
def add_wall(x, y, remove=False): def add_wall(pos, remove=False):
g.world_map.walkable[50+y][50+x] = remove r_pos = g.world_center + pos
g.world_map.walkable[r_pos.y][r_pos.x] = remove
def add_door(x, y): def add_door(pos):
door = g.world[object()] door = g.world[object()]
door.tags.add(IsDoor) door.tags.add(IsDoor)
door.components[Position] = Position(x,y) door.components[Position] = pos
door.components[Graphic] = Graphic(ord("\\")) door.components[Graphic] = Graphic(ord("\\"))
add_wall(x,y) add_wall(pos)
def new_world() -> Registry: def new_world() -> Registry:
@ -46,9 +47,9 @@ def new_world() -> Registry:
for i in range(20): for i in range(20):
if i == 5 or i == 9: if i == 5 or i == 9:
add_door(10,i) add_door(Position(10,i))
else: else:
add_wall(10, i) add_wall(Position(10, i))
return world return world
@ -57,4 +58,4 @@ def unlock_door(door: Entity):
door.components[Graphic] = Graphic(ord("_")) door.components[Graphic] = Graphic(ord("_"))
door.tags.clear() door.tags.clear()
pos = door.components[Position] pos = door.components[Position]
add_wall(pos.x,pos.y, remove=True) add_wall(pos, remove=True)