Fix: tile 3D models load on affinity change (not just at board build)
_update_tile_affinity previously only repainted the placeholder material. Now extracts model loading into _try_load_tile_model() which is called both at initial board build (_add_tile) and on every affinity change (_update_tile_affinity). Handles model swapping correctly — removes all non-collision children before adding the new model so Lake→Hill transitions don't stack meshes. 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:
@@ -389,19 +389,7 @@ func _build_board(board: Dictionary) -> void:
|
|||||||
_tile_bodies.clear()
|
_tile_bodies.clear()
|
||||||
_unit_meshes.clear()
|
_unit_meshes.clear()
|
||||||
_unit_bodies.clear()
|
_unit_bodies.clear()
|
||||||
|
for tile: Dictionary in board.get("tiles", []):
|
||||||
# Debug: show which tile model bindings are in cache right now.
|
|
||||||
var tiles_arr : Array = board.get("tiles", [])
|
|
||||||
var sample_affinities : Array = []
|
|
||||||
for t in tiles_arr.slice(0, 8):
|
|
||||||
sample_affinities.append(t.get("resolvedAffinity", "?"))
|
|
||||||
print("GameScreen: _build_board tiles=%d first8_affinities=%s" % [tiles_arr.size(), sample_affinities])
|
|
||||||
print("GameScreen: tile model cache — Hill_Square='%s' Lake_Square='%s'" % [
|
|
||||||
ContentCache.get_tile_model_url("Hill", "Square"),
|
|
||||||
ContentCache.get_tile_model_url("Lake", "Square"),
|
|
||||||
])
|
|
||||||
|
|
||||||
for tile: Dictionary in tiles_arr:
|
|
||||||
var coord : Dictionary = tile.get("coord", {})
|
var coord : Dictionary = tile.get("coord", {})
|
||||||
_add_tile(coord.get("x", 0), coord.get("y", 0),
|
_add_tile(coord.get("x", 0), coord.get("y", 0),
|
||||||
tile.get("resolvedAffinity", "Void"))
|
tile.get("resolvedAffinity", "Void"))
|
||||||
@@ -435,10 +423,15 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
|
|||||||
_tile_bodies["%d,%d" % [x, y]] = body
|
_tile_bodies["%d,%d" % [x, y]] = body
|
||||||
|
|
||||||
# ── Try to load a 3D tile model for this affinity ──────────────────────
|
# ── Try to load a 3D tile model for this affinity ──────────────────────
|
||||||
|
_try_load_tile_model(body, affinity)
|
||||||
|
|
||||||
|
|
||||||
|
## Load (or replace) the 3D model on a tile body for the given affinity.
|
||||||
|
## Safe to call any time affinity changes — replaces existing mesh if present.
|
||||||
|
func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void:
|
||||||
var model_url := ContentCache.get_tile_model_url(affinity, "Square")
|
var model_url := ContentCache.get_tile_model_url(affinity, "Square")
|
||||||
if model_url.is_empty():
|
if model_url.is_empty():
|
||||||
return
|
return
|
||||||
print("GameScreen: tile [%d,%d] %s → %s" % [x, y, affinity, model_url])
|
|
||||||
|
|
||||||
var body_ref : Variant = weakref(body)
|
var body_ref : Variant = weakref(body)
|
||||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||||
@@ -450,25 +443,29 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
|
|||||||
if model_node == null:
|
if model_node == null:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Remove the placeholder MeshInstance3D.
|
# Remove any existing mesh (placeholder or previously loaded model),
|
||||||
|
# but leave the CollisionShape3D so click detection still works.
|
||||||
for child in b.get_children():
|
for child in b.get_children():
|
||||||
if child is MeshInstance3D:
|
if not (child is CollisionShape3D):
|
||||||
child.queue_free()
|
child.queue_free()
|
||||||
|
|
||||||
# Tile body sits at wp + (0, 0.25, 0); model should author its
|
|
||||||
# centre at y = 0 so it fills the same space as the box placeholder.
|
|
||||||
b.add_child(model_node)
|
b.add_child(model_node)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
||||||
var body = _tile_bodies.get("%d,%d" % [x, y])
|
var body := _tile_bodies.get("%d,%d" % [x, y]) as StaticBody3D
|
||||||
if body == null: return
|
if body == null: return
|
||||||
|
|
||||||
|
# Update the placeholder colour in case there is no model for this affinity.
|
||||||
for child in body.get_children():
|
for child in body.get_children():
|
||||||
if child is MeshInstance3D:
|
if child is MeshInstance3D:
|
||||||
var mat := child.material_override as StandardMaterial3D
|
var mat := child.material_override as StandardMaterial3D
|
||||||
if mat: mat.albedo_color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
if mat: mat.albedo_color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||||||
|
|
||||||
|
# Attempt to swap in a real 3D model.
|
||||||
|
_try_load_tile_model(body, affinity)
|
||||||
|
|
||||||
# ── Unit meshes ───────────────────────────────────────────────────────────────
|
# ── Unit meshes ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
func _rebuild_units(state: Dictionary) -> void:
|
func _rebuild_units(state: Dictionary) -> void:
|
||||||
|
|||||||
Reference in New Issue
Block a user