From: Daniel Karbach Date: Tue, 4 Aug 2015 16:30:29 +0000 (+0200) Subject: move RandomWalk into new "ai" module X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=745729c1935276e6f49d108a0a465214aa93c3cb;p=blank.git move RandomWalk into new "ai" module --- diff --git a/src/ai/RandomWalk.cpp b/src/ai/RandomWalk.cpp new file mode 100644 index 0000000..32fc29f --- /dev/null +++ b/src/ai/RandomWalk.cpp @@ -0,0 +1,57 @@ +#include "RandomWalk.hpp" + +#include "../world/Entity.hpp" + + +namespace blank { + +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 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); +} + +} diff --git a/src/ai/RandomWalk.hpp b/src/ai/RandomWalk.hpp new file mode 100644 index 0000000..0a61974 --- /dev/null +++ b/src/ai/RandomWalk.hpp @@ -0,0 +1,28 @@ +#ifndef BLANK_APP_RANDOMWALK_HPP_ +#define BLANK_APP_RANDOMWALK_HPP_ + +#include + + +namespace blank { + +class Entity; + +/// Randomly start or stop moving in axis directions every now and then. +class RandomWalk { + +public: + explicit RandomWalk(Entity &) noexcept; + + void Update(int dt) noexcept; + +private: + Entity &entity; + + int time_left; + +}; + +} + +#endif diff --git a/src/app/Application.hpp b/src/app/Application.hpp index 5909e3b..9644794 100644 --- a/src/app/Application.hpp +++ b/src/app/Application.hpp @@ -3,7 +3,7 @@ #include "Assets.hpp" #include "FrameCounter.hpp" -#include "RandomWalk.hpp" +#include "../ai/RandomWalk.hpp" #include "../audio/Audio.hpp" #include "../graphics/Viewport.hpp" #include "../ui/Interface.hpp" diff --git a/src/app/FPSController.cpp b/src/app/FPSController.cpp new file mode 100644 index 0000000..9c7820c --- /dev/null +++ b/src/app/FPSController.cpp @@ -0,0 +1,49 @@ +#include "FPSController.hpp" + +#include +#include + + +namespace blank { + +FPSController::FPSController(Entity &entity) noexcept +: entity(entity) +, pitch(0) +, yaw(0) { + +} + + +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)); +} + +} diff --git a/src/app/RandomWalk.hpp b/src/app/RandomWalk.hpp deleted file mode 100644 index 0a61974..0000000 --- a/src/app/RandomWalk.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef BLANK_APP_RANDOMWALK_HPP_ -#define BLANK_APP_RANDOMWALK_HPP_ - -#include - - -namespace blank { - -class Entity; - -/// Randomly start or stop moving in axis directions every now and then. -class RandomWalk { - -public: - explicit RandomWalk(Entity &) noexcept; - - void Update(int dt) noexcept; - -private: - Entity &entity; - - int time_left; - -}; - -} - -#endif diff --git a/src/app/controller.cpp b/src/app/controller.cpp deleted file mode 100644 index 9cf2d6c..0000000 --- a/src/app/controller.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "FPSController.hpp" -#include "RandomWalk.hpp" - -#include -#include - - -namespace blank { - -FPSController::FPSController(Entity &entity) noexcept -: entity(entity) -, pitch(0) -, yaw(0) { - -} - - -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)); -} - - -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 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); -} - -}