From 9b85305481e12d00b4a46277c657791eca592a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20N=C3=B6llemeyer?= Date: Sat, 17 Aug 2024 00:00:01 +0200 Subject: [PATCH] moved walls to static map object --- g.py | 2 ++ game/states.py | 24 ++++++++++++------------ game/world_tools.py | 28 ++++++++++++++++++---------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/g.py b/g.py index 950ef81..8f18308 100644 --- a/g.py +++ b/g.py @@ -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.""" diff --git a/game/states.py b/game/states.py index 1df3274..50dc67b 100644 --- a/game/states.py +++ b/game/states.py @@ -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]): diff --git a/game/world_tools.py b/game/world_tools.py index c32c6da..6eb0df1 100644 --- a/game/world_tools.py +++ b/game/world_tools.py @@ -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)