/
SomeClassThatNeedsATimer.cpp
57 lines (51 loc) · 2.05 KB
/
SomeClassThatNeedsATimer.cpp
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
#include "stdafx.h"
#include "SomeClassThatNeedsATimer.h"
SomeClassThatNeedsATimer::SomeClassThatNeedsATimer(){
mTimerServer;
mTimerIDMap;
mLastTimerWhen = 0;
mTimerLength = 3250;
};
SomeClassThatNeedsATimer::~SomeClassThatNeedsATimer(){
};
void SomeClassThatNeedsATimer::someFunctionThatAddsServers(TimerServer* server){
mTimerServer=server;
};
//Some function that, as part of its normal operations, gets called when a timer is done.
void SomeClassThatNeedsATimer::someFunctionThatDoesSomethingWithATimer(){
//Whatever thread-safe, lag-free code you need to run
// Note that std::cout is *slow* and will lower fps //
#ifdef _DEBUG
std::cout<<"\n5 second timer is up. "
<<"(Timer was behind by "<<(GetTickCount()-(mLastTimerWhen+mTimerLength))<<"ms.)"
<<"\n";
#endif
};
//Some function that, as part of its normal operations, needs to take some action not now, but later
//E.g. An AI function that needs to recalculate in 5 seconds, or
// A turret that can't shoot again until 10 seconds have passed.
//In the first case, the timerDone(double id) function might change something like bool needToRecalculate so
//the next AI pass actually does some calculations.
//In the second case, the timerDone(double id) function might call some resetTurretFireLocks() function,
//so that turrets can fire again. (Instead of checking for bool canFire every time, or incrementing a timer.)
void SomeClassThatNeedsATimer::someFunctionThatNeedsATimer(){
if(mTimerServer){
double ID = mTimerServer->requestTimer(mTimerLength,this); //In 3.25 seconds or longer, the server will call timerDone.
mTimerIDMap[ID]="Test";
mLastTimerWhen = GetTickCount();
// Note that std::cout is *slow* and will lower fps //
#ifdef _DEBUG
std::cout<<"Requested new timer.\n\n";
#endif
}
};
//The server calls this function to signal that the timer is done
//i.e. Server::relayTimerDone(){
// Listener->timerDone();
// }
void SomeClassThatNeedsATimer::timerDone(double id){
if(mTimerIDMap[id] == "Test"){
someFunctionThatDoesSomethingWithATimer();
someFunctionThatNeedsATimer();
}
};