fixed pause menu and made world_map a component of world

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 11:29:39 +02:00
parent bfabba3d82
commit 45e7a8927a
3 changed files with 41 additions and 25 deletions

3
g.py
View file

@ -15,9 +15,6 @@ 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"""
world_center: tuple[int,int] = Position(50, 50) world_center: tuple[int,int] = Position(50, 50)
screens: list[Screen] = [] screens: list[Screen] = []

View file

@ -7,6 +7,7 @@ import tcod.console
import tcod.constants import tcod.constants
import tcod.event import tcod.event
from tcod.event import KeySym from tcod.event import KeySym
from tcod.map import Map
import g import g
from game.components import Gold, Graphic, Position from game.components import Gold, Graphic, Position
@ -33,7 +34,7 @@ class MainScreen(Screen):
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.y+new_pos.y][g.world_center.x+new_pos.x]: if not g.world[None].components[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
@ -44,7 +45,7 @@ class MainScreen(Screen):
gold.clear() gold.clear()
return None return None
case tcod.event.KeyDown(sym=KeySym.ESCAPE): case tcod.event.KeyDown(sym=KeySym.ESCAPE):
return Push(menu_screens()) return Push(menu_screens.MainMenu())
case _: case _:
return None return None
@ -61,7 +62,7 @@ class MainScreen(Screen):
and -h <= screen_pos.y < h): and -h <= screen_pos.y < h):
graphic = e_graph graphic = e_graph
console.rgb[["ch", "fg"]][screen_pos.y + h, screen_pos.x + w] = 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 (y, row) in enumerate(g.world[None].components[Map].walkable):
for (x, val) in enumerate(row): for (x, val) in enumerate(row):
if not val: if not val:
draw(Position(x,y)-g.world_center, Graphic(WALL_CHAR)) draw(Position(x,y)-g.world_center, Graphic(WALL_CHAR))

View file

@ -14,13 +14,18 @@ from game.tags import IsActor, IsItem, IsPlayer, IsDoor
def add_wall(pos, remove=False): def add_wall(pos, remove=False):
r_pos = g.world_center + pos r_pos = g.world_center + pos
g.world_map.walkable[r_pos.y][r_pos.x] = remove map = g.world[None].components[Map]
map.walkable[r_pos.y][r_pos.x] = remove
map.transparent[r_pos.y][r_pos.x] = remove
def add_door(pos): def add_door(pos):
door = g.world[object()] g.world.new_entity(
door.tags.add(IsDoor) components={
door.components[Position] = pos Position: pos,
door.components[Graphic] = Graphic(ord("\\")) Graphic: Graphic(ord('\\'))
},
tags=[IsDoor]
)
add_wall(pos) add_wall(pos)
@ -28,22 +33,36 @@ def new_world() -> Registry:
"""Return a freshly generated world.""" """Return a freshly generated world."""
world = Registry() world = Registry()
g.world = world g.world = world
g.world_map = Map(100, 100)
g.world_map.walkable[:] = True map = world[None].components[Map] = Map(100, 100)
map.walkable[:] = True
map.transparent[:] = True
rng = world[None].components[Random] = Random() rng = world[None].components[Random] = Random()
player = world[object()] world.new_entity(
player.components[Position] = Position(5, 5) components={
player.components[Graphic] = Graphic(ord("@")) Position: Position(5,5),
player.components[Gold] = 0 Graphic: Graphic(ord('@')),
player.tags |= {IsPlayer, IsActor} Gold: 0
},
tags=[IsActor, IsPlayer]
)
# player = world[object()] # <- das hier ist das gleiche wie das world.new_entity darüber
# player.components[Position] = Position(5, 5)
# player.components[Graphic] = Graphic(ord("@"))
# player.components[Gold] = 0
# player.tags |= {IsPlayer, IsActor}
for _ in range(10): for _ in range(10):
gold = world[object()] world.new_entity(
gold.components[Position] = Position(rng.randint(0, 20), rng.randint(0, 20)) components={
gold.components[Graphic] = Graphic(ord("$"), fg=(255, 255, 0)) Position: Position(rng.randint(0, 20), rng.randint(0, 20)),
gold.components[Gold] = rng.randint(1, 10) Graphic: Graphic(ord("$"), fg=(255, 255, 0)),
gold.tags |= {IsItem} Gold: rng.randint(1, 10)
},
tags=[IsItem]
)
for i in range(20): for i in range(20):
if i == 5 or i == 9: if i == 5 or i == 9:
@ -57,5 +76,4 @@ def new_world() -> Registry:
def unlock_door(door: Entity): 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] add_wall(door.components[Position], remove=True)
add_wall(pos, remove=True)