-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItemBactaStim.cs
146 lines (124 loc) · 6.11 KB
/
ItemBactaStim.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
using UnityEngine;
using UnityEngine.UI;
using ThunderRoad;
using System.Collections.Generic;
using System.Collections;
namespace TOR {
public class ItemBactaStim : ThunderBehaviour {
public override ManagedLoops EnabledManagedLoops => ManagedLoops.Update;
protected Item item;
protected ItemModuleBactaStim module;
public float currentCharge = 100;
Collider tip;
Light light;
Text text;
bool holdingLeft;
bool holdingRight;
Creature healer;
AudioSource injectSound;
AudioSource rechargeSound;
HashSet<Collider> playerColliders;
protected void Awake() {
item = GetComponent<Item>();
module = item.data.GetModule<ItemModuleBactaStim>();
for (int i = 0, l = item.collisionHandlers.Count; i < l; i++) {
item.collisionHandlers[i].OnCollisionStartEvent += CollisionHandler;
}
item.OnGrabEvent += OnGrabEvent;
item.OnUngrabEvent += OnUngrabEvent;
tip = item.GetCustomReference("Tip").GetComponent<Collider>();
light = item.GetCustomReference("Light").GetComponent<Light>();
text = item.GetCustomReference("Text").GetComponent<Text>();
injectSound = item.GetCustomReference("InjectSound").GetComponent<AudioSource>();
rechargeSound = item.GetCustomReference("RechargeSound").GetComponent<AudioSource>();
light.enabled = false;
text.enabled = false;
playerColliders = new HashSet<Collider>();
var coroutine = GetPlayerColliders();
StartCoroutine(coroutine);
}
private IEnumerator GetPlayerColliders() {
while (!Player.local) {
yield return Utils.waitSeconds_01;
}
while (Player.local && playerColliders.Count < 1) {
try {
playerColliders.UnionWith(Player.local.handLeft.ragdollHand.colliderGroup.colliders);
playerColliders.UnionWith(Player.local.handRight.ragdollHand.colliderGroup.colliders);
playerColliders.UnionWith(Player.currentCreature.ragdoll.GetPart(RagdollPart.Type.Neck).colliderGroup.colliders);
playerColliders.UnionWith(Player.currentCreature.ragdoll.GetPart(RagdollPart.Type.LeftArm).colliderGroup.colliders);
playerColliders.UnionWith(Player.currentCreature.ragdoll.GetPart(RagdollPart.Type.RightArm).colliderGroup.colliders);
playerColliders.UnionWith(Player.currentCreature.ragdoll.GetPart(RagdollPart.Type.LeftLeg).colliderGroup.colliders);
playerColliders.UnionWith(Player.currentCreature.ragdoll.GetPart(RagdollPart.Type.RightLeg).colliderGroup.colliders);
yield break;
}
catch { }
yield return Utils.waitSeconds_01;
}
yield break;
}
public void OnGrabEvent(Handle handle, RagdollHand interactor) {
healer = interactor.creature;
holdingRight |= interactor.playerHand == Player.local.handRight;
holdingLeft |= interactor.playerHand == Player.local.handLeft;
light.enabled = true;
text.enabled = true;
}
public void OnUngrabEvent(Handle handle, RagdollHand interactor, bool throwing) {
healer = null;
holdingRight &= interactor.playerHand == Player.local.handRight;
holdingLeft &= interactor.playerHand == Player.local.handLeft;
light.enabled = false;
text.enabled = false;
}
void CollisionHandler(CollisionInstance collisionInstance) {
if (collisionInstance.sourceCollider == tip || collisionInstance.targetCollider == tip) {
if (currentCharge >= 100) {
var ragdollPart = collisionInstance.targetColliderGroup?.collisionHandler?.ragdollPart ?? collisionInstance.sourceColliderGroup?.collisionHandler?.ragdollPart;
if (ragdollPart) {
var creature = ragdollPart.ragdoll.creature;
Heal(creature);
return;
}
}
}
}
protected void OnTriggerEnter(Collider other) {
if (!item.holder && currentCharge >= 100 && playerColliders.Contains(other)) {
Heal(Player.currentCreature);
}
}
void Heal(Creature creature) {
if (creature && currentCharge >= 100 && creature.currentHealth < creature.maxHealth && creature.state != Creature.State.Dead) {
Utils.PlaySound(injectSound, null, item);
BactaStimHeal heal = creature.gameObject.AddComponent<BactaStimHeal>();
heal.healAmount = module.healAmount;
heal.healDuration = module.healDuration;
heal.creature = creature;
heal.healer = healer;
currentCharge = 0;
Utils.PlayHaptic(holdingLeft, holdingRight, Utils.HapticIntensity.Major);
}
}
protected override void ManagedUpdate() {
if (currentCharge < 100) {
currentCharge = Mathf.Clamp(currentCharge + (module.chargeRate * Time.deltaTime), 0, 100);
text.text = Mathf.RoundToInt(currentCharge).ToString();
if (currentCharge >= 100) Utils.PlaySound(rechargeSound, null, item);
}
}
}
public class BactaStimHeal : ThunderBehaviour {
public override ManagedLoops EnabledManagedLoops => ManagedLoops.Update;
public float healAmount = 10f;
public float healDuration = 10f;
public Creature creature;
public Creature healer;
float duration;
protected override void ManagedUpdate() {
if (creature == null || creature.state == Creature.State.Dead || duration >= healDuration || creature.currentHealth >= creature.maxHealth) Destroy(this);
creature.Heal(healAmount * Time.deltaTime, healer);
duration += Time.deltaTime;
}
}
}