-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeashooter.cpp
107 lines (89 loc) · 3.27 KB
/
peashooter.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
#include "peashooter.h"
#include <QDebug>
#include "zombie.h"
int Peashooter::seedingTime = 7500;
Peashooter::Peashooter(QRect *plant_row, bool is_snowpea)
{
/*is_snowpea = true, snowpea is being spawned
* is_snowpea = false, normal peashooter is being spawned*/
//peashooter properties
seedingTime = 7500;
life = 10;
plantDamage = 1;
fireRate = 1500;
slowEffect = is_snowpea; //used to know what kind of bullet to use
//setting the position of peashooter based on planting row
this->setPos(plant_row->x(),plant_row->y() + 20);
activeRow = *plant_row; //copies row information to activeRow member
//Checks to see which pixmap to use based on snowpea or peashooter selection
if(!is_snowpea)
{
peashooterImage = new QPixmap(":/Images/peashooter");
cost = 100; // the cost to spawn peashooter
}
else
{
peashooterImage = new QPixmap(":/Images/snowpea");
cost = 175; //the cost to spawn snowpea
}
fireCounter = new QTime; //sets up counter
fireCounter->start(); //starts counting up
/*Creates 2 points, one at the start of the plot where the peashooter is planted and
one at the end of the scene; two create straight line through the active row which
acts like a collision mask that detects zombies and shoots when there is a zombie
colliding with this invisible line*/
//Creates 2 points using active row rectangle and peashooter image size
QPoint p1(activeRow.x()+peashooterImage->width()/2,activeRow.y()+peashooterImage->height()/2);
QPoint p2(activeRow.width()+activeRow.x(),activeRow.y()+peashooterImage->height()/2);
//Creates a collision mask(line) using the points
//Line starts from peashooter center and goes across the row
collisionLine = new QGraphicsLineItem(p1.x(),p1.y(),p2.x(),p2.y());
}
Peashooter::~Peashooter()
{
delete peashooterImage;
delete fireCounter;
delete collisionLine;
}
QRectF Peashooter::boundingRect() const
{
return QRectF(0,0,peashooterImage->width(),peashooterImage->height());
}
void Peashooter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//draws peashooter pixmap to screen using boundingRect() as source and target rects
painter->drawPixmap(boundingRect(),*peashooterImage,boundingRect());
}
void Peashooter::advance(int phase)
{
if(!phase) return;
//Deletes thes snowpea/peashooter if live is 0 or less
if(life <= 0)
{
delete this;
return;
}
//When counter is above fireRate, a bullet is spawned and counter is restarted
if(fireCounter->elapsed() >= fireRate)
{
fireBullet();
fireCounter->restart();
}
}
void Peashooter::fireBullet()
{
//Creates a list of items currently colliding with the mask
QList<QGraphicsItem *> collision_list = scene()->collidingItems(collisionLine);
//Checks for zombies colliding with mask and fires if there is atleast one zombie in row
for(int i = 0; i < (int)collision_list.size(); i++)
{
Zombie * item = dynamic_cast<Zombie *>(collision_list.at(i));
if (item)
{
//Fire a bullet in the row and exits function
bullet = new Bullet(slowEffect,this);
scene()->addItem(bullet);
return;
}
}
}