]> git.localhorst.tv Git - blank.git/commitdiff
split graphics stuff from AI spawner
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 8 Sep 2015 14:12:31 +0000 (16:12 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 8 Sep 2015 14:15:23 +0000 (16:15 +0200)
src/ai/Spawner.cpp
src/ai/Spawner.hpp
src/app/ServerState.cpp
src/app/ServerState.hpp
src/app/WorldState.cpp
src/app/WorldState.hpp
src/model/Skeletons.hpp [new file with mode: 0644]
src/model/composite.cpp

index 6a692b11077b47f3d7b871505d0ab2db0ab606d3..8ba32ffaf0e17ee6e5c50a179efdb94493a42dff 100644 (file)
@@ -2,7 +2,8 @@
 
 #include "Chaser.hpp"
 #include "RandomWalk.hpp"
-#include "../model/shapes.hpp"
+#include "../model/CompositeModel.hpp"
+#include "../model/Skeletons.hpp"
 #include "../world/BlockLookup.hpp"
 #include "../world/BlockType.hpp"
 #include "../world/Entity.hpp"
@@ -11,8 +12,9 @@
 
 namespace blank {
 
-Spawner::Spawner(World &world, std::uint64_t seed)
+Spawner::Spawner(World &world, Skeletons &skeletons, std::uint64_t seed)
 : world(world)
+, skeletons(skeletons)
 , controllers()
 , random(seed)
 , timer(64)
@@ -20,37 +22,6 @@ Spawner::Spawner(World &world, std::uint64_t seed)
 , spawn_distance(16 * 16)
 , max_entities(16)
 , chunk_range(4) {
-       EntityModel::Buffer buf;
-       {
-               AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
-               CuboidShape shape(bounds);
-               shape.Vertices(buf, 1.0f);
-               buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
-               models[0].Update(buf);
-               skeletons[0].Bounds(bounds);
-               skeletons[0].SetNodeModel(&models[0]);
-       }
-       {
-               AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
-               CuboidShape shape(bounds);
-               buf.Clear();
-               shape.Vertices(buf, 2.0f);
-               buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               models[1].Update(buf);
-               skeletons[1].Bounds(bounds);
-               skeletons[1].SetNodeModel(&models[1]);
-       }
-       {
-               AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
-               StairShape shape(bounds, { 0.4f, 0.4f });
-               buf.Clear();
-               shape.Vertices(buf, 3.0f);
-               buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
-               models[2].Update(buf);
-               skeletons[2].Bounds(bounds);
-               skeletons[2].SetNodeModel(&models[2]);
-       }
-
        timer.Start();
 }
 
@@ -120,7 +91,6 @@ void Spawner::TrySpawn() {
                random.Next<unsigned char>() % Chunk::depth
        );
 
-
        // distance check
        glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.Position());
        float dist = dot(diff, diff);
@@ -155,7 +125,7 @@ void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3
        e.Position(chunk, pos);
        e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
        e.WorldCollidable(true);
-       skeletons[random.Next<unsigned char>() % 3].Instantiate(e.GetModel());
+       skeletons[random.Next<unsigned char>() % skeletons.Size()].Instantiate(e.GetModel());
        e.AngularVelocity(rot);
        Controller *ctrl;
        if (random()) {
index a1181d30bf96f77ec0809228f2079c7d0c7fa88f..d4c0a57f2c6b345d5749c04aa81ef31a22a0b8ca 100644 (file)
@@ -2,8 +2,6 @@
 #define BLANK_AI_SPAWNER_HPP_
 
 #include "../app/IntervalTimer.hpp"
-#include "../model/CompositeModel.hpp"
-#include "../model/EntityModel.hpp"
 #include "../rand/GaloisLFSR.hpp"
 
 #include <vector>
@@ -14,12 +12,13 @@ namespace blank {
 
 class Controller;
 class Entity;
+class Skeletons;
 class World;
 
 class Spawner {
 
 public:
-       Spawner(World &, std::uint64_t seed);
+       Spawner(World &, Skeletons &, std::uint64_t seed);
        ~Spawner();
 
        void Update(int dt);
@@ -31,11 +30,9 @@ private:
 
 private:
        World &world;
+       Skeletons &skeletons;
        std::vector<Controller *> controllers;
 
-       EntityModel models[3];
-       CompositeModel skeletons[3];
-
        GaloisLFSR random;
 
        IntervalTimer timer;
index 88e817d81f1a70d61f5011cead6f7e741619e034..c2163ad161976124f08b5bbcfc3cf1dba450d826 100644 (file)
@@ -18,10 +18,13 @@ ServerState::ServerState(
 : env(env)
 , block_types()
 , world(block_types, wc, ws)
+, skeletons()
+, spawner(world, skeletons, wc.gen.seed)
 , server(sc, world)
 , push_timer(16) {
        TextureIndex tex_index;
        env.loader.LoadBlockTypes("default", block_types, tex_index);
+       skeletons.LoadHeadless();
 
        push_timer.Start();
 
@@ -38,8 +41,11 @@ void ServerState::Handle(const SDL_Event &event) {
 
 void ServerState::Update(int dt) {
        push_timer.Update(dt);
-
        server.Handle();
+       spawner.Update(dt);
+       if (!world.Players().empty()) {
+               world.Update(dt);
+       }
        if (push_timer.Hit()) {
                server.Update(dt);
        }
index 5a3c0dcacaac7c1b9feb5ac52c3e89bdfb0b0434..aa7e779c7dd7acf6736e930f50d463c288543775 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "IntervalTimer.hpp"
 #include "State.hpp"
+#include "../ai/Spawner.hpp"
+#include "../model/Skeletons.hpp"
 #include "../net/Server.hpp"
 #include "../world/BlockTypeRegistry.hpp"
 #include "../world/World.hpp"
@@ -31,6 +33,8 @@ private:
        HeadlessEnvironment &env;
        BlockTypeRegistry block_types;
        World world;
+       Skeletons skeletons;
+       Spawner spawner;
        Server server;
        IntervalTimer push_timer;
 
index db8c9635c22a57a734d902fdd06ac8bd873dec17..8c1e93dd47dcfdf1696dfb34e172f5fe5051acec 100644 (file)
@@ -19,7 +19,8 @@ WorldState::WorldState(
 , block_types()
 , world(block_types, wc, save)
 , chunk_renderer(world, wc.load.load_dist)
-, spawner(world, wc.gen.seed)
+, skeletons()
+, spawner(world, skeletons, wc.gen.seed)
 , interface(ic, env, world, *world.AddPlayer(ic.player_name))
 , preload(env, world.Loader(), chunk_renderer)
 , unload(env, world.Loader()) {
@@ -27,6 +28,7 @@ WorldState::WorldState(
        env.loader.LoadBlockTypes("default", block_types, tex_index);
        chunk_renderer.LoadTextures(env.loader, tex_index);
        chunk_renderer.FogDensity(wc.fog_density);
+       skeletons.Load();
        // TODO: better solution for initializing HUD
        interface.SelectNext();
 }
index d47024ff6633ed1fb65cb07dd29d1c8ba6ce2080..c0b4ce10a23829f8012d9a4c144d3d2e2a971ba4 100644 (file)
@@ -5,6 +5,7 @@
 #include "State.hpp"
 #include "UnloadState.hpp"
 #include "../ai/Spawner.hpp"
+#include "../model/Skeletons.hpp"
 #include "../ui/Interface.hpp"
 #include "../world/BlockTypeRegistry.hpp"
 #include "../world/ChunkRenderer.hpp"
@@ -40,6 +41,7 @@ private:
        BlockTypeRegistry block_types;
        World world;
        ChunkRenderer chunk_renderer;
+       Skeletons skeletons;
        Spawner spawner;
        Interface interface;
 
diff --git a/src/model/Skeletons.hpp b/src/model/Skeletons.hpp
new file mode 100644 (file)
index 0000000..c91abe1
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef BLANK_MODEL_SKELETONS_HPP_
+#define BLANK_MODEL_SKELETONS_HPP_
+
+#include <memory>
+#include <vector>
+
+
+namespace blank {
+
+class CompositeModel;
+class EntityModel;
+
+class Skeletons {
+
+public:
+       Skeletons();
+       ~Skeletons();
+
+       void LoadHeadless();
+       void Load();
+
+       std::size_t Size() const noexcept { return skeletons.size(); }
+
+       CompositeModel &operator[](std::size_t i) noexcept { return *skeletons[i]; }
+       const CompositeModel &operator[](std::size_t i) const noexcept { return *skeletons[i]; }
+
+private:
+       std::vector<std::unique_ptr<CompositeModel>> skeletons;
+       std::vector<EntityModel> models;
+
+};
+
+}
+
+#endif
index 4cf2f12f2aef7be9607e4296754d5879f41acee5..ffb58572eae4bafaceea5f14978a8ec36e7b9089 100644 (file)
@@ -1,7 +1,9 @@
 #include "CompositeModel.hpp"
 #include "CompositeInstance.hpp"
+#include "Skeletons.hpp"
 
 #include "EntityModel.hpp"
+#include "shapes.hpp"
 #include "../graphics/DirectionalLighting.hpp"
 
 #include <glm/gtx/quaternion.hpp>
@@ -99,4 +101,64 @@ void CompositeInstance::Render(const glm::mat4 &M, DirectionalLighting &prog) co
        }
 }
 
+
+Skeletons::Skeletons()
+: skeletons()
+, models() {
+
+}
+
+Skeletons::~Skeletons() {
+
+}
+
+void Skeletons::LoadHeadless() {
+       skeletons.clear();
+       skeletons.reserve(3);
+       {
+               AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
+               skeletons.emplace_back(new CompositeModel);
+               skeletons[0]->Bounds(bounds);
+       }
+       {
+               AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
+               skeletons.emplace_back(new CompositeModel);
+               skeletons[1]->Bounds(bounds);
+       }
+       {
+               AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
+               skeletons.emplace_back(new CompositeModel);
+               skeletons[2]->Bounds(bounds);
+       }
+}
+
+void Skeletons::Load() {
+       LoadHeadless();
+       models.resize(3);
+       EntityModel::Buffer buf;
+       {
+               CuboidShape shape(skeletons[0]->Bounds());
+               shape.Vertices(buf, 1.0f);
+               buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
+               models[0].Update(buf);
+               skeletons[0]->SetNodeModel(&models[0]);
+       }
+       {
+               CuboidShape shape(skeletons[1]->Bounds());
+               buf.Clear();
+               shape.Vertices(buf, 2.0f);
+               buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               models[1].Update(buf);
+               skeletons[1]->SetNodeModel(&models[1]);
+       }
+       {
+               StairShape shape(skeletons[2]->Bounds(), { 0.4f, 0.4f });
+               buf.Clear();
+               shape.Vertices(buf, 3.0f);
+               buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
+               models[2].Update(buf);
+               skeletons[2]->SetNodeModel(&models[2]);
+       }
+}
+
 }