Compare commits

..

3 commits

Author SHA1 Message Date
1bd0ab372d enhanced FOV 2024-08-17 12:28:39 +02:00
0a0ff2d1ed added FOV 2024-08-17 12:09:19 +02:00
1b5d512a4b fixed global usage 2024-08-17 11:48:28 +02:00
4 changed files with 45 additions and 22 deletions

2
g.py
View file

@ -15,8 +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_center: tuple[int,int] = Position(50, 50)
screens: list[Screen] = [] screens: list[Screen] = []
"""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.screens import Push, Screen, ScreenResult
from game.tags import IsItem, IsPlayer, IsDoor, IsActor from game.tags import IsItem, IsPlayer, IsDoor, IsActor
from game.constants import WALL_CHAR from game.constants import WALL_CHAR
from game.screens import menu_screens from game.screens import menu_screens
from game.world_tools import world_pos_to_map_pos, map_pos_to_world_pos
@attrs.define() @attrs.define()
class MainScreen(Screen): class MainScreen(Screen):
@ -34,7 +34,8 @@ 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[None].components[Map].walkable[g.world_center.y+new_pos.y][g.world_center.x+new_pos.x]: map_pos = world_pos_to_map_pos(new_pos)
if not g.world[None].components[Map].walkable[map_pos.y][map_pos.x]:
return None return None
player.components[Position] = new_pos player.components[Position] = new_pos
# Auto pickup gold # Auto pickup gold
@ -56,20 +57,37 @@ class MainScreen(Screen):
camera_pos = camera_pos.mod(len(centers)) camera_pos = camera_pos.mod(len(centers))
h = console.height//2 h = console.height//2
w = console.width//2 w = console.width//2
map: Map = g.world[None].components[Map]
cam_map = world_pos_to_map_pos(camera_pos)
map.compute_fov(cam_map.x, cam_map.y, 100)
def draw(e_pos, e_graph): def draw(e_pos, e_graph):
screen_pos = e_pos - camera_pos screen_pos = e_pos - camera_pos
map_pos = world_pos_to_map_pos(e_pos)
if (-w <= screen_pos.x < w\ if (-w <= screen_pos.x < w\
and -h <= screen_pos.y < h): and -h <= screen_pos.y < h):
graphic = e_graph if map.fov[map_pos.y][map_pos.x]:
graphic = e_graph
else:
graphic = Graphic(0x2591, (50, 50, 50))
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[None].components[Map].walkable):
# Draw walls
for (y, row) in enumerate(map.walkable):
for (x, val) in enumerate(row): for (x, val) in enumerate(row):
if not val: pos = map_pos_to_world_pos(Position(x,y))
draw(Position(x,y)-g.world_center, Graphic(WALL_CHAR)) if val:
draw(pos, Graphic(0)) # open air
else:
draw(pos, Graphic(WALL_CHAR))
# draw all entities that are not actors
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.components[Position], entity.components[Graphic]) draw(entity.components[Position], entity.components[Graphic])
# draw all actors
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.components[Position], entity.components[Graphic]) draw(actor.components[Position], entity.components[Graphic])
# draw the player
for player in g.world.Q.all_of(tags=[IsPlayer]): for player in g.world.Q.all_of(tags=[IsPlayer]):
draw(player.components[Position], player.components[Graphic]) 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)):

View file

@ -1,38 +1,45 @@
"""Functions for working with worlds.""" """Functions for working with worlds."""
from __future__ import annotations from __future__ import annotations
from random import Random from random import Random
from typing import Final
import g
from tcod.ecs import Registry, Entity from tcod.ecs import Registry, Entity
from tcod.map import Map from tcod.map import Map
import g
from game.components import Gold, Graphic, Position from game.components import Gold, Graphic, Position
from game.tags import IsActor, IsItem, IsPlayer, IsDoor from game.tags import IsActor, IsItem, IsPlayer, IsDoor
def add_wall(pos, remove=False): world_center: Final = Position(50, 50)
r_pos = g.world_center + pos
map = g.world[None].components[Map] def world_pos_to_map_pos(pos):
return pos+world_center
def map_pos_to_world_pos(pos):
return pos-world_center
def add_wall(world, pos, remove=False):
r_pos = world_pos_to_map_pos(pos)
map = world[None].components[Map]
map.walkable[r_pos.y][r_pos.x] = remove map.walkable[r_pos.y][r_pos.x] = remove
map.transparent[r_pos.y][r_pos.x] = remove map.transparent[r_pos.y][r_pos.x] = remove
def add_door(pos): def add_door(world, pos):
g.world.new_entity( world.new_entity(
components={ components={
Position: pos, Position: pos,
Graphic: Graphic(ord('\\')) Graphic: Graphic(ord('\\'))
}, },
tags=[IsDoor] tags=[IsDoor]
) )
add_wall(pos) add_wall(world, pos)
def new_world() -> Registry: def new_world() -> Registry:
"""Return a freshly generated world.""" """Return a freshly generated world."""
world = Registry() world = Registry()
g.world = world
map = world[None].components[Map] = Map(100, 100) map = world[None].components[Map] = Map(100, 100)
map.walkable[:] = True map.walkable[:] = True
@ -66,9 +73,9 @@ def new_world() -> Registry:
for i in range(20): for i in range(20):
if i == 5 or i == 9: if i == 5 or i == 9:
add_door(Position(10,i)) add_door(world, Position(10,i))
else: else:
add_wall(Position(10, i)) add_wall(world, Position(10, i))
return world return world
@ -76,4 +83,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()
add_wall(door.components[Position], remove=True) add_wall(g.world, door.components[Position], remove=True)

View file

@ -17,7 +17,7 @@ def main() -> None:
tileset = tcod.tileset.load_tilesheet( tileset = tcod.tileset.load_tilesheet(
"data/Alloy_curses_12x12.png", columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437 "data/Alloy_curses_12x12.png", columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437
) )
tcod.tileset.procedural_block_elements(tileset=tileset) #tcod.tileset.procedural_block_elements(tileset=tileset)
g.screens = [MainMenu()] g.screens = [MainMenu()]
g.console = tcod.console.Console(80, 50) g.console = tcod.console.Console(80, 50)