Co-authored-by: staubsauger <staubsauger@users.noreply.github.com>

This commit is contained in:
Lukas Nöllemeyer 2024-08-16 23:20:24 +02:00
parent fe5d2bb24e
commit a4981d1575
3 changed files with 16 additions and 8 deletions

3
g.py
View file

@ -14,6 +14,9 @@ context: tcod.context.Context
world: tcod.ecs.Registry world: tcod.ecs.Registry
"""The active ECS registry and current session.""" """The active ECS registry and current session."""
world_map: tcod.map.Map
"""Wall Map of current World"""
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

@ -16,7 +16,7 @@ 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, IsWall, IsDoor, IsActor
from game.constants import WALL_CHAR
@attrs.define() @attrs.define()
class InGame(State): class InGame(State):
@ -56,19 +56,22 @@ class InGame(State):
center = sum(centers, start=Position(0,0)) center = sum(centers, start=Position(0,0))
center = center.mod(len(centers)) center = center.mod(len(centers))
def draw(e): def draw(e_pos, e_graph):
pos = e.components[Position] - center pos = e_pos - center
if (-console.width//2 <= pos.x < console.width//2\ if (-console.width//2 <= pos.x < console.width//2\
and -console.height//2 <= pos.y < console.height//2): and -console.height//2 <= pos.y < console.height//2):
graphic = e.components[Graphic] 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"]][pos.y + console.height//2, pos.x + console.width//2] = 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))
for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]): for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]):
draw(entity) draw(entity.components[Position], entity.components[Graphic])
for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]): for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]):
draw(actor) draw(actor.components[Position], entity.components[Graphic])
for player in g.world.Q.all_of(tags=[IsPlayer]): for player in g.world.Q.all_of(tags=[IsPlayer]):
draw(player) draw(player.components[Position], player.components[Graphic])
if text := g.world[None].components.get(("Text", str)): if text := g.world[None].components.get(("Text", str)):
console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0)) console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0))

View file

@ -7,6 +7,7 @@ from random import Random
import g import g
from tcod.ecs import Registry, Entity from tcod.ecs import Registry, Entity
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, IsWall, IsDoor
@ -15,6 +16,7 @@ from game.tags import IsActor, IsItem, IsPlayer, IsWall, IsDoor
def new_world() -> Registry: def new_world() -> Registry:
"""Return a freshly generated world.""" """Return a freshly generated world."""
world = Registry() world = Registry()
g.world_map = Map(100, 100)
rng = world[None].components[Random] = Random() rng = world[None].components[Random] = Random()
player = world[object()] player = world[object()]