Refactor detail builder, fix parenting issue

This commit is contained in:
Nick 2022-08-21 15:54:54 -04:00
parent 0c7c54992a
commit e6412175e5
3 changed files with 72 additions and 66 deletions

View File

@ -151,7 +151,7 @@ namespace NewHorizons.Builder.Body
{ {
foreach (var detailInfo in body.Config.Props.proxyDetails) foreach (var detailInfo in body.Config.Props.proxyDetails)
{ {
DetailBuilder.Make(newProxy, null, body.Config, body.Mod, detailInfo); DetailBuilder.Make(newProxy, null, body.Mod, detailInfo);
} }
} }

View File

@ -20,29 +20,80 @@ namespace NewHorizons.Builder.Props
return detailInfoToCorrespondingSpawnedGameObject[detail]; 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) 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 else
{ {
var prefab = SearchUtilities.Find(detail.path); prefab = SearchUtilities.Find(detail.path);
if (prefab == null) Logger.LogError($"Couldn't find detail {detail.path}");
else detailGO = MakeDetail(go, sector, prefab, detail);
} }
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) if (detail.removeChildren != null)
{ {
var detailPath = detailGO.transform.GetPath(); var detailPath = prop.transform.GetPath();
var transforms = detailGO.GetComponentsInChildren<Transform>(true); var transforms = prop.GetComponentsInChildren<Transform>(true);
foreach (var childPath in detail.removeChildren) foreach (var childPath in detail.removeChildren)
{ {
// Multiple children can have the same path so we delete all that match // Multiple children can have the same path so we delete all that match
@ -62,12 +113,12 @@ namespace NewHorizons.Builder.Props
if (detail.removeComponents) if (detail.removeComponents)
{ {
// Just swap all the children to a new game object // Just swap all the children to a new game object
var newDetailGO = new GameObject(detailGO.name); var newDetailGO = new GameObject(prop.name);
newDetailGO.transform.position = detailGO.transform.position; newDetailGO.transform.position = prop.transform.position;
newDetailGO.transform.parent = detailGO.transform.parent; newDetailGO.transform.parent = prop.transform.parent;
// Can't modify parents while looping through children bc idk // Can't modify parents while looping through children bc idk
var children = new List<Transform>(); var children = new List<Transform>();
foreach (Transform child in detailGO.transform) foreach (Transform child in prop.transform)
{ {
children.Add(child); children.Add(child);
} }
@ -75,13 +126,13 @@ namespace NewHorizons.Builder.Props
{ {
child.parent = newDetailGO.transform; child.parent = newDetailGO.transform;
} }
GameObject.Destroy(detailGO); GameObject.Destroy(prop);
detailGO = newDetailGO; prop = newDetailGO;
} }
if (detail.rename != null) if (detail.rename != null)
{ {
detailGO.name = detail.rename; prop.name = detail.rename;
} }
if (!string.IsNullOrEmpty(detail.parentPath)) if (!string.IsNullOrEmpty(detail.parentPath))
@ -89,56 +140,10 @@ namespace NewHorizons.Builder.Props
var newParent = go.transform.Find(detail.parentPath); var newParent = go.transform.Find(detail.parentPath);
if (newParent != null) 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; return prop;
} }

View File

@ -29,7 +29,8 @@ namespace NewHorizons.Builder.Props
{ {
try try
{ {
DetailBuilder.Make(go, sector, config, mod, detail); var detailGO = DetailBuilder.Make(go, sector, mod, detail);
DetailBuilder.RegisterDetailInfo(detail, detailGO);
} }
catch (Exception ex) catch (Exception ex)
{ {