-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.cs
360 lines (314 loc) · 17 KB
/
script.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using UnityEngine;
using System;
using System.Collections.Generic;
// Show specific detail for machinery, right under the object.
// <summary>
// Spawn "Toggle detail view only" to toggle whether mod works only in detail view
// 1. On object spawned, search for the Components (Behaviours)
// class AttributeManifest:
// 2. Each frame, align the text objects to the object
// 3. Each second, update the text objects
// Destroy the text objects when the object is destroyed
// Make text objects invisible when detail view or onlyDetailVIew is off
// </summary>
namespace DisplayMachineryDetail
{
public class Mod
{
// TODO Machinery Adding procedure:
// 1. Add new Getcomponent<>() to if statement in OnItemSpawned
// 2. Add new behaviour at AttributeManifest
// 3. Add X = GetComponent<>(), and X to if statement in void Awake()
// 4. Add case to UpdateInfo()
// Whether mod works only in detail view
public static bool onlyDetailView = true;
public static void Main()
{
ModAPI.Register(
new Modification()
{
// item to derive from (whatever)
OriginalItem = ModAPI.FindSpawnable("Brick"),
// new item name with a suffix to assure it is globally unique
NameOverride = "Toggle detail view only",
// order by this name
NameToOrderByOverride = "b",
// new item description
DescriptionOverride = "Toggle whether to show machinery info only in detail view",
// item category
CategoryOverride = ModAPI.FindCategory("Entities"),
// new item thumbnail (relative path)
ThumbnailOverride = ModAPI.LoadSprite("thumb_ondetailview.png"),
// all code in the AfterSpawn delegate will be executed when the item is spawned
AfterSpawn = (Instance) =>
{
// Destroy item and toggle onlyDetailView
Instance.GetComponent<PhysicalBehaviour>().Disintegrate();
UnityEngine.Object.Destroy(Instance, 3f);
onlyDetailView = !onlyDetailView;
if(onlyDetailView)
{
ModAPI.Notify("show machinery info in detail mode only: on");
}
else
{
ModAPI.Notify("show machinery info in detail mode only: off");
}
}
}
);
ModAPI.Register<AttributeManifest>();
ModAPI.OnItemSpawned += (sender, obj) => {
if(obj.Instance.GetComponent<BoatMotorBehaviour>()
|| obj.Instance.GetComponent<ButtonBehaviour>() // Both big and small buttons
|| obj.Instance.GetComponent<LagboxBehaviour>()
|| obj.Instance.GetComponent<KeyTriggerBehaviour>()
|| obj.Instance.GetComponent<DetectorBehaviour>()
|| obj.Instance.GetComponent<MagnetBehaviour>()
|| obj.Instance.GetComponent<LEDBulbBehaviour>()
|| obj.Instance.GetComponent<GateBehaviour>()
|| obj.Instance.GetComponent<RotorBehaviour>()
|| obj.Instance.GetComponent<ResistorBehaviour>()
|| obj.Instance.GetComponent<MetronomeBehaviour>()
|| obj.Instance.GetComponent<CarBehaviour>()
|| obj.Instance.GetComponent<LaserBehaviour>()
|| obj.Instance.GetComponent<HoverThrusterBehaviour>()
|| obj.Instance.GetComponent<DamagableMachineryBehaviour>())
{
// Add AttributeManifest
obj.Instance.GetOrAddComponent<AttributeManifest>();
}
else if (obj.Instance.GetComponentInChildren<WinchBehaviour>())
{
obj.Instance.GetComponentInChildren<WinchBehaviour>().gameObject.GetOrAddComponent<AttributeManifest>();
}
};
// Delete text on item deleted
ModAPI.OnItemRemoved += (sender, obj) => {
if(obj.Instance.GetComponent<AttributeManifest>())
{
AttributeManifest objInstance = obj.Instance.GetComponent<AttributeManifest>();
UnityEngine.Object.Destroy(objInstance.AttrObject);
UnityEngine.Object.Destroy(objInstance.damageabilityObject);
}
};
}
}
// Show the attributes of an object
public class AttributeManifest : MonoBehaviour
{
// time until a check is made for changes in machinery attributes
float timeUntilCheck = 0f;
// GameObject index:
// boatMotor, button, lagbox, keyTrigger, detector, magnet, ledBulb, gate, rotor, resistor, metronome, wheel
int objIndex = -1;
// machinery behaviours
BoatMotorBehaviour boatMotor; // .Force - -25 = reverse, 25 = forward
ButtonBehaviour button; // .TriggerOnExit - isdouble
LagboxBehaviour lagbox; // .DelayModifier - delay
KeyTriggerBehaviour keyTrigger; //.DoubleTrigger
DetectorBehaviour detector; // .TriggerOnExit, .Range: game units
MagnetBehaviour magnet; // .Reversed
LEDBulbBehaviour ledBulb; // .Color r,g,b
GateBehaviour gate; // .ThresholdPercentage .MaxPower .DoubleTrigger
RotorBehaviour rotor; // .Speed in [-8k, 8k]
ResistorBehaviour resistor; // .ResistorPower in [0, 1]
MetronomeBehaviour metronome; // .TempoModifier (Hz)
CarBehaviour wheel; // .MotorSpeed -500 = forward, 500 = reverse
WinchBehaviour winch; // .LowerLimit, .UpperLimit (m)
LaserBehaviour laser; // .UserSetColour r,g,b
HoverThrusterBehaviour hoverThruster; // .BaseHoverHeight: game units
// General behaviour of machinery: damageability.
DamagableMachineryBehaviour damageableMachinery; // .Destroyed .Indestructible
// machinery position, rotation, scale etc.
Transform pos;
// text objects for each any GameObject
public GameObject AttrObject;
public GameObject damageabilityObject;
// On borne
void Awake()
{
// to transform position, scale etc. of object
pos = GetComponent<Transform>();
// get the behaviour
boatMotor = GetComponent<BoatMotorBehaviour>();
button = GetComponent<ButtonBehaviour>();
lagbox = GetComponent<LagboxBehaviour>();
keyTrigger = GetComponent<KeyTriggerBehaviour>();
detector = GetComponent<DetectorBehaviour>();
magnet = GetComponent<MagnetBehaviour>();
ledBulb = GetComponent<LEDBulbBehaviour>();
gate = GetComponent<GateBehaviour>();
rotor = GetComponent<RotorBehaviour>();
resistor = GetComponent<ResistorBehaviour>();
metronome = GetComponent<MetronomeBehaviour>();
wheel = GetComponent<CarBehaviour>();
winch = GetComponentInChildren<WinchBehaviour>();
laser = GetComponent<LaserBehaviour>();
hoverThruster = GetComponent<HoverThrusterBehaviour>();
damageableMachinery = GetComponent<DamagableMachineryBehaviour>();
if (boatMotor) {objIndex = 0;}
else if (button) { objIndex = 1; }
else if (lagbox) { objIndex = 2; }
else if (keyTrigger) { objIndex = 3; }
else if (detector) { objIndex = 4; }
else if (magnet) { objIndex = 5; }
else if (ledBulb) { objIndex = 6; }
else if (gate) { objIndex = 7; }
else if (rotor) { objIndex = 8; }
else if (resistor) { objIndex = 9; }
else if (metronome) { objIndex = 10; }
else if (wheel) { objIndex = 11; }
else if (winch){objIndex = 12; }
else if (laser) { objIndex = 13; }
else if (hoverThruster) { objIndex = 14; }
// Initial empty text object setup
AttrObject = new GameObject();
AttrObject.transform.localScale = new Vector3(0.03f, 0.03f, 1f);
AttrObject.AddComponent<TextMesh>();
AttrObject.GetComponent<TextMesh>().fontSize = 32;
AttrObject.GetComponent<TextMesh>().anchor = TextAnchor.MiddleCenter;
AttrObject.layer = 16;
damageabilityObject = new GameObject();
damageabilityObject.transform.localScale = new Vector3(0.03f, 0.03f, 1f);
damageabilityObject.AddComponent<TextMesh>();
damageabilityObject.GetComponent<TextMesh>().fontSize = 32;
damageabilityObject.GetComponent<TextMesh>().anchor = TextAnchor.MiddleCenter;
damageabilityObject.layer = 16;
damageabilityObject.GetComponent<TextMesh>().color = Color.yellow;
}
void OnDestroy()
{
// delete text object
UnityEngine.Object.Destroy(AttrObject);
}
// Update each frame
void Update()
{
// update attribute values, less frequent than Update()
timeUntilCheck += Time.unscaledDeltaTime;
if (timeUntilCheck > .5f)
{
timeUntilCheck = 0f;
UpdateInfo();
}
if (!Mod.onlyDetailView || Mod.onlyDetailView && Global.main.ShowLimbStatus)
{
// update attribute position
if (AttrObject != null && AttrObject.GetComponent<TextMesh>().text != ""
|| damageabilityObject != null && damageabilityObject.GetComponent<TextMesh>().text != "")
{
float height = pos.transform.localScale[1];
float width = pos.transform.localScale[0];
AttrObject.transform.position = pos.transform.position + new Vector3(0f, -0.03f - 0.2f * height, 0f);
AttrObject.transform.localScale = new Vector3(1f, 1f, 1f) *Mathf.Sqrt(height) / 50;
AttrObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
damageabilityObject.transform.position = pos.transform.position + new Vector3(0.03f + 0.2f * width ,0f , 0f);
damageabilityObject.transform.localScale = new Vector3(1f, 1f, 1f) *Mathf.Sqrt(width) / 50;
damageabilityObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
}
}
}
// Occasional update of the attributes
void UpdateInfo()
{
// If detail view is on
if (!Mod.onlyDetailView || Mod.onlyDetailView && Global.main.ShowLimbStatus)
{
// Create text anew if it does not exist
if (AttrObject == null)
{
AttrObject = new GameObject();
AttrObject.transform.localScale = new Vector3(0.03f, 0.03f, 1f);
AttrObject.AddComponent<TextMesh>();
AttrObject.GetComponent<TextMesh>().fontSize = 32;
AttrObject.GetComponent<TextMesh>().anchor = TextAnchor.MiddleCenter;
AttrObject.layer = 16;
damageabilityObject = new GameObject();
damageabilityObject.transform.localScale = new Vector3(0.06f, 0.06f, 1f);
damageabilityObject.AddComponent<TextMesh>();
damageabilityObject.GetComponent<TextMesh>().fontSize = 32;
damageabilityObject.GetComponent<TextMesh>().anchor = TextAnchor.MiddleCenter;
damageabilityObject.GetComponent<TextMesh>().color = Color.yellow;
damageabilityObject.layer = 16;
}
// color for some behaviours
Color c;
// Further text object setup for each attribute
switch (objIndex){
case 0: // BoatMotor
AttrObject.GetComponent<TextMesh>().text = Utils.IsForward(boatMotor);
// AttrObject.GetComponent<TextMesh>().text = funkcja;
break;
case 1: // button
AttrObject.GetComponent<TextMesh>().text = Utils.IsDoubleButton(button);
break;
case 2: // lagbox
AttrObject.GetComponent<TextMesh>().text = "Delay: " + lagbox.GetComponent<LagboxBehaviour>().DelayModifier;
break;
case 3: // keyTrigger
AttrObject.GetComponent<TextMesh>().text = Utils.IsDoubleKey(keyTrigger);
break;
case 4: // detector, 1.213636f
AttrObject.GetComponent<TextMesh>().text = $"Range: {detector.GetComponent<DetectorBehaviour>().Range * Global.MetricMultiplier}"
+ "\n" + Utils.IsDoubleDetector(detector);
break;
case 5: // magnet
AttrObject.GetComponent<TextMesh>().text = Utils.IsReversed(magnet);
break;
case 6: // ledBulb
// color
c = ledBulb.GetComponent<LEDBulbBehaviour>().Color;
// rgb(r, g, b)
AttrObject.GetComponent<TextMesh>().text = "rgb("
+ c.r.ToString("F2") + ", "
+ c.g.ToString("F2") + ", "
+ c.b.ToString("F2") + ")";
break;
case 7: // gate
AttrObject.GetComponent<TextMesh>().text = "Threshold: " + gate.GetComponent<GateBehaviour>().ThresholdPercentage
+ "\nMaxPower: " + gate.GetComponent<GateBehaviour>().MaxPower
+ "\n" + Utils.IsDoubleGate(gate);
break;
case 8: // rotor
AttrObject.GetComponent<TextMesh>().text = "Speed: " + rotor.GetComponent<RotorBehaviour>().Speed;
break;
case 9: // resistor
AttrObject.GetComponent<TextMesh>().text = "Power: " + resistor.GetComponent<ResistorBehaviour>().ResistorPower.ToString("F2");
break;
case 10: // metronome
AttrObject.GetComponent<TextMesh>().text = "Hz: " + metronome.GetComponent<MetronomeBehaviour>().TempoModifier;
break;
case 11: // wheel
AttrObject.GetComponent<TextMesh>().text = Utils.IsReversedWheel(wheel)
+ "\n" + Utils.IsBrakeEngaged(wheel);
break;
case 12: // winch
AttrObject.GetComponent<TextMesh>().text = $"in [{winch.GetComponentInChildren<WinchBehaviour>().LowerLimit}, {winch.GetComponentInChildren<WinchBehaviour>().UpperLimit}]";
break;
case 13: // laser
c = laser.GetComponent<LaserBehaviour>().UserSetColour;
AttrObject.GetComponent<TextMesh>().text = "rgb("
+ c.r.ToString("F2") + ", "
+ c.g.ToString("F2") + ", "
+ c.b.ToString("F2") + ")";
break;
case 14: // hoverThruster
AttrObject.GetComponent<TextMesh>().text = "Height:" + hoverThruster.GetComponent<HoverThrusterBehaviour>().BaseHoverHeight * Global.MetricMultiplier;
break;
}
if (GetComponent<DamagableMachineryBehaviour>())
{
// damageabilityObject.GetComponent<TextMesh>().color = Color.yellow;
damageabilityObject.GetComponent<TextMesh>().text = Utils.IsIndestructible(damageableMachinery)
+ "\n" + Utils.IsDestroyed(damageableMachinery);
}
}
else if (AttrObject != null)
{
UnityEngine.Object.Destroy(AttrObject);
UnityEngine.Object.Destroy(damageabilityObject);
}
}
}
}