#include "controller.hpp"
-#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/rotate_vector.hpp>
-#include <glm/gtx/transform.hpp>
namespace blank {
-FPSController::FPSController()
-: velocity(0, 0, 0)
-, position(0, 0, 0)
+FPSController::FPSController(Entity &entity) noexcept
+: entity(entity)
, pitch(0)
-, yaw(0)
-, transform(1.0f)
-, dirty(true)
-, move_velocity(0.003f)
-, pitch_sensitivity(-0.0025f)
-, yaw_sensitivity(-0.001f)
-, front(false)
-, back(false)
-, left(false)
-, right(false)
-, up(false)
-, down(false) {
+, yaw(0) {
}
-const glm::mat4 &FPSController::Transform() const {
- if (dirty) {
- transform = glm::translate(position) * glm::eulerAngleYX(yaw, pitch);
- dirty = false;
- }
- return transform;
-}
-
-Ray FPSController::Aim() const {
- glm::vec4 from = Transform() * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
- from /= from.w;
- glm::vec4 to = Transform() * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
- to /= to.w;
- return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
-}
-
-
-void FPSController::OrientationVelocity(const glm::vec3 &vel) {
- Velocity(glm::rotateY(vel, yaw));
-}
-
-
-void FPSController::Pitch(float p) {
+void FPSController::Pitch(float p) noexcept {
pitch = p;
if (pitch > PI / 2) {
pitch = PI / 2;
} else if (pitch < -PI / 2) {
pitch = -PI / 2;
}
- dirty = true;
}
-void FPSController::RotatePitch(float delta) {
+void FPSController::RotatePitch(float delta) noexcept {
Pitch(pitch + delta);
}
-void FPSController::Yaw(float y) {
+void FPSController::Yaw(float y) noexcept {
yaw = y;
if (yaw > PI) {
yaw -= PI * 2;
} else if (yaw < -PI) {
yaw += PI * 2;
}
- dirty = true;
}
-void FPSController::RotateYaw(float delta) {
+void FPSController::RotateYaw(float delta) noexcept {
Yaw(yaw + delta);
}
-void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
- switch (event.keysym.sym) {
- case SDLK_w:
- front = event.state == SDL_PRESSED;
+void FPSController::Update(int dt) noexcept {
+ entity.Rotation(glm::eulerAngleYX(yaw, pitch));
+ entity.Velocity(glm::rotateY(velocity, yaw));
+}
+
+
+RandomWalk::RandomWalk(Entity &e) noexcept
+: entity(e)
+, time_left(0) {
+
+}
+
+
+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 SDLK_s:
- back = event.state == SDL_PRESSED;
+ case 1:
+ new_vel.x = 0.0f;
break;
- case SDLK_a:
- left = event.state == SDL_PRESSED;
+ case 2:
+ new_vel.x = move_vel;
break;
- case SDLK_d:
- right = event.state == SDL_PRESSED;
+ case 3:
+ new_vel.y = -move_vel;
break;
- case SDLK_q:
- case SDLK_SPACE:
- up = event.state == SDL_PRESSED;
+ case 4:
+ new_vel.y = 0.0f;
break;
- case SDLK_e:
- case SDLK_LSHIFT:
- down = event.state == SDL_PRESSED;
+ 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;
}
-}
-
-void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
- RotateYaw(event.xrel * yaw_sensitivity);
- RotatePitch(event.yrel * pitch_sensitivity);
-}
-
-
-void FPSController::Update(int dt) {
- glm::vec3 vel;
- if (right && !left) {
- vel.x = move_velocity;
- } else if (left && !right) {
- vel.x = -move_velocity;
- }
- if (up && !down) {
- vel.y = move_velocity;
- } else if (down && !up) {
- vel.y = -move_velocity;
- }
- if (back && !front) {
- vel.z = move_velocity;
- } else if (front && !back) {
- vel.z = -move_velocity;
- }
- OrientationVelocity(vel);
- Move(velocity * float(dt));
+ entity.Velocity(new_vel);
}
}