-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
76 lines (63 loc) · 2.29 KB
/
bullet.cpp
File metadata and controls
76 lines (63 loc) · 2.29 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
#include "bullet.h"
Bullet::Bullet(bool slow, Plant *parent)
{
//Uses the parent's position and boundingRect information to create a spawn location
//which appears natural coming out of plant
this->setPos(parent->x()+parent->boundingRect().width()/2,parent->y());
this->setScale(0.9); /*sets scale of bullet instance, 0.9 allows for a smaller hitbox that is
more centered and thus ensures safety in not registering collisions with
zombies in neighbouring rows*/
xVelocity = 3; //velocity of bullet
bulletDamage = parent->getDamage(); //Uses damage of parent plant
triggerSlow = slow; //checks if bullet will slow (default to false)
if(slow) //Uses snowpea projectile if the bullet if slow is true
{
bulletImage = new QPixmap(":/Images/projectileSnowPea");
}
else //Uses normal green pea projectile if the bullet only damages
{
bulletImage = new QPixmap(":/Images/projectilePea");
}
}
Bullet::~Bullet()
{
delete bulletImage;
}
void Bullet::destroyBullet()
{
//destroys instance of class
delete this;
}
QRectF Bullet::boundingRect() const
{
return QRectF(0,0,bulletImage->width(),bulletImage->height());
}
void Bullet::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//draws pixmap representation of bullet to screen
painter->drawPixmap(boundingRect(),*bulletImage,boundingRect());
}
void Bullet::advance(int phase)
{
if(!phase)
return;
//Updates position based on xVelocity
this->setPos(this->x()+xVelocity,this->y());
//Creates a list of items currently colliding with the bullet
QList<QGraphicsItem *> collision_list = scene()->collidingItems(this);
for(int i = 0; i < (int)collision_list.size(); i++)
{
//Checks for zombies colliding with the bullet
Zombie * item = dynamic_cast<Zombie *>(collision_list.at(i));
if(item)
{
//decreases the life of the zombie by the bulletDamage
item->decreaseLife(bulletDamage);
//triggers slow on zombie only if zombie has not already been slowed
if(triggerSlow && !item->getSlowStatus())
item->setSlowEffect();
destroyBullet(); //destroys bullet
return;
}
}
}