-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathshop.cpp
81 lines (76 loc) · 1.87 KB
/
shop.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
#include "shop.h"
Shop::Shop()
{
sun = 200;
counter = 0;
time = int(7.0 * 1000 / 33);
Card *card = nullptr;
for (int i = 0; i < Card::name.size(); ++i)
{
card = new Card(Card::name[i]);
card->setParentItem(this);
card->setPos(-157 + 65 * i, -2);
}
}
QRectF Shop::boundingRect() const
{
return QRectF(-270, -45, 540, 90);
}
void Shop::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawPixmap(QRect(-270, -45, 540, 90), QPixmap(":/images/Shop.png"));
QFont font;
font.setPointSizeF(15);
painter->setFont(font);
painter->drawText(QRectF(-255, 18, 65, 22), Qt::AlignCenter, QString::number(sun));
painter->drawPoint(-220, 0);
}
void Shop::advance(int phase)
{
if (!phase)
return;
update();
if (++counter >= time)
{
counter = 0;
scene()->addItem(new Sun);
}
}
void Shop::addPlant(QString s, QPointF pos)
{
QList<QGraphicsItem *> items = scene()->items(pos);
foreach (QGraphicsItem *item, items)
if (item->type() == Plant::Type)
return;
sun -= Card::cost[Card::map[s]];
Plant *plant = nullptr;
switch (Card::map[s])
{
case 0:
plant = new SunFlower; break;
case 1:
plant = new Peashooter; break;
case 2:
plant = new CherryBomb; break;
case 3:
plant = new WallNut; break;
case 4:
plant = new SnowPea; break;
case 5:
plant = new PotatoMine; break;
case 6:
plant = new Repeater; break;
}
plant->setPos(pos);
scene()->addItem(plant);
QList<QGraphicsItem *> child = childItems();
foreach (QGraphicsItem *item, child)
{
Card *card = qgraphicsitem_cast<Card *>(item);
if (card->text == s)
card->counter = 0;
}
counter = 0;
}