From 05f6826cca6f644444ddb6aaede4955500d48f3b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 15 Mar 2023 18:31:47 +0100 Subject: a --- src/chunk.hpp | 2 +- src/world.cpp | 30 ++++++++++++++++++++++++++---- src/world.hpp | 23 +++++++++++++++++++---- 3 files changed, 46 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/chunk.hpp b/src/chunk.hpp index dc784685..15cb1dc0 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -50,7 +50,7 @@ struct chunk final bool empty(bool force = false) const noexcept; - chunk(struct world& w) noexcept; + explicit chunk(struct world& w) noexcept; ~chunk() noexcept; chunk(const chunk&) = delete; chunk& operator=(const chunk&) = delete; diff --git a/src/world.cpp b/src/world.cpp index f5c378cd..71f6c7a9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -4,6 +4,27 @@ namespace floormat { +world::world(world&& w) noexcept = default; + +world& world::operator=(world&& w) noexcept +{ + fm_assert(!w._teardown); + if (&w != this) [[likely]] + { + _last_collection = w._last_collection; + _collect_every = w._collect_every; + _unique_id = std::move(w._unique_id); + w._unique_id = std::make_shared('D'); + _last_chunk = {}; + _chunks = std::move(w._chunks); + _entities = std::move(w._entities); + + for (auto& [id, c] : _chunks) + c._world = this; + } + return *this; +} + world::world() : world{initial_capacity} { } @@ -88,14 +109,15 @@ void world::collect(bool force) static constexpr std::uint64_t min_id = 1u << 16; std::uint64_t world::entity_counter = min_id; -void world::do_make_entity(const std::shared_ptr& e, chunk& c, global_coords pos) +void world::do_make_entity(const std::shared_ptr& e, global_coords pos) { - fm_debug_assert(e->id > min_id && &c.world() == this); + fm_debug_assert(e->id > min_id); + fm_debug_assert(e->c.world()._unique_id == _unique_id); fm_assert(Vector2ui(e->bbox_size).product() > 0); fm_assert(e->type != entity_type::none); e->coord = pos; _entities[e->id] = e; - c.add_entity(e); + e->c.add_entity(e); } void world::do_kill_entity(std::uint64_t id) @@ -105,7 +127,7 @@ void world::do_kill_entity(std::uint64_t id) fm_debug_assert(cnt > 0); } -std::shared_ptr world::find_entity(std::uint64_t id) +std::shared_ptr world::find_entity_(std::uint64_t id) { auto it = _entities.find(id); return it == _entities.end() ? nullptr : it->second.lock(); diff --git a/src/world.hpp b/src/world.hpp index 039f3387..89a1624c 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -31,6 +31,7 @@ private: std::unordered_map> _entities; std::size_t _last_collection = 0; std::size_t _collect_every = 64; + std::shared_ptr _unique_id = std::make_shared('A'); bool _teardown : 1 = false; @@ -38,8 +39,9 @@ private: explicit world(std::size_t capacity); - void do_make_entity(const std::shared_ptr& e, chunk& c, global_coords pos); + void do_make_entity(const std::shared_ptr& e, global_coords pos); void do_kill_entity(std::uint64_t id); + std::shared_ptr find_entity_(std::uint64_t id); friend struct entity; @@ -73,15 +75,17 @@ public: { static_assert(std::is_base_of_v); auto ret = std::shared_ptr(new T{++entity_counter, operator[](pos.chunk()), entity_type_::value, std::forward(xs)...}); - do_make_entity(std::static_pointer_cast(ret), ret->c, pos); + do_make_entity(std::static_pointer_cast(ret), pos); return ret; } - std::shared_ptr find_entity(std::uint64_t id); + template std::shared_ptr find_entity(std::uint64_t id); bool is_teardown() const { return _teardown; } + world& operator=(world&& w) noexcept; + world(world&& w) noexcept; + fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world); - fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(world); }; template @@ -92,4 +96,15 @@ world::world(std::unordered_map&& chunks operator[](coord) = std::move(c); } +template +std::shared_ptr world::find_entity(std::uint64_t id) +{ + static_assert(std::is_base_of_v); + std::shared_ptr ptr = find_entity_(id); + if (!ptr) + return nullptr; + fm_assert(ptr->type == entity_type_::value); + return std::static_pointer_cast(ptr); +} + } // namespace floormat -- cgit v1.2.3