Quantum-Engineering/RailCart.cs

103 lines
4.0 KiB
C#
Raw Normal View History

using UnityEngine;
public class RailCart : MonoBehaviour
{
public string ID = "unassigned";
2020-08-27 05:06:16 -04:00
public string creationMethod = "built";
public GameObject target;
private Vector3 targetPosition;
2020-09-19 20:29:43 -05:00
private StateManager stateManager;
public int address;
public string targetID;
private bool loadedTarget;
private float stopTimer;
private GameObject builtObjects;
2020-09-16 21:40:25 -05:00
//! Called by unity engine on start up to initialize variables.
2020-09-03 18:26:56 -04:00
public void Start()
{
2021-05-16 14:12:13 -05:00
builtObjects = GameObject.Find("BuiltObjects");
2020-09-19 20:29:43 -05:00
stateManager = FindObjectOfType<StateManager>();
}
//! Called once per frame by unity engine.
public void Update()
{
2020-09-19 20:29:43 -05:00
if (!stateManager.Busy())
{
2020-09-19 20:29:43 -05:00
GetComponent<InventoryManager>().ID = ID;
if (creationMethod.Equals("spawned"))
{
2020-09-19 20:29:43 -05:00
if (target == null && loadedTarget == false && !targetID.Equals(""))
{
2020-09-19 20:29:43 -05:00
RailCartHub[] allHubs = FindObjectsOfType<RailCartHub>();
foreach (RailCartHub hub in allHubs)
{
2020-09-19 20:29:43 -05:00
if (hub.ID.Equals(targetID))
{
target = hub.gameObject;
loadedTarget = true;
}
}
}
}
2020-09-19 20:29:43 -05:00
if (target != null)
{
2020-09-19 20:29:43 -05:00
targetPosition = target.transform.position;
transform.LookAt(targetPosition);
if (Vector3.Distance(transform.position, targetPosition) < 1)
{
2020-09-19 20:29:43 -05:00
if (target.GetComponent<RailCartHub>() != null)
{
2020-09-19 20:29:43 -05:00
targetID = target.GetComponent<RailCartHub>().ID;
if (target.GetComponent<RailCartHub>().stop == true)
{
2020-09-19 20:29:43 -05:00
if (GetComponent<AudioSource>().enabled == true)
{
GetComponent<AudioSource>().enabled = false;
}
if (stopTimer <= target.GetComponent<RailCartHub>().stopTime)
{
stopTimer += 1 * Time.deltaTime;
}
else if (target.GetComponent<RailCartHub>().outputObject != null)
{
stopTimer = 0;
target = target.GetComponent<RailCartHub>().outputObject;
}
}
else if (target.GetComponent<RailCartHub>().outputObject != null)
{
stopTimer = 0;
target = target.GetComponent<RailCartHub>().outputObject;
}
}
}
2020-09-19 20:29:43 -05:00
else
{
2020-09-19 20:29:43 -05:00
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit crashHit, 5))
{
2020-09-19 20:29:43 -05:00
if (crashHit.collider != null)
{
2020-09-19 20:29:43 -05:00
if (crashHit.collider.gameObject != null)
{
2020-09-19 20:29:43 -05:00
if (crashHit.collider.gameObject.GetComponent<RailCartHub>() != null || crashHit.collider.gameObject.tag.Equals("Landscape"))
{
transform.position += 8 * transform.forward * Time.deltaTime;
}
}
}
}
2020-09-19 20:29:43 -05:00
else
{
transform.position += 8 * transform.forward * Time.deltaTime;
}
if (GetComponent<AudioSource>().enabled == false)
{
GetComponent<AudioSource>().enabled = true;
}
}
}
}
}
}