-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAchievementSystem.cpp
382 lines (336 loc) · 11.7 KB
/
AchievementSystem.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
Penjin is Copyright (c)2005, 2006, 2007, 2008, 2009, 2010 Kevin Winfield-Pantoja
This file is part of Penjin.
Penjin is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Penjin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Penjin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AchievementSystem.h"
///------------------------------
/// Constructor / Destructor
///------------------------------
AchievementSystem* AchievementSystem::m_self = NULL;
AchievementSystem::AchievementSystem()
{
offset = Vector2di(0,0);
achievementSize = Vector2di(DEFAULT_ACHIEVEMENT_WIDTH,DEFAULT_ACHIEVEMENT_HEIGHT);
popupSize = achievementSize;
spacing = 0;
popup = Vector2di(0,0);
fadeTime = DEFAULT_FADE_TIME;
showTime = DEFAULT_SHOW_TIME;
showTop = false;
showPopups = true;
popupTimer.start();
achievementFile = "achieve.ach";
colours[coFontColour] = DEFAULT_FONT_COLOUR;
colours[coHeadlineColour] = DEFAULT_HEADLINE_COLOUR;
colours[coBackgroundColour] = DEFAULT_BACKGROUND_COLOUR;
colours[coProgressColour] = DEFAULT_PROGRESS_COLOUR;
secretText[0] = DEFAULT_SECRET_HEADLINE;
secretText[1] = DEFAULT_SECRET_DESCRIPTION;
}
AchievementSystem::~AchievementSystem()
{
vector<Achievement*>::iterator I;
for (I = achievements.begin(); I < achievements.end(); ++I)
delete (*I);
vector<Event*>::iterator K;
for (K = log.begin(); K < log.end(); ++K)
delete (*K);
}
AchievementSystem* AchievementSystem::GetSingleton()
{
if ( m_self == NULL)
m_self = new AchievementSystem();
return m_self;
}
///------------------------------
/// Public
///------------------------------
void AchievementSystem::logEventSpecial(CRstring name, vector<SpecialProperty>* special, CRint count)
{
bool found = false;
vector<Event*>::iterator I;
// goes through the vector of already logged events and increases count if found
for (I = log.begin(); I < log.end() && not found; ++I)
{
if ((*I)->name == name && (*I)->checkSpecial(special))
{
found = true;
(*I)->count += count;
}
}
// if not found, logged event is added to vector
if (not found)
{
Event* ev = new Event(name,special,count,0,0);
log.push_back(ev);
}
}
void AchievementSystem::setPopupPosition(PopupPos pos)
{
SDL_Surface* screen = SDL_GetVideoSurface();
if (pos == ppTOPLEFT || pos == ppTOPCENTER || pos == ppTOPRIGHT)
{
popup.y = 0;
showTop = true;
}
else
{
popup.y = screen->h - popupSize.y;
showTop = false;
}
if (pos == ppTOPLEFT || pos == ppBOTTOMLEFT)
popup.x = 0;
else if (pos == ppTOPCENTER || pos == ppBOTTOMCENTER)
popup.x = round((screen->w - popupSize.x) / 2);
else
popup.x = screen->w - popupSize.x;
}
int AchievementSystem::unlockedCount() const
{
vector<Achievement*>::const_iterator I;
int count = 0;
for (I = achievements.begin(); I < achievements.end(); ++I)
{
if ((**I).getStatus())
++count;
}
return count;
}
float AchievementSystem::getListSize() const
{
SDL_Surface* screen = SDL_GetVideoSurface();
return (float)(screen->h - offset.y + spacing) / (float)(achievementSize.y + spacing);
}
#ifdef PENJIN_SDL
void AchievementSystem::render(SDL_Surface* screen)
{
// Render achievement popups
vector<Popup>::iterator I;
int count = 0;
for (I = popups.begin(); I < popups.end() && showPopups; ++I)
{
int diff = popupTimer.getScaledTicks() - I->time;
I->a->setSize(popupSize);
if (showTop)
{
// fade in
if (diff <= fadeTime)
I->a->setPosition(popup.x, popup.y + (popupSize.y * count) - (popupSize.y * (count + 1) * (fadeTime - diff) / fadeTime) );
// show
else if (diff <= (fadeTime + showTime))
I->a->setPosition(popup.x,popup.y + (popupSize.y * count));
// fade out
else
I->a->setPosition(popup.x, popup.y + (popupSize.y * count) - (popupSize.y * (count + 1) * (diff - (fadeTime + showTime)) / fadeTime));
}
else
{
// fade in
if (diff <= fadeTime)
I->a->setPosition(popup.x, popup.y - (popupSize.y * count) + (popupSize.y * (count + 1) * (fadeTime - diff) / fadeTime) );
// show
else if (diff <= (fadeTime + showTime))
I->a->setPosition(popup.x,popup.y - (popupSize.y * count));
// fade out
else
I->a->setPosition(popup.x, popup.y - (popupSize.y * count) + (popupSize.y * (count + 1) * (diff - (fadeTime + showTime)) / fadeTime));
}
I->a->render(screen);
I->a->setSize(achievementSize);
count++;
}
}
void AchievementSystem::renderList(SDL_Surface* screen, float numOffset)
{
if (floor(numOffset) < 0 || ceil(numOffset) >= achievements.size()) {
#ifdef DEBUG
cout << "[Achievements] Error: Trying to render list with out-of-bounds offset (" << numOffset << ")!" << endl;
#endif
if (floor(numOffset) < 0)
numOffset = 0;
else
numOffset = achievements.size();
}
vector<Achievement*>::iterator I;
float count = floor(numOffset) - numOffset;
for (I = achievements.begin()+floor(numOffset); I < min(achievements.begin()+ceil(numOffset+getListSize()),achievements.end()); ++I)
{
(**I).setPosition(offset.x,round(offset.y+((achievementSize.y+spacing)*count)));
(**I).render(screen);
++count;
}
}
#else
void AchievementSystem::render()
{
// Render achievement popups
vector<Popup>::iterator I;
int count = 0;
for (I = popups.begin(); I < popups.end() && showPopups; ++I)
{
int diff = popupTimer.getScaledTicks() - I->time;
I->a->setSize(popupSize);
if (showTop)
{
// fade in
if (diff <= fadeTime)
I->a->setPosition(popup.x, popup.y + (popupSize.y * count) - (popupSize.y * (count + 1) * (fadeTime - diff) / fadeTime) );
// show
else if (diff <= (fadeTime + showTime))
I->a->setPosition(popup.x,popup.y + (popupSize.y * count));
// fade out
else
I->a->setPosition(popup.x, popup.y + (popupSize.y * count) - (popupSize.y * (count + 1) * (diff - (fadeTime + showTime)) / fadeTime));
}
else
{
// fade in
if (diff <= fadeTime)
I->a->setPosition(popup.x, popup.y - (popupSize.y * count) + (popupSize.y * (count + 1) * (fadeTime - diff) / fadeTime) );
// show
else if (diff <= (fadeTime + showTime))
I->a->setPosition(popup.x,popup.y - (popupSize.y * count));
// fade out
else
I->a->setPosition(popup.x, popup.y - (popupSize.y * count) + (popupSize.y * (count + 1) * (diff - (fadeTime + showTime)) / fadeTime));
}
I->a->render();
I->a->setSize(achievementSize);
count++;
}
}
void AchievementSystem::renderList(float numOffset)
{
if (floor(numOffset) < 0 || ceil(numOffset) >= achievements.size()) {
#ifdef DEBUG
cout << "[Achievements] Error: Trying to render list with out-of-bounds offset (" << numOffset << ")!" << endl;
#endif
if (floor(numOffset) < 0)
numOffset = 0;
else
numOffset = achievements.size();
}
vector<Achievement*>::iterator I;
float count = floor(numOffset) - numOffset;
for (I = achievements.begin()+floor(numOffset); I < min(achievements.begin()+ceil(numOffset+getListSize()),achievements.end()); ++I)
{
(**I).setPosition(offset.x,round(offset.y+((achievementSize.y+spacing)*count)));
(**I).render();
++count;
}
}
#endif
void AchievementSystem::update()
{
vector<Achievement*>::iterator I;
vector<int> unlocked;
// compares all achievements with log and checks for updates
if (log.size() > 0)
{
int index = 0;
for (I = achievements.begin(); I < achievements.end(); ++I)
{
if (not (**I).getStatus() && (**I).check(log))
{
save();
Popup temp = {(*I),popupTimer.getScaledTicks()};
popups.push_back(temp);
unlocked.push_back(index);
}
++index;
}
}
// clear the log
vector<Event*>::iterator L;
for (L = log.begin(); L < log.end(); ++L)
delete *(L);
log.clear();
// if anything was unlocked, log an event for that, too
vector<int>::iterator M;
for (M = unlocked.begin(); M < unlocked.end(); ++M)
{
vector<SpecialProperty>* prop = new vector<SpecialProperty>;
prop->push_back(SpecialProperty("INDEX",(*M),coEQUAL));
logEventSpecial("ACHIEVEMENT_UNLOCK",prop);
}
// removes old popups
vector<Popup>::iterator K;
for (K = popups.begin(); K < popups.end(); ++K)
{
if (K->time <= popupTimer.getScaledTicks() - (2*fadeTime + showTime))
popups.erase(K);
}
}
PenjinErrors::PENJIN_ERRORS AchievementSystem::load(CRstring file)
{
doc.clear();
doc.load(file);
if(!doc.size())
return PENJIN_FILE_NOT_FOUND;
unsigned int i = 0;
bool changed = true;
while(i < doc.size())
{
// get a line
if(changed == false)
++i;
string line = crypt.decryptBuffer(doc.getLine(i));
vector<Achievement*>::iterator I;
// first look for the name of the achievement
changed = false;
for (I=achievements.begin(); I < achievements.end(); ++I)
{
// when we find a name which matches an achievement
string name = (*I)->getName();
if(name == line)
{
// the next line is the count
++i;
(*I)->load(crypt.decryptBuffer(doc.getLine(i)));
// move to the next line
++i;
// break out since we are finished with this achievement
changed = true;
break;
}
}
}
achievementFile = file;
return PENJIN_OK;
}
PenjinErrors::PENJIN_ERRORS AchievementSystem::save(CRstring file)
{
doc.clear();
vector<Achievement*>::iterator I;
// run through the achievements
for (I=achievements.begin(); I < achievements.end(); ++I)
{
// save the name
doc.append(crypt.encryptBuffer((*I)->getName()));
// save the count
doc.append( crypt.encryptBuffer( (*I)->save() ));
}
doc.save(file);
achievementFile = file;
return PENJIN_OK;
}
PenjinErrors::PENJIN_ERRORS AchievementSystem::eraseData()
{
if (remove(achievementFile.c_str()) == 0)
return PENJIN_OK;
else
return PENJIN_FILE_NOT_FOUND;
}
///------------------------------
/// Private
///------------------------------