summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/chunk.hpp2
-rw-r--r--src/world.cpp30
-rw-r--r--src/world.hpp23
3 files changed, 46 insertions, 9 deletions
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<char>('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<entity>& e, chunk& c, global_coords pos)
+void world::do_make_entity(const std::shared_ptr<entity>& 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<entity> world::find_entity(std::uint64_t id)
+std::shared_ptr<entity> 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<std::uint64_t, std::weak_ptr<entity>> _entities;
std::size_t _last_collection = 0;
std::size_t _collect_every = 64;
+ std::shared_ptr<char> _unique_id = std::make_shared<char>('A');
bool _teardown : 1 = false;
@@ -38,8 +39,9 @@ private:
explicit world(std::size_t capacity);
- void do_make_entity(const std::shared_ptr<entity>& e, chunk& c, global_coords pos);
+ void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos);
void do_kill_entity(std::uint64_t id);
+ std::shared_ptr<entity> find_entity_(std::uint64_t id);
friend struct entity;
@@ -73,15 +75,17 @@ public:
{
static_assert(std::is_base_of_v<entity, T>);
auto ret = std::shared_ptr<T>(new T{++entity_counter, operator[](pos.chunk()), entity_type_<T>::value, std::forward<Xs>(xs)...});
- do_make_entity(std::static_pointer_cast<entity>(ret), ret->c, pos);
+ do_make_entity(std::static_pointer_cast<entity>(ret), pos);
return ret;
}
- std::shared_ptr<entity> find_entity(std::uint64_t id);
+ template<typename T = entity, typename U = entity> std::shared_ptr<T> 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<typename Hash, typename Alloc, typename Pred>
@@ -92,4 +96,15 @@ world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks
operator[](coord) = std::move(c);
}
+template<typename T, typename U>
+std::shared_ptr<T> world::find_entity(std::uint64_t id)
+{
+ static_assert(std::is_base_of_v<entity, T>);
+ std::shared_ptr<U> ptr = find_entity_(id);
+ if (!ptr)
+ return nullptr;
+ fm_assert(ptr->type == entity_type_<T>::value);
+ return std::static_pointer_cast<T>(ptr);
+}
+
} // namespace floormat