2 #include "Controller.hpp"
3 #include "RandomWalk.hpp"
5 #include "../model/geometry.hpp"
6 #include "../world/Entity.hpp"
7 #include "../world/World.hpp"
14 Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
29 void Chaser::Update(int dt) {
30 glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
31 float dist = length(diff);
32 glm::vec3 norm_diff(diff / dist);
34 bool line_of_sight = true;
35 // FIXME: this only works if target is in the reference chunk (which is true for the player)
36 Ray aim{Target().Position() - diff, norm_diff};
41 if (world.Intersection(aim, glm::mat4(1.0f), chunk, blkid, distance, normal)) {
42 line_of_sight = distance > dist;
46 Controlled().Velocity(glm::vec3(0.0f));
47 } else if (dist > stop_dist) {
48 Controlled().Velocity(norm_diff * chase_speed);
49 } else if (dist < flee_dist) {
50 Controlled().Velocity(norm_diff * flee_speed);
52 Controlled().Velocity(glm::vec3(0.0f));
57 Controller::Controller(Entity &e) noexcept
62 Controller::~Controller() {
67 RandomWalk::RandomWalk(Entity &e) noexcept
73 RandomWalk::~RandomWalk() {
77 void RandomWalk::Update(int dt) {
79 if (time_left > 0) return;
80 time_left += 2500 + (rand() % 5000);
82 constexpr float move_vel = 0.0005f;
84 glm::vec3 new_vel = Controlled().Velocity();
88 new_vel.x = -move_vel;
97 new_vel.y = -move_vel;
103 new_vel.y = move_vel;
106 new_vel.z = -move_vel;
112 new_vel.z = move_vel;
116 Controlled().Velocity(new_vel);