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 if (Target().Dead()) {
36 glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
37 float dist = length(diff);
38 if (dist < std::numeric_limits<float>::epsilon()) {
39 Controlled().TargetVelocity(glm::vec3(0.0f));
42 glm::vec3 norm_diff(diff / dist);
44 bool line_of_sight = true;
45 Ray aim{Target().Position() - diff, norm_diff};
47 if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) {
48 line_of_sight = coll.depth > dist;
52 Controlled().TargetVelocity(glm::vec3(0.0f));
53 } else if (dist > stop_dist) {
54 Controlled().TargetVelocity(norm_diff * chase_speed);
55 } else if (dist < flee_dist) {
56 Controlled().TargetVelocity(norm_diff * flee_speed);
58 Controlled().TargetVelocity(glm::vec3(0.0f));
63 Controller::Controller(Entity &e) noexcept
68 Controller::~Controller() {
73 RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept
76 , start_vel(e.Velocity())
77 , target_vel(start_vel)
84 RandomWalk::~RandomWalk() {
88 void RandomWalk::Update(int dt) {
91 if (switch_time < 0) {
92 switch_time += 2500 + (random.Next<unsigned short>() % 5000);
93 lerp_max = 1500 + (random.Next<unsigned short>() % 1000);
96 } else if (lerp_time > 0) {
97 float a = std::min(lerp_time / lerp_max, 1.0f);
98 Controlled().TargetVelocity(mix(target_vel, start_vel, a));
100 Controlled().TargetVelocity(target_vel);
104 void RandomWalk::Change() noexcept {
105 start_vel = target_vel;
107 constexpr float base = 0.001f;
109 target_vel.x = base * (random.Next<short>() % 1024);
110 target_vel.y = base * (random.Next<short>() % 1024);
111 target_vel.z = base * (random.Next<short>() % 1024);