-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMod.cs
More file actions
1147 lines (1024 loc) · 49 KB
/
Mod.cs
File metadata and controls
1147 lines (1024 loc) · 49 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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.45a"; } }
public override bool CanBeUnloaded
{
get
{
return true;
}
}
public override string DisplayName
{ get { return "Exploding Cannonballs and Arrow Mod"; } }
public override string Name { get { return "BesiegeExplodingCannonballs"; } }
public override Version Version
{ get { return new Version("0.4.0"); } }
public Exploding_CannonBall_Mod()
{
}
public override void OnLoad()
{
GameObject.DontDestroyOnLoad(ExplodingCannonballScript.Instance);
GameObject.DontDestroyOnLoad(ExplodingArrowsScript.Instance);
ExplodingCannonballScript.Instance.LoadConfiguration();
ExplodingArrowsScript.Instance.LoadConfiguration();
InitCannonSliders();
InitArrowSliders();
Game.OnBlockPlaced += AddSliders;
Game.OnKeymapperOpen += () =>
{
if (!HasCannonSliders(BlockMapper.CurrentInstance.Block) || !HasArrowSliders(BlockMapper.CurrentInstance.Block))
AddSliders(BlockMapper.CurrentInstance.Block);
AddAllSliders();
};
}
public override void OnUnload()
{
ExplodingCannonballScript.Instance.SaveConfiguration();
ExplodingArrowsScript.Instance.SaveConfiguration();
GameObject.Destroy(ExplodingCannonballScript.Instance);
GameObject.Destroy(ExplodingArrowsScript.Instance);
}
#region sliders
// Static references to sliders;
// All blocks share the same slider instance
internal static MMenu CannonballExplosionTypeToggle;
internal static MSlider CannonballImpactDetectionSlider;
internal static MSlider CannonballExplosionDelaySlider;
internal static MSlider CannonballExplosionPowerSlider;
internal static MSlider explosionRangeSlider;
internal static MToggle cannonBallTrailEnabled;
internal static MColourSlider cannonBallTrailColor;
internal static MSlider cannonBallTrailLength;
internal static MMenu ArrowAfterEffectTypeToggle;
internal static MSlider ArrowAfterEffectImpactDetectionSlider;
internal static MSlider ArrowAfterEffectDelaySlider;
internal static MSlider ArrowAfterEffectPowerSlider;
internal static MSlider ArrowAfterEffectRangeSlider;
internal static MToggle ArrowTrailEnabled;
internal static MColourSlider ArrowTrailColor;
internal static MSlider ArrowTrailLength;
/// <summary>
/// Initializes slider instances.
/// Must be called after configuration load and before any slider is initialized.
/// </summary>
private static void InitCannonSliders()
{
CannonballExplosionTypeToggle = new MMenu("explosiontype", ExplodingCannonballScript.Instance.TypeOfExplosion,
new List<string>()
{
"No explosion",
"Bomb", //Changed From Bomb Explosion for Chinese Translation
"Grenade",
"Rocket"
}, "Explosion type");
CannonballExplosionTypeToggle.ValueChanged += (int value) =>
{
ExplodingCannonballScript.Instance.TypeOfExplosion = value;
bool display = value != 0;
CannonballImpactDetectionSlider.DisplayInMapper = display;
CannonballExplosionDelaySlider.DisplayInMapper = display;
CannonballExplosionPowerSlider.DisplayInMapper = display;
explosionRangeSlider.DisplayInMapper = display;
};
CannonballImpactDetectionSlider = new MSlider("Impact detection", "impactdetection", ExplodingCannonballScript.Instance.ImpactDetector, 0, 5);
CannonballImpactDetectionSlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.ImpactDetector = value; };
CannonballExplosionDelaySlider = new MSlider("Explosion delay", "explosiondelay", ExplodingCannonballScript.Instance.ExplosionDelay, 0, 10);
CannonballExplosionDelaySlider.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.ExplosionDelay = value; };
CannonballExplosionPowerSlider = new MSlider("Explosion power", "explosionpower", ExplodingCannonballScript.Instance.PowerMultiplierOfExplosion, 0, 20);
CannonballExplosionPowerSlider.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 Decay Rate", "TrailLength", ExplodingCannonballScript.Instance.TrailLength, 0.01f, 100);
cannonBallTrailLength.ValueChanged += (float value) => { ExplodingCannonballScript.Instance.TrailLength = Mathf.Clamp(value, 0.001f, Mathf.Infinity); };
}
private static void InitArrowSliders()
{
ArrowAfterEffectTypeToggle = new MMenu("aftereffecttype", ExplodingArrowsScript.Instance.TypeOfAfterEffect,
new List<string>()
{
"No after effect",
"Fire",
"Kinetic",
"Grenade"
}, "Explosion type");
ArrowAfterEffectTypeToggle.ValueChanged += (int value) =>
{
ExplodingArrowsScript.Instance.TypeOfAfterEffect = value;
bool display = value != 0;
ArrowAfterEffectImpactDetectionSlider.DisplayInMapper = display;
ArrowAfterEffectDelaySlider.DisplayInMapper = display;
ArrowAfterEffectPowerSlider.DisplayInMapper = display;
ArrowAfterEffectRangeSlider.DisplayInMapper = display;
};
ArrowAfterEffectImpactDetectionSlider = new MSlider("Arrow Impact detection", "arrowimpactdetection", ExplodingArrowsScript.Instance.ImpactDetector, 0, 5);
ArrowAfterEffectImpactDetectionSlider.ValueChanged += (float value) => { ExplodingArrowsScript.Instance.ImpactDetector = value; };
ArrowAfterEffectDelaySlider = new MSlider("Effect Delay", "arroweaftereffectdelay", ExplodingArrowsScript.Instance.AfterEffectDelay, 0, 10);
ArrowAfterEffectDelaySlider.ValueChanged += (float value) => { ExplodingArrowsScript.Instance.AfterEffectDelay = value; };
ArrowAfterEffectPowerSlider = new MSlider("Effect Power", "arroweaftereffectpower", ExplodingArrowsScript.Instance.PowerMultiplierOfExplosion, 0, 20);
ArrowAfterEffectPowerSlider.ValueChanged += (float value) => { ExplodingArrowsScript.Instance.PowerMultiplierOfExplosion = value; };
ArrowAfterEffectRangeSlider = new MSlider("Effect range", "arroweaftereffectrange", ExplodingArrowsScript.Instance.RangeMultiplierOfExplosion, 0, 20);
ArrowAfterEffectRangeSlider.ValueChanged += (float value) => { ExplodingArrowsScript.Instance.RangeMultiplierOfExplosion = value; };
ArrowTrailEnabled = new MToggle("Enable Arrow Trail", "TrailEnabled", ExplodingArrowsScript.Instance.IsTrailOn);
ArrowTrailEnabled.Toggled += (bool value) => { ExplodingArrowsScript.Instance.IsTrailOn = value; ArrowTrailColor.DisplayInMapper = value; cannonBallTrailLength.DisplayInMapper = value; };
ArrowTrailColor = new MColourSlider("Trail Color", "TrailColor", ExplodingArrowsScript.Instance.TrailColor);
ArrowTrailColor.ValueChanged += (Color value) => { ExplodingArrowsScript.Instance.TrailColor = value; };
ArrowTrailLength = new MSlider("Trail Decay Rate", "TrailLength", ExplodingArrowsScript.Instance.TrailLength, 0.01f, 100);
ArrowTrailLength.ValueChanged += (float value) => { ExplodingArrowsScript.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 HasCannonSliders(BlockBehaviour block)
{
return !(block.GetBlockID() == (int)BlockType.Cannon) || block.MapperTypes.Exists(match => match.Key == "explosiontype");
}
public static bool HasArrowSliders(BlockBehaviour block)
{
return !(block.GetBlockID() == (int)BlockType.Crossbow) || block.MapperTypes.Exists(match => match.Key == "aftereffecttype");
}
/// <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 => !HasCannonSliders(block) || !HasArrowSliders(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 (!HasCannonSliders(blockbehaviour) || !HasArrowSliders(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(CannonballExplosionTypeToggle);
currentMapperTypes.Add(CannonballImpactDetectionSlider);
currentMapperTypes.Add(CannonballExplosionDelaySlider);
currentMapperTypes.Add(CannonballExplosionPowerSlider);
currentMapperTypes.Add(explosionRangeSlider);
currentMapperTypes.Add(cannonBallTrailEnabled);
currentMapperTypes.Add(cannonBallTrailColor);
currentMapperTypes.Add(cannonBallTrailLength);
mapperTypesField.SetValue(block, currentMapperTypes);
}
else if (block.GetBlockID() == (int)BlockType.Crossbow)
{
var currentMapperTypes = block.MapperTypes;
currentMapperTypes.Add((ArrowAfterEffectTypeToggle));
currentMapperTypes.Add(ArrowAfterEffectImpactDetectionSlider);
currentMapperTypes.Add(ArrowAfterEffectDelaySlider);
currentMapperTypes.Add(ArrowAfterEffectPowerSlider);
currentMapperTypes.Add(ArrowAfterEffectRangeSlider);
currentMapperTypes.Add(ArrowTrailEnabled);
currentMapperTypes.Add(ArrowTrailColor);
currentMapperTypes.Add(ArrowTrailLength);
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.CannonballExplosionTypeToggle.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.CannonballImpactDetectionSlider.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.CannonballExplosionDelaySlider.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.CannonballExplosionPowerSlider.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, the file /Mods/Resources/CannonTrailTexture.png might not exist."; }
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 = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 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 = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 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("Decay Rate", 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("Decay Rate", TrailLength);
Configuration.Save();
}
}
public class ExplodingArrowsScript : SingleInstance<ExplodingArrowsScript>
{
public int TypeOfAfterEffect = 1;
public float AfterEffectDelay = 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 Arrow Mod";
void Start()
{
Commands.RegisterCommand("ChangeArrowEffectType", (args, notUses) =>
{
try
{
TypeOfAfterEffect = int.Parse(args[0]);
TypeOfAfterEffect = Mathf.Clamp(TypeOfAfterEffect, 0, 3);
Exploding_CannonBall_Mod.CannonballExplosionTypeToggle.Value = TypeOfAfterEffect;
}
catch { return "Wrong Option! There are four options: \n 0-No After Effect \n 1-Fire \n 2-Kinetic \n 3-Grenade \n Example: ChangeArrowEffectType 2"; }
return "Complete!";
}, "Change the after effect type of arrows");
Commands.RegisterCommand("ChangeArrowImpactDetection", (args, notUses) =>
{
try
{
ImpactDetector = float.Parse(args[0]);
Exploding_CannonBall_Mod.CannonballImpactDetectionSlider.Value = ImpactDetector;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change detection of impact for cannonballs");
Commands.RegisterCommand("ChangeArrowAfterEffectDelay", (args, notUses) =>
{
try
{
AfterEffectDelay = float.Parse(args[0]);
AfterEffectDelay = Mathf.Clamp(AfterEffectDelay, 0, Mathf.Infinity);
Exploding_CannonBall_Mod.CannonballExplosionDelaySlider.Value = AfterEffectDelay;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the explosion delay after cannonballs collide");
Commands.RegisterCommand("ChangeArrowAfterEffectPower", (args, notUses) =>
{
try
{
PowerMultiplierOfExplosion = float.Parse(args[0]);
PowerMultiplierOfExplosion = Mathf.Clamp(PowerMultiplierOfExplosion, 0, Mathf.Infinity);
Exploding_CannonBall_Mod.CannonballExplosionPowerSlider.Value = PowerMultiplierOfExplosion;
}
catch { return "Wrong Input"; }
return "Complete!";
}, "Change the explosion power.");
Commands.RegisterCommand("ChangeArrowTrailColor", (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("ChangeArrowTrailTexture", (args, notUses) =>
{
try
{
WWW tex = new WWW("File:///" + Application.dataPath + "/Mods/Resources/ArrowTrailTexture.png");
TrailTexture = tex.texture;
}
catch { return "Wrong Input, the file /Mods/Resources/ArrowTrailTexture.png might not exist."; }
return "Complete!";
}, "Change the texture of the cannon trail.");
Commands.RegisterCommand("ChangeArrowTrailShader", (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("PHYSICS GOAL/CrossbowBolt(Clone)");
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CrossbowBoltEdited(Clone)";
if (IsTrailOn)
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.07f;
tr.endWidth = 0.07f;
tr.material = new Material(Shader.Find("Particles/Additive"));
tr.probeAnchor = go.transform;
tr.material.SetColor("_TintColor", TrailColor);
tr.time = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfAfterEffect != 0)
{
go.AddComponent<AfterEffectsForArrows>();
}
}
}
}
private void FixedUpdate()
{
GameObject go = GameObject.Find("PHYSICS GOAL/CrossbowBolt(Clone)");
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CrossbowBoltEdited(Clone)";
if (IsTrailOn && !go.GetComponent<TrailRenderer>())
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.07f;
tr.endWidth = 0.07f;
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 = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfAfterEffect != 0)
{
go.AddComponent<AfterEffectsForArrows>();
}
}
}
}
internal void LoadConfiguration()
{
TypeOfAfterEffect = Mathf.Clamp(Configuration.GetInt("Arrow After Effect Type", TypeOfAfterEffect), 0, 3);
AfterEffectDelay = Configuration.GetFloat("Arrow After Effect Delay", AfterEffectDelay);
PowerMultiplierOfExplosion = Configuration.GetFloat("Arrow After Effect Power", PowerMultiplierOfExplosion);
RangeMultiplierOfExplosion = Configuration.GetFloat("Arrow After Effect Range", RangeMultiplierOfExplosion);
ImpactDetector = Configuration.GetFloat("Arrow Impact Detection", ImpactDetector);
IsTrailOn = Configuration.GetBool("Arrow Trail Enabled", IsTrailOn);
TrailColor = new Color(
Configuration.GetFloat("Arrow Trail Color R", TrailColor.r),
Configuration.GetFloat("Arrow Trail Color G", TrailColor.g),
Configuration.GetFloat("Arrow Trail Color B", TrailColor.b)
);
TrailLength = Configuration.GetFloat("Arrow Trail Decay Rate", TrailLength);
}
internal void SaveConfiguration()
{
Configuration.SetInt("Arrow After Effect Type", TypeOfAfterEffect);
Configuration.SetFloat("Arrow After Effect Delay", AfterEffectDelay);
Configuration.SetFloat("Arrow After Effect Power", PowerMultiplierOfExplosion);
Configuration.SetFloat("Arrow After Effect Range", RangeMultiplierOfExplosion);
Configuration.SetFloat("Arrow Impact Detection", ImpactDetector);
Configuration.SetBool("Arrow Trail Enabled", IsTrailOn);
Configuration.SetFloat("Arrow Trail Color R", TrailColor.r);
Configuration.SetFloat("Arrow Trail Color G", TrailColor.g);
Configuration.SetFloat("Arrow Trail Color B", TrailColor.b);
Configuration.SetFloat("Arrow Trail Decay Rate", TrailLength);
Configuration.Save();
}
}
public class ExplodingBowScript : BlockScript
{
public int TypeOfAfterEffect = 1;
public float AfterEffectDelay = 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 CrossBowBlock CBB;
public GameObject Copy;
private void Start()
{
CBB = this.GetComponent<CrossBowBlock>();
Copy = GameObject.Instantiate(CBB.projectile);
Copy.SetActive(false);
}
private void Update()
{
GameObject go = CBB.projectile;
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CrossbowBoltEdited(Clone)";
if (IsTrailOn)
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.07f;
tr.endWidth = 0.07f;
tr.material = new Material(Shader.Find("Particles/Additive"));
tr.probeAnchor = go.transform;
tr.material.SetColor("_TintColor", TrailColor);
tr.time = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfAfterEffect != 0)
{
go.AddComponent<AfterEffectsForArrows>();
}
}
}
}
private void FixedUpdate()
{
GameObject go = GameObject.Find("PHYSICS GOAL/CrossbowBolt(Clone)");
if (go != null)
{
if (go.GetComponent<Rigidbody>().velocity.sqrMagnitude > 10 || !IsTrailOn)
{
go.name = "CrossbowBoltEdited(Clone)";
if (IsTrailOn && !go.GetComponent<TrailRenderer>())
{
TrailRenderer tr = go.AddComponent<TrailRenderer>();
tr.startWidth = 0.07f;
tr.endWidth = 0.07f;
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 = (go.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (TrailLength + 0.0001f);
tr.autodestruct = false;
}
if (TypeOfAfterEffect != 0)
{
go.AddComponent<AfterEffectsForArrows>();
}
}
}
}
}
public class ExplosionForCannonballs : MonoBehaviour
{
public ExplodingCannonballScript ECS;
private float 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;
CountDownExplode = ECS.ExplosionDelay * 100;
}
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 AfterEffectsForArrows : MonoBehaviour
{
public ExplodingArrowsScript EAS;
public FireTag FT;
private float CountDownExplode;
private bool Exploding = false;
private int FrameCount = 0;
private Collider coooll;
IEnumerator AE()
{
if (Exploding)
{
while (CountDownExplode >= 0 && EAS.TypeOfAfterEffect == 3)
{
yield return new WaitForFixedUpdate();
--CountDownExplode;
StartCoroutine(AE());
yield break;
}
if (EAS.TypeOfAfterEffect == 3)
{
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 * EAS.RangeMultiplierOfExplosion;
ac.power = 1500 * EAS.PowerMultiplierOfExplosion;
ac.randomDelay = 0.00001f;
ac.upPower = 0f;
ac.StartCoroutine_Auto(ac.Explode());
explo.AddComponent<TimedSelfDestruct>();
Destroy(this);
this.transform.localScale = Vector3.zero;
}
}
}
void Start()
{
EAS = ExplodingArrowsScript.Instance;
FT = GetComponent<FireTag>();
this.transform.localScale = Vector3.one;
CountDownExplode = EAS.AfterEffectDelay * 100;
if (EAS.TypeOfAfterEffect == 1)
{
FT.WaterHit();
FT.fireControllerCode.fireProgress = 0;
FT.fireControllerCode.onFire = false;
FT.burning = false;
}
TheForce();
}
void Update()
{
if (Exploding) { return; }
if (!Exploding)
CountDownExplode = (int)(EAS.AfterEffectDelay * 100);
if (!FT.burning)
{
if (EAS.TypeOfAfterEffect == 1)
{
this.FT.Ignite();
}
}
}
void FixedUpdate()
{
if (Exploding) { return; }
++FrameCount;
TrailRenderer TR = this.GetComponent<TrailRenderer>();
if (TR != null) TR.time = (this.GetComponent<Rigidbody>().velocity.magnitude + 0.0001f) / (EAS.TrailLength + 0.0001f);
if (!FT.burning)
{
if (EAS.TypeOfAfterEffect == 1)
{
this.FT.Ignite();
}
}
}
void TheForce()
{
if (EAS.TypeOfAfterEffect == 2)
{
this.GetComponent<ProjectileScript>().impactForceMultiplier *= EAS.PowerMultiplierOfExplosion;
}
}
void OnTriggerEnter(Collider coll)
{
if (EAS.TypeOfAfterEffect == 1)
{
Destroy(this);
return;
}
if (coll.attachedRigidbody)
{
if ((!Exploding || (coll.attachedRigidbody.velocity - this.GetComponent<Rigidbody>().velocity).sqrMagnitude > EAS.ImpactDetector * EAS.ImpactDetector) && FrameCount > 2)
{
Exploding = true;
coooll = coll;
StartCoroutine(AE());
}
}
else
{
if ((!Exploding || (Vector3.zero - this.GetComponent<Rigidbody>().velocity).sqrMagnitude > EAS.ImpactDetector * EAS.ImpactDetector) && FrameCount > 2)
{
Exploding = true;
coooll = coll;
StartCoroutine(AE());
}
}
}
//void OnCollisionStay(Collision coll)
//{
// if ((!Exploding || coll.relativeVelocity.sqrMagnitude > EAS.ImpactDetector * EAS.ImpactDetector) && FrameCount > 2)
// {
// Exploding = true;
// coooll = coll;
// StartCoroutine(AE());