-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightController.cs
901 lines (722 loc) · 33.1 KB
/
LightController.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
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
using System;
using System.Linq;
using System.Collections;
using HarmonyLib;
using UnityEngine;
using Object = UnityEngine.Object;
using BepInEx.Configuration;
using static CircletExtended.CircletExtended;
using System.Collections.Generic;
namespace CircletExtended
{
[RequireComponent(typeof(Light))]
public class DvergerLightController : MonoBehaviour
{
private ZNetView m_nview;
private Light m_frontLight;
private Light m_spotLight;
private Player m_playerAttached;
private ItemDrop.ItemData m_item;
private int m_zdoIndex;
private GameObject m_visual;
private LightState m_state = new LightState();
private ParticleSystemForceField m_overloadDemister;
private int m_maxLevel = 3;
private float m_minAngle = 30f;
private float m_maxAngle = 110f;
private float m_minIntensity = 1.4f;
private float m_maxIntensity = 2.2f;
private float m_minRange = 45f;
private float m_maxRange = 15f;
private float m_pointIntensity = 1.1f;
private float m_pointRange = 10f;
private int m_overloadCharges = 50;
private MeshRenderer m_gemRenderer;
private Color m_gemColor;
const int intensityIncrement = 10;
const int intensityFactorMax = 150;
const int intensityFactorMin = 50;
const float overloadIntensityInterval = 2f;
const float overloadIntensityMin = 0.5f;
const float overloadIntensityMax = 4f;
private static int s_rayMaskSolids = 0;
private static int s_rayMaskCharacters = 0;
private static readonly MaterialPropertyBlock s_matBlock = new MaterialPropertyBlock();
public static GameObject overloadEffect;
public const string overloadItemName = "circletExtendedOverload";
public static int overloadItemHash = overloadItemName.GetStableHashCode();
public static Color overloadColor;
public static int demisterEffectHash = "Demister".GetStableHashCode();
public static GameObject demisterForceField;
public const string forceFieldDemisterName = "Particle System Force Field";
public static int s_lightMaskNonPlayer;
private float m_updateVisualTimer = 0f;
private static readonly List<DvergerLightController> Instances = new List<DvergerLightController>();
private static readonly Dictionary<ItemDrop.ItemData, LightState> itemState = new Dictionary<ItemDrop.ItemData, LightState>();
const int c_characterLayer = 9;
public static readonly int s_stateHash = "circletStateHash".GetStableHashCode();
public static readonly int s_state = "circletState".GetStableHashCode();
[Serializable]
private class LightState
{
public bool on = true;
public int level = 2;
public int intensity = 100;
public Color color = new Color(0.25f, 0.38f, 0.37f);
public bool shadows = true;
public bool spot = false;
public bool overloaded = false;
public float overload = 1f;
public bool demister = false;
public int quality = 1;
public bool demisted = false;
public float demistedrange = 1f;
public bool forceoff = false;
[NonSerialized]
public int hash = 0;
[NonSerialized]
public string json = "";
public void UpdateHash()
{
json = JsonUtility.ToJson(this);
hash = json.GetStableHashCode();
}
}
private void Awake()
{
m_nview = GetComponentInParent<ZNetView>();
m_gemRenderer = GetComponentInChildren<MeshRenderer>();
if (s_lightMaskNonPlayer == 0)
s_lightMaskNonPlayer = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
}
private void Start()
{
GetSpotLight();
m_visual = m_playerAttached?.GetVisual();
UpdateVisualLayers();
}
void FixedUpdate()
{
if (m_updateVisualTimer > 0)
{
m_updateVisualTimer = Mathf.Max(0f, m_updateVisualTimer - Time.fixedDeltaTime);
if (m_updateVisualTimer == 0f)
UpdateVisualLayers();
}
}
void OnEnable()
{
Instances.Add(this);
if (m_item != null && !itemState.ContainsKey(m_item))
itemState.Add(m_item, m_state);
}
void OnDisable()
{
Instances.Remove(this);
if (m_item != null)
itemState.Remove(m_item);
}
private void ApplyGemColor(Color gemColor)
{
if (m_gemRenderer == null)
return;
if (m_gemColor == gemColor)
return;
m_gemColor = gemColor;
m_gemRenderer.GetPropertyBlock(s_matBlock, 0);
s_matBlock.SetColor("_EmissionColor", m_gemColor);
m_gemRenderer.SetPropertyBlock(s_matBlock, 0);
}
private void Update()
{
if (!modEnabled.Value)
return;
Player player = Player.m_localPlayer;
if (player != null && player == m_playerAttached && player.TakeInput() && StateChanged(player))
SaveState(showMessage:true);
UpdateLights();
}
private void OnDestroy()
{
if (!modEnabled.Value)
return;
Player player = m_playerAttached;
if (player != null && m_state.demister && player.GetSEMan().GetStatusEffect(demisterEffectHash) != null
&& (player.m_utilityItem == null || player.m_utilityItem.m_shared.m_name != "$item_demister"))
player.GetSEMan().RemoveStatusEffect(demisterEffectHash);
}
private bool QualityLevelAvailable(int quality)
{
return !getFeaturesByUpgrade.Value || m_state.quality >= quality;
}
private bool ForceOff()
{
return disableOnSleep.Value && m_playerAttached != null && m_playerAttached.InBed();
}
private bool StateChanged(Player player)
{
if (m_item != null)
m_state.quality = m_item.m_quality;
if (m_state.forceoff != ForceOff())
{
m_state.forceoff = ForceOff();
LogInfo($"Force off {(m_state.forceoff ? "enabled" : "disabled")}");
return true;
}
foreach (int hotkey in hotkeys)
{
if (IsShortcutDown(hotkey, toggleShortcut))
{
m_state.on = !m_state.on;
LogInfo($"Toggle {(m_state.on ? "on" : "off")}");
return true;
}
else if (QualityLevelAvailable(2) && IsShortcutDown(hotkey, toggleSpotShortcut))
{
m_state.spot = !m_state.spot;
LogInfo($"Toggle spot {(m_state.spot ? "on" : "off")}");
return true;
}
else if (enableDemister.Value && QualityLevelAvailable(4) && IsShortcutDown(hotkey, toggleDemisterShortcut))
{
m_state.demister = !m_state.demister;
LogInfo($"Toggle demister {(m_state.demister ? "on" : "off")}");
return true;
}
if (!m_state.on)
continue;
if (m_state.level > 0 && IsShortcutDown(hotkey, widenShortcut))
{
m_state.level--;
LogInfo($"Widen {m_state.level}");
return true;
}
else if (m_state.level < m_maxLevel && IsShortcutDown(hotkey, narrowShortcut))
{
m_state.level++;
LogInfo($"Narrow {m_state.level}");
return true;
}
else if (m_state.intensity < intensityFactorMax && IsShortcutDown(hotkey, increaseIntensityShortcut))
{
m_state.intensity = Mathf.Clamp(m_state.intensity + intensityIncrement, intensityFactorMin, intensityFactorMax);
LogInfo($"Increase intensity {m_state.intensity}%");
return true;
}
else if (m_state.intensity > intensityFactorMin && IsShortcutDown(hotkey, decreaseIntensityShortcut))
{
m_state.intensity = Mathf.Clamp(m_state.intensity - intensityIncrement, intensityFactorMin, intensityFactorMax);
LogInfo($"Decrease intensity {m_state.intensity}%");
return true;
}
else if (IsShortcutDown(hotkey, toggleShadowsShortcut))
{
m_state.shadows = !m_state.shadows;
LogInfo($"Toggle shadows {(m_state.shadows ? "on" : "off")}");
return true;
}
else if (enableOverload.Value && QualityLevelAvailable(3) && IsShortcutDown(hotkey, overloadShortcut))
{
LogInfo("Overload");
if (m_state.overloaded)
MessageHud.instance.ShowMessage(MessageHud.MessageType.Center, "$item_helmet_dverger: $hud_powernotready");
else
ApplyOverloadEffect(player);
}
}
return false;
}
private void ApplyOverloadEffect(Player player)
{
if (m_item == null)
return;
if (s_rayMaskSolids == 0)
{
s_rayMaskSolids = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "vehicle");
s_rayMaskCharacters = LayerMask.GetMask("character", "character_net", "character_ghost", "character_noenv");
}
ZNetScene.instance.SpawnObject(player.transform.position, m_frontLight.transform.rotation, overloadEffect);
if (player.InWater())
{
player.AddLightningDamage(5f);
player.AddStaggerDamage(player.GetStaggerTreshold() + 1f, -player.transform.forward);
}
StartCoroutine(OverloadIntensity());
if (enableOverloadDemister.Value && m_overloadDemister != null && overloadDemisterTime.Value != 0f)
StartCoroutine(OverloadDemister());
float radius = (float)Math.Tan((m_frontLight.spotAngle / 2) * (Math.PI / 180)) * m_frontLight.range;
RaycastHit[] array = Physics.SphereCastAll(m_frontLight.transform.position, radius, m_frontLight.transform.forward, m_frontLight.range, s_rayMaskCharacters);
if (array.Length != 0)
{
for (int i = 0; i < array.Length; i++)
{
RaycastHit hit = array[i];
GameObject gameObject = ((UnityEngine.Object)(object)hit.collider) ? Projectile.FindHitObject(hit.collider) : null;
bool hitCharacter = false;
IDestructible destructible = gameObject ? gameObject.GetComponent<IDestructible>() : null;
if (destructible != null)
{
hitCharacter = (destructible is Character);
if (!IsValidTarget(destructible))
continue;
}
if (!hitCharacter)
continue;
Character character = destructible as Character;
Vector3 charPos = character.m_head != null ? character.GetHeadPoint() : character.GetTopPoint();
Vector3 vector = charPos - m_frontLight.transform.position;
bool angleCheck = Vector3.Angle(m_frontLight.transform.forward, vector.normalized) <= (m_frontLight.spotAngle + 5) / 2;
float distance = Utils.DistanceXZ(m_frontLight.transform.position, charPos);
if (distance <= 5f || angleCheck && distance <= m_frontLight.range && !Physics.Linecast(m_frontLight.transform.position, charPos, s_rayMaskSolids))
character.AddStaggerDamage(character.GetStaggerTreshold() + 1f, vector.normalized);
}
}
m_item.m_shared.m_useDurability = true;
float cost = m_item.GetMaxDurability() / m_overloadCharges;
m_item.m_durability = Mathf.Max(0f, m_item.m_durability - cost);
}
public IEnumerator OverloadIntensity()
{
m_state.overloaded = true;
m_state.overload = overloadIntensityMax;
float increment = ((overloadIntensityMax - overloadIntensityMin) / overloadIntensityInterval) * Time.fixedDeltaTime;
while (m_state.overload > overloadIntensityMin)
{
m_state.overload -= increment;
SaveState();
yield return new WaitForFixedUpdate();
}
yield return new WaitForSeconds(1f);
while (m_state.overload <= 1.05f)
{
m_state.overload += increment;
SaveState();
yield return new WaitForFixedUpdate();
}
yield return new WaitForSeconds(1f);
while (m_state.overload >= 1f)
{
m_state.overload -= increment;
SaveState();
yield return new WaitForFixedUpdate();
}
m_state.overload = 1f;
m_state.overloaded = false;
SaveState();
}
public IEnumerator OverloadDemister()
{
m_state.demisted = true;
SaveState();
for (int i = 0; i < overloadDemisterTime.Value; i++)
{
m_state.demistedrange = 10f + (overloadDemisterRange.Value - 10f) * (1f - i / overloadDemisterTime.Value);
yield return new WaitForSeconds(1f);
}
m_state.demisted = false;
SaveState();
}
public bool IsValidTarget(IDestructible destr)
{
Character character = destr as Character;
if ((bool)character)
{
if (character == m_playerAttached)
return false;
if (m_playerAttached != null)
{
bool flag = BaseAI.IsEnemy(m_playerAttached, character) || ((bool)character.GetBaseAI() && character.GetBaseAI().IsAggravatable() && m_playerAttached.IsPlayer());
if (!m_playerAttached.IsPlayer() && !flag)
return false;
if (m_playerAttached.IsPlayer() && !m_playerAttached.IsPVPEnabled() && !flag)
return false;
}
if (character.IsDodgeInvincible())
return false;
}
return true;
}
private bool IsShortcutDown(int hotkey, ConfigEntry<KeyboardShortcut> shortcut)
{
return hotkey == shortcut.Definition.GetHashCode() && (shortcut.Value.IsDown() || Input.GetKeyDown(shortcut.Value.MainKey) && !shortcut.Value.Modifiers.Any());
}
private void ShowMessage()
{
if (MessageHud.instance != null && m_playerAttached != null && Player.m_localPlayer == m_playerAttached)
{
MessageHud.instance.m_msgQeue.Clear();
MessageHud.instance.m_msgQueueTimer = 1f;
if (!m_state.forceoff && m_state.on)
MessageHud.instance.ShowMessage(MessageHud.MessageType.TopLeft, $"$item_helmet_dverger: $msg_level {m_state.level} ({m_state.intensity}%)");
else
MessageHud.instance.ShowMessage(MessageHud.MessageType.TopLeft, "$item_helmet_dverger: $hud_off");
}
}
public void Initialize(Light light, Player player = null, ItemDrop.ItemData item = null, int zdoIndex = 0)
{
m_frontLight = light;
m_playerAttached = player;
m_item = item;
m_zdoIndex = zdoIndex;
if (enableOverloadDemister.Value && player && (bool)demisterForceField && !m_overloadDemister)
{
GameObject demister = Instantiate(demisterForceField, transform);
demister.name = forceFieldDemisterName;
demister.SetActive(false);
m_overloadDemister = demister.GetComponent<ParticleSystemForceField>();
m_overloadDemister.endRange = overloadDemisterRange.Value;
}
LoadState();
if (m_item != null && !itemState.ContainsKey(m_item))
itemState.Add(m_item, m_state);
}
private void LoadState()
{
if (IsStateLoaded())
SetQuality();
if (m_state.overloaded || m_state.demisted || m_nview != null && m_nview.IsValid() && m_nview.IsOwner() && m_nview.GetZDO().GetInt(s_stateHash, 0) == 0)
{
m_state.overloaded = false;
m_state.demisted = false;
SaveState();
}
}
private bool IsStateLoaded()
{
string stateJSON = "";
if (m_item != null)
{
stateJSON = m_item.m_customData.GetValueSafe(customDataKey);
if (String.IsNullOrWhiteSpace(stateJSON))
{
m_state = new LightState();
m_state.quality = m_item.m_quality;
SaveState();
}
LogInfo($"Loading state from item: {stateJSON}");
}
if (String.IsNullOrWhiteSpace(stateJSON) && m_nview != null && m_nview.IsValid())
{
ZDO zdo = m_nview.GetZDO();
ItemDrop.ItemData item = new ItemDrop.ItemData();
ItemDrop.LoadFromZDO(item, zdo);
stateJSON = item.m_customData.GetValueSafe(customDataKey);
LogInfo($"Loading state from item zdo: {stateJSON}");
if (String.IsNullOrWhiteSpace(stateJSON))
{
ItemDrop.LoadFromZDO(m_zdoIndex, item, zdo);
stateJSON = item.m_customData.GetValueSafe(customDataKey);
LogInfo($"Loading state from zdo index {m_zdoIndex}: {stateJSON}");
}
if (String.IsNullOrWhiteSpace(stateJSON))
{
stateJSON = zdo.GetString(s_state);
LogInfo($"Loading state from zdo state: {stateJSON}");
}
}
if (!String.IsNullOrWhiteSpace(stateJSON))
{
try
{
m_state = JsonUtility.FromJson<LightState>(stateJSON);
}
catch (Exception e)
{
LogInfo($"State parsing error:\n{e}");
return false;
}
}
return true;
}
private void SaveState(bool showMessage = false)
{
m_state.color = circletColor.Value;
m_state.UpdateHash();
if (m_item != null)
m_item.m_customData[customDataKey] = m_state.json;
if (m_nview != null && m_nview.IsValid() && m_nview.IsOwner())
{
m_nview.GetZDO().Set(s_stateHash, m_state.hash);
m_nview.GetZDO().Set(s_state, m_state.json);
}
if (showMessage)
ShowMessage();
m_state.hash = 0;
}
private void SetQuality()
{
m_maxLevel = Mathf.Clamp(GetMaxSteps(m_state.quality), 1, 50);
m_minAngle = Mathf.Clamp(GetMinAngle(m_state.quality), 1, 360);
m_maxAngle = Mathf.Clamp(GetMaxAngle(m_state.quality), 1, 360);
m_minIntensity = Mathf.Clamp(GetMaxIntensity(m_state.quality), 0, 10);
m_maxIntensity = Mathf.Clamp(GetMinIntensity(m_state.quality), 0, 10);
m_minRange = Mathf.Clamp(GetMinRange(m_state.quality), 0, 1000);
m_maxRange = Mathf.Clamp(GetMaxRange(m_state.quality), 0, 1000);
m_pointIntensity = Mathf.Clamp(GetPointIntensity(m_state.quality), 0, 10);
m_pointRange = Mathf.Clamp(GetPointRange(m_state.quality), 0, 1000);
m_overloadCharges = overloadChargesPerLevel.Value * (QualityLevelAvailable(4) ? 2 : 1);
}
private void UpdateLights()
{
bool stateChanged = m_nview != null && m_nview.IsValid() && m_nview.GetZDO().GetInt(s_stateHash, 0) != m_state.hash && IsStateLoaded();
if (!stateChanged && m_state.hash != 0)
return;
m_state.UpdateHash();
if (m_state.level == m_maxLevel)
{
m_frontLight.type = LightType.Point;
m_frontLight.intensity = m_pointIntensity;
m_frontLight.range = m_pointRange;
}
else
{
float t = (float) m_state.level / Math.Max(m_maxLevel - 1, 1);
m_frontLight.type = LightType.Spot;
m_frontLight.spotAngle = Mathf.Lerp(m_minAngle, m_maxAngle, t);
m_frontLight.intensity = Mathf.Lerp(m_minIntensity, m_maxIntensity, t);
m_frontLight.range = Mathf.Lerp(m_minRange, m_maxRange, t);
}
m_frontLight.color = m_state.overload == 1f ? m_state.color : Color.Lerp(m_state.color, overloadColor, (m_state.overload - 1.05f) / (overloadIntensityMax - overloadIntensityMin));
float intensityFactor = m_state.intensity / 100f;
m_frontLight.range *= intensityFactor;
m_frontLight.intensity *= intensityFactor;
m_frontLight.intensity *= m_state.overload;
float qualityFactor = 1f + m_state.quality * 0.05f;
m_frontLight.range *= qualityFactor;
m_frontLight.intensity *= qualityFactor;
m_frontLight.enabled = !m_state.forceoff && m_state.on;
m_frontLight.shadows = enableShadows.Value && m_state.shadows && m_state.level != m_maxLevel ? LightShadows.Soft : LightShadows.None;
m_frontLight.shadowStrength = 1f - m_state.level * 0.1f;
if (m_spotLight != null)
{
m_spotLight.enabled = !m_state.forceoff && m_state.spot;
m_spotLight.intensity = m_state.overload;
m_spotLight.color = m_frontLight.color;
m_spotLight.shadows = enableShadows.Value && m_state.shadows && m_state.level != m_maxLevel ? LightShadows.Soft : LightShadows.None;
m_spotLight.shadowStrength = m_playerAttached != null && m_playerAttached.InInterior() ? 0.9f : 0.8f;
m_spotLight.cullingMask = s_lightMaskNonPlayer;
}
if (m_playerAttached != null && m_nview != null && m_nview.IsValid() && QualityLevelAvailable(4) && (m_playerAttached.m_utilityItem == null || m_playerAttached.m_utilityItem.m_shared.m_name != "$item_demister"))
{
SEMan seman = m_playerAttached.GetSEMan();
if (m_state.demister && seman.GetStatusEffect(demisterEffectHash) == null)
seman.AddStatusEffect(demisterEffectHash);
else if (!m_state.demister && seman.GetStatusEffect(demisterEffectHash) != null)
seman.RemoveStatusEffect(demisterEffectHash);
}
if ((bool)m_overloadDemister)
{
m_overloadDemister.endRange = m_state.demistedrange;
m_overloadDemister.gameObject.SetActive(m_state.demisted);
}
ApplyGemColor(m_state.color);
}
private void GetSpotLight()
{
if (m_spotLight != null)
return;
foreach (Light light in m_frontLight.GetComponentsInParent<Light>())
if (light != m_frontLight)
m_spotLight = light;
}
private void UpdateVisualLayers()
{
if (m_visual == null)
return;
for (int i = 0; i < m_visual.transform.childCount; i++)
{
Transform child = m_visual.transform.GetChild(i);
if (child.gameObject.layer == c_characterLayer)
continue;
child.gameObject.layer = c_characterLayer;
Transform[] children = child.GetComponentsInChildren<Transform>(includeInactive: true);
foreach (Transform chld in children)
chld.gameObject.layer = c_characterLayer;
}
}
private void StartUpdateVisualLayers()
{
m_updateVisualTimer = 0.5f;
}
internal static void UpdateVisualsLayers(GameObject visual)
{
foreach (DvergerLightController instance in Instances)
if (instance.m_visual == visual)
instance.StartUpdateVisualLayers();
}
public static bool IsCircletLightEnabled(ItemDrop.ItemData item)
{
return item != null && itemState.ContainsKey(item) && itemState[item].on;
}
public static void RegisterEffects()
{
demisterForceField = ObjectDB.instance.GetItemPrefab("Demister")?.transform.Find(forceFieldDemisterName)?.gameObject;
if (!(bool)overloadEffect)
{
Incinerator incinerator = Resources.FindObjectsOfTypeAll<Incinerator>().FirstOrDefault();
overloadEffect = InitPrefabClone(incinerator.m_lightingAOEs, overloadItemName);
for (int i = overloadEffect.transform.childCount - 1; i > 0; i--)
{
Transform child = overloadEffect.transform.GetChild(i);
switch (child.name)
{
case "AOE_ROD":
case "AOE_AREA":
case "Lighting":
case "lightning":
case "Lighting_rod":
case "Sparcs":
case "sfx_shockwave (1)":
case "poff_ring":
case "shockwave":
case "vfx_RockHit (1)":
child.parent = null;
UnityEngine.Object.Destroy(child.gameObject);
continue;
case "glow":
child.gameObject.SetActive(true);
continue;
case "Point light (1)":
overloadColor = child.GetComponent<Light>().color;
continue;
}
}
}
if ((bool)overloadEffect && ZNetScene.instance && !ZNetScene.instance.m_namedPrefabs.ContainsKey(overloadItemHash))
{
ZNetScene.instance.m_prefabs.Add(overloadEffect);
ZNetScene.instance.m_namedPrefabs.Add(overloadItemHash, overloadEffect);
}
}
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.AttachItem))]
public static class VisEquipment_AttachItem_HumanoidAttachment
{
private static void Postfix(VisEquipment __instance, GameObject __result, int itemHash)
{
if (!modEnabled.Value)
return;
if (itemHash != CircletItem.itemHashHelmetDverger || __result == null)
return;
DvergerLightController component = __result.GetComponent<DvergerLightController>();
if (component != null)
Object.Destroy(component);
Light[] lights = __result.GetComponentsInChildren<Light>();
if (lights.Length == 0)
return;
ItemDrop.ItemData item = null;
Player player = null;
if (__instance.m_isPlayer)
{
player = __instance.GetComponentInParent<Player>();
if (player == null)
return;
item = player.GetCirclet();
if (item != null)
CircletItem.PatchCircletItemData(item);
}
__instance.UpdateVisuals();
__result.AddComponent<DvergerLightController>().Initialize(lights[0], player, item);
}
}
[HarmonyPatch(typeof(ItemDrop), nameof(ItemDrop.Start))]
public static class ItemDrop_Start_ItemDropAttachment
{
private static void Postfix(ItemDrop __instance)
{
if (!modEnabled.Value)
return;
if (!visualStateItemDrop.Value)
return;
if (__instance.GetPrefabName(__instance.name) != CircletItem.itemNameHelmetDverger)
return;
DvergerLightController component = __instance.GetComponentInChildren<DvergerLightController>();
if (component != null)
Object.Destroy(component);
Light[] lights = __instance.GetComponentsInChildren<Light>();
if (lights.Length == 0)
return;
lights[0].gameObject.transform.parent.gameObject.AddComponent<DvergerLightController>().Initialize(lights[0], item:__instance.m_itemData);
}
}
[HarmonyPatch(typeof(ItemStand), nameof(ItemStand.SetVisualItem))]
public static class ItemStand_SetVisualItem_ItemStandAttachment
{
private static void Prefix(ItemStand __instance, GameObject ___m_visualItem, string ___m_visualName, int ___m_visualVariant, string itemName, int variant, ref bool __state)
{
if (!modEnabled.Value)
return;
if (!visualStateItemStand.Value)
return;
if (__instance.GetAttachedItem() != CircletItem.itemNameHelmetDverger)
return;
if (___m_visualItem != null)
return;
if (___m_visualName == itemName && ___m_visualVariant == variant)
return;
__state = true;
}
private static void Postfix(GameObject ___m_visualItem, bool __state)
{
if (!modEnabled.Value)
return;
if (!__state)
return;
DvergerLightController component = ___m_visualItem.GetComponentInChildren<DvergerLightController>();
if (component != null)
Object.Destroy(component);
Light[] lights = ___m_visualItem.GetComponentsInChildren<Light>();
if (lights.Length == 0)
return;
___m_visualItem.AddComponent<DvergerLightController>().Initialize(lights[0]);
}
}
[HarmonyPatch(typeof(ArmorStand), nameof(ArmorStand.SetVisualItem))]
public static class ArmorStand_SetVisualItem_ArmorStandAttachment
{
private static void Prefix(int index, List<ArmorStand.ArmorStandSlot> ___m_slots, string itemName, int variant, ref bool __state)
{
if (!modEnabled.Value)
return;
if (!visualStateArmorStand.Value)
return;
if (itemName != CircletItem.itemNameHelmetDverger)
return;
ArmorStand.ArmorStandSlot armorStandSlot = ___m_slots[index];
if (armorStandSlot.m_slot != VisSlot.Helmet)
return;
if (armorStandSlot.m_visualName == itemName && armorStandSlot.m_visualVariant == variant)
return;
__state = true;
}
private static void Postfix(int index, VisEquipment ___m_visEquipment, List<ArmorStand.ArmorStandSlot> ___m_slots, bool __state)
{
if (!modEnabled.Value)
return;
if (!__state)
return;
___m_visEquipment.UpdateVisuals();
ArmorStand.ArmorStandSlot armorStandSlot = ___m_slots[index];
if (armorStandSlot.m_visualName != CircletItem.itemNameHelmetDverger)
return;
GameObject visualItem = ___m_visEquipment.m_helmetItemInstance;
if (visualItem == null)
return;
DvergerLightController component = visualItem.GetComponentInChildren<DvergerLightController>();
if (component != null)
Object.Destroy(component);
Light[] lights = visualItem.GetComponentsInChildren<Light>();
if (lights.Length == 0)
return;
visualItem.AddComponent<DvergerLightController>().Initialize(lights[0], zdoIndex:index);
}
}
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.SetupEquipment))]
public static class Humanoid_SetupVisEquipment_AttachLayersFix
{
private static void Postfix(Humanoid __instance)
{
DvergerLightController.UpdateVisualsLayers(__instance.m_visual);
}
}
}