-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.cpp
More file actions
102 lines (87 loc) · 3.63 KB
/
Bullet.cpp
File metadata and controls
102 lines (87 loc) · 3.63 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
#include "Bullet.h"
Bullet::Bullet(int x, int y) {
this->position[0] = x, this->position[1] = y;
if (!texture.loadFromFile("assets/Bullets/peabullet.png")) cout << "Bullet not found\n";
this->sprite.setTexture(this->texture);
this->damage = 20;
this->exists = false;
this->bulletClock.restart();
this->speed = 0.0625;
}
void Bullet::setHitSound(Sound sound) {
this->hitSound = sound;
this->hitSound.setPlayingOffset(Time(seconds(0.5)));
}
void Bullet::saveEverything(ofstream& file) {
file.write(reinterpret_cast<char*>(&position[0]), sizeof(float));
file.write(reinterpret_cast<char*>(&position[1]), sizeof(float));
file.write(reinterpret_cast<char*>(&exists), sizeof(bool));
}
void Bullet::readEverything(ifstream& file) {
file.read(reinterpret_cast<char*>(&position[0]), sizeof(float));
file.read(reinterpret_cast<char*>(&position[1]), sizeof(float));
this->sprite.setPosition(305 + this->position[0] * 80, 95 + this->position[1] * 96);
file.read(reinterpret_cast<char*>(&exists), sizeof(bool));
this->exists = exists;
}
void Bullet::changeSprite(Texture& tex) {
this->sprite = Sprite(tex);
}
bool Bullet::getExist() { return exists; }
void Bullet::setExist(bool val) { exists = val; }
void Bullet::setPosition(float x, float y) {
this->position[0] = x;
this->position[1] = y;
}
void Bullet::move(Zombie** zombies, int zombiesArrayIndex, bool isSnow, Scoreboard* scoreboard) {
if (this->bulletClock.getElapsedTime().asMilliseconds() <= 5) {
return;
}
this->bulletClock.restart();
if (this->exists) {
for (int i = 0; i < zombiesArrayIndex; i++) {
if (zombies[i]->getExist() == true && !zombies[i]->isFlying()) {
float* zombiePos = zombies[i]->getPosition();
if ((this->position[1] == zombiePos[1]) && (this->position[0] == zombiePos[0] || this->position[0] == zombiePos[0] - 0.03125 || this->position[0] == zombiePos[0] - 0.0625 || this->position[0] == zombiePos[0] - 0.09375 || this->position[0] == zombiePos[0] - 0.125 || this->position[0] == zombiePos[0] - 0.15625 || this->position[0] == zombiePos[0] - 0.1875 || this->position[0] == zombiePos[0] - 0.21875 || this->position[0] == zombiePos[0] - 0.25 || this->position[0] == zombiePos[0] - 0.28125 || this->position[0] == zombiePos[0] - 0.3125 || this->position[0] == zombiePos[0] - 0.34375 || this->position[0] == zombiePos[0] - 0.375 || this->position[0] == zombiePos[0] - 0.40625 || this->position[0] == zombiePos[0] - 0.4375 || this->position[0] == zombiePos[0] - 0.46875 || this->position[0] == zombiePos[0] - 0.5 || this->position[0] == zombiePos[0] - 0.53125 || this->position[0] == zombiePos[0] - 0.5625)) {
this->exists = false;
//this->hitSound.play();
zombies[i]->setFlicker(true);
zombies[i]->getSprite().setColor(Color(255, 255, 255, 255 * 0.5));
zombies[i]->reduceHealth(this->damage);
zombies[i]->checkHealth();
if (zombies[i]->getType() == "dancing") {
scoreboard->addScore(50);
}
else if (zombies[i]->getType() == "football") {
scoreboard->addScore(20);
}
else if (zombies[i]->getType() == "normal") {
scoreboard->addScore(10);
}
else {
scoreboard->addScore(0);
}
this->bulletClock.restart();
cout << "Zombie " << i << " health: " << zombies[i]->getHealth() << endl;
if (isSnow) {
zombies[i]->setMoveDelay(500);
}
//this->hitSound.stop();
return;
}
}
}
if (this->position[0] <= 8.5) {
this->position[0] += this->speed;
}
else {
this->exists = false;
}
}
}
void Bullet::draw(RenderWindow& window) {
if (this->exists) {
this->sprite.setPosition(305 + this->position[0] * 80, 95 + this->position[1] * 96);
window.draw(this->sprite);
}
}