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 Ray aim{Target().Position() - diff, norm_diff};
38 if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) {
39 line_of_sight = coll.depth > dist;
43 Controlled().Velocity(glm::vec3(0.0f));
44 } else if (dist > stop_dist) {
45 Controlled().Velocity(norm_diff * chase_speed);
46 } else if (dist < flee_dist) {
47 Controlled().Velocity(norm_diff * flee_speed);
49 Controlled().Velocity(glm::vec3(0.0f));
54 Controller::Controller(Entity &e) noexcept
59 Controller::~Controller() {
64 RandomWalk::RandomWalk(Entity &e) noexcept
70 RandomWalk::~RandomWalk() {
74 void RandomWalk::Update(int dt) {
76 if (time_left > 0) return;
77 time_left += 2500 + (rand() % 5000);
79 constexpr float move_vel = 0.0005f;
81 glm::vec3 new_vel = Controlled().Velocity();
85 new_vel.x = -move_vel;
94 new_vel.y = -move_vel;
100 new_vel.y = move_vel;
103 new_vel.z = -move_vel;
109 new_vel.z = move_vel;
113 Controlled().Velocity(new_vel);