-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTimer.cpp
184 lines (151 loc) · 3.58 KB
/
GameTimer.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// GameTimer.cpp
// SchoolSafety
//
// Created by Noor Fatima on 7/10/17.
//
//
#include "GameTimer.h"
int GameTimer::timerTypeValue;
GameTimer* GameTimer::create(TimerType value)
{
timerTypeValue = static_cast<int>(value);
GameTimer *node = new (std::nothrow)GameTimer();
if(node && node->init())
{
node->autorelease();
return node;
}
// Noor chussar
CC_SAFE_DELETE(node);
return nullptr;
}
bool GameTimer::init()
{
visibleSize = HelperNode::getSize();
visibleSize.width = 1920; //design resolution width
visibleSize.height = 1080; // design resolution height
timerLabelBackground = Sprite::create(TIMER_BACKGROUND);
this->addChild(timerLabelBackground);
timerLabelBackground->setPosition(Vec2(visibleSize.width/2, visibleSize.height - 100));
HelperNode::rescale(timerLabelBackground);
timerLabel = Label::createWithTTF("0:00", "fonts/arial.ttf", 50);
timerLabelBackground->addChild(timerLabel);
timerLabel->setPosition(Vec2(timerLabelBackground->getContentSize().width/2 - 20, timerLabelBackground->getContentSize().height/2 + 10));
timerLabel->setColor(Color3B::BLACK);
timerLabel->enableBold();
HelperNode::rescale(timerLabel);
timerValue = 0;
timeComplete = false;
if(timerTypeValue == 0 )
{
secondsValue = 0;
}
else if(timerTypeValue == 1)
{
secondsValue = 29;
}
return true;
}
void GameTimer::startTimer()
{
//this->schedule(schedule_selector(GameTimer::updateTimer), 0.1);
}
void GameTimer::startForwardTimer()
{
this->schedule(schedule_selector(GameTimer::updateForwardTimer), 0.1);
}
void GameTimer::startReverseTimer()
{
this->schedule(schedule_selector(GameTimer::updateReverseTimer), 0.1);
}
void GameTimer::updateForwardTimer(float dt)
{
if(timerValue == 10)
{
timerValue = 0;
secondsValue = secondsValue + 1;
}
timerValue = timerValue + 1;
totalTimeValue = totalTimeValue + 1;
char c[32];
sprintf(c, "%d", secondsValue);
v = static_cast<std::string>(c);
v.append(":");
char d[32];
sprintf(d, "%d", timerValue);
std::string w = static_cast<std::string>(d);
if(timerValue <= 9)
v.append("0");
v.append(w);
timerLabel->setString(v);
}
std::string GameTimer::getTimerStringValue()
{
return v;
}
void GameTimer::updateReverseTimer(float dt)
{
if(timerValue == 10)
{
timerValue = 0;
secondsValue = secondsValue - 1;
}
if(secondsValue == 0 )
timeComplete = true;
timerValue = timerValue + 1;
char c[32];
sprintf(c, "%d", secondsValue);
//timerLabel->setString(c);
std::string v = static_cast<std::string>(c);
v.append(":");
char d[32];
sprintf(d, "%d", timerValue);
std::string w = static_cast<std::string>(d);
if(timerValue <= 9)
v.append("0");
v.append(w);
timerLabel->setString(v);
}
//void GameTimer::updateTimer(float dt)
//{
// if(timerValue == 10)
// {
// timerValue = 0;
// secondsValue = secondsValue + 1;
//
// }
// timerValue = timerValue + 1;
//
// char c[32];
// sprintf(c, "%d", secondsValue);
// //timerLabel->setString(c);
// std::string v = static_cast<std::string>(c);
// v.append(":");
// char d[32];
// sprintf(d, "%d", timerValue);
// std::string w = static_cast<std::string>(d);
// if(timerValue <= 9)
// v.append("0");
// v.append(w);
// timerLabel->setString(v);
//
//
// if(secondsValue > 29)
// timeComplete = true;
//}
bool GameTimer::isTimeComplete()
{
return timeComplete;
}
void GameTimer::stopTimer()
{
this->unschedule(schedule_selector(GameTimer::updateReverseTimer));
this->unschedule(schedule_selector(GameTimer::updateForwardTimer));
}
void GameTimer::resetAll()
{
timerValue = 0;
secondsValue = 0;
timeComplete = false;
}