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.
205 lines
5.8 KiB
C#
205 lines
5.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Rendering.PostProcessing; // if your MotionBlur comes from the Post-Process stack
|
|
|
|
public class NOSController : MonoBehaviour
|
|
{
|
|
[SerializeField] float CurrentNOS = 0f;
|
|
[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; }
|
|
|
|
VehicleController _vehicle;
|
|
MotionBlur _motionBlur;
|
|
Image _boostBarFill;
|
|
Button _boostButton;
|
|
|
|
private void Start()
|
|
{
|
|
_vehicle = GetComponent<VehicleController>();
|
|
_motionBlur = Camera.main?.GetComponent<MotionBlur>();
|
|
|
|
_boostBarFill = GameUIManager.Instance.boostBarFill;
|
|
_boostButton = GameUIManager.Instance.boostButton;
|
|
|
|
_boostButton.onClick.RemoveAllListeners();
|
|
_boostButton.onClick.AddListener(() => ActivateNOS(true));
|
|
|
|
if (boostAudio != null)
|
|
{
|
|
boostAudio.playOnAwake = false;
|
|
boostAudio.loop = true;
|
|
boostAudio.volume = audioVolume;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (_vehicle == null) return;
|
|
|
|
if (!_vehicle.nosActive)
|
|
{
|
|
CurrentNOS = Mathf.Min(100f, CurrentNOS + Time.fixedDeltaTime * (chargeSpeed * _vehicle.LocalVelocity.z / 30f));
|
|
}
|
|
else
|
|
{
|
|
CurrentNOS = Mathf.Max(0f, CurrentNOS - Time.fixedDeltaTime * depletionSpeed);
|
|
|
|
if (CurrentNOS <= 0f)
|
|
ActivateNOS(false);
|
|
}
|
|
|
|
if (_boostBarFill != null)
|
|
_boostBarFill.fillAmount = CurrentNOS / 100f;
|
|
|
|
if (boostAudio != null)
|
|
{
|
|
if (_vehicle.nosActive && CurrentNOS > 0f)
|
|
{
|
|
if (!boostAudio.isPlaying) boostAudio.Play();
|
|
}
|
|
else
|
|
{
|
|
if (boostAudio.isPlaying) boostAudio.Stop();
|
|
}
|
|
}
|
|
|
|
if (_motionBlur != null)
|
|
_motionBlur.enabled = (_vehicle.nosActive && CurrentNOS > 0f);
|
|
}
|
|
|
|
void ActivateNOS(bool active)
|
|
{
|
|
_isBoosting = active;
|
|
|
|
if (active)
|
|
{
|
|
if (CurrentNOS <= 0f) return;
|
|
_vehicle.ActivateNOS(true, boostAcceleration);
|
|
}
|
|
else
|
|
{
|
|
_vehicle.ActivateNOS(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//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);
|
|
// }
|
|
// }
|
|
//}
|