Optimize wall/fog rendering

This commit is contained in:
gamer147
2026-04-07 10:22:30 -04:00
parent 0882908e4c
commit e42a98fece
3 changed files with 49 additions and 64 deletions

View File

@@ -5,39 +5,35 @@ extends Node2D
## that is not part of any room. Future: drive visibility from map state.
const TILE_SIZE := 100.0
## Fog tile region in aux_terrain.BMP
const FOG_RECT := Rect2(53, 53, 100, 100)
@export var atlas_texture: Texture2D
var _fog_tiles: PackedVector2Array = []
func draw_fog_for_layout(map_layout: MapLayout) -> void:
_clear()
_fog_tiles.clear()
if not map_layout or not atlas_texture:
queue_redraw()
return
for y in map_layout.size.y:
for x in map_layout.size.x:
var tile := Vector2i(x, y)
if map_layout.is_tile_valid(tile):
continue
_create_fog_sprite(Vector2(tile) * TILE_SIZE)
_fog_tiles.append(Vector2(tile) * TILE_SIZE)
queue_redraw()
func _clear() -> void:
for child in get_children():
child.queue_free()
func _create_fog_sprite(pos: Vector2) -> void:
var atlas := AtlasTexture.new()
atlas.atlas = atlas_texture
atlas.region = FOG_RECT
var sprite := Sprite2D.new()
sprite.texture = atlas
sprite.centered = false
sprite.position = pos
sprite.scale = Vector2(
TILE_SIZE / FOG_RECT.size.x,
TILE_SIZE / FOG_RECT.size.y,
)
add_child(sprite)
func _draw() -> void:
if not atlas_texture:
return
var dest_size := Vector2(TILE_SIZE, TILE_SIZE)
for pos in _fog_tiles:
draw_texture_rect_region(
atlas_texture,
Rect2(pos, dest_size),
FOG_RECT,
)