From 79bff420037f150bd6efd2eef08bd06afafeb068 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 4 Aug 2015 18:48:19 +0200 Subject: [PATCH] moved entity spawn and control into its own class called "Spawner" if that is somehow non-obvious --- src/ai/Spawner.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/ai/Spawner.hpp | 32 ++++++++++++++++++++++++++++++++ src/app/Application.hpp | 6 ++---- src/app/app.cpp | 15 ++------------- 4 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 src/ai/Spawner.cpp create mode 100644 src/ai/Spawner.hpp diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp new file mode 100644 index 0000000..d97aa86 --- /dev/null +++ b/src/ai/Spawner.cpp @@ -0,0 +1,41 @@ +#include "Spawner.hpp" + +#include "RandomWalk.hpp" +#include "../world/BlockType.hpp" +#include "../world/BlockTypeRegistry.hpp" +#include "../world/Entity.hpp" +#include "../world/World.hpp" + + +namespace blank { + +Spawner::Spawner(World &world) +: world(world) +, controllers() { + Spawn({ 0.0f, 0.0f, 0.0f }); +} + +Spawner::~Spawner() { + +} + + +void Spawner::Update(int dt) { + for (auto &ctrl : controllers) { + ctrl.Update(dt); + } +} + + +void Spawner::Spawn(const glm::vec3 &pos) { + Entity &e = world.AddEntity(); + e.Name("test"); + e.Position(pos); + e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } }); + e.WorldCollidable(true); + e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f }); + e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f })); + controllers.emplace_back(e); +} + +} diff --git a/src/ai/Spawner.hpp b/src/ai/Spawner.hpp new file mode 100644 index 0000000..e3e8f24 --- /dev/null +++ b/src/ai/Spawner.hpp @@ -0,0 +1,32 @@ +#ifndef BLANK_AI_SPAWNER_HPP_ +#define BLANK_AI_SPAWNER_HPP_ + +#include +#include + + +namespace blank { + +class RandomWalk; +class World; + +class Spawner { + +public: + explicit Spawner(World &); + ~Spawner(); + + void Update(int dt); + +private: + void Spawn(const glm::vec3 &); + +private: + World &world; + std::list controllers; + +}; + +} + +#endif diff --git a/src/app/Application.hpp b/src/app/Application.hpp index 9644794..9732349 100644 --- a/src/app/Application.hpp +++ b/src/app/Application.hpp @@ -3,7 +3,7 @@ #include "Assets.hpp" #include "FrameCounter.hpp" -#include "../ai/RandomWalk.hpp" +#include "../ai/Spawner.hpp" #include "../audio/Audio.hpp" #include "../graphics/Viewport.hpp" #include "../ui/Interface.hpp" @@ -54,8 +54,6 @@ public: /// push the current state to display void Render(); - static Entity &MakeTestEntity(World &); - private: Window &window; Viewport viewport; @@ -66,7 +64,7 @@ private: World world; Interface interface; - RandomWalk test_controller; + Spawner spawner; bool running; diff --git a/src/app/app.cpp b/src/app/app.cpp index 3111091..86c90d8 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -36,7 +36,7 @@ Application::Application(Window &win, const Config &config) , counter() , world(config.world) , interface(config.interface, assets, audio, counter, world) -, test_controller(MakeTestEntity(world)) +, spawner(world) , running(false) { viewport.VSync(config.vsync); } @@ -45,17 +45,6 @@ Application::~Application() { audio.StopAll(); } -Entity &Application::MakeTestEntity(World &world) { - Entity &e = world.AddEntity(); - e.Name("test"); - e.Position({ 0.0f, 0.0f, 0.0f }); - e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } }); - e.WorldCollidable(true); - 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(); @@ -161,7 +150,7 @@ void Application::Handle(const SDL_WindowEvent &event) { void Application::Update(int dt) { counter.EnterUpdate(); interface.Update(dt); - test_controller.Update(dt); + spawner.Update(dt); world.Update(dt); glm::mat4 trans = world.Player().Transform(Chunk::Pos(0, 0, 0)); -- 2.39.2