2 #include "Controller.hpp"
3 #include "RandomWalk.hpp"
5 #include "../model/geometry.hpp"
6 #include "../world/Entity.hpp"
7 #include "../world/World.hpp"
8 #include "../world/WorldCollision.hpp"
10 #include <glm/glm.hpp>
15 Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
30 void Chaser::Update(int dt) {
31 glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
32 float dist = length(diff);
33 glm::vec3 norm_diff(diff / dist);
35 bool line_of_sight = true;
36 // FIXME: this only works if target is in the reference chunk (which is true for the player)
37 Ray aim{Target().Position() - diff, norm_diff};
39 if (world.Intersection(aim, glm::mat4(1.0f), coll)) {
40 line_of_sight = coll.depth > dist;
44 Controlled().Velocity(glm::vec3(0.0f));
45 } else if (dist > stop_dist) {
46 Controlled().Velocity(norm_diff * chase_speed);
47 } else if (dist < flee_dist) {
48 Controlled().Velocity(norm_diff * flee_speed);
50 Controlled().Velocity(glm::vec3(0.0f));
55 Controller::Controller(Entity &e) noexcept
60 Controller::~Controller() {
65 RandomWalk::RandomWalk(Entity &e) noexcept
71 RandomWalk::~RandomWalk() {
75 void RandomWalk::Update(int dt) {
77 if (time_left > 0) return;
78 time_left += 2500 + (rand() % 5000);
80 constexpr float move_vel = 0.0005f;
82 glm::vec3 new_vel = Controlled().Velocity();
86 new_vel.x = -move_vel;
95 new_vel.y = -move_vel;
101 new_vel.y = move_vel;
104 new_vel.z = -move_vel;
110 new_vel.z = move_vel;
114 Controlled().Velocity(new_vel);