You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Driftology/Assets/Scripts/NOSController.cs

115 lines
3.2 KiB
C#

using Fusion;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Rendering.PostProcessing; // if your MotionBlur comes from the Post-Process stack
public class NOSController : NetworkBehaviour
{
// 1) This value is now network-synchronized
[Networked] public float CurrentNOS { get; set; }
[SerializeField] float chargeSpeed = 1f;
[SerializeField] float depletionSpeed = 1f;
[SerializeField] float boostAcceleration = 40f;
[SerializeField] float audioVolume = 0.5f;
[SerializeField] AudioSource boostAudio = null;
public bool _isBoosting { get; private set; }
// cached references
VehicleController _vehicle;
MotionBlur _motionBlur;
Image _boostBarFill;
Button _boostButton;
public override void Spawned()
{
// grab your VehicleController
_vehicle = GetComponent<VehicleController>();
// find motion-blur on the main camera (if you have one)
_motionBlur = Camera.main?.GetComponent<MotionBlur>();
if (Object.HasInputAuthority)
{
// only the local player wires up UI
_boostBarFill = GameUIManager.Instance.boostBarFill;
_boostButton = GameUIManager.Instance.boostButton;
_boostButton.onClick.RemoveAllListeners();
_boostButton.onClick.AddListener(() => ActivateNOS(true));
}
// configure audio once
if (boostAudio != null)
{
boostAudio.playOnAwake = false;
boostAudio.loop = true;
boostAudio.volume = audioVolume;
}
}
public override void FixedUpdateNetwork()
{
// only run logic on the player who owns this object
if (!Object.HasInputAuthority)
return;
// 2) Charge when not boosting
if (!_vehicle.nosActive)
{
CurrentNOS = Mathf.Min(100f,
CurrentNOS + Runner.DeltaTime * (chargeSpeed * _vehicle.LocalVelocity.z / 30f)
);
}
else // 3) Deplete when boosting
{
CurrentNOS = Mathf.Max(0f,
CurrentNOS - Runner.DeltaTime * depletionSpeed
);
if (CurrentNOS <= 0f)
ActivateNOS(false);
}
// 4) Update the UI bar
if (_boostBarFill != null)
_boostBarFill.fillAmount = CurrentNOS / 100f;
// 5) Play/stop the boost SFX
if (boostAudio != null)
{
if (_vehicle.nosActive && CurrentNOS > 0f)
{
if (!boostAudio.isPlaying) boostAudio.Play();
}
else
{
if (boostAudio.isPlaying) boostAudio.Stop();
}
}
// 6) Toggle motion blur
if (_motionBlur != null)
_motionBlur.enabled = (_vehicle.nosActive && CurrentNOS > 0f);
}
/// <summary>
/// Called by your UI button (via Spawned) to start/stop boost.
/// </summary>
// Update ActivateNOS method:
void ActivateNOS(bool active)
{
_isBoosting = active;
if (active)
{
if (CurrentNOS <= 0f) return;
_vehicle.ActivateNOS(true, boostAcceleration);
}
else
{
_vehicle.ActivateNOS(false);
}
}
}