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.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/StartingTimer.cs

79 lines
2.1 KiB
C#

1 month ago
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
using Unity.Netcode;
using Unity.Multiplayer.Samples.Utilities;
using UnityEngine.Events;
using System;
1 month ago
public class StartingTimer : NetworkBehaviour
{
public ServerAdditiveSceneLoader serverAdditiveSceneLoader;
private Image BlackScreen;
public TextMeshProUGUI TimerText;
private NetworkVariable<int> timerValue = new NetworkVariable<int>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
private bool coroutineStartedBool = false;
public static event Action StartingTimerEnded;
1 month ago
private void Start()
{
BlackScreen = GetComponent<Image>();
}
private void Update()
{
if (serverAdditiveSceneLoader.m_SceneState == ServerAdditiveSceneLoader.SceneState.Loaded && coroutineStartedBool == false)
{
if (IsServer)
{
coroutineStartedBool = true;
1 month ago
StartCoroutine(ServerTimerCoroutine());
}
}
// Update the UI on all clients
UpdateTimerUI();
}
private IEnumerator ServerTimerCoroutine()
{
const int countdownStart = 3;
for (int i = countdownStart; i > 0; i--)
{
timerValue.Value = i;
yield return new WaitForSeconds(1);
}
timerValue.Value = -1; // Indicate "GO"
yield return new WaitForSeconds(0.5f);
// Notify clients to disable the black screen
2 weeks ago
1 month ago
BlackScreenAlphaDisablerClientRpc();
}
private void UpdateTimerUI()
{
if (timerValue.Value > 0)
{
TimerText.text = timerValue.Value.ToString();
}
else if (timerValue.Value == -1)
{
TimerText.text = "GO";
}
}
[ClientRpc]
private void BlackScreenAlphaDisablerClientRpc()
{
2 weeks ago
StartingTimerEnded?.Invoke();
BlackScreen.gameObject.SetActive(false);
gameObject.SetActive(false);
//BlackScreen.DOFade(0, 0.25f).OnComplete(() =>
//{
// gameObject.SetActive(false);
//});
1 month ago
}
}