extends Node2D
## Phase 13 — subtle warm overlay drawn over every tile that belongs to an
## enclosed, roofed Room.
##
## Rendering model: Node2D _draw() filled on EventBus.room_changed only — NOT
## every render frame.  At MVP scale (< 20 rooms × ~10 tiles each) that's at
## most ~200 draw_rect calls per room-topology change, which is negligible.
##
## Placement: child of the World Node2D, z_index = 3 (above Floor layer at 1,
## same z as Designation — kept above floor, below pawns/entities at 4+).
##
## The overlay does NOT use CanvasModulate or shaders; a plain translucent rect
## per tile is correct, cheap, and doesn't interact with the day/night modulate.
##
## When the room registry is empty (boot, before RoomDetector fires), _draw()
## simply does nothing — graceful degradation.

class_name IndoorTintOverlay

## Warm candlelight tint.  Alpha is deliberately very low (0.10) so the floor
## and entity sprites beneath remain fully legible.
const INDOOR_COLOR: Color = Color(1.0, 0.95, 0.85, 0.10)

const TILE_SIZE_PX: int = 16  ## Mirror of World.TILE_SIZE_PX; standalone to avoid circular dep.


func _ready() -> void:
	z_index = 3
	# Listen for room topology changes and redraw.  room_changed fires when any
	# Room is created, destroyed, or recomputed by RoomDetector (Agent A).
	EventBus.room_changed.connect(_on_room_changed)


func _on_room_changed(_room_id: int) -> void:
	queue_redraw()


func _draw() -> void:
	# Iterate every room in the registry.  Only draw tiles belonging to roofed rooms.
	for id in World.rooms:
		var r = World.rooms[id]
		if not r.is_under_roof:
			continue
		# r.tiles is an Array[Vector2i] per room.gd contract.
		for t in r.tiles:
			var rect := Rect2(
				float(t.x * TILE_SIZE_PX),
				float(t.y * TILE_SIZE_PX),
				float(TILE_SIZE_PX),
				float(TILE_SIZE_PX)
			)
			draw_rect(rect, INDOOR_COLOR)
