/
Attacker.cpp
91 lines (73 loc) · 1.89 KB
/
Attacker.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
/*
* Attacker.cpp
*
* Created on: Apr 15, 2012
* Author: David Zhang
*/
#include "Attacker.h"
#include <iostream>
using namespace std;
Attacker::Attacker(): FallingObject() {
visible = false;
setTimerLevel(120); // different speed
shotTimerLevel = 50;
shotTimer = shotTimerLevel;
}
Attacker::~Attacker() {
// Clean up projectiles
for (unsigned int i=0; i<projectiles.size(); i++) {
delete projectiles[i]; // safely delete
projectiles[i] = NULL; // clean up pointer
}
}
// Resets location and attacker off
void Attacker::resetState() {
int c = helper.getRandomCol();
int r = helper.getRandomRowOnTopFraction(.78);
rect.moveCenter(QPoint(c, r));
setOn(false);
}
void Attacker::timerEvent(QTimerEvent *event) {
FallingObject::timerEvent(event); // call super class method
if (visible) {
shotTimer--;
if (shotTimer == 0) {
shotTimer = shotTimerLevel;
fire();
}
// check projectiles for exhausted
for (unsigned int i=0; i<projectiles.size(); i++) {
if (projectiles[i]->isExhausted()) {
delete projectiles[i]; // safely delete
projectiles[i] = NULL; // clean up pointer
projectiles.erase(projectiles.begin()+i); // remove from vector
i--; // compensate for deleted projectile ... CHECK HERE
cout << "projectile erased" << endl;
}
}
}
}
void Attacker::fire() {
Projectile *p = new Projectile;
p->setLocation(this->rect.center());
p->setTarget(target);
p->launch();
projectiles.push_back(p);
}
void Attacker::updateTargetLocation(QPoint p) {
target = p;
}
void Attacker::setOn(bool b) {
FallingObject::setOn(b); // super class call
// In addition, turn off/on projectiles
for (unsigned int i=0; i<projectiles.size(); i++) {
projectiles[i]->setOn(b);
}
}
// This makes the attacker visible - i.e., ensures image loaded properly. Before, image is null.
void Attacker::setVisible(bool b) {
visible = b;
if (visible) {
loadImage();
}
}