Fix highlight Y positioning using per-tile visual top tracking

Highlights were clipping into tile tops ~half the time because
_tile_heights stores the collision box height (random 0.8-1.0),
but _fit_model_to_tile scales 3D models to their XZ footprint —
so a taller-than-wide model ends up visually taller than the box.

Also fixes _update_hover nil crash: tile.get("occupantId", "")
returns null (not "") when the key exists with a null server value.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-23 09:32:43 -04:00
co-authored by Claude Happy
parent fe293f6bee
commit 29b66030c8
3 changed files with 23 additions and 10 deletions
@@ -172,8 +172,9 @@ var _prev_unit_hp : Dictionary = {} # instanceId → int, HP snapshot for damag
var _prev_unit_pos: Dictionary = {} # instanceId → Vector2i, position snapshot for move detection
var _move_anims : Dictionary = {} # instanceId → Tween (active movement tweens)
var _last_attack_target_iid : String = "" # target of the most recent send_attack() call
var _tile_heights : Dictionary = {} # "x,y" → float, height of tile block
var _tile_y_bases : Dictionary = {} # "x,y" → float, vertical stagger offset
var _tile_heights : Dictionary = {} # "x,y" → float, height of tile block
var _tile_y_bases : Dictionary = {} # "x,y" → float, vertical stagger offset
var _tile_visual_tops : Dictionary = {} # "x,y" → float, actual visual top Y in world space (updated after model loads)
# Delayed rebuild: when animations need time to play we don't destroy unit nodes immediately.
var _pending_rebuild : Dictionary = {} # latest state waiting for the delayed rebuild
@@ -813,6 +814,7 @@ func _build_board(board: Dictionary) -> void:
_unit_bodies.clear()
_tile_heights.clear()
_tile_y_bases.clear()
_tile_visual_tops.clear()
for tile: Dictionary in board.get("tiles", []):
var coord : Dictionary = tile.get("coord", {})
_add_tile(coord.get("x", 0), coord.get("y", 0),
@@ -832,6 +834,7 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
var y_base : float = rng.randf_range(-0.15, 0.15)
_tile_heights["%d,%d" % [x, y]] = h
_tile_y_bases["%d,%d" % [x, y]] = y_base
_tile_visual_tops["%d,%d" % [x, y]] = y_base + h # default = collision-box top; updated after 3D model loads
var body := StaticBody3D.new()
body.position = wp + Vector3(0, y_base + h * 0.5, 0)
@@ -900,6 +903,14 @@ func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void:
b.add_child(model_node)
# Scale so the XZ footprint fits the 0.98-unit tile square.
_fit_model_to_tile(model_node, 0.98)
# Update the visual-top cache so highlights land just above the model surface,
# not the collision box top (the two can differ when model height ≠ box height).
var _tc : Vector2i = b.get_meta("tile_coord", Vector2i(-1, -1))
if _tc.x >= 0:
var _ma := _model_local_aabb(model_node)
if _ma.size != Vector3.ZERO:
_tile_visual_tops["%d,%d" % [_tc.x, _tc.y]] = \
b.position.y + _ma.size.y * model_node.scale.y
# Attach a random stalagmite bottom model below the tile (if any are configured).
# PureVoid tiles are skipped — they float in empty space with no decoration below.
@@ -1892,7 +1903,8 @@ func _update_hover(mouse_pos: Vector2) -> void:
# Check for an occupant on this tile and highlight with team colour.
var tile : Dictionary = Game.get_tile(coord.x, coord.y)
var occ_id : String = tile.get("occupantId", "")
var _raw_occ = tile.get("occupantId")
var occ_id : String = str(_raw_occ) if _raw_occ != null else ""
if occ_id.is_empty():
_hover_team_overlay.visible = false
return
@@ -1975,9 +1987,9 @@ func _add_highlight(x: int, y: int, type_key: String) -> void:
mat.albedo_color = col
mat.emission = Color(col.r, col.g, col.b)
mi.material_override = mat
var _hi_tile_h : float = _tile_heights.get("%d,%d" % [x, y], 1.0)
var _hi_tile_base : float = _tile_y_bases.get("%d,%d" % [x, y], 0.0)
mi.position = _coord_to_world(x, y) + Vector3(0, _hi_tile_base + _hi_tile_h + 0.05, 0)
var _hi_tile_top : float = _tile_visual_tops.get("%d,%d" % [x, y],
_tile_y_bases.get("%d,%d" % [x, y], 0.0) + _tile_heights.get("%d,%d" % [x, y], 1.0))
mi.position = _coord_to_world(x, y) + Vector3(0, _hi_tile_top + 0.05, 0)
_hi_root.add_child(mi)
@@ -1991,9 +2003,9 @@ func _highlight_tiles(coords: Array, type_key: String) -> void:
## Position a permanently-allocated highlight quad above tile (x, y).
func _place_highlight_quad(quad: MeshInstance3D, x: int, y: int, col: Color, texture: Texture2D = null) -> void:
var tile_h : float = _tile_heights.get("%d,%d" % [x, y], 1.0)
var tile_base : float = _tile_y_bases.get("%d,%d" % [x, y], 0.0)
quad.position = _coord_to_world(x, y) + Vector3(0, tile_base + tile_h + 0.05, 0)
var tile_top : float = _tile_visual_tops.get("%d,%d" % [x, y],
_tile_y_bases.get("%d,%d" % [x, y], 0.0) + _tile_heights.get("%d,%d" % [x, y], 1.0))
quad.position = _coord_to_world(x, y) + Vector3(0, tile_top + 0.05, 0)
var mat := quad.material_override as StandardMaterial3D
mat.albedo_color = col
mat.emission = Color(col.r, col.g, col.b)