diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-01 22:27:30 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-01 22:27:30 +0200 |
commit | 31fd5bbc08234686cf798a93a18e0bb73615d1bf (patch) | |
tree | 59b964d01885916c5d49fef3c168ff10dcbdd93f /src/world.hpp | |
parent | 053ea3aa1c443c368f8b43591e3e970e12b50c70 (diff) |
rename entity -> object
Diffstat (limited to 'src/world.hpp')
-rw-r--r-- | src/world.hpp | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/src/world.hpp b/src/world.hpp index c176dbbe..8f82efab 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -2,7 +2,7 @@ #include "compat/defs.hpp" #include "chunk.hpp" #include "global-coords.hpp" -#include "entity-type.hpp" +#include "object-type.hpp" #include "compat/int-hash.hpp" #include <memory> #include <unordered_map> @@ -16,15 +16,15 @@ struct std::hash<floormat::chunk_coords_> final { namespace floormat { -struct entity; -template<typename T> struct entity_type_; +struct object; +template<typename T> struct object_type_; struct object_id_hasher { size_t operator()(object_id id) const noexcept { return int_hash(id); } }; struct world final { - static constexpr object_id entity_counter_init = 1024; + static constexpr object_id object_counter_init = 1024; static constexpr size_t initial_capacity = 512; static constexpr float max_load_factor = .5; static constexpr size_t initial_collect_every = 64; @@ -37,22 +37,22 @@ private: } _last_chunk; std::unordered_map<chunk_coords_, chunk> _chunks; - tsl::robin_map<object_id, std::weak_ptr<entity>, object_id_hasher> _entities; + tsl::robin_map<object_id, std::weak_ptr<object>, object_id_hasher> _objects; size_t _last_collection = 0; size_t _collect_every = initial_collect_every; std::shared_ptr<char> _unique_id = std::make_shared<char>('A'); - object_id _entity_counter = entity_counter_init; - uint64_t _current_frame = 1; // zero is special for struct entity + object_id _object_counter = object_counter_init; + uint64_t _current_frame = 1; // zero is special for struct object bool _teardown : 1 = false; explicit world(size_t capacity); - void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, bool sorted); - void do_kill_entity(object_id id); - std::shared_ptr<entity> find_entity_(object_id id); - [[noreturn]] static void throw_on_wrong_entity_type(object_id id, entity_type actual, entity_type expected); + void do_make_object(const std::shared_ptr<object>& e, global_coords pos, bool sorted); + void do_kill_object(object_id id); + std::shared_ptr<object> find_object_(object_id id); + [[noreturn]] static void throw_on_wrong_object_type(object_id id, object_type actual, object_type expected); - friend struct entity; + friend struct object; public: explicit world(); @@ -82,21 +82,21 @@ public: template<typename T, bool sorted = true, typename... Xs> requires requires(chunk& c) { T{object_id(), c, std::declval<Xs>()...}; - std::is_base_of_v<entity, T>; + std::is_base_of_v<object, T>; } - std::shared_ptr<T> make_entity(object_id id, global_coords pos, Xs&&... xs) + std::shared_ptr<T> make_object(object_id id, global_coords pos, Xs&&... xs) { auto ret = std::shared_ptr<T>(new T{id, operator[](chunk_coords_{pos.chunk(), pos.z()}), Utility::forward<Xs>(xs)...}); - do_make_entity(static_pointer_cast<entity>(ret), pos, sorted); + do_make_object(static_pointer_cast<object>(ret), pos, sorted); return ret; } - template<typename T = entity> std::shared_ptr<T> find_entity(object_id id); + template<typename T = object> std::shared_ptr<T> find_object(object_id id); bool is_teardown() const { return _teardown; } - object_id entity_counter() const { return _entity_counter; } - [[nodiscard]] object_id make_id() { return ++_entity_counter; } - void set_entity_counter(object_id value); + object_id object_counter() const { return _object_counter; } + [[nodiscard]] object_id make_id() { return ++_object_counter; } + void set_object_counter(object_id value); struct neighbor_pair final { chunk* c; chunk_coords_ coord; }; @@ -113,19 +113,19 @@ public: }; template<typename T> -std::shared_ptr<T> world::find_entity(object_id id) +std::shared_ptr<T> world::find_object(object_id id) { - static_assert(std::is_same_v<entity, T> || std::is_base_of_v<entity, T>); - // make it a dependent name so that including "src/entity.hpp" isn't needed - using U = std::conditional_t<std::is_same_v<T, entity>, T, entity>; - if (std::shared_ptr<U> ptr = find_entity_(id); !ptr) + static_assert(std::is_same_v<object, T> || std::is_base_of_v<object, T>); + // make it a dependent name so that including "src/object.hpp" isn't needed + using U = std::conditional_t<std::is_same_v<T, object>, T, object>; + if (std::shared_ptr<U> ptr = find_object_(id); !ptr) return {}; - else if constexpr(std::is_same_v<T, entity>) + else if constexpr(std::is_same_v<T, object>) return ptr; else { - if (!(ptr->type() == entity_type_<T>::value)) [[unlikely]] - throw_on_wrong_entity_type(id, ptr->type(), entity_type_<T>::value); + if (!(ptr->type() == object_type_<T>::value)) [[unlikely]] + throw_on_wrong_object_type(id, ptr->type(), object_type_<T>::value); return static_pointer_cast<T>(Utility::move(ptr)); } } |