-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItemThermalDetonator.cs
315 lines (279 loc) · 14.1 KB
/
ItemThermalDetonator.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using UnityEngine;
using UnityEngine.AI;
using ThunderRoad;
using System.Linq;
using System.Collections.Generic;
using System;
using ThunderRoad.Skill.SpellPower;
namespace TOR {
public class ItemThermalDetonator : ThunderBehaviour {
public override ManagedLoops EnabledManagedLoops => ManagedLoops.Update;
protected Item item;
protected ItemModuleThermalDetonator module;
Renderer renderer;
Animator animator;
ParticleSystem particles;
AudioSource armedSound;
AudioSource idleSound;
AudioSource beepSound1;
AudioSource beepSound2;
AudioSource beepSound3;
AudioSource explosionSound;
AudioSource explosionSound2;
NavMeshObstacle obstacle;
NoiseManager.Noise armedNoise;
NoiseManager.Noise idleNoise;
float primaryControlHoldTime;
float secondaryControlHoldTime;
public bool isOpen;
public bool isArmed;
public bool armedByPlayer;
SpellTelekinesis telekinesis;
float detonateTime;
float beepTime;
bool[] lastBeep = { false, false, false };
MaterialInstance _materialInstance;
public MaterialInstance materialInstance {
get {
if (_materialInstance == null) {
renderer.gameObject.TryGetOrAddComponent(out MaterialInstance mi);
_materialInstance = mi;
}
return _materialInstance;
}
}
static readonly Dictionary<RagdollPart.Type, float> validParts = new Dictionary<RagdollPart.Type, float> {
{ RagdollPart.Type.Head, 0.9f },
{ RagdollPart.Type.LeftHand, 0.6f },
{ RagdollPart.Type.RightHand, 0.6f },
{ RagdollPart.Type.LeftArm, 0.7f },
{ RagdollPart.Type.RightArm, 0.8f },
{ RagdollPart.Type.LeftFoot, 0.6f },
{ RagdollPart.Type.RightFoot, 0.6f },
{ RagdollPart.Type.LeftLeg, 0.8f },
{ RagdollPart.Type.RightLeg, 0.8f }
};
protected void Awake() {
item = GetComponent<Item>();
module = item.data.GetModule<ItemModuleThermalDetonator>();
item.OnHeldActionEvent += OnHeldAction;
item.OnGrabEvent += OnGrabEvent;
item.OnUngrabEvent += OnUngrabEvent;
item.OnTelekinesisGrabEvent += OnTelekinesisGrabEvent;
item.OnTelekinesisReleaseEvent += OnTelekinesisReleaseEvent;
animator = item.GetCustomReference("Animator").GetComponent<Animator>();
armedSound = item.GetCustomReference("ArmedSound").GetComponent<AudioSource>();
idleSound = item.GetCustomReference("IdleSound").GetComponent<AudioSource>();
beepSound1 = item.GetCustomReference("BeepSound1").GetComponent<AudioSource>();
beepSound2 = item.GetCustomReference("BeepSound2").GetComponent<AudioSource>();
beepSound3 = item.GetCustomReference("BeepSound3").GetComponent<AudioSource>();
explosionSound = item.GetCustomReference("ExplosionSound").GetComponent<AudioSource>();
explosionSound2 = item.GetCustomReference("ExplosionSound2").GetComponent<AudioSource>();
particles = item.GetCustomReference("Explosion").GetComponent<ParticleSystem>();
renderer = item.GetCustomReference("Mesh").GetComponent<MeshRenderer>();
obstacle = GetComponent<NavMeshObstacle>();
obstacle.radius = GlobalSettings.ThermalDetonatorRange * module.radius;
item.OnCullEvent += OnCullEvent;
}
private void OnCullEvent(bool culled) {
if (culled && isArmed) gameObject.SetActive(true);
}
public void OnGrabEvent(Handle handle, RagdollHand interactor) {
if (!interactor.playerHand) {
if (!isOpen) ToggleSlider(interactor);
if (!isArmed) Arm(interactor);
}
if (isArmed && ((armedByPlayer && interactor.playerHand) || (!armedByPlayer && !interactor.playerHand))) detonateTime = 0;
}
public void OnUngrabEvent(Handle handle, RagdollHand interactor, bool throwing) {
if (!interactor.playerHand && isOpen) {
if (throwing) {
if (!isArmed) Arm(interactor);
} else {
if (interactor.creature && !interactor.creature.isKilled) ToggleSlider(interactor);
}
}
if (isArmed) {
detonateTime = module.detonateTime;
if (item.currentArea != null) {
item.currentArea.SpawnedArea.UnRegisterItem(item);
item.currentArea = null;
}
}
}
public void OnTelekinesisReleaseEvent(Handle handle, SpellTelekinesis teleGrabber, bool tryThrow, bool isGrabbing) {
telekinesis = null;
}
public void OnTelekinesisGrabEvent(Handle handle, SpellTelekinesis teleGrabber) {
telekinesis = teleGrabber;
}
public void ExecuteAction(string action, RagdollHand interactor = null) {
if (action == "arm") {
Arm(interactor);
} else if (action == "toggleSlider") {
ToggleSlider(interactor);
}
}
public void OnHeldAction(RagdollHand interactor, Handle handle, Interactable.Action action) {
// If primary hold action available
if (!string.IsNullOrEmpty(module.gripPrimaryActionHold)) {
// start primary control timer
if (action == Interactable.Action.UseStart) {
primaryControlHoldTime = GlobalSettings.ControlsHoldDuration;
} else if (action == Interactable.Action.UseStop) {
// if not held for long run standard action
if (primaryControlHoldTime > 0 && primaryControlHoldTime > (primaryControlHoldTime / 2)) {
ExecuteAction(module.gripPrimaryAction, interactor);
}
primaryControlHoldTime = 0;
}
} else if (action == Interactable.Action.UseStart) ExecuteAction(module.gripPrimaryAction, interactor);
// If secondary hold action available
if (!string.IsNullOrEmpty(module.gripSecondaryActionHold)) {
// start secondary control timer
if (action == Interactable.Action.AlternateUseStart) {
secondaryControlHoldTime = GlobalSettings.ControlsHoldDuration;
} else if (action == Interactable.Action.AlternateUseStop) {
// if not held for long run standard action
if (secondaryControlHoldTime > 0 && secondaryControlHoldTime > (secondaryControlHoldTime / 2)) {
ExecuteAction(module.gripSecondaryAction, interactor);
}
secondaryControlHoldTime = 0;
}
} else if (action == Interactable.Action.AlternateUseStart) ExecuteAction(module.gripSecondaryAction, interactor);
}
public void Arm(RagdollHand interactor = null) {
if (isOpen) {
isArmed = !isArmed;
if (interactor && interactor.playerHand) {
PlayerControl.GetHand(interactor.playerHand.side).HapticShort(1f);
armedByPlayer = true;
} else armedByPlayer = false;
if (isArmed) {
armedNoise = Utils.PlaySoundLoop(armedSound, null, item, Utils.NoiseLevel.LOUD);
Utils.StopSoundLoop(idleSound, ref idleNoise);
beepTime = 1f;
obstacle.enabled = true;
} else {
Utils.StopSoundLoop(armedSound, ref armedNoise);
idleNoise = Utils.PlaySoundLoop(idleSound, null, item, Utils.NoiseLevel.LOUD);
beepTime = 0;
SetLights(new bool[] { false, false, false });
obstacle.enabled = false;
}
}
}
public void Detonate() {
var pos = transform.position;
List<Creature> hitCreatures = new List<Creature>();
var toHitMask = (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13) | (1 << 24) | (1 << 25) | (1 << 26) | (1 << 27) | (1 << 31);
var raycastIgnoreMask = ~((1 << 10) | (1 << 13) | (1 << 26) | (1 << 27) | (1 << 31));
var creatureMask = ~((1 << 13) | (1 << 26) | (1 << 27) | (1 << 31));
Utils.StopSoundLoop(armedSound, ref armedNoise);
beepTime = 0;
renderer.enabled = false;
item.physicBody.rigidBody.isKinematic = true;
var radius = module.radius * GlobalSettings.ThermalDetonatorRange;
var colliders = Physics.OverlapSphere(pos, radius, toHitMask, QueryTriggerInteraction.Ignore);
foreach (var hit in colliders) {
var distance = Vector3.Distance(hit.transform.position, pos);
var multiplier = (radius - distance) / radius;
var rb = hit.GetComponent<Rigidbody>() ?? hit.GetComponentInParent<Rigidbody>();
if (rb && (distance < 0.3 || !Physics.Linecast(pos, hit.transform.position, raycastIgnoreMask, QueryTriggerInteraction.Ignore))) {
rb.AddExplosionForce(module.impuse * multiplier, pos, radius, 1.0f);
var creature = hit.transform.GetComponentInParent<Creature>();
if (creature) {
if (creature != Player.currentCreature) {
var rp = hit.GetComponent<RagdollPart>() ?? hit.GetComponentInParent<RagdollPart>();
if (rp && rp.sliceAllowed && validParts.ContainsKey(rp.type) && validParts[rp.type] < multiplier) {
try {
rp.TrySlice();
}
catch { }
}
}
if (!hitCreatures.Contains(creature) && !Physics.Linecast(pos, hit.transform.position, creatureMask, QueryTriggerInteraction.Ignore)) {
hitCreatures.Add(creature);
var damage = new CollisionInstance(new DamageStruct(DamageType.Energy, module.damage), null, null);
damage.damageStruct.damage = module.damage * GlobalSettings.ThermalDetonatorDamage * multiplier;
try {
creature.Damage(damage);
}
catch (NullReferenceException) {
// BrainModuleHitReaction seems to randomly fail when damaging NPCs
}
}
}
}
}
var handler = item?.lastHandler?.creature;
Utils.PlayParticleEffect(particles, true);
Utils.PlaySound(explosionSound, module.explosionSoundAsset, handler, Utils.NoiseLevel.VERY_LOUD);
Utils.PlaySound(explosionSound2, module.explosionSoundAsset2, handler, Utils.NoiseLevel.VERY_LOUD);
item.Despawn(3f);
}
public void ToggleSlider(RagdollHand interactor = null) {
if (interactor && interactor.playerHand) PlayerControl.GetHand(interactor.playerHand.side).HapticShort(1f);
isOpen = !isOpen;
animator.SetTrigger(isOpen ? "open" : "close");
animator.ResetTrigger(isOpen ? "close" : "open");
if (isOpen) idleNoise = Utils.PlaySoundLoop(idleSound, null, item, Utils.NoiseLevel.MODERATE);
else {
Utils.StopSoundLoop(idleSound, ref idleNoise);
Utils.StopSoundLoop(armedSound, ref armedNoise);
detonateTime = 0;
beepTime = 0;
isArmed = false;
SetLights(new bool[] { false, false, false});
}
}
bool[] RandomBeep() {
bool RandomBool() {
return Utils.random.Next() > (int.MaxValue / 2);
}
return new bool[] { RandomBool(), RandomBool(), RandomBool() };
}
void SetLights(bool[] beep) {
materialInstance.material.SetFloat("Light1", beep[0] ? 1f : 0f);
materialInstance.material.SetFloat("Light2", beep[1] ? 1f : 0f);
materialInstance.material.SetFloat("Light3", beep[2] ? 1f : 0f);
var handler = item?.lastHandler?.creature;
if (beep[0]) Utils.PlaySound(beepSound1, null, handler);
if (beep[1]) Utils.PlaySound(beepSound2, null, handler);
if (beep[2]) Utils.PlaySound(beepSound3, null, handler);
lastBeep = beep;
}
protected override void ManagedUpdate() {
if (primaryControlHoldTime > 0) {
primaryControlHoldTime -= Time.deltaTime;
if (primaryControlHoldTime <= 0) ExecuteAction(module.gripPrimaryActionHold);
}
if (secondaryControlHoldTime > 0) {
secondaryControlHoldTime -= Time.deltaTime;
if (secondaryControlHoldTime <= 0) ExecuteAction(module.gripSecondaryActionHold);
}
if (telekinesis != null && telekinesis.spinMode) {
Arm();
if (isArmed) detonateTime = module.detonateTime;
telekinesis.SetSpinMode(false);
}
if (beepTime > 0) {
beepTime -= Time.deltaTime;
if (beepTime <= 0 && isArmed) {
beepTime = 1f;
bool[] beep = RandomBeep();
while (beep.SequenceEqual(lastBeep) || beep.All(x => x == false)) {
beep = RandomBeep();
}
SetLights(beep);
}
}
if (detonateTime > 0) {
detonateTime -= Time.deltaTime;
if (detonateTime <= 0 && isArmed) {
Detonate();
}
}
}
}
}