From: Daniel Karbach Date: Tue, 8 Sep 2015 14:12:31 +0000 (+0200) Subject: split graphics stuff from AI spawner X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=43820582217f7e4b707d98f2e69d969eb77fc7c3;p=blank.git split graphics stuff from AI spawner --- diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp index 6a692b1..8ba32ff 100644 --- a/src/ai/Spawner.cpp +++ b/src/ai/Spawner.cpp @@ -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() % 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() % 3].Instantiate(e.GetModel()); + skeletons[random.Next() % skeletons.Size()].Instantiate(e.GetModel()); e.AngularVelocity(rot); Controller *ctrl; if (random()) { diff --git a/src/ai/Spawner.hpp b/src/ai/Spawner.hpp index a1181d3..d4c0a57 100644 --- a/src/ai/Spawner.hpp +++ b/src/ai/Spawner.hpp @@ -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 @@ -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 controllers; - EntityModel models[3]; - CompositeModel skeletons[3]; - GaloisLFSR random; IntervalTimer timer; diff --git a/src/app/ServerState.cpp b/src/app/ServerState.cpp index 88e817d..c2163ad 100644 --- a/src/app/ServerState.cpp +++ b/src/app/ServerState.cpp @@ -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); } diff --git a/src/app/ServerState.hpp b/src/app/ServerState.hpp index 5a3c0dc..aa7e779 100644 --- a/src/app/ServerState.hpp +++ b/src/app/ServerState.hpp @@ -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; diff --git a/src/app/WorldState.cpp b/src/app/WorldState.cpp index db8c963..8c1e93d 100644 --- a/src/app/WorldState.cpp +++ b/src/app/WorldState.cpp @@ -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(); } diff --git a/src/app/WorldState.hpp b/src/app/WorldState.hpp index d47024f..c0b4ce1 100644 --- a/src/app/WorldState.hpp +++ b/src/app/WorldState.hpp @@ -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 index 0000000..c91abe1 --- /dev/null +++ b/src/model/Skeletons.hpp @@ -0,0 +1,35 @@ +#ifndef BLANK_MODEL_SKELETONS_HPP_ +#define BLANK_MODEL_SKELETONS_HPP_ + +#include +#include + + +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> skeletons; + std::vector models; + +}; + +} + +#endif diff --git a/src/model/composite.cpp b/src/model/composite.cpp index 4cf2f12..ffb5857 100644 --- a/src/model/composite.cpp +++ b/src/model/composite.cpp @@ -1,7 +1,9 @@ #include "CompositeModel.hpp" #include "CompositeInstance.hpp" +#include "Skeletons.hpp" #include "EntityModel.hpp" +#include "shapes.hpp" #include "../graphics/DirectionalLighting.hpp" #include @@ -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]); + } +} + }