using UnityEngine; public class RealWaterSpeedManager : MonoBehaviour { [Tooltip("The simulation speed of all instances of RealWater in this scene.")] [Range(0f, 120f)] public float Speed = 60f; [Tooltip("The mouse/touch input manager for this scene (if using one).")] public RealWaterMouseInput MouseInputManager; [Tooltip("Set to true if using collision nodes [BETA]")] public bool FindCollisionNodes_BETA; private RealWaterInterface[] realWaterPlanes; private RealWaterCollider[] realWaterColliders; private RealWaterCollisionNode[] realWaterCollisionNodes; private int numberOfPlanes; private int numberOfColliders; private int numberOfCollisionNodes; private float speedStored; private void Start() { FindPlanes(); FindColliders(); if (FindCollisionNodes_BETA) { FindCollisionNodes(); } if (MouseInputManager != null) { MouseInputManager.InvokeRepeating("CheckInput", 0.001f, 1f / Speed); } speedStored = Speed; InvokeRepeating("SpeedChangeCheck", 0.0001f, 1f / 60f); } private void SpeedChangeCheck() { if (Speed == speedStored) { return; } for (int i = 0; i < numberOfPlanes; i++) { realWaterPlanes[i].SetSpeed(Speed); ((MonoBehaviour)realWaterPlanes[i]).CancelInvoke("MainLoop"); ((MonoBehaviour)realWaterPlanes[i]).CancelInvoke("AmbientMainLoopInteractive"); if (realWaterPlanes[i].GetVisibility() && Speed != 0f && !realWaterPlanes[i].GetAmbientMode() && !realWaterPlanes[i].GetPaused()) { ((MonoBehaviour)realWaterPlanes[i]).InvokeRepeating("MainLoop", 0.001f, 1f / Speed); } else if (realWaterPlanes[i].GetAmbientMode() && realWaterPlanes[i].GetInteractiveAmbientMode()) { ((MonoBehaviour)realWaterPlanes[i]).InvokeRepeating("AmbientMainLoopInteractive", 0.001f, 1f / Speed); } } if (MouseInputManager != null) { MouseInputManager.CancelInvoke("CheckInput"); } if (MouseInputManager != null && Speed != 0f) { MouseInputManager.InvokeRepeating("CheckInput", 0.001f, 1f / Speed); } for (int j = 0; j < numberOfColliders; j++) { realWaterColliders[j].CancelInvoke("CheckForCollisions"); realWaterColliders[j].Reset(); realWaterColliders[j].SetCollisionFrequency(Speed); if (realWaterColliders[j].GetVisibility() && Speed != 0f) { realWaterColliders[j].InvokeRepeating("CheckForCollisions", 0.001f, 1f / Speed); } } for (int k = 0; k < numberOfCollisionNodes; k++) { realWaterCollisionNodes[k].CancelInvoke("CheckForCollisions"); realWaterCollisionNodes[k].collisionCheckFrequency = Speed; realWaterCollisionNodes[k].InvokeRepeating("CheckForCollisions", 0.001f, 1f / Speed); } for (int l = 0; l < numberOfPlanes; l++) { if (realWaterPlanes[l].GetVisibility() && Speed != 0f) { if (!realWaterPlanes[l].GetAmbientMode() || (realWaterPlanes[l].GetAmbientMode() && realWaterPlanes[l].GetInteractiveAmbientMode())) { ((MonoBehaviour)realWaterPlanes[l]).CancelInvoke("SetobstructionTexture"); } if (realWaterPlanes[l].GetObstructionTextureSettings().ObstructionTexturesEnabled && realWaterPlanes[l].GetObstructionTextureSettings().SetRate == ObstructionTextureSettings.setRate.Half && (!realWaterPlanes[l].GetAmbientMode() || (realWaterPlanes[l].GetAmbientMode() && realWaterPlanes[l].GetInteractiveAmbientMode()))) { ((MonoBehaviour)realWaterPlanes[l]).InvokeRepeating("SetobstructionTexture", 0.003f, 1f / (Speed * 0.55f)); } if (realWaterPlanes[l].GetObstructionTextureSettings().ObstructionTexturesEnabled && realWaterPlanes[l].GetObstructionTextureSettings().SetRate == ObstructionTextureSettings.setRate.Full && (!realWaterPlanes[l].GetAmbientMode() || (realWaterPlanes[l].GetAmbientMode() && realWaterPlanes[l].GetInteractiveAmbientMode()))) { ((MonoBehaviour)realWaterPlanes[l]).InvokeRepeating("SetobstructionTexture", 0.003f, 1f / Speed); } ((MonoBehaviour)realWaterPlanes[l]).CancelInvoke("SetObstructions"); if (realWaterPlanes[l].GetObstructionSettings().ObstructionsEnabled && realWaterPlanes[l].GetObstructionSettings().SetRate == ObstructionSettings.setRate.Half && (!realWaterPlanes[l].GetAmbientMode() || (realWaterPlanes[l].GetAmbientMode() && realWaterPlanes[l].GetInteractiveAmbientMode()))) { ((MonoBehaviour)realWaterPlanes[l]).InvokeRepeating("SetObstructions", 0.003f, 1f / (Speed * 0.55f)); } if (realWaterPlanes[l].GetObstructionSettings().ObstructionsEnabled && realWaterPlanes[l].GetObstructionSettings().SetRate == ObstructionSettings.setRate.Full && (!realWaterPlanes[l].GetAmbientMode() || (realWaterPlanes[l].GetAmbientMode() && realWaterPlanes[l].GetInteractiveAmbientMode()))) { ((MonoBehaviour)realWaterPlanes[l]).InvokeRepeating("SetObstructions", 0.003f, 1f / Speed); } } } speedStored = Speed; } public void FindPlanes() { realWaterPlanes = InterfaceHelper.FindObjects(); numberOfPlanes = realWaterPlanes.Length; for (int i = 0; i < numberOfPlanes; i++) { ((MonoBehaviour)realWaterPlanes[i]).CancelInvoke("MainLoop"); realWaterPlanes[i].SetSpeed(Speed); if (realWaterPlanes[i].GetVisibility() && !realWaterPlanes[i].GetCollisionCulling() && !realWaterPlanes[i].GetAmbientMode()) { ((MonoBehaviour)realWaterPlanes[i]).InvokeRepeating("MainLoop", 0.001f, 1f / Speed); realWaterPlanes[i].SetPaused(val: false); } else if (realWaterPlanes[i].GetAmbientMode() && realWaterPlanes[i].GetInteractiveAmbientMode()) { ((MonoBehaviour)realWaterPlanes[i]).Invoke("InitializeAmbientMode", 0.0001f); ((MonoBehaviour)realWaterPlanes[i]).InvokeRepeating("AmbientMainLoopInteractive", 0.001f, 1f / Speed); realWaterPlanes[i].SetPaused(val: false); } realWaterPlanes[i].SetReady(val: true); } } public void FindColliders() { realWaterColliders = Object.FindObjectsOfType(typeof(RealWaterCollider)) as RealWaterCollider[]; numberOfColliders = realWaterColliders.Length; int num = 0; for (int i = 0; i < numberOfColliders; i++) { realWaterColliders[i].CancelInvoke("CheckForCollisions"); if (realWaterColliders[i].mainInput) { num++; } realWaterColliders[i].SetCollisionFrequency(Speed); if (realWaterColliders[i].GetVisibility()) { realWaterColliders[i].InvokeRepeating("CheckForCollisions", 0.001f, 1f / Speed); } } if (num > 1) { Debug.LogError("There is more than one Main Input water collider present in this scene. This may cause problems."); } } public void FindCollisionNodes() { realWaterCollisionNodes = Object.FindObjectsOfType(typeof(RealWaterCollisionNode)) as RealWaterCollisionNode[]; numberOfCollisionNodes = realWaterCollisionNodes.Length; for (int i = 0; i < numberOfCollisionNodes; i++) { realWaterCollisionNodes[i].CancelInvoke("CheckForCollisions"); realWaterCollisionNodes[i].collisionCheckFrequency = Speed; realWaterCollisionNodes[i].InvokeRepeating("CheckForCollisions", 0.001f, 1f / Speed); } } }