From cb959294a8271969ddfe411471d7f04e82c4788a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 9 Mar 2015 00:24:24 +0100 Subject: [PATCH] split entity from controller --- src/app.cpp | 8 ++++--- src/app.hpp | 1 + src/controller.cpp | 38 ++++--------------------------- src/controller.hpp | 18 ++++----------- src/entity.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++ src/entity.hpp | 44 +++++++++++++++++++++++++++++++++++ src/world.cpp | 1 + src/world.hpp | 6 ++--- 8 files changed, 120 insertions(+), 53 deletions(-) create mode 100644 src/entity.cpp create mode 100644 src/entity.hpp diff --git a/src/app.cpp b/src/app.cpp index 539858f..202f98c 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -19,6 +19,7 @@ Application::Application() , cam() , hud() , world() +, controller(world.Player()) , outline() , outline_visible(false) , outline_transform(1.0f) @@ -68,7 +69,7 @@ void Application::HandleEvents() { switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: - world.Controller().HandleKeyboard(event.key); + controller.HandleKeyboard(event.key); break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == 1) { @@ -83,7 +84,7 @@ void Application::HandleEvents() { } break; case SDL_MOUSEMOTION: - world.Controller().HandleMouse(event.motion); + controller.HandleMouse(event.motion); break; case SDL_QUIT: running = false; @@ -105,9 +106,10 @@ void Application::HandleEvents() { } void Application::Update(int dt) { + controller.Update(dt); world.Update(dt); - Ray aim = world.Controller().Aim(); + Ray aim = controller.Aim(); Chunk *chunk; int blkid; float dist; diff --git a/src/app.hpp b/src/app.hpp index 0443942..0b0e64e 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -42,6 +42,7 @@ private: Camera cam; HUD hud; World world; + FPSController controller; OutlineModel outline; bool outline_visible; diff --git a/src/controller.cpp b/src/controller.cpp index 3911e0f..ef09ace 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -1,20 +1,15 @@ #include "controller.hpp" -#include #include #include -#include namespace blank { -FPSController::FPSController() -: velocity(0, 0, 0) -, position(0, 0, 0) +FPSController::FPSController(Entity &entity) +: entity(entity) , pitch(0) , yaw(0) -, transform(1.0f) -, dirty(true) , move_velocity(0.003f) , pitch_sensitivity(-0.0025f) , yaw_sensitivity(-0.001f) @@ -28,28 +23,6 @@ FPSController::FPSController() } -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) { pitch = p; if (pitch > PI / 2) { @@ -57,7 +30,6 @@ void FPSController::Pitch(float p) { } else if (pitch < -PI / 2) { pitch = -PI / 2; } - dirty = true; } void FPSController::RotatePitch(float delta) { @@ -71,7 +43,6 @@ void FPSController::Yaw(float y) { } else if (yaw < -PI) { yaw += PI * 2; } - dirty = true; } void FPSController::RotateYaw(float delta) { @@ -127,9 +98,8 @@ void FPSController::Update(int dt) { } else if (front && !back) { vel.z = -move_velocity; } - OrientationVelocity(vel); - - Move(velocity * float(dt)); + entity.Rotation(glm::eulerAngleYX(yaw, pitch)); + entity.Velocity(glm::rotateY(vel, yaw)); } } diff --git a/src/controller.hpp b/src/controller.hpp index 853eeff..63c102f 100644 --- a/src/controller.hpp +++ b/src/controller.hpp @@ -1,6 +1,7 @@ #ifndef BLANK_CONTROLLER_HPP_ #define BLANK_CONTROLLER_HPP_ +#include "entity.hpp" #include "geometry.hpp" #include @@ -12,15 +13,9 @@ namespace blank { class FPSController { public: - FPSController(); + explicit FPSController(Entity &); - const glm::mat4 &Transform() const; - Ray Aim() const; - - void Velocity(glm::vec3 vel) { velocity = vel; dirty = true; } - void OrientationVelocity(const glm::vec3 &vel); - void Position(glm::vec3 pos) { position = pos; dirty = true; } - void Move(glm::vec3 delta) { Position(position + delta); } + Ray Aim() const { return entity.Aim(); } // all angles in radians (full circle = 2π) float Pitch() const { return pitch; } @@ -36,14 +31,11 @@ public: void Update(int dt); private: - glm::vec3 velocity; - glm::vec3 position; + Entity &entity; + float pitch; float yaw; - mutable glm::mat4 transform; - mutable bool dirty; - float move_velocity; float pitch_sensitivity; float yaw_sensitivity; diff --git a/src/entity.cpp b/src/entity.cpp new file mode 100644 index 0000000..ef9275f --- /dev/null +++ b/src/entity.cpp @@ -0,0 +1,57 @@ +#include "entity.hpp" + +#include + + +namespace blank { + +Entity::Entity() +: velocity() +, position() +, rotation(1.0f) +, transform(1.0f) +, dirty(false) { + +} + + +void Entity::Velocity(const glm::vec3 &vel) { + velocity = vel; +} + +void Entity::Position(const glm::vec3 &pos) { + position = pos; + dirty = true; +} + +void Entity::Move(const glm::vec3 &delta) { + position += delta; + dirty = true; +} + +void Entity::Rotation(const glm::mat4 &rot) { + rotation = rot; +} + +const glm::mat4 &Entity::Transform() const { + if (dirty) { + transform = glm::translate(position) * rotation; + dirty = false; + } + return transform; +} + +Ray Entity::Aim() const { + Transform(); + 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 Entity::Update(int dt) { + Move(velocity * float(dt)); +} + +} diff --git a/src/entity.hpp b/src/entity.hpp new file mode 100644 index 0000000..59a3388 --- /dev/null +++ b/src/entity.hpp @@ -0,0 +1,44 @@ +#ifndef BLANK_ENTITY_HPP_ +#define BLANK_ENTITY_HPP_ + +#include "geometry.hpp" + +#include + + +namespace blank { + +class Entity { + +public: + Entity(); + + const glm::vec3 &Velocity() const { return velocity; } + void Velocity(const glm::vec3 &); + + const glm::vec3 &Position() const { return position; } + void Position(const glm::vec3 &); + void Move(const glm::vec3 &delta); + + const glm::mat4 &Rotation() const { return rotation; } + void Rotation(const glm::mat4 &); + + const glm::mat4 &Transform() const; + Ray Aim() const; + + void Update(int dt); + +private: + glm::vec3 velocity; + glm::vec3 position; + + glm::mat4 rotation; + + mutable glm::mat4 transform; + mutable bool dirty; + +}; + +} + +#endif diff --git a/src/world.cpp b/src/world.cpp index d115378..0121a98 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -13,6 +13,7 @@ World::World() , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }}) , blockNoise(0) , colorNoise(1) +, player() , loaded() , to_generate() { blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block diff --git a/src/world.hpp b/src/world.hpp index 60ec6f4..8ef2b21 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -3,7 +3,7 @@ #include "block.hpp" #include "chunk.hpp" -#include "controller.hpp" +#include "entity.hpp" #include "noise.hpp" #include "shader.hpp" #include "shape.hpp" @@ -32,7 +32,7 @@ public: BlockTypeRegistry &BlockTypes() { return blockType; } std::list &LoadedChunks() { return loaded; } - FPSController &Controller() { return player; } + Entity &Player() { return player; } Chunk &Next(const Chunk &, const glm::vec3 &dir); @@ -52,7 +52,7 @@ private: SimplexNoise blockNoise; SimplexNoise colorNoise; - FPSController player; + Entity player; std::list loaded; std::list to_generate; -- 2.39.2