Skip to content

Commit f36492b

Browse files
author
GauravDe-git
committed
Added enemy reposition state
1 parent ad31ea5 commit f36492b

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

Mini_Assailants/inc/Enemy.cpp

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "Enemy.hpp"
2-
#include "Enemy.hpp"
32

43
#include <Graphics/ResourceManager.hpp>
54

@@ -18,8 +17,6 @@ static std::map<Enemy::State, std::string> g_stateNames =
1817
{Enemy::State::Hurt, "Hurt"},
1918
};
2019

21-
Enemy::Enemy() = default;
22-
2320
Enemy::Enemy(const glm::vec2& pos,Type _type)
2421
:Entity{ pos }
2522
,type{ _type }
@@ -200,6 +197,9 @@ void Enemy::update(float deltaTime)
200197
case State::Attack:
201198
doAttack(deltaTime);
202199
break;
200+
case State::Reposition:
201+
doReposition(deltaTime);
202+
break;
203203
case State::Hurt:
204204
doHurt(deltaTime);
205205
break;
@@ -225,6 +225,9 @@ void Enemy::draw(Image& image, const Camera& camera)
225225
case State::Attack:
226226
image.drawSprite(attackAnim, tempTransform);
227227
break;
228+
case State::Reposition:
229+
image.drawSprite(chaseAnim, tempTransform);
230+
break;
228231
case State::Hurt:
229232
image.drawSprite(hurtAnim, tempTransform);
230233
break;
@@ -330,7 +333,7 @@ void Enemy::endState(State oldState)
330333

331334
void Enemy::doMovement(float deltaTime)
332335
{
333-
auto initialPos = transform.getPosition();
336+
glm::vec2 initialPos = transform.getPosition();
334337
const auto targetPos = target ? target->getPosition() : initialPos;
335338

336339
auto direction = targetPos - initialPos;
@@ -387,11 +390,21 @@ void Enemy::doChase(float deltaTime)
387390

388391
void Enemy::doAttack(float deltaTime)
389392
{
390-
if (target && glm::distance(transform.getPosition(), target->getPosition()) > attackDistance)
393+
if (!target)
391394
{
392-
setState(State::Chase);
395+
setState(State::Idle);
393396
}
394-
else
397+
glm::vec2 targetPos = target ? target->getPosition() : transform.getPosition();
398+
float xDifference = std::abs(transform.getPosition().x - targetPos.x);
399+
float yDifference = std::abs(transform.getPosition().y - targetPos.y);
400+
401+
// If the target is too close in the x direction and y direction, move away
402+
if (target && xDifference < 5.f && yDifference < attackDistance)
403+
{
404+
setState(State::Reposition);
405+
}
406+
// If the target is at the right distance, attack
407+
else if (target && glm::distance(transform.getPosition(), targetPos) < attackDistance)
395408
{
396409
attackAnim.update(deltaTime);
397410
if (attackAnim.getCurrentFrame() >= attackFrame)
@@ -402,11 +415,42 @@ void Enemy::doAttack(float deltaTime)
402415
{
403416
attackAnim.reset();
404417
if (!target)
405-
setState(State::Idle);
418+
setState(State::Idle);
406419
}
407420
}
421+
// If the target is too far, chase
422+
else if (target && glm::distance(transform.getPosition(), targetPos) > attackDistance)
423+
{
424+
setState(State::Chase);
425+
}
408426
}
409427

428+
void Enemy::doReposition(float deltaTime)
429+
{
430+
if (!target)
431+
{
432+
setState(State::Idle);
433+
}
434+
chaseAnim.update(deltaTime);
435+
glm::vec2 targetPos = target ? target->getPosition() : transform.getPosition();
436+
float xDifference = std::abs(transform.getPosition().x - targetPos.x);
437+
float yDifference = std::abs(transform.getPosition().y - targetPos.y);
438+
439+
// Calculate the reposition point
440+
glm::vec2 repositionPoint = targetPos + glm::vec2((targetPos.x < transform.getPosition().x) ? attackDistance : -attackDistance, 0);
441+
442+
// Move towards the reposition point
443+
glm::vec2 direction = glm::normalize(repositionPoint - transform.getPosition());
444+
transform.translate(direction * speed * deltaTime);
445+
446+
// If the target is at the right distance, go back to attacking
447+
if (glm::distance(transform.getPosition(), targetPos) >= attackDistance)
448+
{
449+
setState(State::Attack);
450+
}
451+
}
452+
453+
410454
void Enemy::doHurt(float deltaTime)
411455
{
412456
hurtAnim.update(deltaTime);
@@ -434,11 +478,14 @@ void Enemy::doDead(float deltaTime)
434478

435479
void Enemy::setFacingDirection(const glm::vec2& direction)
436480
{
437-
if (direction.x < 0) {
438-
transform.setScale(glm::vec2(1.0f, 1.0f));
439-
}
440-
else if (direction.x > 0) {
441-
transform.setScale(glm::vec2(-1.0f, 1.0f));
481+
if (getState() != State::Dead)
482+
{
483+
if (direction.x < 0) {
484+
transform.setScale(glm::vec2(1.0f, 1.0f));
485+
}
486+
else if (direction.x > 0) {
487+
transform.setScale(glm::vec2(-1.0f, 1.0f));
488+
}
442489
}
443490
}
444491

Mini_Assailants/inc/Enemy.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class Enemy : public Entity
3232
Idle,
3333
Chase,
3434
Attack,
35+
Reposition,
3536
Hurt,
3637
Dead,
3738
JustDefeated
3839
};
3940

40-
Enemy();
4141
explicit Enemy(const glm::vec2& pos, Type type);
4242

4343
virtual void update(float deltaTime) override;
@@ -72,6 +72,7 @@ class Enemy : public Entity
7272
void doIdle(float deltaTime);
7373
void doChase(float deltaTime);
7474
void doAttack(float deltaTime);
75+
void doReposition(float deltaTime);
7576
void doHurt(float deltaTime);
7677
void doDead(float deltaTime);
7778

Mini_Assailants/src/Player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void Player::endState(State oldState)
302302

303303
void Player::doMovement(float deltaTime)
304304
{
305-
const auto initialPos = transform.getPosition();
305+
const glm::vec2 initialPos = transform.getPosition();
306306
auto position = initialPos;
307307

308308
position.x += Input::getAxis("Horizontal") * speed * deltaTime;

Mini_Assailants/src/Todo.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11

2-
Maybe-Enemy shouldnt turn in direction of player in middle of attack
3-
or when dying?
4-
5-
- Change level looks like a button. (x)
6-
- Buttons sprites look bad, have artifacts compression. (x)
72
- defender complaining about .exe . ???? - added icon (x)
8-
- sfx like the coin/hp/mp too loud/harsh. (x)
93

104
-AI, rathr than making it flinch back every time its hit, maybe have some value per monster type that makes it resistant to flinching,
115
and only have it flinch when its hit enough (and would reset after not being hit for N amount of time)
126
that would give it some resistance to the spam attack problem
137
being able to block might also be a thing
148
would introduce light+heavy hit mechanics
159

16-
- One way to improve the ai is by adding collision. Just push the player out of the enemy when inside.
10+
-Add a pause menu. Maybe a button on top right, can be clicked, or press esc/p to go menu,
11+
with options to go back to main menu or quit.
1712

1813
QOL:
1914
Player Face Portrait
2015
Floating damage numbers.
21-
Add a pause menu. Maybe a button on top right, can be clicked, or press esc/p to go menu,
22-
with options to go back to main menu or quit.
2316

2417
Change the stats like player hp,mp,dmg,enemy- hp,dmg using JSON instead
2518
of hardcoding

0 commit comments

Comments
 (0)