diff --git a/NewHorizons/Builder/Props/QuantumBuilder.cs b/NewHorizons/Builder/Props/QuantumBuilder.cs index b3c16e0e..5bdf571a 100644 --- a/NewHorizons/Builder/Props/QuantumBuilder.cs +++ b/NewHorizons/Builder/Props/QuantumBuilder.cs @@ -52,10 +52,12 @@ namespace NewHorizons.Builder.Props foreach(var prop in propsInGroup) { + prop.SetActive(false); var quantumObject = prop.AddComponent(); quantumObject._socketRoot = groupRoot; quantumObject._socketList = sockets; - + prop.SetActive(true); + if (prop.GetComponentInChildren() != null) continue; var boxBounds = GetBoundsOfSelfAndChildMeshes(prop); @@ -109,22 +111,28 @@ namespace NewHorizons.Builder.Props empty.AddComponent(); } + groupRoot.SetActive(false); var multiState = groupRoot.AddComponent(); multiState._loop = quantumGroup.loop; multiState._sequential = quantumGroup.sequential; multiState._states = states.ToArray(); + multiState._prerequisiteObjects = new MultiStateQuantumObject[0]; // TODO: support this + multiState._initialState = 0; + groupRoot.SetActive(true); } public static void MakeShuffleGroup(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, PropModule.QuantumGroupInfo quantumGroup, GameObject[] propsInGroup) { //var averagePosition = propsInGroup.Aggregate(Vector3.zero, (avg, prop) => avg + prop.transform.position) / propsInGroup.Count(); GameObject shuffleParent = new GameObject("Quantum Shuffle - " + quantumGroup.id); + shuffleParent.SetActive(false); shuffleParent.transform.parent = sector.transform; shuffleParent.transform.localPosition = Vector3.zero; propsInGroup.ToList().ForEach(p => p.transform.parent = shuffleParent.transform); var shuffle = shuffleParent.AddComponent(); shuffle._shuffledObjects = propsInGroup.Select(p => p.transform).ToArray(); + NewHorizons.Utility.Logger.Log($"Shuffled objects {shuffle._shuffledObjects}, propsInGroup count {propsInGroup.Count()}"); shuffle.Awake(); // this doesn't get called on its own for some reason var boxBounds = GetBoundsOfSelfAndChildMeshes(shuffleParent); // TODO: add a box shape to each prop instead of to the parent @@ -133,6 +141,7 @@ namespace NewHorizons.Builder.Props boxShape.extents = boxBounds.size; shuffleParent.AddComponent(); + shuffleParent.SetActive(true); } public static Bounds GetBoundsOfSelfAndChildMeshes(GameObject g) diff --git a/NewHorizons/Patches/TemporaryDebugOnlyPatchesDeleteBeforePR.cs b/NewHorizons/Patches/TemporaryDebugOnlyPatchesDeleteBeforePR.cs index 564aa3fe..cff0c955 100644 --- a/NewHorizons/Patches/TemporaryDebugOnlyPatchesDeleteBeforePR.cs +++ b/NewHorizons/Patches/TemporaryDebugOnlyPatchesDeleteBeforePR.cs @@ -14,8 +14,8 @@ namespace NewHorizons.Patches // This is some dark magic // this creates a method called base_DropItem that basically just calls OWItem.PickUpItem whenever it (VisionTorchItemPatches.base_PickUpItem) is called [HarmonyReversePatch] - [HarmonyPatch(typeof(QuantumObject), nameof(QuantumObject.Awake))] - private static void base_Awake(QuantumObject __instance) { } + [HarmonyPatch(typeof(VisibilityObject), nameof(VisibilityObject.Awake))] + private static void base_Awake(VisibilityObject __instance) { } // Make the vision torch droppable. In the base game you can only drop it if you're in the dream world. @@ -33,6 +33,84 @@ namespace NewHorizons.Patches return false; } + + + [HarmonyPrefix] + [HarmonyPatch(typeof(MultiStateQuantumObject), nameof(MultiStateQuantumObject.ChangeQuantumState))] + public static bool MultiStateQuantumObject_ChangeQuantumState(MultiStateQuantumObject __instance, bool skipInstantVisibilityCheck) + { + for (int i = 0; i < __instance._prerequisiteObjects.Length; i++) + { + if (!__instance._prerequisiteObjects[i].HasCollapsed()) + { + return false; + } + } + int stateIndex = __instance._stateIndex; + if (__instance._stateIndex == -1 && __instance._initialState != -1) + { + __instance._stateIndex = __instance._initialState; + } + else if (__instance._sequential) + { + __instance._stateIndex = (__instance._reverse ? (__instance._stateIndex - 1) : (__instance._stateIndex + 1)); + if (__instance._loop) + { + if (__instance._stateIndex < 0) + { + __instance._stateIndex = __instance._states.Length - 1; + } + else if (__instance._stateIndex > __instance._states.Length - 1) + { + __instance._stateIndex = 0; + } + } + else + { + __instance._stateIndex = Mathf.Clamp(__instance._stateIndex, 0, __instance._states.Length - 1); + } + } + else + { + int num = 0; + for (int j = 0; j < __instance._states.Length; j++) + { + if (j != stateIndex) + { + __instance._probabilities[j] = __instance._states[j].GetProbability(); + num += __instance._probabilities[j]; + } + } + int num2 = UnityEngine.Random.Range(0, num); + int num3 = 0; + int num4 = 0; + for (int k = 0; k < __instance._states.Length; k++) + { + if (k != stateIndex) + { + num3 = num4; + num4 += __instance._probabilities[k]; + if (__instance._probabilities[k] > 0 && num2 >= num3 && num2 < num4) + { + __instance._stateIndex = k; + break; + } + } + } + } + if (stateIndex != __instance._stateIndex && stateIndex >= 0 && stateIndex < __instance._states.Length) + { + __instance._states[stateIndex].SetVisible(visible: false); + } + NewHorizons.Utility.Logger.Log($"states length {__instance._states.Length} index {__instance._stateIndex}"); + + __instance._states[__instance._stateIndex].SetVisible(visible: true); + if (__instance._sequential && !__instance._loop && __instance._stateIndex == __instance._states.Length - 1) + { + __instance.SetActivation(active: false); + } + return true; + } } }