using UnityEngine; public class Press : MonoBehaviour { public float amount; public int speed = 1; public int power; public int heat; public bool hasHeatExchanger; public int cooling; public string inputType; public string outputType; public string ID = "unassigned"; public string inputID; public string outputID; public bool powerON; public string creationMethod = "built"; public GameObject inputObject; public GameObject outputObject; public GameObject powerObject; public ConduitItem conduitItem; public Material lineMat; private LineRenderer connectionLine; public PowerReceiver powerReceiver; private float updateTick; public int address; private int machineTimer; public int connectionAttempts; public bool connectionFailed; private GameObject builtObjects; // Called by unity engine on start up to initialize variables public void Start() { powerReceiver = gameObject.AddComponent(); connectionLine = gameObject.AddComponent(); conduitItem = GetComponentInChildren(true); connectionLine.startWidth = 0.2f; connectionLine.endWidth = 0.2f; connectionLine.material = lineMat; connectionLine.loop = true; connectionLine.enabled = false; builtObjects = GameObject.Find("Built_Objects"); } // Called once per frame by unity engine public void Update() { updateTick += 1 * Time.deltaTime; if (updateTick > 0.5f + (address * 0.001f)) { GetComponent().UpdatePhysics(); UpdatePowerReceiver(); updateTick = 0; if (speed > power && power != 0) { speed = power; } if (speed > 1) { heat = speed - 1 - cooling; } else { heat = 0; } if (heat < 0) { heat = 0; } if (inputObject == null || outputObject == null) { connectionAttempts += 1; if (creationMethod.Equals("spawned")) { if (connectionAttempts >= 30) { connectionAttempts = 0; connectionFailed = true; } } else { if (connectionAttempts >= 120) { connectionAttempts = 0; connectionFailed = true; } } if (connectionFailed == false) { ConnectToOutput(); } } if (inputObject != null) { if (inputObject.GetComponent() != null) { if (amount < 1) { SetOutputType(); } if (inputObject.GetComponent().conduitItem.active == false) { conduitItem.active = false; GetComponent().enabled = false; } } } else { conduitItem.active = false; GetComponent().enabled = false; } HandleOutput(); if (inputObject != null && outputObject != null) { connectionAttempts = 0; } } } // The object exists, is active and is not a standard building block private bool IsValidObject(GameObject obj) { if (obj != null) { return obj.transform.parent != builtObjects.transform && obj.activeInHierarchy; } return false; } // The object is a potential output connection private bool IsValidOutputObject(GameObject obj) { return outputObject == null && inputObject != null && obj != inputObject && obj != gameObject; } // Finds a universal conduit to use as an output private void ConnectToOutput() { GameObject[] allObjects = GameObject.FindGameObjectsWithTag("Built"); foreach (GameObject obj in allObjects) { if (IsValidObject(obj)) { if (obj.GetComponent() != null) { float distance = Vector3.Distance(transform.position, obj.transform.position); if (IsValidOutputObject(obj) && distance < 20) { if (obj.GetComponent().inputObject == null) { if (creationMethod.Equals("spawned") && obj.GetComponent().ID.Equals(outputID)) { outputObject = obj; obj.GetComponent().type = outputType; obj.GetComponent().inputObject = this.gameObject; connectionLine.SetPosition(0, transform.position); connectionLine.SetPosition(1, obj.transform.position); connectionLine.enabled = true; creationMethod = "built"; } else if (creationMethod.Equals("built")) { outputObject = obj; obj.GetComponent().type = outputType; obj.GetComponent().inputObject = this.gameObject; connectionLine.SetPosition(0, transform.position); connectionLine.SetPosition(1, obj.transform.position); connectionLine.enabled = true; } } } } } } } // Sets the appropriate output item according to the input private void SetOutputType() { string incoming = inputObject.GetComponent().type; if (incoming != "" && incoming != "nothing") { inputType = inputObject.GetComponent().type; } if (inputType.Equals("Copper Ingot")) { outputType = "Copper Plate"; } if (inputType.Equals("Iron Ingot")) { outputType = "Iron Plate"; } if (inputType.Equals("Tin Ingot")) { outputType = "Tin Plate"; } if (inputType.Equals("Bronze Ingot")) { outputType = "Bronze Plate"; } if (inputType.Equals("Steel Ingot")) { outputType = "Steel Plate"; } if (inputType.Equals("Aluminum Ingot")) { outputType = "Aluminum Plate"; } if (inputType.Equals("Regolith")) { outputType = "Brick"; } } // If everything is working, items are moved to the output conduit; sounds and other effects are enabled. private void HandleOutput() { if (outputObject != null) { if (outputObject.GetComponent() != null) { outputObject.GetComponent().inputID = ID; outputObject.GetComponent().type = outputType; outputObject.GetComponent().speed = speed; outputID = outputObject.GetComponent().ID; if (amount >= speed) { if (outputType.Equals(outputObject.GetComponent().type)) { if (powerON == true && connectionFailed == false && inputObject != null && speed > 0) { conduitItem.active = true; GetComponent().enabled = true; machineTimer += 1; if (machineTimer > 5 - (address * 0.01f)) { outputObject.GetComponent().amount += speed - heat; amount -= speed - heat; machineTimer = 0; } } else { conduitItem.active = false; GetComponent().enabled = false; machineTimer = 0; } } } } } else { connectionLine.enabled = false; if (connectionFailed == true) { if (creationMethod.Equals("spawned")) { creationMethod = "built"; } } } } // Gets power values from power receiver private void UpdatePowerReceiver() { powerReceiver.ID = ID; power = powerReceiver.power; powerON = powerReceiver.powerON; powerObject = powerReceiver.powerObject; } }