-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathshop.cpp
More file actions
342 lines (324 loc) · 19.7 KB
/
shop.cpp
File metadata and controls
342 lines (324 loc) · 19.7 KB
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
#include "shop.h"
#include "global.h"
#include <QApplication>
#include <iostream>
using namespace std;
Shop::Shop()
{
selectedShopItem_ = 0;
timerId_ = startTimer(10);
const int tier1LevelRequirement = 0;
const int tier2LevelRequirement = 2;
const int tier3LevelRequirement = 3;
const int tier4LevelRequirement = 4;
const int tier5LevelRequirement = 5;
unlocked_[0] = (level >= tier1LevelRequirement);
unlocked_[1] = (level >= tier1LevelRequirement);
unlocked_[2] = (level >= tier1LevelRequirement);
unlocked_[3] = (level >= tier1LevelRequirement);
unlocked_[4] = (level >= tier2LevelRequirement);
unlocked_[5] = (level >= tier2LevelRequirement);
unlocked_[6] = (level >= tier2LevelRequirement);
unlocked_[7] = (level >= tier2LevelRequirement);
unlocked_[8] = (level >= tier3LevelRequirement);
unlocked_[9] = (level >= tier3LevelRequirement);
unlocked_[10] = (level >= tier3LevelRequirement);
unlocked_[11] = (level >= tier3LevelRequirement);
unlocked_[12] = (level >= tier4LevelRequirement);
unlocked_[13] = (level >= tier4LevelRequirement);
unlocked_[14] = (level >= tier4LevelRequirement);
unlocked_[15] = (level >= tier4LevelRequirement);
unlocked_[16] = (level >= tier5LevelRequirement);
unlocked_[17] = (level >= tier5LevelRequirement);
unlocked_[18] = (level >= tier5LevelRequirement);
unlocked_[19] = (level >= tier5LevelRequirement);
costs_[0] = 75; // shield
costs_[1] = 100; // bullet 25mm
costs_[2] = 175; // max shield
costs_[3] = 220; // gatling guns
costs_[4] = 360; // laser rifle
costs_[5] = 130; // red laser
costs_[6] = 170; // yellow laser
costs_[7] = 220; // blue laser
costs_[8] = 380; // missile launcher
costs_[9] = 310; // hydra missile
costs_[10] = 1250; // mini nuke
costs_[11] = 165; // area of effect
costs_[12] = 540; // plasma cannon
costs_[13] = 390; // plasma spike
costs_[14] = 1420; // plasma bomb
costs_[15] = 5000; // autorepair
costs_[16] = 730; // fission battery
costs_[17] = 600; // bubble shield
costs_[18] = 2330; // tesla coil
costs_[19] = 2770; // railgun
}
Shop::~Shop()
{
}
void Shop::timerEvent(QTimerEvent *event)
{
repaint();
}
void Shop::paintEvent(QPaintEvent *event)
{
int const offset = 110;
QPainter painter(this);
QImage shopTitle;
QImage shopItem;
QImage shopItemSelection;
QImage tierTitle;
int tierTitle_x, tierTitle_y;
int shopItem_x, shopItem_y;
int stats_x, stats_y;
int itemIndex;
int tierLevelRequirement;
QFont statsFont("Courier New", 10, QFont::DemiBold);
QColor notEnoughCredits = qRgb(255, 0, 0);
notEnoughCredits.setAlpha(75);
QColor outOfStock = qRgb(36, 36, 36);
outOfStock.setAlpha(200);
// Title
shopTitle.load(":img/title_thundershop.png");
painter.drawImage((largeWindowWidth-shopTitle.rect().width())/2, shopTitle.rect().height()/2, shopTitle);
shopItemSelection.load(":img/buy_selection.png");
// TIER 1
tierTitle.load(":img/title_tier1.png");
tierTitle_x = (windowWidth-375)/2; tierTitle_y = shopTitle.rect().height()*2;
painter.drawImage(tierTitle_x, tierTitle_y, tierTitle);
itemIndex = 0;
shopItem.load(":img/buy_shield.png");
shopItem_x = (windowWidth-390)/2; shopItem_y = shopTitle.rect().height()*3;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
shopItem.load(":img/buy_bullet25mm.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
shopItem.load(":img/buy_maxShield.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
shopItem.load(":img/buy_gatlingGuns.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
// TIER 2
tierTitle.load(":img/title_tier2.png");
tierTitle_x += offset;
painter.drawImage(tierTitle_x, tierTitle_y, tierTitle);
shopItem_x += offset; shopItem_y = shopTitle.rect().height()*3;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_laserRifle.png");
else shopItem.load(":img/buy_locked.png");
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[4]) shopItem.load(":img/buy_redLaser.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[4]) shopItem.load(":img/buy_yellowLaser.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[4]) shopItem.load(":img/buy_blueLaser.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
// TIER 3
tierTitle.load(":img/title_tier3.png");
tierTitle_x += offset;
painter.drawImage(tierTitle_x, tierTitle_y, tierTitle);
shopItem_x += offset; shopItem_y = shopTitle.rect().height()*3;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_missileLauncher.png");
else shopItem.load(":img/buy_locked.png");
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[8]) shopItem.load(":img/buy_hydraMissile.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[8]) shopItem.load(":img/buy_miniNuke.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_areaOfEffect.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
// TIER 4
tierTitle.load(":img/title_tier4.png");
tierTitle_x += offset;
painter.drawImage(tierTitle_x, tierTitle_y, tierTitle);
shopItem_x += offset; shopItem_y = shopTitle.rect().height()*3;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_plasmaCannon.png");
else shopItem.load(":img/buy_locked.png");
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[12]) shopItem.load(":img/buy_plasmaSpike.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[12]) shopItem.load(":img/buy_plasmaBomb.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_autoRepair.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
// TIER 5
tierTitle.load(":img/title_tier5.png");
tierTitle_x += offset;
painter.drawImage(tierTitle_x, tierTitle_y, tierTitle);
shopItem_x += offset; shopItem_y = shopTitle.rect().height()*3;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_fissionBattery.png");
else shopItem.load(":img/buy_locked.png");
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex]) shopItem.load(":img/buy_bubbleShield.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[16]) shopItem.load(":img/buy_teslaCoil.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
if(unlocked_[itemIndex] && hasItem[16]) shopItem.load(":img/buy_railgun.png");
else shopItem.load(":img/buy_locked.png");
shopItem_y += offset;
painter.drawImage(shopItem_x, shopItem_y, shopItem);
if(hasItem[itemIndex]) { painter.setBrush(outOfStock); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
else if(credits < costs_[itemIndex]) { painter.setBrush(notEnoughCredits); painter.drawRect(shopItem_x, shopItem_y, shopItem.rect().width(), shopItem.rect().height()); }
if(selectedShopItem_ == itemIndex) painter.drawImage(shopItem_x, shopItem_y, shopItemSelection);
itemIndex++;
// other info
stats_x = (windowWidth-375)/2; stats_y = shopItem_y + 1.25*offset;
painter.setFont(statsFont);
painter.setPen(Qt::white);
painter.drawText(stats_x,stats_y,200,16,Qt::AlignLeft,"Shields: " + QString::number(shield) + "/" + QString::number(maxShield));
stats_x += 200;
painter.setPen(Qt::yellow);
painter.drawText(stats_x,stats_y,200,16,Qt::AlignLeft,"Credits: " + QString::number(credits));
stats_x += 150;
painter.setPen(Qt::red);
painter.drawText(stats_x,stats_y,200,16,Qt::AlignLeft,"([TAB] to continue)");
}
void Shop::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
{
case Qt::Key_W:
if(selectedShopItem_%4 != 0) selectedShopItem_ -= 1;
break;
case Qt::Key_S:
if((selectedShopItem_+1)%4 != 0) selectedShopItem_ += 1;
break;
case Qt::Key_A:
if(selectedShopItem_ >= 4) selectedShopItem_ -= 4;
break;
case Qt::Key_D:
if(selectedShopItem_ <= 15) selectedShopItem_ += 4;
break;
case Qt::Key_Space:
buySelectedItem();
break;
case Qt::Key_Tab:
qApp->exit();
break;
case Qt::Key_Escape:
restarting = true;
qApp->exit();
break;
default:
QWidget::keyPressEvent(event);
}
}
void Shop::buySelectedItem()
{
if(unlocked_[selectedShopItem_] && credits >= costs_[selectedShopItem_]) // item is unlocked and can be bought
{
if(selectedShopItem_ == 0) // if first item, give shield
if(shield < maxShield)
if(shield <= maxShield - 50) shield += 50; // add 50 shield
else shield = maxShield; // don't let shield go above maxShield
else
return;
else if(selectedShopItem_ == 2) { maxShield += 100; shield += 100; } // increase maxShield
else if(hasItem[selectedShopItem_]) return; // prevent buying same item twice by accident
else if(selectedShopItem_ >= 5 && selectedShopItem_ <= 7 && !hasItem[4]) return; // prevent buying certain items before unlocking them
else if(selectedShopItem_ >= 9 && selectedShopItem_ <= 10 && !hasItem[8]) return; // ""
else if(selectedShopItem_ >= 13 && selectedShopItem_ <= 14 && !hasItem[12]) return; // ""
else if(selectedShopItem_ >= 18 && selectedShopItem_ <= 19 && !hasItem[16]) return; // ""
else hasItem[selectedShopItem_] = true; // buy item and set it as bought
credits -= costs_[selectedShopItem_]; // subtract cost
if(hasItem[11]) areaOfEffectRadius = 160; // upgrades
if(hasItem[16]) maxPower = 175; // ""
if(hasItem[17]) maxPower = 280; // ""
}
}