From c52405fad9c070e1370f852234c6eb723b52916c Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 6 Aug 2015 18:41:46 +0200 Subject: [PATCH] another type of entity controller just to mix it up also, I realy have to fix rotation, they're getting all deformed ^^ --- src/ai/Chaser.hpp | 31 +++++++++++++ src/ai/Controller.hpp | 27 +++++++++++ src/ai/RandomWalk.cpp | 57 ------------------------ src/ai/RandomWalk.hpp | 19 +++----- src/ai/Spawner.cpp | 18 ++++++-- src/ai/Spawner.hpp | 6 +-- src/ai/ai.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 183 insertions(+), 76 deletions(-) create mode 100644 src/ai/Chaser.hpp create mode 100644 src/ai/Controller.hpp delete mode 100644 src/ai/RandomWalk.cpp create mode 100644 src/ai/ai.cpp diff --git a/src/ai/Chaser.hpp b/src/ai/Chaser.hpp new file mode 100644 index 0000000..6daaf36 --- /dev/null +++ b/src/ai/Chaser.hpp @@ -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 index 0000000..d2d6b1c --- /dev/null +++ b/src/ai/Controller.hpp @@ -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 index 32fc29f..0000000 --- a/src/ai/RandomWalk.cpp +++ /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); -} - -} diff --git a/src/ai/RandomWalk.hpp b/src/ai/RandomWalk.hpp index 41319dd..a25d4db 100644 --- a/src/ai/RandomWalk.hpp +++ b/src/ai/RandomWalk.hpp @@ -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 +#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; }; diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp index b3a9f24..8a3e75e 100644 --- a/src/ai/Spawner.cpp +++ b/src/ai/Spawner.cpp @@ -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 &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); } } diff --git a/src/ai/Spawner.hpp b/src/ai/Spawner.hpp index a0da02f..120db60 100644 --- a/src/ai/Spawner.hpp +++ b/src/ai/Spawner.hpp @@ -3,13 +3,13 @@ #include "../app/IntervalTimer.hpp" -#include +#include #include namespace blank { -class RandomWalk; +class Controller; class World; class Spawner { @@ -27,7 +27,7 @@ private: private: World &world; - std::list controllers; + std::vector controllers; IntervalTimer timer; float despawn_range; diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp new file mode 100644 index 0000000..b3e1a0c --- /dev/null +++ b/src/ai/ai.cpp @@ -0,0 +1,101 @@ +#include "Chaser.hpp" +#include "Controller.hpp" +#include "RandomWalk.hpp" + +#include "../world/Entity.hpp" + +#include + + +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); +} + +} -- 2.39.2