]> git.localhorst.tv Git - blank.git/commitdiff
random walk test controller
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 16 Mar 2015 17:28:34 +0000 (18:28 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 16 Mar 2015 19:17:28 +0000 (20:17 +0100)
src/app.cpp
src/app.hpp
src/controller.cpp
src/controller.hpp
src/entity.cpp
src/entity.hpp
src/world.cpp

index 4a890d56a61f0cdc10bf5b7a7d9b5cfc3661d256..cfb16822897a01027da39d62c3624da6fe7fd52b 100644 (file)
@@ -17,12 +17,21 @@ Application::Application()
 , cam()
 , world()
 , interface(world)
+, test_controller(MakeTestEntity(world))
 , running(false) {
        GLContext::EnableVSync();
 
        glClearColor(0.0, 0.0, 0.0, 1.0);
 }
 
+Entity &Application::MakeTestEntity(World &world) {
+       Entity &e = world.AddEntity();
+       e.Position({ 0.0f, 0.0f, 0.0f });
+       e.SetShape(world.BlockTypes()[1]->shape, { 1.0f, 1.0f, 0.0f });
+       e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
+       return e;
+}
+
 
 void Application::RunN(size_t n) {
        Uint32 last = SDL_GetTicks();
@@ -116,6 +125,7 @@ void Application::HandleEvents() {
 
 void Application::Update(int dt) {
        interface.Update(dt);
+       test_controller.Update(dt);
        world.Update(dt);
 }
 
index 6bd4e0d9a14a65e615eb62f9f14df5f8b33f5c9b..413b6f19869430f22b665d690d4d8a67cd0ff50d 100644 (file)
@@ -2,6 +2,7 @@
 #define BLANK_APP_HPP_
 
 #include "camera.hpp"
+#include "controller.hpp"
 #include "init.hpp"
 #include "interface.hpp"
 #include "shader.hpp"
@@ -33,6 +34,8 @@ public:
        void Update(int dt);
        void Render();
 
+       static Entity &MakeTestEntity(World &);
+
 private:
        InitSDL init_sdl;
        InitIMG init_img;
@@ -46,6 +49,8 @@ private:
        World world;
        Interface interface;
 
+       RandomWalk test_controller;
+
        bool running;
 
 };
index b996f9e37a04c80301a5776c288b5af09202b38e..fcdb407f43b047885881304584a88562113e580a 100644 (file)
@@ -46,4 +46,54 @@ void FPSController::Update(int dt) {
        entity.Velocity(glm::rotateY(velocity, yaw));
 }
 
+
+RandomWalk::RandomWalk(Entity &e)
+: entity(e)
+, time_left(0) {
+
+}
+
+
+void RandomWalk::Update(int dt) {
+       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);
+}
+
 }
index c13a14ec5c8043fe5e1c6d92006bb33b327a2490..7dd3d110181120b4f60ab5e4015d243dbc8a186a 100644 (file)
@@ -39,6 +39,21 @@ private:
 
 };
 
+
+class RandomWalk {
+
+public:
+       explicit RandomWalk(Entity &);
+
+       void Update(int dt);
+
+private:
+       Entity &entity;
+
+       int time_left;
+
+};
+
 }
 
 #endif
index b24835030884be373db2d3a9adfcaeea2d8f42b5..52502c26f256aceebcd7b2a30958cf98ec84c9ff 100644 (file)
@@ -1,6 +1,7 @@
 #include "entity.hpp"
 
-#include "chunk.hpp"
+#include "geometry.hpp"
+#include "shape.hpp"
 
 #include <cmath>
 #include <glm/gtx/transform.hpp>
@@ -25,7 +26,7 @@ Entity::Entity()
 }
 
 
-void Entity::SetShape(Shape *s, const glm::vec3 &color) {
+void Entity::SetShape(const Shape *s, const glm::vec3 &color) {
        shape = s;
        model_buffer.Clear();
        shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices);
index 423cb88d3a18bb5877b50d5d18e518b4089bc7c9..5ef8fbedd420f5eb244abf0f6b84405be92ba9e5 100644 (file)
@@ -3,9 +3,7 @@
 
 #include "block.hpp"
 #include "chunk.hpp"
-#include "geometry.hpp"
 #include "model.hpp"
-#include "shape.hpp"
 
 #include <glm/glm.hpp>
 #include <glm/gtc/quaternion.hpp>
@@ -13,6 +11,7 @@
 
 namespace blank {
 
+class Ray;
 class Shape;
 
 class Entity {
@@ -22,7 +21,7 @@ public:
 
        bool HasShape() const { return shape; }
        const Shape *GetShape() const { return shape; }
-       void SetShape(Shape *, const glm::vec3 &color);
+       void SetShape(const Shape *, const glm::vec3 &color);
        void SetShapeless();
 
        const glm::vec3 &Velocity() const { return velocity; }
@@ -49,7 +48,7 @@ public:
        void Draw();
 
 private:
-       Shape *shape;
+       const Shape *shape;
        Model model;
 
        glm::vec3 velocity;
index 3f186ea67b298e753f7b3da49b05a64588ba3f2e..27daecd3f27c78ffad686f5a4b0c2b494930b1c7 100644 (file)
@@ -88,11 +88,6 @@ World::World()
        player = &AddEntity();
        player->Position({ 4.0f, 4.0f, 4.0f });
 
-       Entity &test_entity = AddEntity();
-       test_entity.Position({ 0.0f, 0.0f, 0.0f });
-       test_entity.SetShape(&blockShape, { 1.0f, 1.0f, 0.0f });
-       test_entity.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
-
        chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
 }