moved walls to static map object

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 00:00:01 +02:00
parent a4981d1575
commit 9b85305481
3 changed files with 32 additions and 22 deletions

2
g.py
View file

@ -17,6 +17,8 @@ world: tcod.ecs.Registry
world_map: tcod.map.Map
"""Wall Map of current World"""
world_center: tuple[int,int] = (50, 50)
states: list[game.state.State] = []
"""A stack of states with the last item being the active state."""

View file

@ -34,7 +34,7 @@ class InGame(State):
raise SystemExit()
case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
new_pos = player.components[Position] + DIRECTION_KEYS[sym]
if g.world.Q.all_of(tags=[new_pos, IsWall]) or g.world.Q.all_of(tags=[new_pos, IsDoor]):
if not g.world_map.walkable[g.world_center[1]+new_pos.y][g.world_center[0]+new_pos.x]:
return None
player.components[Position] = new_pos
# Auto pickup gold
@ -51,21 +51,21 @@ class InGame(State):
def on_draw(self, console: tcod.console.Console) -> None:
"""Draw the standard screen."""
centers = g.world.Q.all_of(tags=[IsPlayer])
centers = [a.components[Position] for a in centers]
center = sum(centers, start=Position(0,0))
center = center.mod(len(centers))
centers = [a.components[Position] for a in g.world.Q.all_of(tags=[IsPlayer])]
camera_pos = sum(centers, start=Position(0,0))
camera_pos = camera_pos.mod(len(centers))
h = console.height//2
w = console.width//2
def draw(e_pos, e_graph):
pos = e_pos - center
if (-console.width//2 <= pos.x < console.width//2\
and -console.height//2 <= pos.y < console.height//2):
screen_pos = e_pos - camera_pos
if (-w <= screen_pos.x < w\
and -h <= screen_pos.y < h):
graphic = e_graph
console.rgb[["ch", "fg"]][pos.y + console.height//2, pos.x + console.width//2] = graphic.ch, graphic.fg
console.rgb[["ch", "fg"]][screen_pos.y + h, screen_pos.x + w] = graphic.ch, graphic.fg
for (y, row) in enumerate(g.world_map.walkable):
for (x, val) in enumerate(row):
if val:
draw(Position(x,y), Graphic(WALL_CHAR))
if not val:
draw(Position(x,y)-g.world_center, Graphic(WALL_CHAR))
for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]):
draw(entity.components[Position], entity.components[Graphic])
for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]):

View file

@ -12,11 +12,23 @@ from tcod.map import Map
from game.components import Gold, Graphic, Position
from game.tags import IsActor, IsItem, IsPlayer, IsWall, IsDoor
def add_wall(x, y, remove=False):
g.world_map.walkable[50+y][50+x] = remove
def add_door(x, y):
door = g.world[object()]
door.tags.add(IsDoor)
door.components[Position] = Position(x,y)
door.components[Graphic] = Graphic(ord("\\"))
add_wall(x,y)
def new_world() -> Registry:
"""Return a freshly generated world."""
world = Registry()
g.world = world
g.world_map = Map(100, 100)
g.world_map.walkable[:] = True
rng = world[None].components[Random] = Random()
player = world[object()]
@ -34,19 +46,15 @@ def new_world() -> Registry:
for i in range(20):
if i == 5 or i == 9:
door = world[object()]
door.components[Position] = Position(10, i)
door.components[Graphic] = Graphic(ord("\\"))
door.tags |= {IsDoor}
continue
wall = world[object()]
wall.components[Position] = Position(10, i)
wall.components[Graphic] = Graphic(ord("#"))
wall.tags |= {IsWall}
add_door(10,i)
else:
add_wall(10, i)
return world
def unlock_door(door: Entity):
door.components[Graphic] = Graphic(ord("_"))
door.tags.discard(IsDoor)
door.tags.clear()
pos = door.components[Position]
add_wall(pos.x,pos.y, remove=True)