Skip to content

Commit 40bdb3f

Browse files
committed
Added enemy death logic
1 parent b1cdd24 commit 40bdb3f

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

Enemy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void Enemy::animate(int xStart, int xEnd, int yStart, int yEnd) {
9191
}
9292
}
9393

94-
void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition) {
94+
void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition, bool playerAttacking) {
9595
// Chase player
9696
sf::Vector2f position = sprite.getPosition();
9797
sf::Vector2f direction = playerPosition - position; // Calculate direction
@@ -108,6 +108,9 @@ void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition) {
108108
sprite.move(normalized * speed * deltaTime); // Move enemy sprite
109109
direction = normalized; // Set direction
110110
}
111+
}
112+
else if (distance <= attackRadius * 1.5 && playerAttacking) {
113+
dead = true;
111114
// Stop if too close to player
112115
} else if (distance <= attackRadius) {
113116
attacking = true;
@@ -116,7 +119,6 @@ void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition) {
116119
} else if (distance >= stopRadius) {
117120
attacking = false;
118121
moving = false;
119-
120122
}
121123

122124
// Calculate current direction

Enemy.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Enemy {
77
public:
88
Enemy();
9-
void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition);
9+
void Enemy::update(float deltaTime, const sf::Vector2f& playerPosition, bool playerAttacking);
1010
sf::Vector2f getPosition();
1111
void Enemy::draw(sf::RenderWindow& window);
1212
void Enemy::animate(int xStart, int xEnd, int yStart, int yEnd);
@@ -41,6 +41,8 @@ class Enemy {
4141
int attackSouthWestXStart, attackSouthWestYStart, attackSouthWestXEnd, attackSouthWestYEnd;
4242
int attackWestXStart, attackWestYStart, attackWestXEnd, attackWestYEnd;
4343
int attackNorthWestXStart, attackNorthWestYStart, attackNorthWestXEnd, attackNorthWestYEnd;
44+
45+
bool dead = false;
4446
};
4547

4648
#endif // ENEMY_HPP

Enemy.obj

91.7 KB
Binary file not shown.

main.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
#include "Player.hpp"
1111

1212
#include "Enemy.cpp"
13-
#include "Enemy.hpp"
13+
#include "Enemy.hpp"
1414

1515
int main()
1616
{
17-
// Create a player sprite
17+
// Create a player
1818
Player player;
1919

20-
// Create a enemy sprite
20+
// Create a enemy
2121
Enemy enemy;
22+
std::vector<Enemy> enemies;
2223

2324
// Create game window
2425
sf::RenderWindow window(sf::VideoMode({800, 600}), "2D Game", sf::Style::Titlebar | sf::Style::Close);
@@ -74,19 +75,15 @@ int main()
7475

7576
window.handleEvents(onClose, onKeyPressed, onKeyReleased);
7677

77-
// Handle player controls
78+
// Handle player controls and enemy updates
7879
player.handleInput();
7980
player.update();
80-
enemy.update(deltaTime, player.getPosition());
81+
enemy.update(deltaTime, player.getPosition(), player.attacking);
8182

82-
// Create new window with sprite drawn in
83+
// Create new window with sprites drawn in
8384
window.clear(sf::Color::White);
84-
8585
player.draw(window);
8686
enemy.draw(window);
87-
88-
std::cout << "Player: " << player.getPosition().x << ", " << player.getPosition().y << "\n";
89-
std::cout << "Enemy: " << enemy.getPosition().x << ", " << enemy.getPosition().y << "\n";
9087

9188
window.display();
9289
}

main.exe

1.5 KB
Binary file not shown.

main.obj

7.77 KB
Binary file not shown.

0 commit comments

Comments
 (0)