-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNetworkTimer.cs
More file actions
82 lines (69 loc) · 2.82 KB
/
NetworkTimer.cs
File metadata and controls
82 lines (69 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;
namespace Meta.Multiplayer.Networking
{
/// <summary>
/// Class representing a timer that is synchronized over the network.
/// </summary>
public class NetworkTimer : NetworkBehaviour
{
[SerializeField] private double m_transitionTriggerTime = 1.5f;
public UnityEvent<NetworkTimer> OnTimerStartedOnServer;
public UnityEvent<NetworkTimer> OnTimerExpiredOnServer;
public UnityEvent OnClientTransitionTimeReached;
private readonly NetworkVariable<double> m_duration = new(-1);
private readonly NetworkVariable<double> m_startTime = new(float.PositiveInfinity);
private bool m_transitionTriggered;
#if HAS_NAUGHTY_ATTRIBUTES
[NaughtyAttributes.ShowNativeProperty]
#endif
public double TimeRemaining => HasStarted ? Duration + m_startTime.Value - LocalTime : -1;
#if HAS_NAUGHTY_ATTRIBUTES
[NaughtyAttributes.ShowNativeProperty]
#endif
public double Duration => m_duration.Value;
public double LocalTime => NetworkObject == null ? float.NaN : NetworkManager.LocalTime.Time;
#if HAS_NAUGHTY_ATTRIBUTES
[NaughtyAttributes.ShowNativeProperty]
#endif
public bool HasStarted => LocalTime >= m_startTime.Value;
#if HAS_NAUGHTY_ATTRIBUTES
[NaughtyAttributes.ShowNativeProperty]
#endif
public bool IsCompleted => LocalTime >= m_startTime.Value + Duration;
private void Update()
{
if (HasStarted && TimeRemaining <= m_transitionTriggerTime && !m_transitionTriggered)
{
m_transitionTriggered = true;
OnClientTransitionTimeReached?.Invoke();
}
if (!IsServer || !HasStarted || !IsCompleted) { return; }
m_startTime.Value = float.PositiveInfinity;
OnTimerExpiredOnServer?.Invoke(this);
}
/// <summary>
/// Sets the timer's current duration.
/// </summary>
/// <param name="timerDurationInSeconds">The duration of the timer.</param>
public void SetTimer(double timerDurationInSeconds)
{
if (!IsServer) { return; }
// Set the duration so that the expiration time is `timerDurationInSeconds` from now,
// without modifying the start time.
m_duration.Value = LocalTime + timerDurationInSeconds - m_startTime.Value;
}
/// <summary>
/// Starts the timer.
/// </summary>
public void StartTimer(double? duration = null)
{
if (!IsServer || HasStarted) { return; }
if (duration.HasValue) { m_duration.Value = duration.Value; }
m_startTime.Value = LocalTime;
OnTimerStartedOnServer?.Invoke(this);
}
}
}