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, std::uint64_t seed) noexcept
71 RandomWalk::~RandomWalk() {
75 void RandomWalk::Update(int dt) {
77 if (time_left > 0) return;
78 time_left += 2500 + (random.Next<unsigned short>() % 5000);
80 constexpr float move_vel = 0.0005f;
82 glm::vec3 new_vel = Controlled().Velocity();
84 switch (random.Next<unsigned char>() % 9) {
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);