-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeClock.cs
118 lines (98 loc) · 2.37 KB
/
TimeClock.cs
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
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace HiddenSwitch.Multiplayer
{
/// <summary>
/// A default time or frame based clock.
/// </summary>
public class TimeClock : IClock
{
public event Action<int> Tick;
public event Action<int> LateTick;
protected int m_elapsedFrameCount;
public virtual long ElapsedTicks {
get {
return m_timeClockHelper.ElapsedTicks;
}
}
public virtual int ElapsedFrameCount { get { return m_elapsedFrameCount + StartFrame; } }
public virtual int FramesPerSecond { get; protected set; }
public virtual bool EndOfFrame { get; protected set; }
public virtual int StartFrame { get; set; }
internal TimeClockHelper m_timeClockHelper;
public TimeClock ()
{
}
public TimeClock (int framesPerSecond, bool autostart = true, bool endOfFrame = false, int startFrame = 0)
{
FramesPerSecond = framesPerSecond;
EndOfFrame = endOfFrame;
StartFrame = startFrame;
var timer = new GameObject ("Timer Helper");
m_timeClockHelper = timer.AddComponent<TimeClockHelper> ();
m_timeClockHelper.timeClock = this;
m_timeClockHelper.framesPerSecond = framesPerSecond;
m_timeClockHelper.endOfFrame = endOfFrame;
if (autostart) {
m_timeClockHelper.running = true;
}
}
internal virtual void HelperTick ()
{
m_elapsedFrameCount++;
if (Tick != null) {
Tick (ElapsedFrameCount);
}
if (LateTick != null) {
LateTick (ElapsedFrameCount);
}
}
internal virtual void HelperFrameTick ()
{
m_elapsedFrameCount++;
if (Tick != null) {
Tick (ElapsedFrameCount);
}
}
internal virtual void HelperLateFrameTick ()
{
if (LateTick != null) {
LateTick (ElapsedFrameCount);
}
}
protected virtual void OnHelperTick ()
{
HelperTick ();
}
/// <summary>
/// Stops the timer.
/// </summary>
public virtual void Stop ()
{
m_timeClockHelper.running = false;
}
/// <summary>
/// Starts the timer.
/// </summary>
public virtual void Start ()
{
m_timeClockHelper.running = true;
}
/// <summary>
/// Is the timer running?
/// </summary>
/// <value><c>true</c> if running; otherwise, <c>false</c>.</value>
public virtual bool Running {
get {
return m_timeClockHelper.running;
}
set {
m_timeClockHelper.running = value;
}
}
}
}