X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fcontroller.cpp;h=e86551f4c71ff20e8264753b2c5a89b4e8155039;hb=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;hp=d0a026b7069111284a38ef259819c83ed9af2377;hpb=6af76d9e1a6499ebdab405c1d679d24b9e19fded;p=blank.git diff --git a/src/controller.cpp b/src/controller.cpp index d0a026b..e86551f 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -1,34 +1,99 @@ #include "controller.hpp" -#include #include #include -#include namespace blank { -FPSController::FPSController() -: velocity(0, 0, 0) -, position(0, 0, 0) +FPSController::FPSController(Entity &entity) noexcept +: entity(entity) , pitch(0) , yaw(0) { } -glm::mat4 FPSController::Transform() const { - return glm::translate(position) * glm::eulerAngleYX(yaw, pitch); +void FPSController::Pitch(float p) noexcept { + pitch = p; + if (pitch > PI / 2) { + pitch = PI / 2; + } else if (pitch < -PI / 2) { + pitch = -PI / 2; + } +} + +void FPSController::RotatePitch(float delta) noexcept { + Pitch(pitch + delta); +} + +void FPSController::Yaw(float y) noexcept { + yaw = y; + if (yaw > PI) { + yaw -= PI * 2; + } else if (yaw < -PI) { + yaw += PI * 2; + } +} + +void FPSController::RotateYaw(float delta) noexcept { + Yaw(yaw + delta); +} + + +void FPSController::Update(int dt) noexcept { + entity.Rotation(glm::eulerAngleYX(yaw, pitch)); + entity.Velocity(glm::rotateY(velocity, yaw)); } -void FPSController::OrientationVelocity(const glm::vec3 &vel) { - velocity = glm::rotateY(vel, yaw); +RandomWalk::RandomWalk(Entity &e) noexcept +: entity(e) +, time_left(0) { + } -void FPSController::Update(int dt) { - position += velocity * float(dt); +void RandomWalk::Update(int dt) noexcept { + time_left -= dt; + if (time_left > 0) return; + time_left += 2500 + (rand() % 5000); + + constexpr float move_vel = 0.0005f; + + glm::vec3 new_vel = entity.Velocity(); + + switch (rand() % 9) { + case 0: + new_vel.x = -move_vel; + break; + case 1: + new_vel.x = 0.0f; + break; + case 2: + new_vel.x = move_vel; + break; + case 3: + new_vel.y = -move_vel; + break; + case 4: + new_vel.y = 0.0f; + break; + case 5: + new_vel.y = move_vel; + break; + case 6: + new_vel.z = -move_vel; + break; + case 7: + new_vel.z = 0.0f; + break; + case 8: + new_vel.z = move_vel; + break; + } + + entity.Velocity(new_vel); } }