commit f53d4b6e
Monsters now become aggressive then hit
Changed files
| M | src/saland/Game.cpp before |
| M | src/saland/GameUpdates.cpp before |
| M | src/saland/GameUpdates.hpp before |
| M | src/saland/model/placeables.hpp before |
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp
index 7319e86..7863a88 100644
--- a/src/saland/Game.cpp
+++ b/src/saland/Game.cpp
@@ -641,7 +641,7 @@ void Game::Update() {
}
Monster* monster = dynamic_cast<Monster*> (entity.get());
if (monster) {
- UpdateMonster(monster, deltaTime);
+ UpdateMonster(monster, deltaTime, data->human->X, data->human->Y);
}
MiscItem* item = dynamic_cast<MiscItem*> (entity.get());
if (item) {
diff --git a/src/saland/GameUpdates.cpp b/src/saland/GameUpdates.cpp
index 47a406c..d8947f0 100644
--- a/src/saland/GameUpdates.cpp
+++ b/src/saland/GameUpdates.cpp
@@ -22,6 +22,8 @@ https://github.com/sago007/saland
*/
#include "GameUpdates.hpp"
+#include <cmath>
+#include <cstdlib>
static void SetDesiredVelocity(b2Body* body, float x, float y) {
b2Vec2 vel = body->GetLinearVelocity();
@@ -80,25 +82,54 @@ void UpdateHuman(Human* entity, float fDeltaTime) {
entity->Y = place.y*pixel2unit;
}
-static void MonsterThink(Monster* entity) {
- if (rand()%2==0) {
- //1 in 2 chance of changing direction
- if (rand()%2==0) {
- entity->moveX = 0.1f;
- }
- else {
- entity->moveX = -0.1f;
- }
- if (rand()%2 == 0) {
- entity->moveY = 0.1f;
- }
- else {
- entity->moveY = -0.1f;
- }
+static void MonsterThink(Monster* entity, float playerX, float playerY) {
+ switch (entity->aiState) {
+ case Monster::State::Roaming:
+ // Random wandering behavior
+ if (rand()%2==0) {
+ //1 in 2 chance of changing direction
+ if (rand()%2==0) {
+ entity->moveX = 0.1f;
+ }
+ else {
+ entity->moveX = -0.1f;
+ }
+ if (rand()%2 == 0) {
+ entity->moveY = 0.1f;
+ }
+ else {
+ entity->moveY = -0.1f;
+ }
+ }
+ break;
+ case Monster::State::Aggressive:
+ // Move towards player
+ {
+ float dirX = playerX - entity->X;
+ float dirY = playerY - entity->Y;
+ float length = std::sqrt(dirX * dirX + dirY * dirY);
+ if (length > 0.01f) {
+ entity->moveX = (dirX / length) * entity->speed;
+ entity->moveY = (dirY / length) * entity->speed;
+ }
+ }
+ break;
+ case Monster::State::Fleeing:
+ // Move away from player
+ {
+ float dirX = entity->X - playerX;
+ float dirY = entity->Y - playerY;
+ float length = std::sqrt(dirX * dirX + dirY * dirY);
+ if (length > 0.01f) {
+ entity->moveX = (dirX / length) * entity->speed * 1.2f; // Flee slightly faster
+ entity->moveY = (dirY / length) * entity->speed * 1.2f;
+ }
+ }
+ break;
}
}
-void UpdateMonster(Monster* entity, float fDeltaTime) {
+void UpdateMonster(Monster* entity, float fDeltaTime, float playerX, float playerY) {
SetCreatureMovementEntity(entity, entity->moveX, entity->moveY);
b2Vec2 place = entity->body->GetPosition();
entity->X = place.x*pixel2unit;
@@ -108,7 +139,7 @@ void UpdateMonster(Monster* entity, float fDeltaTime) {
}
else {
entity->aiNextThink = 2000.0f;
- MonsterThink(entity);
+ MonsterThink(entity, playerX, playerY);
}
if (entity->health <= 0.0) {
entity->removeMe = true;
@@ -126,7 +157,7 @@ void UpdateProjectile(Projectile* entity, float fDeltaTime) {
}
void ProjectileHit(Projectile* p, Placeable* target) {
- //Monster* monster = dynamic_cast<Monster*> (target);
+ Monster* monster = dynamic_cast<Monster*> (target);
if (target->destructible) {
float damageAmount = p->damage.getDamage();
target->health -= damageAmount;
@@ -140,4 +171,8 @@ void ProjectileHit(Projectile* p, Placeable* target) {
dmg.createdAt = SDL_GetTicks();
target->damageNumbers.push_back(dmg);
}
+ if (monster) {
+ // Potentially change monster state to aggressive
+ monster->aiState = Monster::State::Aggressive;
+ }
}
\ No newline at end of file
diff --git a/src/saland/GameUpdates.hpp b/src/saland/GameUpdates.hpp
index e164aa6..8995f36 100644
--- a/src/saland/GameUpdates.hpp
+++ b/src/saland/GameUpdates.hpp
@@ -31,7 +31,7 @@ https://github.com/sago007/saland
void ProjectileHit(Projectile* p, Placeable* target);
void UpdateHuman(Human* entity, float fDeltaTime);
-void UpdateMonster(Monster* entity, float fDeltaTime);
+void UpdateMonster(Monster* entity, float fDeltaTime, float playerX, float playerY);
void UpdateProjectile(Projectile* entity, float fDeltaTime);
#endif /* GAMEUPDATES_HPP */
diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp
index 51b23cb..b665c1c 100644
--- a/src/saland/model/placeables.hpp
+++ b/src/saland/model/placeables.hpp
@@ -106,10 +106,18 @@ public:
class Monster : public Creature {
public:
+ enum class State {
+ Roaming,
+ Aggressive,
+ Fleeing
+ };
std::string race = "bat";
float speed = 1.0f;
// AI logic:
float aiNextThink = 0.0;
+ State aiState = State::Roaming;
+ float targetX = 0.0f; // Used for fleeing direction
+ float targetY = 0.0f;
};