ClientServer/Client/Assets/Scripts/Player/UnderWaterTimer.cs
TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

65 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
//thomas09
//all logic for this is handled on the server, this is just a visual for the player.
public class UnderWaterTimer : MonoBehaviour
{
[SerializeField] Image underWaterTimer;
[System.NonSerialized] public float MaxDuration;
private float timer = 0;
private void OnEnable()
{
transform.localScale = Vector3.zero;
timer = 0;
underWaterTimer.fillAmount = 1;
transform.DOScale(Vector3.one, 0.15f).SetEase(Ease.InOutBack);
}
private void Update()
{
if (Player.Instance.IsAlive())
{
//underWaterTimer.fillAmount = 1f - Mathf.Clamp01(timer / MaxDuration); //use this intead if you want smooth timer.
float clampedTime = Mathf.Clamp01(timer / MaxDuration);
float stepCountFloat = (1f - clampedTime) * MaxDuration;
float fill = Mathf.CeilToInt(stepCountFloat) / MaxDuration;
underWaterTimer.fillAmount = fill;
//Debug.Log($"Max Time is {MaxDuration}, Time Percentage is {clampedTime}, Step Count value {stepCountFloat}, final fill amount: {fill}");
timer += Time.deltaTime;
if (timer >= MaxDuration)
{
timer = MaxDuration;
}
//if the player left the water, Disable this object.
if (Player.Instance.GetPlayerFirstPersonCameraPosition().y > 22)
{
timer = 0;
Debug.ClearDeveloperConsole();
ResetState();
}
}
else
{
ResetState();
}
}
private void ResetState()
{
timer = 0;
transform.DOScale(Vector3.zero, 0.15f).OnComplete(() =>
{
underWaterTimer.fillAmount = 1;
gameObject.SetActive(false);
});
}
}