FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Gopher-In-Unity-Simulation/Assets/Scripts/Unity/Timer.cs at main · hiro-wpi/Gopher-In-Unity-Simulation · GitHub
hiro-wpi
Gopher-In-Unity-Simulation
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Gopher-In-Unity-Simulation
/
Assets
/
Scripts
/
Unity
/
Timer.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
57 lines (52 loc) · 1.66 KB
Breadcrumbs
Gopher-In-Unity-Simulation
/
Assets
/
Scripts
/
Unity
/
Timer.cs
Copy path
File metadata and controls
57 lines (52 loc) · 1.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
UnityEngine
;
using
UnityEngine
.
Assertions
;
/// <summary>
/// A Timer class used to control functions' process rate.
/// Serve as a similar function as InvokeRepeating(),
/// but allow enable/disable at run time.
///
/// UpdateTimer() should be called in each loop with specified deltaTime.
/// ShouldProcess could be accessed and modified by other scripts.
/// </summary>
public
class
Timer
{
// Rate
private
float
processPeriod
;
// Elapsed time
private
float
timer
;
private
float
maxTimerScale
;
// Result flag
public
bool
ShouldProcess
{
get
;
set
;
}
=
false
;
public
Timer
(
int
publishRate
,
float
maxTimerScale
=
10.0f
)
{
// Rate check
Assert
.
IsTrue
(
publishRate
>
0
)
;
processPeriod
=
1.0f
/
publishRate
;
this
.
maxTimerScale
=
maxTimerScale
;
}
public
void
UpdateTimer
(
float
deltaTime
)
{
// The ShouldProcess is NOT initialized to false.
// It would remain the same as input.
// This is to ensure that
// the true flag could remain until it gets processed.
timer
+=
deltaTime
;
// Check if time to publish
if
(
timer
>=
processPeriod
)
{
ShouldProcess
=
true
;
// Using "-=" instead of "= 0" to ensure
// consistent publish rate
timer
-=
processPeriod
;
}
// A safe mechanism
// As "-=" was used to calculate elapsedTime previously,
// reset elapsedTime in case it somehow gets too large
if
(
timer
>
processPeriod
*
maxTimerScale
)
{
timer
=
0.0f
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL