Files
dungeon-divers/scripts/dungeon/hazard_overlay_renderer.gd
T

112 lines
3.3 KiB
GDScript

class_name HazardOverlayRenderer
extends Node2D
## Renders colored tile overlays for active hazard zones (Ability Type 29).
##
## Each hazard has a world-space center (x, y), radius, tintColor, and tintOpacity.
## We compute which tiles fall within the radius and draw a tinted rectangle for each.
## Overlapping hazards stack by drawing in id-ascending order with alpha blending.
const WORLD_TILE_SIZE := 32 # 16px tiles * 2x scale
# Cached state: hazard id -> parsed data used for drawing
var _active_hazards: Dictionary = {} # id -> { tiles: Array[Vector2i], color: Color }
func _ready():
# Render above ground/floor tiles, below entities and projectiles
z_index = 5
func update_hazards(hazards: Dictionary) -> void:
## Rebuild overlay state from the authoritative server hazard dict.
var changed := false
# Detect additions / changes
for id in hazards:
var hz = hazards[id]
var center_x: float = hz.get("x", 0.0)
var center_y: float = hz.get("y", 0.0)
var radius: float = hz.get("radius", 0.0)
var tint_hex: String = hz.get("tintColor", "#ff0000")
var tint_opacity: float = hz.get("tintOpacity", 0.35)
var color := Color.from_string(tint_hex, Color.RED)
color.a = clampf(tint_opacity, 0.0, 1.0)
var tiles := _compute_tiles_in_radius(center_x, center_y, radius)
if _active_hazards.has(id):
var prev = _active_hazards[id]
# Quick change check: color or tile count changed
if prev.color != color or prev.tiles.size() != tiles.size():
_active_hazards[id] = { "tiles": tiles, "color": color }
changed = true
else:
_active_hazards[id] = { "tiles": tiles, "color": color }
changed = true
# Detect removals
var to_remove: Array = []
for id in _active_hazards:
if not hazards.has(id):
to_remove.append(id)
if to_remove.size() > 0:
changed = true
for id in to_remove:
_active_hazards.erase(id)
if changed:
queue_redraw()
func clear() -> void:
if _active_hazards.size() > 0:
_active_hazards.clear()
queue_redraw()
func _draw():
if _active_hazards.is_empty():
return
# Sort by id ascending for deterministic overlap blending
var sorted_ids = _active_hazards.keys()
sorted_ids.sort()
for id in sorted_ids:
var entry = _active_hazards[id]
var color: Color = entry.color
var tiles: Array = entry.tiles
for tile_pos in tiles:
var rect := Rect2(
tile_pos.x * WORLD_TILE_SIZE,
tile_pos.y * WORLD_TILE_SIZE,
WORLD_TILE_SIZE,
WORLD_TILE_SIZE
)
draw_rect(rect, color)
func _compute_tiles_in_radius(cx: float, cy: float, radius: float) -> Array:
## Return an array of Vector2i tile coordinates whose centers fall within the radius.
var tiles: Array = []
if radius <= 0.0:
return tiles
# Tile grid bounds that could possibly be in range
var half_tile := WORLD_TILE_SIZE * 0.5
var min_tx := int(floor((cx - radius) / WORLD_TILE_SIZE))
var max_tx := int(floor((cx + radius) / WORLD_TILE_SIZE))
var min_ty := int(floor((cy - radius) / WORLD_TILE_SIZE))
var max_ty := int(floor((cy + radius) / WORLD_TILE_SIZE))
var radius_sq := radius * radius
for tx in range(min_tx, max_tx + 1):
for ty in range(min_ty, max_ty + 1):
# Tile center in world space
var tile_cx := tx * WORLD_TILE_SIZE + half_tile
var tile_cy := ty * WORLD_TILE_SIZE + half_tile
var dx := tile_cx - cx
var dy := tile_cy - cy
if dx * dx + dy * dy <= radius_sq:
tiles.append(Vector2i(tx, ty))
return tiles