]> git.localhorst.tv Git - blank.git/commitdiff
split entity from controller
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 8 Mar 2015 23:24:24 +0000 (00:24 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 8 Mar 2015 23:24:24 +0000 (00:24 +0100)
src/app.cpp
src/app.hpp
src/controller.cpp
src/controller.hpp
src/entity.cpp [new file with mode: 0644]
src/entity.hpp [new file with mode: 0644]
src/world.cpp
src/world.hpp

index 539858f58df74ba0adc865adda884169bcbd94f9..202f98c5b4fe46af2ae85a8824d69908fc354a31 100644 (file)
@@ -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;
index 0443942108906b14e59dcaf87b10ff17fae1a376..0b0e64e46681c26d167c5214e2d8193c256ca0eb 100644 (file)
@@ -42,6 +42,7 @@ private:
        Camera cam;
        HUD hud;
        World world;
+       FPSController controller;
 
        OutlineModel outline;
        bool outline_visible;
index 3911e0f26c5c0716226a9303ae3c6411dc56bb40..ef09ace617c244261e4d874375334a176b4f1c47 100644 (file)
@@ -1,20 +1,15 @@
 #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)
+: 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));
 }
 
 }
index 853eeff3bd244603727866d03df28f0f858621b7..63c102f489445a26bf241309ba0153b15de4c7d8 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef BLANK_CONTROLLER_HPP_
 #define BLANK_CONTROLLER_HPP_
 
+#include "entity.hpp"
 #include "geometry.hpp"
 
 #include <SDL.h>
@@ -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 (file)
index 0000000..ef9275f
--- /dev/null
@@ -0,0 +1,57 @@
+#include "entity.hpp"
+
+#include <glm/gtx/transform.hpp>
+
+
+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 (file)
index 0000000..59a3388
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef BLANK_ENTITY_HPP_
+#define BLANK_ENTITY_HPP_
+
+#include "geometry.hpp"
+
+#include <glm/glm.hpp>
+
+
+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
index d115378f10b6926e771e65eb9fad34d6ccdb44e1..0121a98a83e36ed13a87b48b93fe06d688665ebb 100644 (file)
@@ -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
index 60ec6f4dbeca36c1b7364f4834ad3cec54a3e3b7..8ef2b21b2b05ee7d7106a8af536e0a2bf36e85b4 100644 (file)
@@ -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<Chunk> &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<Chunk> loaded;
        std::list<Chunk> to_generate;