11using System ;
22using System . Linq ;
33using UnityEngine ;
4- using UnityEngine . Serialization ;
54using Zenject ;
65
76namespace BeatLeader {
87 internal abstract class BattleRoyaleVRController : VRController {
9- #region Injection
10- [ Inject ]
11- TimeHelper timeHelper ;
12-
13- #endregion
14-
158 #region Unity Events
169
1710 private void Awake ( ) {
18- gameObject . SetActive ( false ) ;
1911 _propertyBlock = new MaterialPropertyBlock ( ) ;
2012 _saberTrail = gameObject . AddComponent < SaberTrail > ( ) ;
21- _saberTrail . _trailRendererPrefab = InstantiateTrailRenderer ( ) ;
13+ _saberTrail . _trailRendererPrefab = TrailRendererPrefab ;
2214 _saberTrail . _trailDuration = 0.4f ;
2315 _saberTrail . _whiteSectionMaxDuration = 0.001f ;
2416 _saberTrail . _samplingFrequency = 120 ;
2517 _saberTrail . _granularity = 45 ;
2618 _saberTrail . Setup ( coreColor , _movementData ) ;
27- gameObject . SetActive ( true ) ;
19+
20+ // Disabling so the trail won't initialize until turned back on
21+ _saberTrail . enabled = false ;
2822 }
2923
3024 private new void Update ( ) {
@@ -40,11 +34,9 @@ private void OnValidate() {
4034
4135 #region Properties
4236
43- [ SerializeField ]
44- private Color coreColor = Color . red ;
37+ [ SerializeField ] private Color coreColor = Color . red ;
4538
46- [ SerializeField ]
47- private float coreIntensity = 1f ;
39+ [ SerializeField ] private float coreIntensity = 1f ;
4840
4941 public Color CoreColor {
5042 get => coreColor ;
@@ -69,11 +61,25 @@ public float CoreIntensity {
6961
7062 #region Trail
7163
64+ private bool _initialized ;
65+
66+ public void Init ( DiContainer container ) {
67+ container . Inject ( _saberTrail ) ;
68+ // Will be initialized later if not yet turned on
69+ if ( TrailEnabled ) {
70+ _saberTrail . enabled = true ;
71+ }
72+
73+ _initialized = true ;
74+ }
75+
7276 public float TrailLength {
7377 get => _saberTrail . _trailDuration ;
7478 set {
7579 _saberTrail . _trailDuration = value ;
76- _saberTrail . Init ( ) ;
80+ if ( _saberTrail . _trailRenderer != null ) {
81+ _saberTrail . Init ( ) ;
82+ }
7783 }
7884 }
7985
@@ -87,8 +93,15 @@ public float TrailOpacity {
8793 }
8894
8995 public bool TrailEnabled {
90- get => _saberTrail . enabled ;
91- set => _saberTrail . enabled = value ;
96+ get ;
97+ set {
98+ if ( _initialized ) {
99+ // SaberTrail disables the renderer automatically
100+ _saberTrail . enabled = value ;
101+ }
102+
103+ field = value ;
104+ }
92105 }
93106
94107 private readonly SaberMovementData _movementData = new ( ) ;
@@ -100,11 +113,20 @@ private void UpdateMovementData() {
100113 _movementData . AddNewData ( topPos , bottomPos , TimeHelper . GetShaderTimeValue ( ) ) ;
101114 }
102115
103- private static SaberTrailRenderer InstantiateTrailRenderer ( ) {
104- var prefab = Resources
105- . FindObjectsOfTypeAll < SaberTrailRenderer > ( )
106- . First ( x => x . name == "SaberTrailRenderer" ) ;
107- return Instantiate ( prefab ) ;
116+ private static SaberTrailRenderer ? TrailRendererPrefab {
117+ get {
118+ if ( field == null ) {
119+ var prefab = Resources
120+ . FindObjectsOfTypeAll < SaberTrailRenderer > ( )
121+ . First ( x => x . name == "SaberTrailRenderer" ) ;
122+
123+ // We create a renamed prefab to avoid shenanigans
124+ field = Instantiate ( prefab ) ;
125+ field . name = "BattleRoyaleTrailRenderer" ;
126+ }
127+
128+ return field ;
129+ }
108130 }
109131
110132 #endregion
0 commit comments