Skip to content

Commit 066c350

Browse files
authored
Merge pull request #185 from battleroyaletrails
Fixed battle royale trails
2 parents 69c8e5b + d6e28ff commit 066c350

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

Source/2_Core/Replayer/Emulation/Players/Body/VirtualPlayerBattleRoyaleSabers.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class VirtualPlayerBattleRoyaleSabers : IVirtualPlayerBody {
99

1010
public class Pool : MemoryPool<IVirtualPlayer, VirtualPlayerBattleRoyaleSabers> {
1111
public override void OnCreated(VirtualPlayerBattleRoyaleSabers item) {
12-
item.Initialize();
12+
item.Initialize(Container);
1313
}
1414

1515
public override void Reinitialize(IVirtualPlayer player, VirtualPlayerBattleRoyaleSabers item) {
@@ -21,13 +21,16 @@ public override void Reinitialize(IVirtualPlayer player, VirtualPlayerBattleRoya
2121

2222
#region Setup
2323

24-
private void Initialize() {
24+
private void Initialize(DiContainer container) {
2525
_leftHandTransform = Object.Instantiate(BundleLoader.SaberPrefab, null, false).transform;
2626
_rightHandTransform = Object.Instantiate(BundleLoader.SaberPrefab, null, false).transform;
2727

2828
_leftHand = _leftHandTransform.GetComponent<BattleRoyaleVRController>();
2929
_rightHand = _rightHandTransform.GetComponent<BattleRoyaleVRController>();
3030

31+
_leftHand.Init(container);
32+
_rightHand.Init(container);
33+
3134
_leftHand.CoreIntensity = 1f;
3235
_rightHand.CoreIntensity = 1f;
3336
}
@@ -59,8 +62,8 @@ private static void ApplyControllerSettings(
5962
BattleRoyaleVRController controller,
6063
BattleRoyaleBodySettings basicBodySettings
6164
) {
62-
controller.TrailEnabled = basicBodySettings.TrailEnabled;
6365
controller.TrailLength = basicBodySettings.TrailLength;
66+
controller.TrailEnabled = basicBodySettings.TrailEnabled;
6467
controller.TrailOpacity = basicBodySettings.TrailOpacity;
6568
}
6669

Source/3_AssetBundle/Replayer/BattleRoyale/BattleRoyaleVRController.cs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
using System;
22
using System.Linq;
33
using UnityEngine;
4-
using UnityEngine.Serialization;
54
using Zenject;
65

76
namespace 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

Comments
 (0)