diff --git a/game/screens/game_screens.py b/game/screens/game_screens.py index 288bc91..299f983 100644 --- a/game/screens/game_screens.py +++ b/game/screens/game_screens.py @@ -1,6 +1,7 @@ """All states the are used in-game""" from __future__ import annotations +from random import Random import attrs import tcod.console @@ -13,11 +14,44 @@ import g from game.components import Action, Gold, Graphic, Position from game.constants import DIRECTION_KEYS, ACTION_KEYS from game.screens import Push, Screen, ScreenResult -from game.tags import IsItem, IsPlayer, IsActor +from game.tags import IsDoor, IsItem, IsPlayer, IsActor from game.constants import WALL_CHAR, VERTICAL_WALL_CHAR from game.screens import menu_screens from game.world_tools import world_pos_to_map_pos, map_pos_to_world_pos +def _recalc_fov(pos): + cam_map = world_pos_to_map_pos(pos) + g.world[None].components[Map].compute_fov(cam_map.x, cam_map.y, 100) + +def _handle_movement(player, map, dir): + pos = player.components[Position] + new_pos = pos + dir + map_pos = world_pos_to_map_pos(new_pos) + if a := g.world.Q.all_of(components=[Action], tags=[new_pos]): + for aa in a: + aa.components[Action](aa) + map_pos = world_pos_to_map_pos(pos) + map.compute_fov(map_pos.x, map_pos.y, 100) + return None + if not map.walkable[map_pos.y, map_pos.x]: + return None + player.components[Position] = new_pos + _recalc_fov(new_pos) + + # Auto pickup gold + for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]): + player.components[Gold] += gold.components[Gold] + text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g" + g.world[None].components[("Text", str)] = text + gold.clear() + +def _handle_action(player): + for entity in g.world.Q.all_of(components=[Action, Position]): + player_pos = player.components[Position] + if (player_pos - entity.components[Position]).length() < 2: + entity.components[Action](entity) + _recalc_fov(player_pos) + @attrs.define() class MainScreen(Screen): """Primary in-game state.""" @@ -28,38 +62,12 @@ class MainScreen(Screen): map: Map = g.world[None].components[Map] match event: case tcod.event.KeyDown(sym=sym) if sym in ACTION_KEYS: - for entity in g.world.Q.all_of(components=[Action, Position]): - player_pos = player.components[Position] - if (player_pos - entity.components[Position]).length() < 2: - entity.components[Action](entity) - cam_map = world_pos_to_map_pos(player_pos) - map.compute_fov(cam_map.x, cam_map.y, 100) - + _handle_action(player) case tcod.event.Quit(): raise SystemExit() case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS: - pos = player.components[Position] - new_pos = pos + DIRECTION_KEYS[sym] - map_pos = world_pos_to_map_pos(new_pos) - if a := g.world.Q.all_of(components=[Action], tags=[new_pos]): - for aa in a: - aa.components[Action](aa) - map_pos = world_pos_to_map_pos(pos) - map.compute_fov(map_pos.x, map_pos.y, 100) - return None - if not map.walkable[map_pos.y, map_pos.x]: - return None - player.components[Position] = new_pos - cam_map = world_pos_to_map_pos(new_pos) - map.compute_fov(cam_map.x, cam_map.y, 100) - - # Auto pickup gold - for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]): - player.components[Gold] += gold.components[Gold] - text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g" - g.world[None].components[("Text", str)] = text - gold.clear() - return None + dir = DIRECTION_KEYS[sym] + return _handle_movement(player, map, dir) case tcod.event.KeyDown(sym=KeySym.ESCAPE): return Push(menu_screens.MainMenu()) case _: @@ -91,10 +99,11 @@ class MainScreen(Screen): console.rgb[["ch", "fg"]][screen_pos.y + h, screen_pos.x + w] = graphic.ch, graphic.fg # Draw walls + doors = [ d.components[Position] for d in g.world.Q.all_of(tags=[IsDoor]) ] for (y, row) in enumerate(map.walkable): for (x, val) in enumerate(row): pos = map_pos_to_world_pos(Position(x,y)) - graphic = Graphic(0 if val else WALL_CHAR if map.walkable[y+1,x] else VERTICAL_WALL_CHAR) + graphic = Graphic(0 if val else WALL_CHAR if (map.walkable[y+1,x] and Position(x ,y+1) in doors) else VERTICAL_WALL_CHAR) draw(pos, graphic) # draw all entities that are not actors for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]): diff --git a/game/tags.py b/game/tags.py index 00f8a0d..69017e0 100644 --- a/game/tags.py +++ b/game/tags.py @@ -12,3 +12,6 @@ IsActor: Final = "IsActor" IsItem: Final = "IsItem" """Entity is an item.""" + +IsDoor: Final = "IsDoor" +"""Entity is a door.""" diff --git a/game/world_tools.py b/game/world_tools.py index e08a584..5ac2e64 100644 --- a/game/world_tools.py +++ b/game/world_tools.py @@ -10,7 +10,7 @@ from tcod.map import Map import g from game.components import Action, Gold, Graphic, Position -from game.tags import IsActor, IsItem, IsPlayer +from game.tags import IsActor, IsItem, IsPlayer, IsDoor world_center: Final = Position(50, 50) @@ -32,7 +32,8 @@ def add_door(world, pos): Position: pos, Graphic: Graphic(ord('\\')), Action: unlock_door - } + }, + tags=[IsDoor] ) add_wall(world, pos)