summaryrefslogtreecommitdiffhomepage
path: root/src/chunk.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-03-14 07:33:47 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-03-14 07:33:47 +0100
commitdc5e66b5a29fd7de8ddf59852ceefd982289b7c3 (patch)
tree18bf0a274f1595d6d2d6cfb32a3b3825d843e315 /src/chunk.cpp
parent29bdd5f2170b9d46a8b3b0973c4c0845d6a2b61e (diff)
a
Diffstat (limited to 'src/chunk.cpp')
-rw-r--r--src/chunk.cpp64
1 files changed, 59 insertions, 5 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp
index 6cc7ee78..6edced05 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -1,5 +1,7 @@
#include "chunk.hpp"
#include "src/tile-atlas.hpp"
+#include "anim-atlas.hpp"
+#include <algorithm>
#include <Magnum/GL/Context.h>
namespace floormat {
@@ -22,17 +24,16 @@ bool chunk::empty(bool force) const noexcept
if (!force && !_maybe_empty)
return false;
for (auto i = 0_uz; i < TILE_COUNT; i++)
- if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1] || _scenery_atlases[i])
+ if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1] || !_entities.empty())
return _maybe_empty = false;
+ if (!_entities.empty())
+ return false;
return true;
}
tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); }
tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); }
-std::shared_ptr<anim_atlas>& chunk::scenery_atlas_at(std::size_t i) noexcept { return _scenery_atlases[i]; }
-scenery& chunk::scenery_at(std::size_t i) noexcept { return _scenery_variants[i]; }
-
tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; }
tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); }
tile_ref chunk::operator[](local_coords xy) noexcept { return operator[](xy.to_index()); }
@@ -89,11 +90,64 @@ void chunk::mark_modified() noexcept
}
chunk::chunk() noexcept = default;
-chunk::~chunk() noexcept = default;
+chunk::~chunk() noexcept
+{
+ _teardown = true;
+ _entities.clear();
+ _rtree.RemoveAll();
+}
chunk::chunk(chunk&&) noexcept = default;
chunk& chunk::operator=(chunk&&) noexcept = default;
bool chunk::bbox::operator==(const bbox& other) const noexcept = default;
+void chunk::add_entity_unsorted(const std::shared_ptr<entity>& e)
+{
+ if (e->atlas->info().fps == 0)
+ mark_scenery_modified(false);
+ if (bbox bb; _bbox_for_scenery(*e, bb))
+ _add_bbox(bb);
+ _entities.push_back(e);
+}
+
+void chunk::sort_entities()
+{
+ mark_scenery_modified(false);
+
+ std::sort(_entities.begin(), _entities.end(), [](const auto& a, const auto& b) {
+ return a->ordinal() < b->ordinal();
+ });
+}
+
+void chunk::add_entity(const std::shared_ptr<entity>& e)
+{
+ if (e->atlas->info().fps == 0)
+ mark_scenery_modified(false);
+ if (bbox bb; _bbox_for_scenery(*e, bb))
+ _add_bbox(bb);
+
+ _entities.reserve(_entities.size() + 1);
+ auto it = std::lower_bound(_entities.cbegin(), _entities.cend(), e, [ord = e->ordinal()](const auto& a, const auto&) {
+ return a->ordinal() < ord;
+ });
+ _entities.insert(it, e);
+}
+
+void chunk::remove_entity(entity_const_iterator it)
+{
+ const auto& e = *it;
+ if (e->atlas->info().fps == 0)
+ mark_scenery_modified(false);
+ if (bbox bb; _bbox_for_scenery(*e, bb))
+ _remove_bbox(bb);
+
+ _entities.erase(it);
+}
+
+const std::vector<std::shared_ptr<entity>>& chunk::entities() const
+{
+ return _entities;
+}
+
} // namespace floormat