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 if (dist < std::numeric_limits<float>::epsilon()) {
34 Controlled().Velocity(glm::vec3(0.0f));
37 glm::vec3 norm_diff(diff / dist);
39 bool line_of_sight = true;
40 Ray aim{Target().Position() - diff, norm_diff};
42 if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) {
43 line_of_sight = coll.depth > dist;
47 Controlled().Velocity(glm::vec3(0.0f));
48 } else if (dist > stop_dist) {
49 Controlled().Velocity(norm_diff * chase_speed);
50 } else if (dist < flee_dist) {
51 Controlled().Velocity(norm_diff * flee_speed);
53 Controlled().Velocity(glm::vec3(0.0f));
58 Controller::Controller(Entity &e) noexcept
63 Controller::~Controller() {
68 RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept
71 , start_vel(e.Velocity())
72 , target_vel(start_vel)
73 , start_rot(e.AngularVelocity())
74 , target_rot(start_rot)
81 RandomWalk::~RandomWalk() {
85 void RandomWalk::Update(int dt) {
88 if (switch_time < 0) {
89 switch_time += 2500 + (random.Next<unsigned short>() % 5000);
90 lerp_max = 1500 + (random.Next<unsigned short>() % 1000);
93 } else if (lerp_time > 0) {
94 float a = std::min(lerp_time / lerp_max, 1.0f);
95 Controlled().Velocity(mix(target_vel, start_vel, a));
96 Controlled().AngularVelocity(mix(target_rot, start_rot, a));
98 Controlled().Velocity(target_vel);
99 Controlled().AngularVelocity(target_rot);
103 void RandomWalk::Change() noexcept {
104 start_vel = target_vel;
105 start_rot = target_rot;
107 constexpr float base = 0.000001f;
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);
113 target_rot.x = base * (random.Next<short>() % 1024);
114 target_rot.y = base * (random.Next<short>() % 1024);
115 target_rot.z = base * (random.Next<short>() % 1024);