forked from wangw571/Besiege-Exploding-CannonBall-Mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMod.cs
More file actions
516 lines (456 loc) · 22 KB
/
Mod.cs
File metadata and controls
516 lines (456 loc) · 22 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
using spaar.ModLoader;
using spaar.ModLoader.UI;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using System.Reflection;
namespace Exploding_CannonBall_Mod
{
public class Exploding_CannonBall_Mod : Mod
{
public override string Author
{ get { return "TesseractCat - Maintained By Wang_W571 and MaxTCC Improvement by Lench"; } }
public override string BesiegeVersion
{ get { return "v0.35"; } }
public override bool CanBeUnloaded
{
get
{
return true;
}
}
public override string DisplayName
{ get { return "Exploding Cannonballs Mod"; } }
public override string Name { get { return "BesiegeExplodingCannonballs"; } }
public override Version Version
{ get { return new Version("0.3.7"); } }
public Exploding_CannonBall_Mod()
{
}
public override void OnLoad()
{
GameObject.DontDestroyOnLoad(ExplodingCannonballScript.Instance);
ExplodingCannonballScript.Instance.LoadConfiguration();
InitSliders();
Game.OnBlockPlaced += AddSliders;
Game.OnKeymapperOpen += () =>
{
if (!HasSliders(BlockMapper.CurrentInstance.Block))
AddSliders(BlockMapper.CurrentInstance.Block);
AddAllSliders();
};
}
public override void OnUnload()
{
ExplodingCannonballScript.Instance.SaveConfiguration();
GameObject.Destroy(ExplodingCannonballScript.Instance);
}
#region sliders
// Static references to sliders;
// All blocks share the same slider instance
internal static MMenu explosionTypeToggle;
internal static MSlider impactDetectionSlider;
internal static MSlider explosionDelaySlider;
internal static MSlider explosionPowerSlider;
internal static MSlider explosionRangeSlider;
internal static MToggle cannonBallTrailEnabled;
internal static MColourSlider cannonBallTrailColor;
internal static MSlider cannonBallTrailLength;
/// <summary>
/// Initializes slider instances.
/// Must be called after configuration load and before any slider is initialized.
/// </summary>
private static void InitSliders()
{
explosionTypeToggle = new MMenu("explosiontype", ExplodingCannonballScript.Instance.TypeOfExplosion,
new List<string>()
{
"No explosion",
"Bomb", //Changed From Bomb Explosion for Chinese Translation
"Grenade",
"Rocket"
}, "Explosion type");
explosionTypeToggle.ValueChanged += (int value) =>
{
ExplodingCannonballScript.Instance.TypeOfExplosion = value;
bool display = value != 0;
impactDetectionSlider.DisplayInMapper = display;
explosionDelaySlider.DisplayInMapper = display;
explosionPowerSlider.DisplayInMapper = display;
explosionRangeSlider.DisplayInMapper = display;
};
impactDetectionSlider = new MSlider("Impact detection", "impactdetection", ExplodingCannonballScript.Instance.ImpactDetector, 0, 5);
impactDetectionSlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.ImpactDetector = value; };
explosionDelaySlider = new MSlider("Explosion delay", "explosiondelay", ExplodingCannonballScript.Instance.ExplosionDelay, 0, 10);
explosionDelaySlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.ExplosionDelay = value; };
explosionPowerSlider = new MSlider("Explosion power", "explosionpower", ExplodingCannonballScript.Instance.PowerMultiplierOfExplosion, 0, 20);
explosionPowerSlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.PowerMultiplierOfExplosion = value; };
explosionRangeSlider = new MSlider("Explosion range", "explosionrange", ExplodingCannonballScript.Instance.RangeMultiplierOfExplosion, 0, 20);
explosionRangeSlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.RangeMultiplierOfExplosion = value; };
cannonBallTrailEnabled = new MToggle("Enable Cannon Ball Trail", "TrailEnabled", ExplodingCannonballScript.Instance.IsTrailOn);
cannonBallTrailEnabled.Toggled += (bool value) => { ExplodingCannonballScript.Instance.IsTrailOn = value; cannonBallTrailColor.DisplayInMapper = value; cannonBallTrailLength.DisplayInMapper = value; };
cannonBallTrailColor = new MColourSlider("Trail Color", "TrailColor", ExplodingCannonballScript.Instance.TrailColor);
cannonBallTrailColor.ValueChanged += (Color value) => { ExplodingCannonballScript.Instance.TrailColor = value; };
cannonBallTrailLength = new MSlider("Trail Length", "TrailLength", ExplodingCannonballScript.Instance.TrailLength, 0.01f, 100);
cannonBallTrailLength.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.TrailLength = Mathf.Clamp(value, 0.001f, Mathf.Infinity); };
}
/// <summary>
/// BlockBehaviour private readonly mapperTypes field.
/// Of type List<MapperType>.
/// </summary>
private static FieldInfo mapperTypesField = typeof(BlockBehaviour).GetField("mapperTypes", BindingFlags.Instance | BindingFlags.NonPublic);
/// <summary>
/// Returns true if block already has added sliders.
/// Returns true on other blocks than the Cannon.
/// </summary>
/// <param name="block">BlockBehaviour of the block.</param>
public static bool HasSliders(BlockBehaviour block)
{
return !(block.GetBlockID() == (int)BlockType.Cannon) || block.MapperTypes.Exists(match => match.Key == "explosiontype");
}
/// <summary>
/// Adds sliders to all blocks that don't yet have them.
/// </summary>
public static void AddAllSliders()
{
foreach (BlockBehaviour block in Machine.Active().BuildingBlocks.FindAll(block => !HasSliders(block)))
{
AddSliders(block);
}
}
/// <summary>
/// Wrapper for AddSliders(BlocKBehaviour) with a check and component retrieval.
/// </summary>
/// <param name="block">block Transform</param>
public static void AddSliders(Transform block)
{
BlockBehaviour blockbehaviour = block.GetComponent<BlockBehaviour>();
if (!HasSliders(blockbehaviour))
AddSliders(blockbehaviour);
}
/// <summary>
/// Adds sliders to the block.
/// </summary>
/// <param name="block">BlockBehaviour script</param>
private static void AddSliders(BlockBehaviour block)
{
if (block.GetBlockID() == (int)BlockType.Cannon)
{
var currentMapperTypes = block.MapperTypes;
currentMapperTypes.Add(explosionTypeToggle);
currentMapperTypes.Add(impactDetectionSlider);
currentMapperTypes.Add(explosionDelaySlider);
currentMapperTypes.Add(explosionPowerSlider);
currentMapperTypes.Add(explosionRangeSlider);
currentMapperTypes.Add(cannonBallTrailEnabled);
currentMapperTypes.Add(cannonBallTrailColor);
currentMapperTypes.Add(cannonBallTrailLength);
mapperTypesField.SetValue(block, currentMapperTypes);
}
}
#endregion
}
public class ExplodingCannonballScript : SingleInstance<ExplodingCannonballScript>
{
public int TypeOfExplosion = 1;
public float ExplosionDelay = 0;
public float PowerMultiplierOfExplosion = 1;
public float RangeMultiplierOfExplosion = 1;
public float ImpactDetector = 0;
public bool IsTrailOn = true;
public Color TrailColor = Color.yellow;
public float TrailLength = 1;
public String UsingShader = "Particles/Additive";
public Texture TrailTexture;
public override string Name { get; } = "Exploding Cannonball Mod";
void Start()
{
Commands.RegisterCommand("ChangeExplosionType", (args, notUses) =>
{
try
{
TypeOfExplosion = int.Parse(args[0]);
TypeOfExplosion = Mathf.Clamp(TypeOfExplosion, 0, 3);
Exploding_CannonBall_Mod.explosionTypeToggle.Value = TypeOfExplosion;
}
catch { return "Wrong Option! There are four options: \n 0-No Explosion \n 1-Bomb Explosion \n 2-Grenade \n 3-Rocket \n Example: ChangeExplosionType 2"; }
return "Complete!";
}, "Change the explosion type of cannonballs");
Commands.RegisterCommand("ChangeImpactDetection", (args, notUses) =>
{
try
{
ImpactDetector = float.Parse(args[0]);
Exploding_CannonBall_Mod.impactDetectionSlider.Value = ImpactDetector;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change detection of impact for cannonballs");
Commands.RegisterCommand("ChangeExplosionDelay", (args, notUses) =>
{
try
{
ExplosionDelay = float.Parse(args[0]);
ExplosionDelay = Mathf.Clamp(ExplosionDelay, 0, Mathf.Infinity);
Exploding_CannonBall_Mod.explosionDelaySlider.Value = ExplosionDelay;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the explosion delay after cannonballs collide");
Commands.RegisterCommand("ChangeExplosionPowerX", (args, notUses) =>
{
try
{
PowerMultiplierOfExplosion = float.Parse(args[0]);
PowerMultiplierOfExplosion = Mathf.Clamp(PowerMultiplierOfExplosion, 0, Mathf.Infinity);
Exploding_CannonBall_Mod.explosionPowerSlider.Value = PowerMultiplierOfExplosion;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the explosion power.");
Commands.RegisterCommand("ChangeExplosionRangeX", (args, notUses) =>
{
try
{
RangeMultiplierOfExplosion = float.Parse(args[0]);
RangeMultiplierOfExplosion = Mathf.Clamp(RangeMultiplierOfExplosion, 0, Mathf.Infinity);
Exploding_CannonBall_Mod.explosionRangeSlider.Value = RangeMultiplierOfExplosion;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the explosion range.");
Commands.RegisterCommand("ChangeCannonballTrailColor", (args, notUses) =>
{
try
{
TrailColor = new Color(float.Parse(args[0]) / 255, float.Parse(args[1]) / 255, float.Parse(args[2]) / 255, float.Parse(args[3]) / 100);
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the trail color.");
Commands.RegisterCommand("ChangeCannonballTrailTexture", (args, notUses) =>
{
try
{
WWW tex = new WWW("File:///" + Application.dataPath + "/Mods/Resources/CannonTrailTexture.png");
TrailTexture = tex.texture;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the texture of the cannon trail.");
Commands.RegisterCommand("ChangeCannonballTrailShader", (args, notUses) =>
{
switch (UsingShader)
{
case "Particles/Additive":
UsingShader = ("FX/Glass/Stained BumpDistort");
break;
case ("FX/Glass/Stained BumpDistort"):
UsingShader = ("Particles/Additive");
break;
}
return "Complete!";
}, "Change the trail shader.");
}
private void Update()
{
GameObject go = GameObject.Find("CanonBallHeavy(Clone)");
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CannonBomb(Clone)";
if (IsTrailOn)
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.6f;
tr.endWidth = 0.6f;
tr.material = new Material(Shader.Find("Particles/Additive"));
tr.useLightProbes = true;
tr.probeAnchor = go.transform;
tr.material.SetColor("_TintColor", TrailColor);
tr.time = (TrailLength + 0.0001f) / (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfExplosion != 0)
{
go.AddComponent<ExplosionForCannonballs>();
}
}
}
}
private void FixedUpdate()
{
GameObject go = GameObject.Find("CanonBallHeavy(Clone)");
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CannonBomb(Clone)";
if (IsTrailOn)
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.6f;
tr.endWidth = 0.6f;
tr.material = new Material(Shader.Find(UsingShader));
tr.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.UseProxyVolume;
tr.probeAnchor = go.transform;
if (TrailTexture == null)
{
tr.material.SetColor("_TintColor", TrailColor);
}
else
{
tr.material.SetTexture("_MainTex", TrailTexture);
tr.material.SetTexture("_BumpMap", TrailTexture);
}
tr.time = (TrailLength + 0.0001f) / (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfExplosion != 0)
{
go.AddComponent<ExplosionForCannonballs>();
}
}
}
}
internal void LoadConfiguration()
{
TypeOfExplosion = Mathf.Clamp(Configuration.GetInt("Explosion Type", TypeOfExplosion), 0, 3);
ExplosionDelay = Configuration.GetFloat("Explosion Delay", ExplosionDelay);
PowerMultiplierOfExplosion = Configuration.GetFloat("Explosion Power", PowerMultiplierOfExplosion);
RangeMultiplierOfExplosion = Configuration.GetFloat("Explosion Range", RangeMultiplierOfExplosion);
ImpactDetector = Configuration.GetFloat("Impact Detection", ImpactDetector);
IsTrailOn = Configuration.GetBool("Trail Enabled", IsTrailOn);
TrailColor = new Color(
Configuration.GetFloat("Trail Color R", TrailColor.r),
Configuration.GetFloat("Trail Color G", TrailColor.g),
Configuration.GetFloat("Trail Color B", TrailColor.b)
);
TrailLength = Configuration.GetFloat("Trail Length", TrailLength);
}
internal void SaveConfiguration()
{
Configuration.SetInt("Explosion Type", TypeOfExplosion);
Configuration.SetFloat("Explosion Delay", ExplosionDelay);
Configuration.SetFloat("Explosion Power", PowerMultiplierOfExplosion);
Configuration.SetFloat("Explosion Range", RangeMultiplierOfExplosion);
Configuration.SetFloat("Impact Detection", ImpactDetector);
Configuration.SetBool("Trail Enabled", IsTrailOn);
Configuration.SetFloat("Trail Color R", TrailColor.r);
Configuration.SetFloat("Trail Color G", TrailColor.g);
Configuration.SetFloat("Trail Color B", TrailColor.b);
Configuration.SetFloat("Trail Length", TrailLength);
Configuration.Save();
}
}
public class ExplosionForCannonballs : MonoBehaviour
{
public ExplodingCannonballScript ECS;
private int CountDownExplode;
private bool Exploding = false;
private int FrameCount = 0;
IEnumerator Explode()
{
if (Exploding)
{
while (CountDownExplode >= 0)
{
yield return new WaitForFixedUpdate();
--CountDownExplode;
StartCoroutine(Explode());
yield break;
}
ECS = GameObject.Find("Exploding Cannonball Mod").GetComponent<ExplodingCannonballScript>();
if (ECS.TypeOfExplosion == 1)
{
GameObject explo = (GameObject)GameObject.Instantiate(PrefabMaster.BlockPrefabs[23].gameObject, this.transform.position, this.transform.rotation);
explo.transform.localScale = Vector3.one * 0.01f;
ExplodeOnCollideBlock ac = explo.GetComponent<ExplodeOnCollideBlock>();
ac.radius = 7 * ECS.RangeMultiplierOfExplosion;
ac.power = 2100f * ECS.PowerMultiplierOfExplosion;
ac.torquePower = 100000 * ECS.PowerMultiplierOfExplosion;
ac.upPower = 0;
ac.Explodey();
explo.AddComponent<TimedSelfDestruct>();
Destroy(this.gameObject);
}
else if (ECS.TypeOfExplosion == 2)
{
GameObject explo = (GameObject)GameObject.Instantiate(PrefabMaster.BlockPrefabs[54].gameObject, this.transform.position, this.transform.rotation);
explo.transform.localScale = Vector3.one * 0.01f;
ControllableBomb ac = explo.GetComponent<ControllableBomb>();
ac.radius = 3 * ECS.RangeMultiplierOfExplosion;
ac.power = 1500 * ECS.PowerMultiplierOfExplosion;
ac.randomDelay = 0.00001f;
ac.upPower = 0f;
ac.StartCoroutine_Auto(ac.Explode());
explo.AddComponent<TimedSelfDestruct>();
Destroy(this.gameObject);
}
else if (ECS.TypeOfExplosion == 3)
{
GameObject explo = (GameObject)GameObject.Instantiate(PrefabMaster.BlockPrefabs[59].gameObject, this.transform.position, this.transform.rotation);
explo.transform.localScale = Vector3.one * 0.01f;
TimedRocket ac = explo.GetComponent<TimedRocket>();
ac.SetSlip(Color.white);
ac.radius = 3 * ECS.RangeMultiplierOfExplosion;
ac.power = 1500 * ECS.PowerMultiplierOfExplosion;
ac.randomDelay = 0.000001f;
ac.upPower = 0;
ac.StartCoroutine(ac.Explode(0.01f));
explo.AddComponent<TimedSelfDestruct>();
Destroy(this.gameObject);
}
}
}
void Start()
{
ECS = ExplodingCannonballScript.Instance;
}
void Update()
{
if (!Exploding)
CountDownExplode = (int)(ECS.ExplosionDelay * 100);
}
void FixedUpdate()
{
++FrameCount;
TrailRenderer TR = this.GetComponent<TrailRenderer>();
if (TR != null) TR.time = (this.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (ECS.TrailLength + 0.0001f);
}
void OnCollisionEnter(Collision coll)
{
if ((!Exploding || coll.relativeVelocity.magnitude > ECS.ImpactDetector) && FrameCount > 2)
{
Exploding = true;
StartCoroutine(Explode());
}
}
void OnCollisionStay(Collision coll)
{
if ((!Exploding || coll.relativeVelocity.sqrMagnitude > ECS.ImpactDetector * ECS.ImpactDetector) && FrameCount > 2)
{
Exploding = true;
StartCoroutine(Explode());
}
}
}
public class TimedSelfDestruct : MonoBehaviour
{
float timer = 0;
void FixedUpdate()
{
++timer;
if (timer > 260)
{
Destroy(this.gameObject);
}
if (this.GetComponent<TimedRocket>())
{
Destroy(this.GetComponent<TimedRocket>());
}
}
}
}