enhanced FOV

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 12:28:39 +02:00
parent 0a0ff2d1ed
commit 1bd0ab372d
4 changed files with 20 additions and 15 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

@ -64,18 +64,23 @@ class MainScreen(Screen):
def draw(e_pos, e_graph): def draw(e_pos, e_graph):
screen_pos = e_pos - camera_pos screen_pos = e_pos - camera_pos
mp = world_pos_to_map_pos(e_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):
and map.fov[mp.y][mp.x]: if map.fov[map_pos.y][map_pos.x]:
graphic = e_graph 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
# Draw walls # Draw walls
for (y, row) in enumerate(map.walkable): 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(map_pos_to_world_pos(Position(x,y)), Graphic(WALL_CHAR)) if val:
draw(pos, Graphic(0)) # open air
else:
draw(pos, Graphic(WALL_CHAR))
# draw all entities that are not actors # 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])

View file

@ -1,25 +1,27 @@
"""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
world_center: Final = Position(50, 50)
def world_pos_to_map_pos(pos): def world_pos_to_map_pos(pos):
return pos+g.world_center return pos+world_center
def map_pos_to_world_pos(pos): def map_pos_to_world_pos(pos):
return pos-g.world_center return pos-world_center
def add_wall(world, pos, remove=False): def add_wall(world, pos, remove=False):
r_pos = g.world_center + pos r_pos = world_pos_to_map_pos(pos)
map = world[None].components[Map] 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

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)