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().Velocity(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().Velocity(glm::vec3(0.0f));
53 } else if (dist > stop_dist) {
54 Controlled().Velocity(norm_diff * chase_speed);
55 } else if (dist < flee_dist) {
56 Controlled().Velocity(norm_diff * flee_speed);
58 Controlled().Velocity(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)
78 , start_rot(e.AngularVelocity())
79 , target_rot(start_rot)
86 RandomWalk::~RandomWalk() {
90 void RandomWalk::Update(int dt) {
93 if (switch_time < 0) {
94 switch_time += 2500 + (random.Next<unsigned short>() % 5000);
95 lerp_max = 1500 + (random.Next<unsigned short>() % 1000);
98 } else if (lerp_time > 0) {
99 float a = std::min(lerp_time / lerp_max, 1.0f);
100 Controlled().Velocity(mix(target_vel, start_vel, a));
101 Controlled().AngularVelocity(mix(target_rot, start_rot, a));
103 Controlled().Velocity(target_vel);
104 Controlled().AngularVelocity(target_rot);
108 void RandomWalk::Change() noexcept {
109 start_vel = target_vel;
110 start_rot = target_rot;
112 constexpr float base = 0.000001f;
114 target_vel.x = base * (random.Next<short>() % 1024);
115 target_vel.y = base * (random.Next<short>() % 1024);
116 target_vel.z = base * (random.Next<short>() % 1024);
118 target_rot.x = base * (random.Next<short>() % 1024);
119 target_rot.y = base * (random.Next<short>() % 1024);
120 target_rot.z = base * (random.Next<short>() % 1024);