-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.h
238 lines (197 loc) · 5.79 KB
/
Enemy.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once
#include <math.h>
#include <vector>
#include <SFML/Graphics.hpp>
#include "TextureManager.h"
#include "Platform.h"
class Enemy {
int width, height;
float pos_x, pos_y;
float vel_x, vel_y;
bool top_collision, bottom_collision, left_collision, right_collision;
float gravity = 32;
bool fall = true;
TextureManager* textureManager;
sf::Sprite sprite;
// for animations
sf::Clock animateTime;
float maxKeyframeTime;
float timeSinceLastKeyframe;
float walkFrame;
public:
int getWidth(), getHeight();
float getX(), getY();
float getVelocityX();
bool canMoveLeft, canMoveRight;
void moveLeft();
void moveRight();
sf::Sprite getSprite();
void changeSpriteTexture();
void update(std::vector<Platform> levelPlatforms);
Enemy(float x, float y, TextureManager* textureManager) {
pos_x = x;
pos_y = y;
width = 15;
height = 20;
vel_x = 0;
vel_y = 0;
maxKeyframeTime = 0.5;
canMoveLeft = false;
canMoveRight = true;
this->textureManager = textureManager;
sprite.setTexture(textureManager->getEnemyTexture(0));
sprite.setOrigin({ 0, 0 });
sprite.setPosition(sf::Vector2f(pos_x, pos_y));
}
};
int Enemy::getWidth() {
return width;
}
int Enemy::getHeight() {
return height;
}
float Enemy::getX() {
return pos_x;
}
float Enemy::getY() {
return pos_y;
}
float Enemy::getVelocityX() {
return vel_x;
}
void Enemy::moveLeft() {
if (vel_x > -1) {
vel_x += -gravity * 0.001802f;
}
pos_x += vel_x;
}
void Enemy::moveRight() {
if (vel_x < 1) {
vel_x += gravity * 0.001802f;
}
pos_x += vel_x;
}
void Enemy::changeSpriteTexture() {
timeSinceLastKeyframe = animateTime.getElapsedTime().asSeconds();
// Flips sprite based on if they're moving left/right
if (vel_x > 0) {
sprite.setOrigin({ 0, 0 });
sprite.setScale({ 1, 1 });
}
else if (vel_x < 0) {
sprite.setOrigin({ sprite.getLocalBounds().width, 0 });
sprite.setScale({ -1, 1 });
}
// When enemy is on the ground
if (vel_y == 0) {
if (vel_x == 0) {
sprite.setTexture(textureManager->getEnemyTexture(0));
}
else if (vel_x > 0 || vel_x < 0) {
if (timeSinceLastKeyframe > (maxKeyframeTime - (std::abs(vel_x) * .3))) {
++walkFrame;
if (walkFrame > 3) {
walkFrame = 1;
}
sprite.setTexture(textureManager->getEnemyTexture(walkFrame));
animateTime.restart();
}
}
}
}
void Enemy::update(std::vector<Platform> levelPlatforms) {
// Controls how player falls
if (fall) {
if (vel_y < 10) {
vel_y += gravity * 0.001802f * 2;
}
pos_y += vel_y;
sprite.move(0.f, vel_y);
}
if (canMoveLeft) {
moveLeft();
}
if (canMoveRight) {
moveRight();
}
// Player's hitbox
float leftX = pos_x;
float rightX = (pos_x + width);
float topY = pos_y;
float bottomY = pos_y + height;
// Tracks if player can move in a certain direction
bool playerTouchedBottom = false;
float adjustment;
// Player Collision Detection
bool leftXCollission, rightXCollision, topYCollision, bottomYCollision;
for (auto &platform : levelPlatforms) {
if (platform.isCollidable()) {
leftXCollission = (leftX > platform.getX()) && (leftX < (platform.getX() + platform.getWidth()));
rightXCollision = (rightX > platform.getX()) && (rightX < (platform.getX() + platform.getWidth()));
topYCollision = (topY > platform.getY()) && (topY < (platform.getY() + platform.getHeight()));
bottomYCollision = (bottomY > platform.getY() && (bottomY < platform.getY() + platform.getHeight()));
// Checks bottom collisions
if (vel_y > 0) {
float leftXCollission_bottom = (leftX + 1 > platform.getX()) && (leftX + 1 < (platform.getX() + platform.getWidth()));
float rightXCollision_bottom = (rightX - 1 > platform.getX()) && (rightX - 1 < (platform.getX() + platform.getWidth()));
if (bottomYCollision && (rightXCollision_bottom || leftXCollission_bottom)) {
float adjustment = platform.getY() - height;
playerTouchedBottom = true;
pos_y = adjustment;
bottomY = pos_y + height;
vel_y = 0;
sprite.setPosition(pos_x, adjustment);
}
}
// Checks top collisions
if (vel_y < 0) {
float leftXCollission_bottom = (leftX + 1 > platform.getX()) && (leftX + 1 < (platform.getX() + platform.getWidth()));
float rightXCollision_bottom = (rightX - 1 > platform.getX()) && (rightX - 1 < (platform.getX() + platform.getWidth()));
if (topYCollision && (rightXCollision_bottom || leftXCollission_bottom)) {
float adjustment = platform.getY() + platform.getHeight();
pos_y = adjustment;
topY = pos_y;
vel_y = 0;
sprite.setPosition(pos_x, adjustment);
}
}
// Checks left collisions
if (vel_x < 0) {
if (leftXCollission) {
bool checkTopYPlatInBetween = platform.getY() > topY && platform.getY() + 1 < bottomY;
bool checkBottomYPlatInBetween = platform.getY() + platform.getHeight() > topY && platform.getY() + platform.getHeight() < bottomY;
if (checkTopYPlatInBetween || checkBottomYPlatInBetween) {
pos_x = platform.getX() + platform.getWidth();
leftX = pos_x;
sprite.setPosition(pos_x, pos_y);
vel_x = 0;
canMoveLeft = false;
canMoveRight = true;
}
}
}
// Checks right collisions
if (vel_x > 0) {
if (rightXCollision) {
bool checkTopYPlatInBetween = platform.getY() > topY && platform.getY() + 1 < bottomY;
bool checkBottomYPlatInBetween = platform.getY() + platform.getHeight() > topY && platform.getY() + platform.getHeight() < bottomY;
if (checkTopYPlatInBetween || checkBottomYPlatInBetween) {
pos_x = platform.getX() - width;
rightX = (pos_x + width);
sprite.setPosition(pos_x, pos_y);
vel_x = 0;
canMoveLeft = true;
canMoveRight = false;
}
}
}
if (!playerTouchedBottom) {
fall = true;
}
}
changeSpriteTexture();
}
}
sf::Sprite Enemy::getSprite(){
return sprite;
}