diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index c07c69d..c2fb936 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -450,9 +450,48 @@ func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void: child.queue_free() 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) ) +## Scales model so its largest XZ dimension equals `tile_size`, then centres +## it on XZ and seats the bottom at y = 0 in the parent body's local space. +func _fit_model_to_tile(model: Node3D, tile_size: float) -> void: + var aabb := _model_local_aabb(model) + if aabb.size == Vector3.ZERO: + return + var footprint := max(aabb.size.x, aabb.size.z) + if footprint <= 0.0: + return + var s := tile_size / footprint + model.scale = Vector3.ONE * s + var c := aabb.get_center() + model.position = Vector3(-c.x * s, -aabb.position.y * s, -c.z * s) + + +## Returns the combined AABB of all VisualInstance3D descendants expressed +## in the root node's own local coordinate space (before root.scale/position). +func _model_local_aabb(root: Node3D) -> AABB: + var result := AABB() + var first := true + # Each entry: { n: Node3D, x: Transform3D } where x transforms from + # that node's local space to root's local space. + var stack : Array = [{ "n": root, "x": Transform3D.IDENTITY }] + while not stack.is_empty(): + var e : Dictionary = stack.pop_back() + var node : Node3D = e["n"] + var xform : Transform3D = e["x"] + if node is VisualInstance3D: + var local_aabb := xform * (node as VisualInstance3D).get_aabb() + result = local_aabb if first else result.merge(local_aabb) + first = false + for child in node.get_children(): + if child is Node3D: + stack.push_back({ "n": child, "x": xform * (child as Node3D).transform }) + return result + + func _update_tile_affinity(x: int, y: int, affinity: String) -> void: var body := _tile_bodies.get("%d,%d" % [x, y]) as StaticBody3D if body == null: return