using UnityEngine; using System.Collections; public class AddressManager { private StateManager stateManager; private int totalObjects; public bool machineIdCoroutineActive; public bool blockIdCoroutineActive; //! This class assigns a unique ID to every block in the world. public AddressManager(StateManager stateManager) { this.stateManager = stateManager; } public IEnumerator MachineIdCoroutine() { machineIdCoroutineActive = true; int idCount = 0; int addressingInterval = 0; string objectName = ""; GameObject[] machines = GameObject.FindGameObjectsWithTag("Machine"); foreach (GameObject go in machines) { if (go != null) { if (go.transform.parent != stateManager.builtObjects.transform) { if (go.GetComponent() != null) { objectName = stateManager.worldName + "Auger"; go.GetComponent().ID = objectName + idCount; go.GetComponent().address = idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "ElectricLight"; go.GetComponent().ID = objectName + idCount; go.GetComponent().address = idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "DarkMatterCollector"; go.GetComponent().ID = objectName + idCount; go.GetComponent().address = idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "DarkMatterConduit"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "RailCart"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "RailCartHub"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "UniversalConduit"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "HeatExchanger"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "Retriever"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "AutoCrafter"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "Smelter"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "Turret"; go.GetComponent().address = idCount; go.GetComponent().ID = (objectName + idCount); } if (go.GetComponent() != null) { objectName = stateManager.worldName + "MissileTurret"; go.GetComponent().address = idCount; go.GetComponent().ID = (objectName + idCount); } if (go.GetComponent() != null) { if (go.GetComponent().type == "Solar Panel") { objectName = stateManager.worldName + "SolarPanel"; } else if (go.GetComponent().type == "Generator") { objectName = stateManager.worldName + "Generator"; } else if (go.GetComponent().type == "Reactor Turbine") { objectName = stateManager.worldName + "ReactorTurbine"; } go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "NuclearReactor"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "PowerConduit"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "AlloySmelter"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "Press"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "Extruder"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "GearCutter"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { objectName = stateManager.worldName + "UniversalExtractor"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (stateManager.IsStorageContainer(go)) { objectName = stateManager.worldName + "StorageContainer"; go.GetComponent().ID = objectName + idCount; go.GetComponent().address = idCount; go.GetComponent().SaveData(); } if (go.GetComponent() != null) { objectName = stateManager.worldName + "StorageComputer"; go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { if (go.GetComponent().type == "Door") { objectName = stateManager.worldName + "Door"; } else if (go.GetComponent().type == "Quantum Hatchway") { objectName = stateManager.worldName + "QuantumHatchway"; } go.GetComponent().address = idCount; go.GetComponent().ID = objectName + idCount; } if (go.GetComponent() != null) { ModMachine machine = go.GetComponent(); objectName = stateManager.worldName + "ModMachine"; go.GetComponent().ID = objectName + idCount; go.GetComponent().address = idCount; } idCount++; addressingInterval++; if (addressingInterval >= 10) { yield return null; addressingInterval = 0; } } } } machineIdCoroutineActive = false; } public IEnumerator BlockIdCoroutine() { blockIdCoroutineActive = true; int idCount = 0; int addressingInterval = 0; string objectName = ""; Transform[] blocks = stateManager.builtObjects.GetComponentsInChildren(true); foreach (Transform T in blocks) { if (T != null) { if (T.gameObject.GetComponent() != null) { if (T.gameObject.name.Equals("IronRamp(Clone)")) { objectName = stateManager.worldName + "IronRamp"; } else { objectName = stateManager.worldName + "IronBlock"; } T.gameObject.GetComponent().ID = objectName + idCount; T.gameObject.GetComponent().address = idCount; idCount++; } if (T.gameObject.GetComponent() != null) { if (T.gameObject.name.Equals("SteelRamp(Clone)")) { objectName = stateManager.worldName + "SteelRamp"; } else { objectName = stateManager.worldName + "Steel"; } T.gameObject.GetComponent().ID = objectName + idCount; T.gameObject.GetComponent().address = idCount; idCount++; } if (T.gameObject.GetComponent() != null) { objectName = stateManager.worldName + "Brick"; T.gameObject.GetComponent().ID = objectName + idCount; T.gameObject.GetComponent().address = idCount; idCount++; } if (T.gameObject.GetComponent() != null) { objectName = stateManager.worldName + "Glass"; T.gameObject.GetComponent().ID = objectName + idCount; T.gameObject.GetComponent().address = idCount; idCount++; } if (T.gameObject.GetComponent() != null) { objectName = stateManager.worldName + "ModBlock"; T.gameObject.GetComponent().ID = objectName + idCount; T.gameObject.GetComponent().address = idCount; idCount++; } addressingInterval++; if (addressingInterval >= 10) { yield return null; addressingInterval = 0; } } } blockIdCoroutineActive = false; } }