git repos / saland

commit 83ee3eb4

Poul Sander · 2021-01-09 12:30
83ee3eb4a23e546d2ebef12c34067eeebbc3c02f patch · browse files
parent c26b24f34d1145650b0495c4cba4de9c05224dfa

Monsters now move

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 7341a8c..f6cd1db 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -560,7 +560,7 @@ void Game::Update() {
}
Monster* monster = dynamic_cast<Monster*> (entity.get());
if (monster) {
- UpdateMonster(monster);
+ UpdateMonster(monster, deltaTime);
}
MiscItem* item = dynamic_cast<MiscItem*> (entity.get());
if (item) {
diff --git a/src/saland/GameUpdates.cpp b/src/saland/GameUpdates.cpp index dfeba10..ddea0ee 100644 --- a/src/saland/GameUpdates.cpp +++ b/src/saland/GameUpdates.cpp
@@ -80,11 +80,36 @@ void UpdateHuman(Human* entity, float fDeltaTime) {
entity->Y = place.y*pixel2unit;
}
-void UpdateMonster(Monster* entity) {
+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;
+ }
+ }
+}
+
+void UpdateMonster(Monster* entity, float fDeltaTime) {
SetCreatureMovementEntity(entity, entity->moveX, entity->moveY);
b2Vec2 place = entity->body->GetPosition();
entity->X = place.x*pixel2unit;
entity->Y = place.y*pixel2unit;
+ if (entity->aiNextThink > 0.0) {
+ entity->aiNextThink -= fDeltaTime;
+ }
+ else {
+ entity->aiNextThink = 2000.0f;
+ MonsterThink(entity);
+ }
if (entity->health <= 0.0) {
entity->removeMe = true;
}
diff --git a/src/saland/GameUpdates.hpp b/src/saland/GameUpdates.hpp index c8c5f01..e164aa6 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);
+void UpdateMonster(Monster* entity, float fDeltaTime);
void UpdateProjectile(Projectile* entity, float fDeltaTime);
#endif /* GAMEUPDATES_HPP */
diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp index 07b0a9d..c0d4948 100644 --- a/src/saland/model/placeables.hpp +++ b/src/saland/model/placeables.hpp
@@ -80,6 +80,9 @@ public:
class Monster : public Creature {
public:
std::string race = "bat";
+ float speed = 1.0f;
+ // AI logic:
+ float aiNextThink = 0.0;
};
class Projectile : public Placeable {