forked from KyleTheScientist/Bark
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRockets.cs
216 lines (192 loc) · 6.13 KB
/
Rockets.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
using System;
using UnityEngine;
using Grate.Gestures;
using Grate.GUI;
using Grate.Tools;
using Grate.Extensions;
using GorillaLocomotion;
using BepInEx.Configuration;
using Grate.Interaction;
using Random = UnityEngine.Random;
namespace Grate.Modules.Movement
{
public class Rockets : GrateModule
{
public static readonly string DisplayName = "Rockets";
public static Rockets Instance;
private GameObject rocketPrefab;
Rocket rocketL, rocketR;
void Awake()
{
try
{
Instance = this;
rocketPrefab = Plugin.assetBundle.LoadAsset<GameObject>("Rocket");
}
catch (Exception e)
{
Logging.Exception(e);
}
}
protected override void Start()
{
base.Start();
}
void Setup()
{
try
{
if (!rocketPrefab)
rocketPrefab = Plugin.assetBundle.LoadAsset<GameObject>("Rocket");
rocketL = SetupRocket(Instantiate(rocketPrefab), true);
rocketR = SetupRocket(Instantiate(rocketPrefab), false);
ReloadConfiguration();
}
catch (Exception e)
{
Logging.Exception(e);
}
}
Rocket SetupRocket(GameObject rocketObj, bool isLeft)
{
try
{
rocketObj.name = isLeft ? "Grate Rocket Left" : "Grate Rocket Right";
var rocket = rocketObj.AddComponent<Rocket>().Init(isLeft);
rocket.LocalPosition = new Vector3(0.51f, -3, 0f);
rocket.LocalRotation = new Vector3(0, 0, -90);
return rocket;
}
catch (Exception e)
{
Logging.Exception(e);
}
return null;
}
public Vector3 AddedVelocity()
{
return rocketL.force + rocketR.force;
}
protected override void Cleanup()
{
try
{
rocketL?.gameObject?.Obliterate();
rocketR?.gameObject?.Obliterate();
}
catch (Exception e) { Logging.Exception(e); }
}
protected override void OnEnable()
{
if (!MenuController.Instance.Built) return;
base.OnEnable();
Setup();
}
public static ConfigEntry<int> Power;
public static ConfigEntry<int> Volume;
protected override void ReloadConfiguration()
{
var rockets = new Rocket[] { rocketL?.GetComponent<Rocket>(), rocketR?.GetComponent<Rocket>() };
foreach (var rocket in rockets)
{
if (!rocket) continue;
rocket.power = Power.Value * 2f;
rocket.volume = MathExtensions.Map(Volume.Value, 0, 10, 0, 1);
}
}
public static void BindConfigEntries()
{
Power = Plugin.configFile.Bind(
section: DisplayName,
key: "power",
defaultValue: 5,
description: "The power of each rocket"
);
Volume = Plugin.configFile.Bind(
section: DisplayName,
key: "thruster volume",
defaultValue: 10,
description: "How loud the thrusters sound"
);
}
public override string GetDisplayName()
{
return DisplayName;
}
public override string Tutorial()
{
return $"Hold either [Grip] to summon a rocket.";
}
}
public class Rocket : GrateGrabbable
{
public float power = 5f, volume = .2f;
public Vector3 force { get; private set; }
bool isLeft;
GestureTracker gt;
Rigidbody rb;
public AudioSource exhaustSound;
protected override void Awake()
{
base.Awake();
this.rb = GetComponent<Rigidbody>();
this.exhaustSound = GetComponent<AudioSource>();
this.exhaustSound.Stop();
}
public Rocket Init(bool isLeft)
{
this.isLeft = isLeft;
gt = GestureTracker.Instance;
if(isLeft)
gt.leftGrip.OnPressed += Attach;
else
gt.rightGrip.OnPressed += Attach;
return this;
}
void Attach(InputTracker _)
{
var parent = (isLeft ? gt.leftPalmInteractor : gt.rightPalmInteractor);
if (!this.CanBeSelected(parent)) return;
this.transform.parent = null;
this.transform.localScale = Vector3.one * Player.Instance.scale;
parent.Select(this);
exhaustSound.Stop();
exhaustSound.time = Random.Range(0, exhaustSound.clip.length);
exhaustSound.Play();
}
void FixedUpdate()
{
Player player = Player.Instance;
force = this.transform.forward * this.power * Time.fixedDeltaTime * Player.Instance.scale;
if (Selected)
player.AddForce(force);
else
{
rb.velocity += force * 10;
force = Vector3.zero;
transform.Rotate(Random.insideUnitSphere);
}
this.exhaustSound.volume = Mathf.Lerp(.5f, 0, Vector3.Distance(
player.headCollider.transform.position,
transform.position
) / 20f) * volume;
}
public override void OnDeselect(GrateInteractor interactor)
{
base.OnDeselect(interactor);
rb.velocity = Player.Instance.GetComponent<Rigidbody>().velocity;
}
public void SetupInteraction()
{
this.throwOnDetach = true;
gameObject.layer = GrateInteractor.InteractionLayer;
}
protected override void OnDestroy()
{
base.OnDestroy();
if (!gt) return;
gt.leftGrip.OnPressed -= Attach;
gt.rightGrip.OnPressed -= Attach;
}
}
}