rimlike/scenes/world/designation.gd
megaproxy 96f4982dd3 Phase 5 cabin polish — door, floor, interior crate, double-render fix
Make the demo cabin readable as a real building so the rendering pattern
is solid before Phase 6+ adds more building types.

Demo seed (world.gd._seed_phase5_demo_buildings):
- 8×6 stone cabin at (44, 23) — 23 walls (perimeter minus door slot) +
  1 door (south wall centre at (47, 28)) + 24 wood-floor designations
  for the interior. ConstructionProvider picks them all up; pawns build
  the whole thing.
- One pre-built crate inside at (50, 24) so the interior reads as a
  furnished room on first frame.
- Two external stockpile-target crates unchanged at (17, 60) / (18, 60).

Door visual rewrite (door.gd):
- Was the old 16×24 bottom-anchored shape that encroached on the cell
  above. Now fits strictly within its 16×16 tile, matching the wall's
  3/4 band layout (5 px lit lintel + 11 px shaded frame + inset panel
  + hinge dot). Door and walls now share a top horizon line so they
  line up visually.

Designation gained TOOL_BUILD_DOOR + atlas mapping; world.gd's
_on_designation_added now branches on build_door to spawn a Door entity.

THE DOUBLE-RENDER BUG (caught by MCP inspection):
- World.mark_floor_tile stamps the Floor TileMap with atlas (2, 0) which
  is *stone-grey* in the placeholder atlas, regardless of material name.
- The Floor TileMap layer was visible=true with z_index=1, so it drew
  ON TOP of the brown Floor entity sprites underneath.
- Result before fix: interior tiles looked gray-stone, not wood.
- Fix: set Floor TileMap layer visible=false (data-only, same as Wall).
  Entities own the visual; the TileMap retains tile-level data for
  Phase 13's room detection + Phase 16's save format.

Pattern locked for future building types: 'render at entity level,
TileMap layers are data-only'. Phase 13's roof and any future wall
materials follow the same template.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 22:46:27 +01:00

173 lines
6.5 KiB
GDScript

extends Node
class_name Designation
## Phase 5 — Designation paint mode.
##
## Captures mouse input to drag-paint ghost tiles on Layer 3 (designation_layer)
## of the world TileMap. Each new cell painted emits EventBus.designation_added;
## removing a ghost emits EventBus.designation_cleared.
##
## Integrates with Selection: raises Selection.designation_active while a non-none
## tool is active so Selection does not also process those clicks.
##
## Opus wires the node into world.tscn and calls bind() + set_active_tool() from
## the build-drawer UI.
# ── tool constants ───────────────────────────────────────────────────────────
const TOOL_NONE: StringName = &"none"
const TOOL_BUILD_WALL: StringName = &"build_wall"
const TOOL_BUILD_FLOOR: StringName = &"build_floor"
const TOOL_BUILD_DOOR: StringName = &"build_door"
# Atlas coords on the shared placeholder tileset (source 0).
# build_wall → stone-grey (2, 0); build_floor → dirt-brown (1, 0).
# build_door → dark stone (3, 0) so the ghost reads visually distinct from walls.
const _ATLAS_BY_TOOL: Dictionary = {
&"build_wall": Vector2i(2, 0),
&"build_floor": Vector2i(1, 0),
&"build_door": Vector2i(3, 0),
}
# Placeholder source ID — mirrors World.PLACEHOLDER_SOURCE_ID.
const _SOURCE_ID: int = 0
# Tile-size in pixels — mirrors World.TILE_SIZE_PX.
const _TILE_SIZE_PX: float = 16.0
# ── signals ──────────────────────────────────────────────────────────────────
signal designation_added(cell: Vector2i, tool: StringName)
signal designation_cleared(cell: Vector2i)
# ── state ────────────────────────────────────────────────────────────────────
var _tool: StringName = TOOL_NONE
var _paint_layer: TileMapLayer = null
var _selection: Selection = null # optional; raised/lowered for input hand-off
# cell → tool that placed the ghost.
var _painted: Dictionary = {} # Dictionary[Vector2i, StringName]
# Stroke deduplication — cells touched in the current mouse-down session.
var _stroke_cells: Dictionary = {} # Dictionary[Vector2i, bool]
var _stroke_active: bool = false
# ── public API ───────────────────────────────────────────────────────────────
## Call once from World._ready() with the TileMapLayer at index 3 (Designation)
## and the sibling Selection node.
func bind(paint_layer: TileMapLayer, selection: Selection = null) -> void:
assert(paint_layer != null, "Designation.bind: paint_layer is null")
_paint_layer = paint_layer
_selection = selection
## Activate a paint tool. Pass TOOL_NONE to deactivate.
func set_active_tool(tool: StringName) -> void:
assert(
tool in [TOOL_NONE, TOOL_BUILD_WALL, TOOL_BUILD_FLOOR, TOOL_BUILD_DOOR],
"Designation.set_active_tool: unknown tool '%s'" % tool
)
_tool = tool
_stroke_active = false
_stroke_cells.clear()
_sync_selection_flag()
if tool == TOOL_NONE:
Audit.log("designation", "tool deactivated")
else:
Audit.log("designation", "tool set to '%s'" % tool)
func active_tool() -> StringName:
return _tool
## Remove the ghost for a single cell and emit designation_cleared.
func clear_cell(cell: Vector2i) -> void:
if not _painted.has(cell):
return
_painted.erase(cell)
if _paint_layer != null:
_paint_layer.erase_cell(cell)
designation_cleared.emit(cell)
EventBus.designation_cleared.emit(cell)
Audit.log("designation", "cleared cell %s" % cell)
## Returns all cells that currently have an active ghost.
func cells() -> Array[Vector2i]:
var result: Array[Vector2i] = []
for c: Vector2i in _painted.keys():
result.append(c)
return result
# ── input ────────────────────────────────────────────────────────────────────
func _unhandled_input(event: InputEvent) -> void:
if _tool == TOOL_NONE or _paint_layer == null:
return
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
_stroke_active = true
_stroke_cells.clear()
_paint_at_screen(event.position)
else:
_stroke_active = false
_stroke_cells.clear()
get_viewport().set_input_as_handled()
return
if event is InputEventMouseMotion and _stroke_active:
_paint_at_screen(event.position)
get_viewport().set_input_as_handled()
# ── helpers ──────────────────────────────────────────────────────────────────
func _paint_at_screen(screen_pos: Vector2) -> void:
var world_pos: Vector2 = get_viewport().get_canvas_transform().affine_inverse() * screen_pos
var cell := Vector2i(
floori(world_pos.x / _TILE_SIZE_PX),
floori(world_pos.y / _TILE_SIZE_PX),
)
# Deduplication within the current stroke.
if _stroke_cells.has(cell):
return
_stroke_cells[cell] = true
# Don't re-paint the same tool over itself.
if _painted.get(cell) == _tool:
return
_apply_ghost(cell)
func _apply_ghost(cell: Vector2i) -> void:
var atlas_coord: Vector2i = _ATLAS_BY_TOOL.get(_tool, Vector2i(2, 0))
_paint_layer.set_cell(cell, _SOURCE_ID, atlas_coord)
_painted[cell] = _tool
# Modulate the whole paint layer based on whether this cell is placeable.
# Phase 5 simplification: validity applies globally to the layer.
var ok: bool = _cell_is_placeable(cell)
_paint_layer.modulate = Color(0.4, 1.0, 0.4, 0.7) if ok else Color(1.0, 0.4, 0.4, 0.7)
designation_added.emit(cell, _tool)
EventBus.designation_added.emit(cell, _tool)
Audit.log("designation", "painted %s at %s (placeable=%s)" % [_tool, cell, ok])
func _cell_is_placeable(cell: Vector2i) -> bool:
# For Phase 5: a cell is placeable for build_wall if it is currently walkable
# (no wall present), and for build_floor if it is walkable. Items-on-tile
# check is deferred to Phase 5 BuildJob validation; we only gate on geometry here.
if World.pathfinder == null:
return true
return World.pathfinder.is_walkable(cell)
func _sync_selection_flag() -> void:
if _selection == null:
return
_selection.designation_active = (_tool != TOOL_NONE)