-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
157 lines (141 loc) · 3.21 KB
/
Enemy.cpp
File metadata and controls
157 lines (141 loc) · 3.21 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
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
#include "Enemy.h"
Enemy::Enemy(int type, float spawnPositionX, float spawnPositionY)
{
initVariables();
initShape(type, spawnPositionX, spawnPositionY);
}
void Enemy::initVariables()
{
enemySpeed = 1.f;
enemyVerticalSpeed = 6.5f;
enemyRadius = 20.f;
//Directional Booleans
moveRight = true;
moveLeft = false;
leftBorder = false;
rightBorder = false;
//bullet collisions
bulletCollision = false;
earthCollision = false;
playerCollision = false;
}
void Enemy::initShape(int type,
float spawnPositionX, float spawnPositionY)
{
switch (type)
{
case 4:
initSquare(spawnPositionX, spawnPositionY);
break;
case 6:
initHexagon(spawnPositionX, spawnPositionY);
break;
case 8:
initOctagon(spawnPositionX, spawnPositionY);
break;
}
}
void Enemy::initSquare(float spawnPositionX, float spawnPositionY)
{
shape.setRadius(enemyRadius);
shape.setPointCount(4);
shape.setFillColor(sf::Color::Red);
shape.setOrigin(shape.getRadius(), 0);
shape.setPosition(spawnPositionX, spawnPositionY);
}
void Enemy::initHexagon(float spawnPositionX, float spawnPositionY)
{
shape.setRadius(enemyRadius);
shape.setPointCount(6);
shape.setFillColor(sf::Color::Red);
shape.setOrigin(shape.getRadius(), 0);
shape.setPosition(spawnPositionX, spawnPositionY);
}
void Enemy::initOctagon(float spawnPositionX, float spawnPositionY)
{
shape.setRadius(enemyRadius);
shape.setPointCount(8);
shape.setFillColor(sf::Color::Red);
shape.setOrigin(shape.getRadius(), 0);
shape.setPosition(spawnPositionX, spawnPositionY);
}
void Enemy::update(sf::RenderTarget& target, Player& player)
{
checkBorder(target);
moveEnemyLeftRight();
checkEarthCollision(target.getSize().y);
checkPlayerCollision(player);
}
//Movement functions
void Enemy::checkBorder(sf::RenderTarget& target)
{
//touching leftbound
if (shape.getGlobalBounds().left <= 0.f)
{
leftBorder = true;
}
//touching right bound
else if (shape.getGlobalBounds().left + shape.getGlobalBounds().width
>= target.getSize().x)
{
rightBorder = true;
}
}
void Enemy::moveEnemyLeftRight()
{
if (moveRight)
{
shape.move(enemySpeed, 0.0f);
}
else if (moveLeft)
{
shape.move(-enemySpeed, 0.0f);
}
}
void Enemy::updateEnemySpeed(float num)
{
//Enemy speed increases, multiplied by
// inverse porportion of current enemies
enemySpeed = .8f * num;
}
void Enemy::repositionDown()
{
shape.setPosition(shape.getPosition().x, shape.getGlobalBounds().top
+ enemyVerticalSpeed);
}
//Enemy fire bullet methods
void Enemy::shootBullets(float resolutionHeight,
std::vector<EnemyBullet*>& allEnemyBullets)
{
allEnemyBullets.push_back(
new EnemyBullet(resolutionHeight, shape.getPosition()));
}
//Collisions with Player Bullet
void Enemy::checkBulletCollision(PlayerBullet* bullet)
{
if (shape.getGlobalBounds().intersects(bullet->line.getGlobalBounds()))
{
bulletCollision = true;
}
}
void Enemy::checkEarthCollision(float resolutionHeight)
{
if (shape.getGlobalBounds().top >= resolutionHeight)
{
earthCollision = true;
}
}
void Enemy::checkPlayerCollision(Player& player)
{
if (shape.getGlobalBounds().intersects(player.triangle.getGlobalBounds()))
{
playerCollision = true;
}
}
void Enemy::render(sf::RenderTarget& target)
{
target.draw(shape);
}
Enemy::~Enemy()
{
}