-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathachievements.cpp
232 lines (218 loc) · 6.44 KB
/
achievements.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
#include "achievements.h"
#include <QPainter>
#define LAST_TIME 100
#define ANIM_TIME 20
#define MAX_X 0.7578
#define MAX_Y 0.2
extern Statistic statistic;
Achievements::Achievements() :
count(0)
{
}
void Achievements::paint(QPainter *painter, int width, int height)
{
if (!items.isEmpty())
items[0]->paint(painter, width / 2, height / 2, count);
}
void Achievements::advance()
{
if (count == LAST_TIME && (!items.isEmpty()))
{
delete items[0];
items.pop_front();
count = 0;
}
if (!items.isEmpty())
{
double y;
if (count < ANIM_TIME)
y = MAX_Y * 2 * count / ANIM_TIME - MAX_Y;
else if (count > LAST_TIME - ANIM_TIME)
y = MAX_Y * 2 * (LAST_TIME - count) / ANIM_TIME - MAX_Y;
else
y = MAX_Y;
items[0]->setPos(QPointF(MAX_X, y));
++count;
}
else
count = 0;
}
int calculateLevel(AbstractAchievementItem::ItemType type, int value)
{
int count;
const int *stages;
switch (type)
{
case AbstractAchievementItem::FlameGet:
count = 3;
stages = kFlameGetStage;
break;
case AbstractAchievementItem::StarGet:
count = 3;
stages = kStarGetStage;
break;
case AbstractAchievementItem::RotateClassic:
count = 3;
stages = kRotateClassicStage;
break;
case AbstractAchievementItem::Timing:
count = 3;
stages = kTimingStage;
break;
case AbstractAchievementItem::RotatePuzzle:
return value;
break;
default:
return 0;
break;
}
for (int i = count - 1;i >= 0;--i)
if (value >= stages[i])
return i + 1;
return 0;
}
void calculateLevel(AbstractAchievementItem::ItemType type,
int lastValue,
int currentValue,
int& levelFrom,
int& levelTo)
{
levelFrom = calculateLevel(type, lastValue);
levelTo = calculateLevel(type, currentValue);
}
AbstractAchievementItem::ItemType typeConvert(Statistic::StatisticType type)
{
switch (type)
{
case Statistic::FlameGetCount:
return AbstractAchievementItem::FlameGet;
break;
case Statistic::StarGetCount:
return AbstractAchievementItem::StarGet;
break;
case Statistic::RotateClassicPoint:
return AbstractAchievementItem::RotateClassic;
break;
case Statistic::SwapTimingPoint:
case Statistic::RotateTimingPoint:
return AbstractAchievementItem::Timing;
break;
case Statistic::RotatePuzzleFinished:
return AbstractAchievementItem::RotatePuzzle;
break;
default:
return (AbstractAchievementItem::ItemType)-1;
break;
}
}
void Achievements::statisticChanged(Statistic::StatisticType type,
int lastValue,
int currentValue)
{
int levelFrom;
int levelTo;
AbstractAchievementItem::ItemType itemType = typeConvert(type);
calculateLevel(itemType, lastValue, currentValue, levelFrom, levelTo);
if (levelTo > levelFrom)
{
AbstractAchievementItem *item = getAchievementItem(itemType);
if (item != NULL)
{
item->setPos(QPointF(MAX_X, -MAX_Y));
items.push_back(item);
}
}
}
int Achievements::getAchievementLevel(AbstractAchievementItem::ItemType type,
int value)
{
if (value == -1)
{
switch (type)
{
case AbstractAchievementItem::FlameGet:
value = statistic.getStatistic(Statistic::FlameGetCount);
break;
case AbstractAchievementItem::StarGet:
value = statistic.getStatistic(Statistic::StarGetCount);
break;
case AbstractAchievementItem::RotateClassic:
value = statistic.getStatistic(Statistic::RotateClassicPoint);
break;
case AbstractAchievementItem::Timing:
value = qMax(statistic.getStatistic(Statistic::SwapTimingPoint),
statistic.getStatistic(Statistic::RotateTimingPoint));
break;
case AbstractAchievementItem::RotatePuzzle:
if (statistic.getStatistic(Statistic::RotatePuzzleFinished) == 0)
value = 0;
else if (statistic.getStatistic(Statistic::RotatePuzzleFinished) ==
statistic.getStatistic(Statistic::RotatePuzzleTotal))
value = 2;
else
value = 1;
break;
default:
return 0;
break;
}
}
return calculateLevel(type, value);
}
AbstractAchievementItem *Achievements::getAchievementItem
(AbstractAchievementItem::ItemType type, QPainter *painter)
{
AbstractAchievementItem *item = NULL;
switch (type)
{
case AbstractAchievementItem::FlameGet:
item = new FlameGetItem
(getAchievementLevel(AbstractAchievementItem::FlameGet),
statistic.getStatistic(Statistic::FlameGetCount),
painter);
break;
case AbstractAchievementItem::StarGet:
item = new StarGetItem
(getAchievementLevel(AbstractAchievementItem::StarGet),
statistic.getStatistic(Statistic::StarGetCount),
painter);
break;
case AbstractAchievementItem::RotateClassic:
item = new RotateClassicPointItem
(getAchievementLevel(AbstractAchievementItem::RotateClassic),
statistic.getStatistic(Statistic::RotateClassicPoint),
painter);
break;
case AbstractAchievementItem::Timing:
item = new TimingPointItem
(getAchievementLevel(AbstractAchievementItem::Timing),
qMax(statistic.getStatistic(Statistic::SwapTimingPoint),
statistic.getStatistic(Statistic::RotateTimingPoint)),
painter);
break;
case AbstractAchievementItem::RotatePuzzle:
item = new RotatePuzzleFinishedItem
(statistic.getStatistic(Statistic::RotatePuzzleFinished),
statistic.getStatistic(Statistic::RotatePuzzleTotal),
painter);
break;
default:
break;
}
return item;
}
QVector<AbstractAchievementItem *> Achievements::getAchievementItems(QPainter *painter)
{
QVector<AbstractAchievementItem *> result;
result.push_back(getAchievementItem(AbstractAchievementItem::FlameGet,
painter));
result.push_back(getAchievementItem(AbstractAchievementItem::StarGet,
painter));
result.push_back(getAchievementItem(AbstractAchievementItem::RotateClassic,
painter));
result.push_back(getAchievementItem(AbstractAchievementItem::Timing,
painter));
result.push_back(getAchievementItem(AbstractAchievementItem::RotatePuzzle,
painter));
return result;
}