-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
164 lines (147 loc) · 4.54 KB
/
Enemy.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/** This creates the enemies for the main driver function
*
*
*
* @author Nathan Clark
* @author Sebastian Mark
**/
#include "Enemy.h"
// First constructor
Enemy::Enemy(std::string path) {
this->texture.loadFromFile(path);
this->sprite.setTexture(texture);
this->alive = true;
this->isBoss = false;
}
// overloaded constructor
Enemy::Enemy(std::string path, sf::IntRect textRec) {
this->texture.loadFromFile(path);
this->sprite.setTextureRect(textRec);
this->sprite.setTexture(texture);
this->alive = true;
this->isBoss = false;
}
// setting the position of the enemy
void Enemy::setPosition(float x, float y) {
this->sprite.setPosition(x, y);
}
// setting the scale/ size of the enemy
void Enemy::setScale(float x, float y) {
this->sprite.setScale(x, y);
}
// return the enemy when called
sf::Sprite * Enemy::getSprite() {
return &this->sprite;
}
// setting the sprite path i.e. "../Assets/Character_animation/Knight.png"
void Enemy::setSprite(std::string path) {
this->texture.loadFromFile(path);
this->sprite.setTexture(texture);
}
// boolean if the enemy is alive i.e. if false the enemy is not visible
bool Enemy::isAlive(){
return this->alive;
}
// initially setting the enemy as visible i.e. alive = true
// if collision then the alive = false
//retunrs if enemy is alive
//sets if enemy is alive or not
void Enemy::set_Alive(bool alive) {
this->alive = alive;
}
/********************************************************
* randomEnemyMove - randomly moves an enemy
*
* Parameters
* number - random number
* Level* - Pointer to level object for function calls based on level
*
* Return - false if no fireballs shot, true if boss shoots fireball
********************************************************/
bool Enemy::randomEnemyMove(int number, Level* currentLevel){
// we need to comment NO! COMMENTING ISN'T FOR REAL MEN!
//if the enemy is not a boss randomly move up, down, left, or right
if (!isBoss) {
number = number % 4;
if (number == 0 && currentLevel->isTileXWalkable(this->currentTile - 16)) {
this->currentTile -= 16;
sprite.move(0.0f, -121.0f);
}
if (number == 1 && currentLevel->isTileXWalkable(this->currentTile - 1)) {
this->currentTile -= 1;
sprite.move(-121.0f, 0.0f);
}
if (number == 2 && currentLevel->isTileXWalkable(this->currentTile + 16)) {
this->currentTile += 16;
sprite.move(0.0f, 121.0f);
}
if (number == 3 && currentLevel->isTileXWalkable(this->currentTile + 1)) {
this->currentTile += 1;
sprite.move(121.0f, 0.0f);
}
return false;
}
else{
//if enemy is a boss randomly move left, right, up, down, or shoot fireballs
number = number % 5;
if (number == 0 && currentLevel->isTileXWalkable(this->currentTile - 16)) {
this->currentTile -= 16;
sprite.move(0.0f, -121.0f);
}
if (number == 1 && currentLevel->isTileXWalkable(this->currentTile - 1)) {
this->currentTile -= 1;
sprite.move(-121.0f, 0.0f);
}
if (number == 2 && currentLevel->isTileXWalkable(this->currentTile + 16)) {
this->currentTile += 16;
sprite.move(0.0f, 121.0f);
}
if (number == 3 && currentLevel->isTileXWalkable(this->currentTile + 1)) {
this->currentTile += 1;
sprite.move(121.0f, 0.0f);
}
if (number == 4){
return true;
}
return false;
}
}
//A random respawn method in case an enemy object "dies"
void Enemy::respawn(int number){
number = number % 4;
switch (number){
case 0:
sprite.setPosition(968.f, 484.f);
currentTile = 72;
break;
case 1:
sprite.setPosition(968.f, 484.f);
currentTile = 72;
break;
case 2:
sprite.setPosition(968.f, 484.f);
currentTile = 72;
break;
case 3:
sprite.setPosition(968.f, 484.f);
currentTile = 72;
break;
default:
break;
}
}
// returning current tile for the enemy
int Enemy::getCurrentTile() {
return this->currentTile;
}
// setting tile for the 16:9 dimensional grid
void Enemy::setCurrentTile(int currentTile) {
this->currentTile = currentTile;
}
//function to make enemy use Boss AI.
void Enemy::setBoss(bool isBoss) {
this->isBoss = isBoss;
}
bool Enemy::getBoss(){
return this->isBoss;
}