-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPart.cs
More file actions
211 lines (189 loc) · 6.86 KB
/
Part.cs
File metadata and controls
211 lines (189 loc) · 6.86 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using UnityEngine;
using System.Collections;
using System.Linq;
using System;
namespace BSFDTestbed
{
public class Part : MonoBehaviour
{
//Bolt related variables
public GameObject boltParent; // GameObject, Child of Part, Parent of ALL BOLTS.
public Bolt[] bolts; // Array of bolts, define in Unity inspector
public int tightness = 0; // Current part tightness, calculated by UpdatePartTightness()
public int MaxTightness; // Part will not fall off if tightness = MaxTightness
//part(self) related variables
public bool isFitted; // Self explanatory
public bool disableColliders = false;
public bool destroyRigidbody = false;
public Collider partTrigger; // Trigger of part, used for collision test between attachmentTrigger.
//part(thing you are attaching to) related variables
public GameObject attachmentPoint; // GameObject, parent of Part upon attachment.
public Collider attachmentTrigger; // Collider, Trigger, used for collision test between partTrigger.
//events
public delegate void AttachDelegate();
public event AttachDelegate OnAttach;
public delegate void DetachDelegate();
public event DetachDelegate OnDetach;
Rigidbody rb;
float mass;
CollisionDetectionMode collmode;
RigidbodyInterpolation interpolationmode;
// Use this for initialization
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
mass = rb.mass;
collmode = rb.collisionDetectionMode;
interpolationmode = rb.interpolation;
if (bolts.Length != 0 && boltParent != null)
{
StartCoroutine(UpdatePartTightness());
}
}
void FixedUpdate()
{
if (isFitted) PartAttached();
}
IEnumerator UpdatePartTightness()
{
while (true)
{
int _tightness = 0;
foreach (var b in bolts) _tightness += b.currentBoltStep;
tightness = _tightness;
yield return new WaitForSeconds(3f);
}
}
IEnumerator FixParent(Transform parent)
{
yield return new WaitForEndOfFrame();
while (transform.parent != parent)
{
transform.parent = parent;
transform.localPosition = Vector3.zero;
transform.localEulerAngles = Vector3.zero;
yield return new WaitForEndOfFrame();
}
}
void OnTriggerStay(Collider other)
{
if (other == attachmentTrigger && canAttach())
{
BSFDinteraction.GUIAssemble.Value = true;
if (Input.GetMouseButtonDown(0))
{
Attach(true);
BSFDinteraction.GUIAssemble.Value = false;
}
}
}
bool canAttach() { return transform.IsChildOf(BSFDinteraction.ItemPivot) && attachmentTrigger.transform.childCount == 0 && !isFitted; }
public void Attach(bool playAudio)
{
if (isFitted) return;
transform.parent = attachmentPoint.transform;
transform.localPosition = Vector3.zero;
transform.localEulerAngles = Vector3.zero;
StartCoroutine(FixParent(attachmentPoint.transform));
StartCoroutine(LateAttach(playAudio));
if (boltParent != null)
{
boltParent.SetActive(true);
}
OnAttach?.Invoke();
}
IEnumerator LateAttach(bool playAudio)
{
if (!destroyRigidbody)
{
while (!rb.isKinematic || rb.useGravity)
{
rb.isKinematic = true;
rb.useGravity = false;
if (disableColliders) { rb.detectCollisions = false; }
yield return new WaitForEndOfFrame();
}
}
else
{
Component.Destroy(rb);
}
if (playAudio) MasterAudio.PlaySound3DAtTransform("CarBuilding", partTrigger.transform, 1f, 1f, 0f, "assemble");
partTrigger.enabled = false;
attachmentTrigger.enabled = false;
gameObject.tag = "Untagged";
isFitted = true;
}
void PartAttached()
{
if (boltParent == null)
{
partTrigger.enabled = true;
}
else
{
if (tightness >= MaxTightness)
{
partTrigger.enabled = false;
}
else if (tightness <= 0)
{
partTrigger.enabled = true;
}
}
}
public void Detach()
{
if (!isFitted) return;
MasterAudio.PlaySound3DAtTransform("CarBuilding", partTrigger.transform, 1f, 1f, 0f, "disassemble");
gameObject.tag = "PART";
transform.parent = null;
if (!destroyRigidbody)
{
rb.isKinematic = false;
rb.useGravity = true;
rb.detectCollisions = true;
}
attachmentTrigger.enabled = true;
isFitted = false;
StartCoroutine(FixParent(null));
if (disableColliders && !destroyRigidbody) { rb.detectCollisions = true; }
StartCoroutine(LateDetach());
if (boltParent != null)
{
boltParent.SetActive(false);
}
OnDetach?.Invoke();
if (boltParent != null && bolts.Length != 0)
{
UntightenAllBolts();
}
}
IEnumerator LateDetach()
{
if (!destroyRigidbody)
{
while (rb.isKinematic || !rb.useGravity)
{
rb.isKinematic = false;
rb.useGravity = true;
if (disableColliders) { rb.detectCollisions = true; }
yield return new WaitForEndOfFrame();
}
}
else
{
rb = gameObject.AddComponent<Rigidbody>();
rb.mass = mass;
rb.collisionDetectionMode = collmode;
rb.interpolation = interpolationmode;
}
attachmentTrigger.enabled = true;
isFitted = false;
}
void UntightenAllBolts()
{
foreach (var b in bolts) b.SetBoltStep(0);
}
}
}