Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class VirtualPlayerBattleRoyaleSabers : IVirtualPlayerBody {

public class Pool : MemoryPool<IVirtualPlayer, VirtualPlayerBattleRoyaleSabers> {
public override void OnCreated(VirtualPlayerBattleRoyaleSabers item) {
item.Initialize();
item.Initialize(Container);
}

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

#region Setup

private void Initialize() {
private void Initialize(DiContainer container) {
_leftHandTransform = Object.Instantiate(BundleLoader.SaberPrefab, null, false).transform;
_rightHandTransform = Object.Instantiate(BundleLoader.SaberPrefab, null, false).transform;

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

_leftHand.Init(container);
_rightHand.Init(container);

_leftHand.CoreIntensity = 1f;
_rightHand.CoreIntensity = 1f;
}
Expand Down Expand Up @@ -59,8 +62,8 @@ private static void ApplyControllerSettings(
BattleRoyaleVRController controller,
BattleRoyaleBodySettings basicBodySettings
) {
controller.TrailEnabled = basicBodySettings.TrailEnabled;
controller.TrailLength = basicBodySettings.TrailLength;
controller.TrailEnabled = basicBodySettings.TrailEnabled;
controller.TrailOpacity = basicBodySettings.TrailOpacity;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
using Zenject;

namespace BeatLeader {
internal abstract class BattleRoyaleVRController : VRController {
#region Injection
[Inject]
TimeHelper timeHelper;

#endregion

#region Unity Events

private void Awake() {
gameObject.SetActive(false);
_propertyBlock = new MaterialPropertyBlock();
_saberTrail = gameObject.AddComponent<SaberTrail>();
_saberTrail._trailRendererPrefab = InstantiateTrailRenderer();
_saberTrail._trailRendererPrefab = TrailRendererPrefab;
_saberTrail._trailDuration = 0.4f;
_saberTrail._whiteSectionMaxDuration = 0.001f;
_saberTrail._samplingFrequency = 120;
_saberTrail._granularity = 45;
_saberTrail.Setup(coreColor, _movementData);
gameObject.SetActive(true);

// Disabling so the trail won't initialize until turned back on
_saberTrail.enabled = false;
}

private new void Update() {
Expand All @@ -40,11 +34,9 @@ private void OnValidate() {

#region Properties

[SerializeField]
private Color coreColor = Color.red;
[SerializeField] private Color coreColor = Color.red;

[SerializeField]
private float coreIntensity = 1f;
[SerializeField] private float coreIntensity = 1f;

public Color CoreColor {
get => coreColor;
Expand All @@ -69,11 +61,25 @@ public float CoreIntensity {

#region Trail

private bool _initialized;

public void Init(DiContainer container) {
container.Inject(_saberTrail);
// Will be initialized later if not yet turned on
if (TrailEnabled) {
_saberTrail.enabled = true;
}

_initialized = true;
}

public float TrailLength {
get => _saberTrail._trailDuration;
set {
_saberTrail._trailDuration = value;
_saberTrail.Init();
if (_saberTrail._trailRenderer != null) {
_saberTrail.Init();
}
}
}

Expand All @@ -87,8 +93,15 @@ public float TrailOpacity {
}

public bool TrailEnabled {
get => _saberTrail.enabled;
set => _saberTrail.enabled = value;
get;
set {
if (_initialized) {
// SaberTrail disables the renderer automatically
_saberTrail.enabled = value;
}

field = value;
}
}

private readonly SaberMovementData _movementData = new();
Expand All @@ -100,11 +113,20 @@ private void UpdateMovementData() {
_movementData.AddNewData(topPos, bottomPos, TimeHelper.GetShaderTimeValue());
}

private static SaberTrailRenderer InstantiateTrailRenderer() {
var prefab = Resources
.FindObjectsOfTypeAll<SaberTrailRenderer>()
.First(x => x.name == "SaberTrailRenderer");
return Instantiate(prefab);
private static SaberTrailRenderer? TrailRendererPrefab {
get {
if (field == null) {
var prefab = Resources
.FindObjectsOfTypeAll<SaberTrailRenderer>()
.First(x => x.name == "SaberTrailRenderer");

// We create a renamed prefab to avoid shenanigans
field = Instantiate(prefab);
field.name = "BattleRoyaleTrailRenderer";
}

return field;
}
}

#endregion
Expand Down
Loading