4 #include "RandomWalk.hpp"
5 #include "../model/shapes.hpp"
6 #include "../world/BlockLookup.hpp"
7 #include "../world/BlockType.hpp"
8 #include "../world/Entity.hpp"
9 #include "../world/World.hpp"
14 Spawner::Spawner(World &world)
18 , despawn_range(128 * 128)
19 , spawn_distance(16 * 16)
22 EntityModel::Buffer buf;
24 AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
25 CuboidShape shape(bounds);
26 shape.Vertices(buf, 1.0f);
27 buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
28 models[0].Update(buf);
29 skeletons[0].Bounds(bounds);
30 skeletons[0].SetNodeModel(&models[0]);
33 AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
34 CuboidShape shape(bounds);
36 shape.Vertices(buf, 2.0f);
37 buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
38 models[1].Update(buf);
39 skeletons[1].Bounds(bounds);
40 skeletons[1].SetNodeModel(&models[1]);
43 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
44 StairShape shape(bounds, { 0.4f, 0.4f });
46 shape.Vertices(buf, 3.0f);
47 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
48 models[2].Update(buf);
49 skeletons[2].Bounds(bounds);
50 skeletons[2].SetNodeModel(&models[2]);
54 Spawn(world.Player().ChunkCoords(), { 0.5f, 0.5f, 0.5f });
58 for (auto &ctrl : controllers) {
64 void Spawner::Update(int dt) {
70 for (auto &ctrl : controllers) {
76 void Spawner::CheckDespawn() noexcept {
77 const Entity &reference = world.Player();
78 for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
79 Entity &e = (*iter)->Controlled();
82 iter = controllers.erase(iter);
85 glm::vec3 diff(reference.AbsoluteDifference(e));
86 if (dot(diff, diff) > despawn_range) {
89 iter = controllers.erase(iter);
96 void Spawner::TrySpawn() {
97 if (controllers.size() >= max_entities) return;
100 (rand() % (chunk_range * 2 + 1)) - chunk_range,
101 (rand() % (chunk_range * 2 + 1)) - chunk_range,
102 (rand() % (chunk_range * 2 + 1)) - chunk_range
106 rand() % Chunk::width,
107 rand() % Chunk::height,
108 rand() % Chunk::depth
113 glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + world.Player().Position());
114 float dist = dot(diff, diff);
115 if (dist > despawn_range || dist < spawn_distance) {
119 // check if the spawn block and the one above it are loaded and inhabitable
120 BlockLookup spawn_block(&world.PlayerChunk(), chunk * Chunk::Extent() + pos);
121 if (!spawn_block || spawn_block.GetType().collide_block) {
125 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
126 if (!head_block || head_block.GetType().collide_block) {
130 Spawn(world.Player().ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
133 void Spawner::Spawn(const glm::ivec3 &chunk, const glm::vec3 &pos) {
134 glm::vec3 rot(0.000001f);
135 rot.x *= (rand() % 1024);
136 rot.y *= (rand() % 1024);
137 rot.z *= (rand() % 1024);
139 Entity &e = world.AddEntity();
141 e.Position(chunk, pos);
142 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
143 e.WorldCollidable(true);
144 skeletons[rand() % 3].Instantiate(e.GetModel());
145 e.AngularVelocity(rot);
148 ctrl = new RandomWalk(e);
150 ctrl = new Chaser(world, e, world.Player());
152 controllers.emplace_back(ctrl);