From 46509f82dcea114b004c53a7f3a9608f2518077f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 16 Mar 2015 18:28:34 +0100 Subject: [PATCH] random walk test controller --- src/app.cpp | 10 ++++++++++ src/app.hpp | 5 +++++ src/controller.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/controller.hpp | 15 ++++++++++++++ src/entity.cpp | 5 +++-- src/entity.hpp | 7 +++---- src/world.cpp | 5 ----- 7 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 4a890d5..cfb1682 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -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); } diff --git a/src/app.hpp b/src/app.hpp index 6bd4e0d..413b6f1 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -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; }; diff --git a/src/controller.cpp b/src/controller.cpp index b996f9e..fcdb407 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -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); +} + } diff --git a/src/controller.hpp b/src/controller.hpp index c13a14e..7dd3d11 100644 --- a/src/controller.hpp +++ b/src/controller.hpp @@ -39,6 +39,21 @@ private: }; + +class RandomWalk { + +public: + explicit RandomWalk(Entity &); + + void Update(int dt); + +private: + Entity &entity; + + int time_left; + +}; + } #endif diff --git a/src/entity.cpp b/src/entity.cpp index b248350..52502c2 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,6 +1,7 @@ #include "entity.hpp" -#include "chunk.hpp" +#include "geometry.hpp" +#include "shape.hpp" #include #include @@ -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); diff --git a/src/entity.hpp b/src/entity.hpp index 423cb88..5ef8fbe 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -3,9 +3,7 @@ #include "block.hpp" #include "chunk.hpp" -#include "geometry.hpp" #include "model.hpp" -#include "shape.hpp" #include #include @@ -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; diff --git a/src/world.cpp b/src/world.cpp index 3f186ea..27daecd 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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}); } -- 2.39.2