-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGameTimer.cs
More file actions
128 lines (107 loc) · 3.21 KB
/
Copy pathGameTimer.cs
File metadata and controls
128 lines (107 loc) · 3.21 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace MapStudio.UI
{
/// <summary>
/// Represents a timer to control the game calculations seperate from rendering.
/// </summary>
public class GameTimer : IDisposable
{
/// <summary>
/// Detemines to pause or play the timer.
/// </summary>
public GameState TimerState = GameState.Pause;
/// <summary>
/// Determines if the current timer is being played.
/// </summary>
public bool IsPlaying => TimerState == GameState.Playing;
/// <summary>
/// A callback to run before the frame update event.
/// </summary>
public EventHandler OnFrameUpdate;
private Stopwatch stopWatch;
private System.Timers.Timer animationTimer;
private float FrameRate = 60.0f;
private float CurrentFrame;
private int timing = 0;
private bool disposed = false;
public GameTimer()
{
stopWatch = new Stopwatch();
stopWatch.Start();
animationTimer = new System.Timers.Timer()
{
Interval = (int)(1000.0f / 60.0f),
};
animationTimer.Elapsed += timer_Tick;
}
void timer_Tick(object sender, EventArgs e) {
if (disposed) return;
CurrentFrame += 1.0f;
OnFrameAdvanced();
}
/// <summary>
/// Updates the current frame rate of the timer.
/// </summary>
public void UpdateFramerate(float rate) {
FrameRate = rate;
animationTimer.Interval = (int)(1000.0f / FrameRate);
}
/// <summary>
/// Activates the timer to start playing.
/// </summary>
public void Play()
{
animationTimer.Start();
TimerState = GameState.Playing;
}
/// <summary>
/// Pauses the timer if it is currently playing.
/// </summary>
public void Pause()
{
animationTimer.Stop();
TimerState = GameState.Pause;
}
private void OnFrameAdvanced()
{
timing += (int)stopWatch.ElapsedMilliseconds;
stopWatch.Reset();
stopWatch.Start();
//Update by interval
if (timing > 16)
{
timing = timing % 16;
OnFrameUpdate?.Invoke(this, EventArgs.Empty);
OnUpdateFrame(CurrentFrame);
}
}
/// <summary>
/// Method for updating the frame during the timer playback.
/// </summary>
/// <param name="frame"></param>
protected virtual void OnUpdateFrame(float frame)
{
}
/// <summary>
/// Disposes the timer.
/// </summary>
public void Dispose()
{
animationTimer.Stop();
animationTimer.Dispose();
disposed = true;
TimerState = GameState.Pause;
stopWatch.Stop();
}
public enum GameState
{
Playing,
Pause,
}
}
}