X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2Fai.cpp;h=5f5474b5f3320030e2f7e902badbb86c58328a79;hb=d2fa8ca97d291508ce3812fb052a8255d3190d00;hp=b3e1a0ceae5fb852c2f8fd653bfa2675cd193022;hpb=c52405fad9c070e1370f852234c6eb723b52916c;p=blank.git diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index b3e1a0c..5f5474b 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -2,19 +2,24 @@ #include "Controller.hpp" #include "RandomWalk.hpp" +#include "../model/geometry.hpp" #include "../world/Entity.hpp" +#include "../world/World.hpp" +#include "../world/WorldCollision.hpp" #include namespace blank { -Chaser::Chaser(Entity &ctrl, Entity &tgt) noexcept +Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept : Controller(ctrl) +, world(world) , tgt(tgt) -, speed(0.002f) -, stop_dist(5 * 5) -, flee_dist(3 * 3) { +, chase_speed(0.002f) +, flee_speed(-0.005f) +, stop_dist(10) +, flee_dist(5) { } @@ -24,12 +29,23 @@ 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); + float dist = length(diff); + glm::vec3 norm_diff(diff / dist); + + bool line_of_sight = true; + // FIXME: this only works if target is in the reference chunk (which is true for the player) + Ray aim{Target().Position() - diff, norm_diff}; + WorldCollision coll; + if (world.Intersection(aim, glm::mat4(1.0f), coll)) { + line_of_sight = coll.depth > dist; + } + + if (!line_of_sight) { + Controlled().Velocity(glm::vec3(0.0f)); + } else if (dist > stop_dist) { + Controlled().Velocity(norm_diff * chase_speed); } else if (dist < flee_dist) { - Controlled().Velocity(normalize(diff) * -speed); + Controlled().Velocity(norm_diff * flee_speed); } else { Controlled().Velocity(glm::vec3(0.0f)); }