From: Daniel Karbach Date: Wed, 5 Aug 2015 16:31:16 +0000 (+0200) Subject: randomly spawn entities around the player X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=aefc5482b27e3d259b6c9f3f1e4cdd9ef2e6a8d2;p=blank.git randomly spawn entities around the player and remove those too far away --- diff --git a/src/ai/RandomWalk.hpp b/src/ai/RandomWalk.hpp index 0a61974..41319dd 100644 --- a/src/ai/RandomWalk.hpp +++ b/src/ai/RandomWalk.hpp @@ -14,6 +14,9 @@ class RandomWalk { public: explicit RandomWalk(Entity &) noexcept; + Entity &Controlled() noexcept { return entity; } + const Entity &Controlled() const noexcept { return entity; } + void Update(int dt) noexcept; private: diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp index d97aa86..b3a9f24 100644 --- a/src/ai/Spawner.cpp +++ b/src/ai/Spawner.cpp @@ -11,8 +11,14 @@ namespace blank { Spawner::Spawner(World &world) : world(world) -, controllers() { - Spawn({ 0.0f, 0.0f, 0.0f }); +, controllers() +, timer(8096) +, despawn_range(128 * 128) +, spawn_distance(16 * 16) +, max_entities(16) +, chunk_range(4) { + timer.Start(); + Spawn(world.Player().ChunkCoords(), { 0.5f, 0.5f, 0.5f }); } Spawner::~Spawner() { @@ -21,19 +27,81 @@ Spawner::~Spawner() { void Spawner::Update(int dt) { + CheckDespawn(); + timer.Update(dt); + if (timer.Hit()) { + TrySpawn(); + } for (auto &ctrl : controllers) { ctrl.Update(dt); } } -void Spawner::Spawn(const glm::vec3 &pos) { +void Spawner::CheckDespawn() noexcept { + const Entity &reference = world.Player(); + for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) { + Entity &e = iter->Controlled(); + glm::vec3 diff(reference.AbsoluteDifference(e)); + if (dot(diff, diff) > despawn_range) { + e.Remove(); + iter = controllers.erase(iter); + } else { + ++iter; + } + } +} + +void Spawner::TrySpawn() { + if (controllers.size() >= max_entities) return; + + glm::tvec3 chunk( + (rand() % (chunk_range * 2 + 1)) - chunk_range, + (rand() % (chunk_range * 2 + 1)) - chunk_range, + (rand() % (chunk_range * 2 + 1)) - chunk_range + ); + + glm::tvec3 pos( + rand() % Chunk::width, + rand() % Chunk::height, + rand() % Chunk::depth + ); + + + // distance check + glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + world.Player().Position()); + float dist = dot(diff, diff); + if (dist > despawn_range || dist < spawn_distance) { + return; + } + + // block check + // TODO: avoid force load, abort spawn if chunk unavailble + Chunk &tgt_chunk = world.Next(world.PlayerChunk(), chunk); + // TODO: don't use visibility for spawn check + // also, check for more than one block space + if (tgt_chunk.Type(tgt_chunk.BlockAt(pos)).visible) { + return; + } + + Spawn(world.Player().ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f)); +} + +void Spawner::Spawn(const glm::tvec3 &chunk, const glm::vec3 &pos) { + glm::vec3 color(rand() % 6, rand() % 6, rand() % 6); + color = color * 0.15f + 0.25f; + + glm::vec3 rot(0.000001f); + rot.x *= (rand() % 32); + rot.y *= (rand() % 32); + rot.z *= (rand() % 32); + Entity &e = world.AddEntity(); e.Name("test"); - e.Position(pos); + e.Position(chunk, 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.SetShape(world.BlockTypes()[1].shape, color); 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 index e3e8f24..a0da02f 100644 --- a/src/ai/Spawner.hpp +++ b/src/ai/Spawner.hpp @@ -1,6 +1,8 @@ #ifndef BLANK_AI_SPAWNER_HPP_ #define BLANK_AI_SPAWNER_HPP_ +#include "../app/IntervalTimer.hpp" + #include #include @@ -19,12 +21,20 @@ public: void Update(int dt); private: - void Spawn(const glm::vec3 &); + void CheckDespawn() noexcept; + void TrySpawn(); + void Spawn(const glm::tvec3 &, const glm::vec3 &); private: World &world; std::list controllers; + IntervalTimer timer; + float despawn_range; + float spawn_distance; + unsigned int max_entities; + int chunk_range; + }; } diff --git a/src/world/Entity.cpp b/src/world/Entity.cpp index b06dbc3..49ad34b 100644 --- a/src/world/Entity.cpp +++ b/src/world/Entity.cpp @@ -46,6 +46,11 @@ void Entity::Velocity(const glm::vec3 &vel) noexcept { velocity = vel; } +void Entity::Position(const Chunk::Pos &c, const Block::Pos &pos) noexcept { + chunk = c; + position = pos; +} + void Entity::Position(const Block::Pos &pos) noexcept { position = pos; while (position.x >= Chunk::width) { diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index 48646e8..25818c6 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -38,6 +38,7 @@ public: void Velocity(const glm::vec3 &) noexcept; const Block::Pos &Position() const noexcept { return position; } + void Position(const Chunk::Pos &, const Block::Pos &) noexcept; void Position(const Block::Pos &) noexcept; void Move(const glm::vec3 &delta) noexcept; @@ -46,6 +47,9 @@ public: glm::vec3 AbsolutePosition() const noexcept { return glm::vec3(chunk * Chunk::Extent()) + position; } + glm::vec3 AbsoluteDifference(const Entity &other) const noexcept { + return glm::vec3((chunk - other.chunk) * Chunk::Extent()) + position - other.position; + } const glm::quat &AngularVelocity() const noexcept { return angular_velocity; } void AngularVelocity(const glm::quat &) noexcept;