diff --git a/AirLock.cs b/AirLock.cs index d50dddd..7a03734 100644 --- a/AirLock.cs +++ b/AirLock.cs @@ -1,8 +1,7 @@ using UnityEngine; -public class AirLock : MonoBehaviour +public class AirLock : Machine { - private float updateTick; private StateManager stateManager; public string ID = "unassigned"; public int address; @@ -22,20 +21,9 @@ public class AirLock : MonoBehaviour } //! Called once per frame by unity engine. - public void Update() + public override void UpdateMachine() { - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - - GetComponent().UpdatePhysics(); - updateTick = 0; - } + GetComponent().UpdatePhysics(); } //! Toggle the open or closed state of the hatchway. diff --git a/Block.cs b/Block.cs new file mode 100644 index 0000000..a6aaef3 --- /dev/null +++ b/Block.cs @@ -0,0 +1,9 @@ +using UnityEngine; + +public class Block : MonoBehaviour +{ + public virtual void UpdateBlock() + { + + } +} \ No newline at end of file diff --git a/BlockManager.cs b/BlockManager.cs new file mode 100644 index 0000000..e09cf1f --- /dev/null +++ b/BlockManager.cs @@ -0,0 +1,29 @@ +using System.Collections; +using UnityEngine; + +public class BlockManager : MonoBehaviour +{ + private bool busy; + private Coroutine blockUpdateCoroutine; + + //! Called once per frame by unity engine. + public void Update() + { + if (busy == false && GetComponent().worldLoaded == true) + { + blockUpdateCoroutine = StartCoroutine(BlockUpdateCoroutine()); + } + } + + private IEnumerator BlockUpdateCoroutine() + { + busy = true; + Block[] blocks = FindObjectsOfType(); + foreach (Block block in blocks) + { + block.UpdateBlock(); + yield return null; + } + busy = false; + } +} \ No newline at end of file diff --git a/Brick.cs b/Brick.cs index 36de72e..cb7b44f 100644 --- a/Brick.cs +++ b/Brick.cs @@ -6,7 +6,6 @@ public class Brick : MonoBehaviour public string ID = "unassigned"; public string creationMethod; public int address; - private float updateTick; private StateManager stateManager; //! Called by unity engine on start up to initialize variables. @@ -18,20 +17,9 @@ public class Brick : MonoBehaviour //! Called once per frame by unity engine. public void Update() { - if (ID == "unassigned") + if (ID == "unassigned" || stateManager.Busy()) return; - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - - GetComponent().UpdatePhysics(); - updateTick = 0; - } + GetComponent().UpdatePhysics(); } } diff --git a/Glass.cs b/Glass.cs index 2f22c31..50bce1a 100644 --- a/Glass.cs +++ b/Glass.cs @@ -1,12 +1,11 @@ using UnityEngine; //! This class is attached to all glass block prefabs. -public class Glass : MonoBehaviour +public class Glass : Block { public string ID = "unassigned"; public string creationMethod; public int address; - private float updateTick; private StateManager stateManager; //! Called by unity engine on start up to initialize variables. @@ -16,22 +15,11 @@ public class Glass : MonoBehaviour } //! Called once per frame by unity engine. - public void Update() + public override void UpdateBlock() { - if (ID == "unassigned") + if (ID == "unassigned" || stateManager.Busy()) return; - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - - GetComponent().UpdatePhysics(); - updateTick = 0; - } + GetComponent().UpdatePhysics(); } } diff --git a/IronBlock.cs b/IronBlock.cs index 2782429..3c6c15f 100644 --- a/IronBlock.cs +++ b/IronBlock.cs @@ -1,12 +1,11 @@ using UnityEngine; //! This class is attached to all iron block prefabs. -public class IronBlock : MonoBehaviour +public class IronBlock : Block { public string ID = "unassigned"; public string creationMethod; public int address; - private float updateTick; private StateManager stateManager; //! Called by unity engine on start up to initialize variables. @@ -16,22 +15,11 @@ public class IronBlock : MonoBehaviour } //! Called once per frame by unity engine. - public void Update() + public override void UpdateBlock() { - if (ID == "unassigned") + if (ID == "unassigned" || stateManager.Busy()) return; - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - - GetComponent().UpdatePhysics(); - updateTick = 0; - } + GetComponent().UpdatePhysics(); } } diff --git a/MachineManager.cs b/MachineManager.cs index 349d837..0f35c75 100644 --- a/MachineManager.cs +++ b/MachineManager.cs @@ -26,5 +26,4 @@ public class MachineManager : MonoBehaviour } busy = false; } -} - +} \ No newline at end of file diff --git a/ModBlock.cs b/ModBlock.cs index b4a832c..5fdb7cc 100644 --- a/ModBlock.cs +++ b/ModBlock.cs @@ -1,6 +1,6 @@ using UnityEngine; -public class ModBlock : MonoBehaviour +public class ModBlock : Block { private Material material; private StateManager stateManager; @@ -8,7 +8,6 @@ public class ModBlock : MonoBehaviour private GameManager gameManager; public GameObject glassBreak; private bool init; - private float updateTick; public int address; public string ID = "unassigned"; public string blockName; @@ -23,7 +22,7 @@ public class ModBlock : MonoBehaviour } //! Called once per frame by unity engine. - public void Update() + public override void UpdateBlock() { if (!stateManager.Busy() && init == false) { @@ -42,16 +41,7 @@ public class ModBlock : MonoBehaviour if (ID != "unassigned") { - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - GetComponent().UpdatePhysics(); - } + GetComponent().UpdatePhysics(); } } } \ No newline at end of file diff --git a/Steel.cs b/Steel.cs index bda61f0..b1459b8 100644 --- a/Steel.cs +++ b/Steel.cs @@ -1,13 +1,12 @@ using UnityEngine; //! This class is attached to all steel block prefabs. -public class Steel : MonoBehaviour +public class Steel : Block { public string ID = "unassigned"; private StateManager stateManager; public string creationMethod; public int address; - private float updateTick; //! Called by unity engine on start up to initialize variables. public void Start() @@ -16,22 +15,11 @@ public class Steel : MonoBehaviour } //! Called once per frame by unity engine. - public void Update() + public override void UpdateBlock() { - if (ID == "unassigned") + if (ID == "unassigned" || stateManager.Busy()) return; - updateTick += 1 * Time.deltaTime; - if (updateTick > 0.5f + (address * 0.001f)) - { - if (stateManager.Busy()) - { - updateTick = 0; - return; - } - - GetComponent().UpdatePhysics(); - updateTick = 0; - } + GetComponent().UpdatePhysics(); } }