feature/tesla-coil-tower #8

Merged
matt merged 5 commits from feature/tesla-coil-tower into main 2026-06-25 15:57:55 -07:00
Showing only changes of commit 1afc78d64f - Show all commits

View file

@ -601,9 +601,9 @@ namespace TD.Gameplay
} }
Vector3 spawnPos = GridCoordinates.GetFootprintCenterWorld(anchor, def.FootprintSize); Vector3 spawnPos = GridCoordinates.GetFootprintCenterWorld(anchor, def.FootprintSize);
// Towers sit on the buildable plane (Y=0); raise slightly so the mesh base // Place on the buildable plane (Y=0); SeatOnGround below corrects the final Y
// sits flush rather than half-clipped. A cube of scale 1 needs +0.5 on Y. // from the mesh bounds so the tower rests flush regardless of its pivot.
spawnPos.y = 0.5f; spawnPos.y = 0f;
var go = Instantiate(def.TowerPrefab, spawnPos, Quaternion.identity); var go = Instantiate(def.TowerPrefab, spawnPos, Quaternion.identity);
var instance = go.GetComponent<TowerInstance>(); var instance = go.GetComponent<TowerInstance>();
@ -626,9 +626,32 @@ namespace TD.Gameplay
Destroy(go); Destroy(go);
return; return;
} }
// Seat the tower flush on the ground from its mesh bounds — BEFORE Spawn so the
// corrected transform is captured in the spawn sync. Handles any pivot.
SeatOnGround(go, groundY: 0f);
netObj.Spawn(destroyWithScene: true); netObj.Spawn(destroyWithScene: true);
} }
/// <summary>
/// Shifts <paramref name="go"/> vertically so the lowest point of its combined
/// renderer bounds rests at <paramref name="groundY"/>. Lets towers be authored with
/// any mesh pivot (center, base, etc.) and still sit flush on the ground — no
/// per-prefab Y tweaking. No-op if the prefab has no renderers.
/// </summary>
private static void SeatOnGround(GameObject go, float groundY)
{
var renderers = go.GetComponentsInChildren<Renderer>();
if (renderers.Length == 0) return;
Bounds b = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
b.Encapsulate(renderers[i].bounds);
go.transform.position += Vector3.up * (groundY - b.min.y);
}
// ----- Utility ---------------------------------------------------- // ----- Utility ----------------------------------------------------
private static bool TryGetDefinition(int typeId, out TowerDefinition def) private static bool TryGetDefinition(int typeId, out TowerDefinition def)