forked from Facepunch/Rust.Community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunityEntity.UI.Countdown.cs
More file actions
128 lines (108 loc) · 3.8 KB
/
CommunityEntity.UI.Countdown.cs
File metadata and controls
128 lines (108 loc) · 3.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using UnityEngine;
using System;
public partial class CommunityEntity
{
private class Countdown : MonoBehaviour
{
public string command = "";
public float endTime = 0f;
public float startTime = 0f;
public float step = 1f;
public float interval = 1f;
public TimerFormat timerFormat = TimerFormat.None;
public string numberFormat = "0.####";
public bool destroyIfDone = true;
public enum TimerFormat
{
None,
SecondsHundreth,
MinutesSeconds,
MinutesSecondsHundreth,
HoursMinutes,
HoursMinutesSeconds,
HoursMinutesSecondsMilliseconds,
HoursMinutesSecondsTenths,
DaysHoursMinutes,
DaysHoursMinutesSeconds,
Custom
}
#if CLIENT
private string sign = "";
private string tempText = "";
private UnityEngine.UI.Text textComponent;
void Start()
{
// dont let the timer update more than 50 times per second, though even that is excessive
interval = Mathf.Max(interval, 0.02f);
textComponent = GetComponent<UnityEngine.UI.Text>();
if ( textComponent )
{
tempText = textComponent.text;
UpdateDisplay(startTime);
}
if ( startTime == endTime )
{
End();
}
if ( step == 0f )
{
step = 1f;
}
if ( startTime > endTime && step > 0f )
{
sign = "-";
step = 0 - step;
}
InvokeRepeating( "UpdateCountdown", interval, interval );
}
void UpdateCountdown()
{
startTime = startTime + step;
if ( textComponent )
{
UpdateDisplay(startTime);
}
if ( (sign == "-" && startTime <= endTime) || (sign == "" && startTime >= endTime) )
{
if ( textComponent )
UpdateDisplay(endTime);
if ( !string.IsNullOrEmpty( command ) )
{
ConsoleNetwork.ClientRunOnServer( command );
}
End();
}
}
void End()
{
CancelInvoke( "UpdateCountdown" );
if(!destroyIfDone) return;
CommunityEntity.ClientInstance.DestroyPanel(gameObject.name);
}
public void Reset()
{
CancelInvoke( "UpdateCountdown" );
InvokeRepeating( "UpdateCountdown", interval, interval );
}
void UpdateDisplay(float time)
{
TimeSpan t = TimeSpan.FromSeconds( time );
string formattedTime = timerFormat switch
{
TimerFormat.SecondsHundreth => t.ToString("ss\\.ff"),
TimerFormat.MinutesSeconds => t.ToString("mm\\:ss"),
TimerFormat.MinutesSecondsHundreth => t.ToString("mm\\:ss\\.ff"),
TimerFormat.HoursMinutes => $"{(int)t.TotalHours:0}:{t:mm}",
TimerFormat.HoursMinutesSeconds => $"{(int)t.TotalHours:0}:{t:mm\\:ss}",
TimerFormat.HoursMinutesSecondsMilliseconds => $"{(int)t.TotalHours:0}:{t:mm\\:ss\\:fff}",
TimerFormat.HoursMinutesSecondsTenths => $"{(int)t.TotalHours:0}:{t:mm\\:ss\\.f}",
TimerFormat.DaysHoursMinutes => $"{t:%d}.{t:hh\\:mm}",
TimerFormat.DaysHoursMinutesSeconds => $"{t:%d}.{t:hh\\:mm\\:ss}",
TimerFormat.Custom => t.ToString(numberFormat),
_ => time.ToString(numberFormat)
};
textComponent.text = tempText.Replace( "%TIME_LEFT%", formattedTime );
}
#endif
}
}