diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-18 02:02:49 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-18 02:02:49 +0100 |
commit | 87b07a5c872ba37676ea02e6f9961307839137b2 (patch) | |
tree | 5e8d02a9658ead0779fd012f6faf426fd0e72b8b /src | |
parent | 142adf90cdc69bbd608ff03354b54db053540bb7 (diff) |
a
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk.cpp | 5 | ||||
-rw-r--r-- | src/chunk.hpp | 3 | ||||
-rw-r--r-- | src/world.cpp | 10 | ||||
-rw-r--r-- | src/world.hpp | 14 |
4 files changed, 18 insertions, 14 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp index a99facf6..e0a09012 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -108,6 +108,7 @@ bool chunk::bbox::operator==(const bbox& other) const noexcept = default; void chunk::add_entity_unsorted(const std::shared_ptr<entity>& e) { + _entities_sorted = false; if (!e->is_dynamic()) mark_scenery_modified(false); if (bbox bb; _bbox_for_scenery(*e, bb)) @@ -117,6 +118,7 @@ void chunk::add_entity_unsorted(const std::shared_ptr<entity>& e) void chunk::sort_entities() { + _entities_sorted = true; mark_scenery_modified(false); std::sort(_entities.begin(), _entities.end(), [](const auto& a, const auto& b) { @@ -126,6 +128,7 @@ void chunk::sort_entities() void chunk::add_entity(const std::shared_ptr<entity>& e) { + fm_assert(_entities_sorted); if (e->atlas->info().fps == 0) mark_scenery_modified(false); if (bbox bb; _bbox_for_scenery(*e, bb)) @@ -140,6 +143,7 @@ void chunk::add_entity(const std::shared_ptr<entity>& e) void chunk::remove_entity(std::size_t i) { + fm_assert(_entities_sorted); fm_debug_assert(i < _entities.size()); const auto& e = _entities[i]; if (!e->is_dynamic()) @@ -152,6 +156,7 @@ void chunk::remove_entity(std::size_t i) const std::vector<std::shared_ptr<entity>>& chunk::entities() const { + fm_assert(_entities_sorted); return _entities; } diff --git a/src/chunk.hpp b/src/chunk.hpp index e825ce33..e51c55ac 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -129,7 +129,8 @@ private: _walls_modified : 1 = true, _scenery_modified : 1 = true, _pass_modified : 1 = true, - _teardown : 1 = false; + _teardown : 1 = false, + _entities_sorted : 1 = true; struct bbox final // NOLINT(cppcoreguidelines-pro-type-member-init) { diff --git a/src/world.cpp b/src/world.cpp index ee1ef459..8743981f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -106,15 +106,19 @@ void world::collect(bool force) fm_debug("world: collected %zu/%zu chunks", len, len0); } -void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos) +void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, bool sorted) { - fm_debug_assert(e->id > 0); + fm_assert(e->id > 0); fm_debug_assert(e->c->world()._unique_id == _unique_id); + fm_assert(!_entities.contains(e->id)); fm_assert(Vector2ui(e->bbox_size).product() > 0); fm_assert(e->type != entity_type::none); e->coord = pos; _entities[e->id] = e; - e->c->add_entity(e); + if (sorted) + e->c->add_entity(e); + else + e->c->add_entity_unsorted(e); } void world::do_kill_entity(std::uint64_t id) diff --git a/src/world.hpp b/src/world.hpp index 08e4d384..0b7b87a5 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -37,7 +37,7 @@ private: explicit world(std::size_t capacity); - void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos); + void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, bool sorted); void do_kill_entity(std::uint64_t id); std::shared_ptr<entity> find_entity_(std::uint64_t id); @@ -67,27 +67,21 @@ public: void set_collect_threshold(std::size_t value) { _collect_every = value; } std::size_t collect_threshold() const noexcept { return _collect_every; } - template<typename T, typename... Xs> + template<typename T, bool sorted = true, typename... Xs> requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval<Xs>()...}; } std::shared_ptr<T> make_entity(std::uint64_t id, global_coords pos, Xs&&... xs) { static_assert(std::is_base_of_v<entity, T>); auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), entity_type_<T>::value, std::forward<Xs>(xs)...}); - do_make_entity(std::static_pointer_cast<entity>(ret), pos); + do_make_entity(std::static_pointer_cast<entity>(ret), pos, sorted); return ret; } - template<typename T, typename... Xs> - requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval<Xs>()...}; } - inline std::shared_ptr<T> make_entity(global_coords pos, Xs&&... xs) - { - return make_entity<T, Xs...>(++_entity_counter, pos, std::forward<Xs>(xs)...); - } - template<typename T = entity, typename U = entity> std::shared_ptr<T> find_entity(std::uint64_t id); bool is_teardown() const { return _teardown; } std::uint64_t entity_counter() const { return _entity_counter; } + [[nodiscard]] std::uint64_t make_id() { return ++_entity_counter; } void set_entity_counter(std::uint64_t value); world& operator=(world&& w) noexcept; |