-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRailCart.cs
103 lines (100 loc) · 4.01 KB
/
RailCart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using UnityEngine;
public class RailCart : MonoBehaviour
{
public string ID = "unassigned";
public string creationMethod = "built";
public GameObject target;
private Vector3 targetPosition;
public Vector3 startPosition;
private StateManager stateManager;
public string targetID;
private bool loadedTarget;
private float stopTimer;
private GameObject builtObjects;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
builtObjects = GameObject.Find("BuiltObjects");
stateManager = FindObjectOfType<StateManager>();
}
//! Called once per frame by unity engine.
public void Update()
{
if (!stateManager.Busy())
{
GetComponent<InventoryManager>().ID = ID;
if (creationMethod.Equals("spawned"))
{
if (target == null && loadedTarget == false && !targetID.Equals(""))
{
RailCartHub[] allHubs = FindObjectsOfType<RailCartHub>();
foreach (RailCartHub hub in allHubs)
{
if (hub.ID.Equals(targetID))
{
target = hub.gameObject;
loadedTarget = true;
}
}
}
}
if (target != null)
{
targetPosition = target.transform.position;
transform.LookAt(targetPosition);
if (Vector3.Distance(transform.position, targetPosition) < 1)
{
if (target.GetComponent<RailCartHub>() != null)
{
targetID = target.GetComponent<RailCartHub>().ID;
if (target.GetComponent<RailCartHub>().stop == true)
{
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;
}
}
}
else
{
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit crashHit, 5))
{
if (crashHit.collider != null)
{
if (crashHit.collider.gameObject != null)
{
if (crashHit.collider.gameObject.GetComponent<RailCartHub>() != null || crashHit.collider.gameObject.tag.Equals("Landscape"))
{
transform.position += 8 * transform.forward * Time.deltaTime;
}
}
}
}
else
{
transform.position += 8 * transform.forward * Time.deltaTime;
}
if (GetComponent<AudioSource>().enabled == false)
{
GetComponent<AudioSource>().enabled = true;
}
}
}
}
}
}