]> git.localhorst.tv Git - blank.git/commitdiff
another type of entity controller
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 6 Aug 2015 16:41:46 +0000 (18:41 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 6 Aug 2015 17:51:20 +0000 (19:51 +0200)
just to mix it up

also, I realy have to fix rotation, they're getting all deformed ^^

src/ai/Chaser.hpp [new file with mode: 0644]
src/ai/Controller.hpp [new file with mode: 0644]
src/ai/RandomWalk.cpp [deleted file]
src/ai/RandomWalk.hpp
src/ai/Spawner.cpp
src/ai/Spawner.hpp
src/ai/ai.cpp [new file with mode: 0644]

diff --git a/src/ai/Chaser.hpp b/src/ai/Chaser.hpp
new file mode 100644 (file)
index 0000000..6daaf36
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef BLANK_AI_CHASER_HPP_
+#define BLANK_AI_CHASER_HPP_
+
+#include "Controller.hpp"
+
+
+namespace blank {
+
+class Chaser
+: public Controller {
+
+public:
+       Chaser(Entity &ctrl, Entity &tgt) noexcept;
+       ~Chaser();
+
+       Entity &Target() noexcept { return tgt; }
+       const Entity &Target() const noexcept { return tgt; }
+
+       void Update(int dt) override;
+
+private:
+       Entity &tgt;
+       float speed;
+       float stop_dist;
+       float flee_dist;
+
+};
+
+}
+
+#endif
diff --git a/src/ai/Controller.hpp b/src/ai/Controller.hpp
new file mode 100644 (file)
index 0000000..d2d6b1c
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef BLANK_AI_CONTROLLER_HPP_
+#define BLANK_AI_CONTROLLER_HPP_
+
+
+namespace blank {
+
+class Entity;
+
+class Controller {
+
+public:
+       explicit Controller(Entity &e) noexcept;
+       virtual ~Controller();
+
+       Entity &Controlled() noexcept { return entity; }
+       const Entity &Controlled() const noexcept { return entity; }
+
+       virtual void Update(int dt) = 0;
+
+private:
+       Entity &entity;
+
+};
+
+}
+
+#endif
diff --git a/src/ai/RandomWalk.cpp b/src/ai/RandomWalk.cpp
deleted file mode 100644 (file)
index 32fc29f..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-#include "RandomWalk.hpp"
-
-#include "../world/Entity.hpp"
-
-
-namespace blank {
-
-RandomWalk::RandomWalk(Entity &e) noexcept
-: entity(e)
-, time_left(0) {
-
-}
-
-
-void RandomWalk::Update(int dt) noexcept {
-       time_left -= dt;
-       if (time_left > 0) return;
-       time_left += 2500 + (rand() % 5000);
-
-       constexpr float move_vel = 0.0005f;
-
-       glm::vec3 new_vel = entity.Velocity();
-
-       switch (rand() % 9) {
-               case 0:
-                       new_vel.x = -move_vel;
-                       break;
-               case 1:
-                       new_vel.x = 0.0f;
-                       break;
-               case 2:
-                       new_vel.x = move_vel;
-                       break;
-               case 3:
-                       new_vel.y = -move_vel;
-                       break;
-               case 4:
-                       new_vel.y = 0.0f;
-                       break;
-               case 5:
-                       new_vel.y = move_vel;
-                       break;
-               case 6:
-                       new_vel.z = -move_vel;
-                       break;
-               case 7:
-                       new_vel.z = 0.0f;
-                       break;
-               case 8:
-                       new_vel.z = move_vel;
-                       break;
-       }
-
-       entity.Velocity(new_vel);
-}
-
-}
index 41319dd53ae3d6d696c053791fb59de5d1856d9f..a25d4db502bedf56ca98affa5997ff89646185cf 100644 (file)
@@ -1,27 +1,22 @@
-#ifndef BLANK_APP_RANDOMWALK_HPP_
-#define BLANK_APP_RANDOMWALK_HPP_
+#ifndef BLANK_AI_RANDOMWALK_HPP_
+#define BLANK_AI_RANDOMWALK_HPP_
 
-#include <glm/glm.hpp>
+#include "Controller.hpp"
 
 
 namespace blank {
 
-class Entity;
-
 /// Randomly start or stop moving in axis directions every now and then.
-class RandomWalk {
+class RandomWalk
+: public Controller {
 
 public:
        explicit RandomWalk(Entity &) noexcept;
+       ~RandomWalk();
 
-       Entity &Controlled() noexcept { return entity; }
-       const Entity &Controlled() const noexcept { return entity; }
-
-       void Update(int dt) noexcept;
+       void Update(int dt) override;
 
 private:
-       Entity &entity;
-
        int time_left;
 
 };
index b3a9f240105f34f39b97ca489395dd76c3904cbc..8a3e75e3db737abbea177f5fba71da66b39eb1f7 100644 (file)
@@ -1,5 +1,6 @@
 #include "Spawner.hpp"
 
+#include "Chaser.hpp"
 #include "RandomWalk.hpp"
 #include "../world/BlockType.hpp"
 #include "../world/BlockTypeRegistry.hpp"
@@ -22,7 +23,9 @@ Spawner::Spawner(World &world)
 }
 
 Spawner::~Spawner() {
-
+       for (auto &ctrl : controllers) {
+               delete ctrl;
+       }
 }
 
 
@@ -33,7 +36,7 @@ void Spawner::Update(int dt) {
                TrySpawn();
        }
        for (auto &ctrl : controllers) {
-               ctrl.Update(dt);
+               ctrl->Update(dt);
        }
 }
 
@@ -41,10 +44,11 @@ void Spawner::Update(int dt) {
 void Spawner::CheckDespawn() noexcept {
        const Entity &reference = world.Player();
        for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
-               Entity &e = iter->Controlled();
+               Entity &e = (*iter)->Controlled();
                glm::vec3 diff(reference.AbsoluteDifference(e));
                if (dot(diff, diff) > despawn_range) {
                        e.Remove();
+                       delete *iter;
                        iter = controllers.erase(iter);
                } else {
                        ++iter;
@@ -103,7 +107,13 @@ void Spawner::Spawn(const glm::tvec3<int> &chunk, const glm::vec3 &pos) {
        e.WorldCollidable(true);
        e.SetShape(world.BlockTypes()[1].shape, color);
        e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
-       controllers.emplace_back(e);
+       Controller *ctrl;
+       if (rand() % 2) {
+               ctrl = new RandomWalk(e);
+       } else {
+               ctrl = new Chaser(e, world.Player());
+       }
+       controllers.emplace_back(ctrl);
 }
 
 }
index a0da02fb0ec939b4e7a6c8e2375a82b00d83d03a..120db6003424a1dfa91a0e0e387a502b5733554c 100644 (file)
@@ -3,13 +3,13 @@
 
 #include "../app/IntervalTimer.hpp"
 
-#include <list>
+#include <vector>
 #include <glm/glm.hpp>
 
 
 namespace blank {
 
-class RandomWalk;
+class Controller;
 class World;
 
 class Spawner {
@@ -27,7 +27,7 @@ private:
 
 private:
        World &world;
-       std::list<RandomWalk> controllers;
+       std::vector<Controller *> controllers;
 
        IntervalTimer timer;
        float despawn_range;
diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp
new file mode 100644 (file)
index 0000000..b3e1a0c
--- /dev/null
@@ -0,0 +1,101 @@
+#include "Chaser.hpp"
+#include "Controller.hpp"
+#include "RandomWalk.hpp"
+
+#include "../world/Entity.hpp"
+
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+Chaser::Chaser(Entity &ctrl, Entity &tgt) noexcept
+: Controller(ctrl)
+, tgt(tgt)
+, speed(0.002f)
+, stop_dist(5 * 5)
+, flee_dist(3 * 3) {
+
+}
+
+Chaser::~Chaser() {
+
+}
+
+void Chaser::Update(int dt) {
+       glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
+       float dist = dot (diff, diff);
+       // TODO: line of sight test
+       if (dist > stop_dist) {
+               Controlled().Velocity(normalize(diff) * speed);
+       } else if (dist < flee_dist) {
+               Controlled().Velocity(normalize(diff) * -speed);
+       } else {
+               Controlled().Velocity(glm::vec3(0.0f));
+       }
+}
+
+
+Controller::Controller(Entity &e) noexcept
+: entity(e) {
+
+}
+
+Controller::~Controller() {
+
+}
+
+
+RandomWalk::RandomWalk(Entity &e) noexcept
+: Controller(e)
+, time_left(0) {
+
+}
+
+RandomWalk::~RandomWalk() {
+
+}
+
+void RandomWalk::Update(int dt) {
+       time_left -= dt;
+       if (time_left > 0) return;
+       time_left += 2500 + (rand() % 5000);
+
+       constexpr float move_vel = 0.0005f;
+
+       glm::vec3 new_vel = Controlled().Velocity();
+
+       switch (rand() % 9) {
+               case 0:
+                       new_vel.x = -move_vel;
+                       break;
+               case 1:
+                       new_vel.x = 0.0f;
+                       break;
+               case 2:
+                       new_vel.x = move_vel;
+                       break;
+               case 3:
+                       new_vel.y = -move_vel;
+                       break;
+               case 4:
+                       new_vel.y = 0.0f;
+                       break;
+               case 5:
+                       new_vel.y = move_vel;
+                       break;
+               case 6:
+                       new_vel.z = -move_vel;
+                       break;
+               case 7:
+                       new_vel.z = 0.0f;
+                       break;
+               case 8:
+                       new_vel.z = move_vel;
+                       break;
+       }
+
+       Controlled().Velocity(new_vel);
+}
+
+}