mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Refactor detail builder, fix parenting issue (#277)
Makes detail making stuff just be in a single method call. The Make method now just handles getting the prefab, if its from a path or from an asset bundle. No longer inconsistent when using different detail options like parenting, renaming, removing components, etc.
This commit is contained in:
commit
647baf556f
@ -151,7 +151,7 @@ namespace NewHorizons.Builder.Body
|
||||
{
|
||||
foreach (var detailInfo in body.Config.Props.proxyDetails)
|
||||
{
|
||||
DetailBuilder.Make(newProxy, null, body.Config, body.Mod, detailInfo);
|
||||
DetailBuilder.Make(newProxy, null, body.Mod, detailInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,29 +20,80 @@ namespace NewHorizons.Builder.Props
|
||||
return detailInfoToCorrespondingSpawnedGameObject[detail];
|
||||
}
|
||||
|
||||
public static void Make(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, PropModule.DetailInfo detail)
|
||||
public static void RegisterDetailInfo(PropModule.DetailInfo detail, GameObject detailGO)
|
||||
{
|
||||
GameObject detailGO = null;
|
||||
detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO;
|
||||
}
|
||||
|
||||
public static GameObject Make(GameObject go, Sector sector, IModBehaviour mod, PropModule.DetailInfo detail)
|
||||
{
|
||||
GameObject prefab;
|
||||
if (detail.assetBundle != null)
|
||||
{
|
||||
var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
|
||||
// Shouldn't happen
|
||||
if (mod == null) return null;
|
||||
|
||||
detailGO = MakeDetail(go, sector, prefab, detail);
|
||||
prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
|
||||
}
|
||||
else
|
||||
{
|
||||
var prefab = SearchUtilities.Find(detail.path);
|
||||
if (prefab == null) Logger.LogError($"Couldn't find detail {detail.path}");
|
||||
else detailGO = MakeDetail(go, sector, prefab, detail);
|
||||
prefab = SearchUtilities.Find(detail.path);
|
||||
}
|
||||
|
||||
if (detailGO == null) return;
|
||||
if (prefab == null) Logger.LogError($"Couldn't find detail {detail.path}");
|
||||
|
||||
return MakeDetail(go, sector, prefab, detail);
|
||||
}
|
||||
|
||||
public static GameObject MakeDetail(GameObject go, Sector sector, GameObject prefab, PropModule.DetailInfo detail)
|
||||
{
|
||||
if (prefab == null) return null;
|
||||
|
||||
GameObject prop = prefab.InstantiateInactive();
|
||||
prop.name = prefab.name;
|
||||
prop.transform.parent = sector?.transform ?? go.transform;
|
||||
|
||||
StreamingHandler.SetUpStreaming(prop, sector);
|
||||
|
||||
var isTorch = prop.GetComponent<VisionTorchItem>() != null;
|
||||
|
||||
foreach (var component in prop.GetComponentsInChildren<Component>(true))
|
||||
{
|
||||
if (sector == null)
|
||||
{
|
||||
if (FixUnsectoredComponent(component)) continue;
|
||||
}
|
||||
else FixSectoredComponent(component, sector, isTorch);
|
||||
|
||||
FixComponent(component, go, prefab.name);
|
||||
}
|
||||
|
||||
prop.transform.position = detail.position == null ? go.transform.position : go.transform.TransformPoint(detail.position);
|
||||
|
||||
Quaternion rot = detail.rotation == null ? Quaternion.identity : Quaternion.Euler(detail.rotation);
|
||||
|
||||
if (detail.alignToNormal)
|
||||
{
|
||||
// Apply the rotation after aligning it with normal
|
||||
var up = go.transform.InverseTransformPoint(prop.transform.position).normalized;
|
||||
prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up);
|
||||
prop.transform.rotation *= rot;
|
||||
}
|
||||
else
|
||||
{
|
||||
prop.transform.rotation = go.transform.TransformRotation(rot);
|
||||
}
|
||||
|
||||
prop.transform.localScale = detail.scale != 0 ? Vector3.one * detail.scale : prefab.transform.localScale;
|
||||
|
||||
prop.SetActive(true);
|
||||
|
||||
if (prop == null) return null;
|
||||
|
||||
if (detail.removeChildren != null)
|
||||
{
|
||||
var detailPath = detailGO.transform.GetPath();
|
||||
var transforms = detailGO.GetComponentsInChildren<Transform>(true);
|
||||
var detailPath = prop.transform.GetPath();
|
||||
var transforms = prop.GetComponentsInChildren<Transform>(true);
|
||||
foreach (var childPath in detail.removeChildren)
|
||||
{
|
||||
// Multiple children can have the same path so we delete all that match
|
||||
@ -62,12 +113,12 @@ namespace NewHorizons.Builder.Props
|
||||
if (detail.removeComponents)
|
||||
{
|
||||
// Just swap all the children to a new game object
|
||||
var newDetailGO = new GameObject(detailGO.name);
|
||||
newDetailGO.transform.position = detailGO.transform.position;
|
||||
newDetailGO.transform.parent = detailGO.transform.parent;
|
||||
var newDetailGO = new GameObject(prop.name);
|
||||
newDetailGO.transform.position = prop.transform.position;
|
||||
newDetailGO.transform.parent = prop.transform.parent;
|
||||
// Can't modify parents while looping through children bc idk
|
||||
var children = new List<Transform>();
|
||||
foreach (Transform child in detailGO.transform)
|
||||
foreach (Transform child in prop.transform)
|
||||
{
|
||||
children.Add(child);
|
||||
}
|
||||
@ -75,13 +126,13 @@ namespace NewHorizons.Builder.Props
|
||||
{
|
||||
child.parent = newDetailGO.transform;
|
||||
}
|
||||
GameObject.Destroy(detailGO);
|
||||
detailGO = newDetailGO;
|
||||
GameObject.Destroy(prop);
|
||||
prop = newDetailGO;
|
||||
}
|
||||
|
||||
if (detail.rename != null)
|
||||
{
|
||||
detailGO.name = detail.rename;
|
||||
prop.name = detail.rename;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(detail.parentPath))
|
||||
@ -89,56 +140,10 @@ namespace NewHorizons.Builder.Props
|
||||
var newParent = go.transform.Find(detail.parentPath);
|
||||
if (newParent != null)
|
||||
{
|
||||
detailGO.transform.parent = newParent.transform;
|
||||
prop.transform.parent = newParent.transform;
|
||||
}
|
||||
}
|
||||
|
||||
detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO;
|
||||
}
|
||||
|
||||
public static GameObject MakeDetail(GameObject planetGO, Sector sector, GameObject prefab, PropModule.DetailInfo info)
|
||||
{
|
||||
if (prefab == null) return null;
|
||||
|
||||
GameObject prop = prefab.InstantiateInactive();
|
||||
prop.name = prefab.name;
|
||||
prop.transform.parent = sector?.transform ?? planetGO.transform;
|
||||
|
||||
StreamingHandler.SetUpStreaming(prop, sector);
|
||||
|
||||
var isTorch = prop.GetComponent<VisionTorchItem>() != null;
|
||||
|
||||
foreach (var component in prop.GetComponentsInChildren<Component>(true))
|
||||
{
|
||||
if (sector == null)
|
||||
{
|
||||
if (FixUnsectoredComponent(component)) continue;
|
||||
}
|
||||
else FixSectoredComponent(component, sector, isTorch);
|
||||
|
||||
FixComponent(component, planetGO, prefab.name);
|
||||
}
|
||||
|
||||
prop.transform.position = info.position == null ? planetGO.transform.position : planetGO.transform.TransformPoint(info.position);
|
||||
|
||||
Quaternion rot = info.rotation == null ? Quaternion.identity : Quaternion.Euler(info.rotation);
|
||||
|
||||
if (info.alignToNormal)
|
||||
{
|
||||
// Apply the rotation after aligning it with normal
|
||||
var up = planetGO.transform.InverseTransformPoint(prop.transform.position).normalized;
|
||||
prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up);
|
||||
prop.transform.rotation *= rot;
|
||||
}
|
||||
else
|
||||
{
|
||||
prop.transform.rotation = planetGO.transform.TransformRotation(rot);
|
||||
}
|
||||
|
||||
prop.transform.localScale = info.scale != 0 ? Vector3.one * info.scale : prefab.transform.localScale;
|
||||
|
||||
prop.SetActive(true);
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
|
||||
@ -29,7 +29,8 @@ namespace NewHorizons.Builder.Props
|
||||
{
|
||||
try
|
||||
{
|
||||
DetailBuilder.Make(go, sector, config, mod, detail);
|
||||
var detailGO = DetailBuilder.Make(go, sector, mod, detail);
|
||||
DetailBuilder.RegisterDetailInfo(detail, detailGO);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user