diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/character.cpp | 16 | ||||
-rw-r--r-- | src/character.hpp | 14 | ||||
-rw-r--r-- | src/chunk-collision.cpp | 12 | ||||
-rw-r--r-- | src/chunk-scenery.cpp | 30 | ||||
-rw-r--r-- | src/chunk-scenery.hpp | 10 | ||||
-rw-r--r-- | src/chunk.cpp | 44 | ||||
-rw-r--r-- | src/chunk.hpp | 32 | ||||
-rw-r--r-- | src/light.cpp | 8 | ||||
-rw-r--r-- | src/light.hpp | 12 | ||||
-rw-r--r-- | src/object-type.hpp (renamed from src/entity-type.hpp) | 4 | ||||
-rw-r--r-- | src/object.cpp (renamed from src/entity.cpp) | 74 | ||||
-rw-r--r-- | src/object.hpp (renamed from src/entity.hpp) | 36 | ||||
-rw-r--r-- | src/scenery.cpp | 12 | ||||
-rw-r--r-- | src/scenery.hpp | 14 | ||||
-rw-r--r-- | src/world.cpp | 54 | ||||
-rw-r--r-- | src/world.hpp | 54 |
16 files changed, 213 insertions, 213 deletions
diff --git a/src/character.cpp b/src/character.cpp index b05d8093..3e252e7a 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -2,7 +2,7 @@ #include "src/anim-atlas.hpp" #include "loader/loader.hpp" #include "src/world.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include "shaders/shader.hpp" #include "src/RTree-search.hpp" #include "compat/exception.hpp" @@ -91,16 +91,16 @@ character_proto& character_proto::operator=(const character_proto&) = default; character_proto::character_proto() { - type = entity_type::character; + type = object_type::character; atlas = loader.anim_atlas("npc-walk", loader.ANIM_PATH); } -bool character_proto::operator==(const entity_proto& e0) const +bool character_proto::operator==(const object_proto& e0) const { if (type != e0.type) return false; - if (!entity_proto::operator==(e0)) + if (!object_proto::operator==(e0)) return false; const auto& s0 = static_cast<const character_proto&>(e0); @@ -195,26 +195,26 @@ done: } } -entity_type character::type() const noexcept { return entity_type::character; } +object_type character::type() const noexcept { return object_type::character; } character::operator character_proto() const { character_proto ret; - static_cast<entity_proto&>(ret) = entity::operator entity_proto(); + static_cast<object_proto&>(ret) = object::operator object_proto(); ret.name = name; ret.playable = playable; return ret; } character::character(object_id id, struct chunk& c, const character_proto& proto) : - entity{id, c, proto}, + object{id, c, proto}, name{proto.name}, playable{proto.playable} { if (!name) name = "(Unnamed)"_s; fm_soft_assert(atlas->check_rotation(r)); - entity::set_bbox_(offset, bbox_offset, Vector2ub(iTILE_SIZE2/2), pass); + object::set_bbox_(offset, bbox_offset, Vector2ub(iTILE_SIZE2/2), pass); } } // namespace floormat diff --git a/src/character.hpp b/src/character.hpp index 06833f75..2c7543a5 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -1,7 +1,7 @@ #pragma once #include "src/global-coords.hpp" #include "src/rotation.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include <Corrade/Containers/String.h> namespace floormat { @@ -9,7 +9,7 @@ namespace floormat { struct anim_atlas; struct world; -struct character_proto : entity_proto +struct character_proto : object_proto { String name; bool playable : 1 = false; @@ -18,12 +18,12 @@ struct character_proto : entity_proto character_proto(const character_proto&); ~character_proto() noexcept override; character_proto& operator=(const character_proto&); - bool operator==(const entity_proto& proto) const override; + bool operator==(const object_proto& proto) const override; }; -struct character final : entity +struct character final : object { - entity_type type() const noexcept override; + object_type type() const noexcept override; explicit operator character_proto() const; void update(size_t i, float dt) override; @@ -44,7 +44,7 @@ private: character(object_id id, struct chunk& c, const character_proto& proto); }; -template<> struct entity_type_<struct character> : std::integral_constant<entity_type, entity_type::character> {}; -template<> struct entity_type_<struct character_proto> : std::integral_constant<entity_type, entity_type::character> {}; +template<> struct object_type_<struct character> : std::integral_constant<object_type, object_type::character> {}; +template<> struct object_type_<struct character_proto> : std::integral_constant<object_type, object_type::character> {}; } // namespace floormat diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp index 9810cee3..db7f3ede 100644 --- a/src/chunk-collision.cpp +++ b/src/chunk-collision.cpp @@ -1,6 +1,6 @@ #include "chunk.hpp" #include "tile-atlas.hpp" -#include "entity.hpp" +#include "object.hpp" #include "src/RTree-search.hpp" #include "src/chunk-scenery.hpp" #include "src/tile-bbox.hpp" @@ -22,7 +22,7 @@ constexpr object_id make_id(collision_type type, pass_mode p, object_id id) void chunk::ensure_passability() noexcept { - fm_assert(_entities_sorted); // not strictly necessary + fm_assert(_objects_sorted); // not strictly necessary if (!_pass_modified) return; @@ -30,7 +30,7 @@ void chunk::ensure_passability() noexcept _rtree.RemoveAll(); - for (const std::shared_ptr<entity>& s : entities()) + for (const std::shared_ptr<object>& s : objects()) { bbox box; if (_bbox_for_scenery(*s, box)) @@ -64,7 +64,7 @@ void chunk::ensure_passability() noexcept } } -bool chunk::_bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept +bool chunk::_bbox_for_scenery(const object& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept { auto [start, end] = scenery_tile(local, offset, bbox_offset, bbox_size); auto id = make_id(collision_type::scenery, s.pass, s.id); @@ -72,7 +72,7 @@ bool chunk::_bbox_for_scenery(const entity& s, local_coords local, Vector2b offs return s.atlas && !Vector2ui(s.bbox_size).isZero(); } -bool chunk::_bbox_for_scenery(const entity& s, bbox& value) noexcept +bool chunk::_bbox_for_scenery(const object& s, bbox& value) noexcept { return _bbox_for_scenery(s, s.coord.local(), s.offset, s.bbox_offset, s.bbox_size, value); } @@ -116,7 +116,7 @@ void chunk::_replace_bbox(const bbox& x0, const bbox& x1, bool b0, bool b1) CORRADE_ASSUME(false); } -bool chunk::can_place_entity(const entity_proto& proto, local_coords pos) +bool chunk::can_place_object(const object_proto& proto, local_coords pos) { (void)ensure_scenery_mesh(); diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp index 6a3987aa..d211b6a4 100644 --- a/src/chunk-scenery.cpp +++ b/src/chunk-scenery.cpp @@ -1,6 +1,6 @@ #include "chunk-scenery.hpp" #include "shaders/shader.hpp" -#include "entity.hpp" +#include "object.hpp" #include "anim-atlas.hpp" #include "tile-atlas.hpp" #include <bit> @@ -11,7 +11,7 @@ namespace floormat { auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple { - Array<entity_draw_order> array; + Array<object_draw_order> array; Array<std::array<vertex, 4>> scenery_vertexes; Array<std::array<UnsignedShort, 6>> scenery_indexes; return ensure_scenery_mesh({array, scenery_vertexes, scenery_indexes}); @@ -23,7 +23,7 @@ bool chunk::topo_sort_data::intersects(const topo_sort_data& o) const min[1] <= o.max[1] && max[1] >= o.min[1]; } -static void topo_dfs(Array<chunk::entity_draw_order>& array, size_t& output, size_t i, size_t size) // NOLINT(misc-no-recursion) +static void topo_dfs(Array<chunk::object_draw_order>& array, size_t& output, size_t i, size_t size) // NOLINT(misc-no-recursion) { using m = typename chunk::topo_sort_data::m; @@ -69,7 +69,7 @@ static void topo_dfs(Array<chunk::entity_draw_order>& array, size_t& output, siz output++; } -static void topological_sort(Array<chunk::entity_draw_order>& array, size_t size) +static void topological_sort(Array<chunk::object_draw_order>& array, size_t size) { size_t output = 0; @@ -79,7 +79,7 @@ static void topological_sort(Array<chunk::entity_draw_order>& array, size_t size fm_assert(output == size); } -auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data +auto chunk::make_topo_sort_data(object& e, uint32_t mesh_idx) -> topo_sort_data { const auto& a = *e.atlas; const auto& f = a.frame(e.r, e.frame); @@ -94,7 +94,7 @@ auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data .in_mesh_idx = mesh_idx, .ord = e.ordinal(), }; - if (e.type() == entity_type::scenery && !e.is_dynamic()) + if (e.type() == object_type::scenery && !e.is_dynamic()) { const auto bb_min_ = world_pos - Vector3(Vector2(e.bbox_size/2), 0); const auto bb_max_ = bb_min_ + Vector3(Vector2(e.bbox_size), 0); @@ -122,7 +122,7 @@ auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data break; } } - else if (e.type() == entity_type::character) + else if (e.type() == object_type::character) data.mode = topo_sort_data::mode_character; return data; } @@ -131,7 +131,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce { ensure_scenery_buffers(buffers); - fm_assert(_entities_sorted); + fm_assert(_objects_sorted); if (_scenery_modified) { @@ -139,7 +139,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce const auto count = fm_begin( size_t ret = 0; - for (const auto& e : _entities) + for (const auto& e : _objects) ret += !e->is_dynamic(); return ret; ); @@ -147,7 +147,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce auto& scenery_vertexes = buffers.scenery_vertexes; auto& scenery_indexes = buffers.scenery_indexes; - for (auto i = 0uz; const auto& e : _entities) + for (auto i = 0uz; const auto& e : _objects) { if (e->is_dynamic()) continue; @@ -177,22 +177,22 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce scenery_mesh = Utility::move(mesh); } - const auto size = _entities.size(); + const auto size = _objects.size(); auto& array = buffers.array; uint32_t j = 0, i = 0; - for (const auto& e : _entities) + for (const auto& e : _objects) { auto index = e->is_dynamic() ? (uint32_t)-1 : j++; array[i++] = { e.get(), (uint32_t)-1, e->ordinal(), make_topo_sort_data(*e, index) }; } topological_sort(array, i); - return { scenery_mesh, ArrayView<entity_draw_order>{array, size}, j }; + return { scenery_mesh, ArrayView<object_draw_order>{array, size}, j }; } void chunk::ensure_scenery_buffers(scenery_scratch_buffers bufs) { - const size_t len_ = _entities.size(); + const size_t len_ = _objects.size(); if (len_ <= bufs.array.size()) return; @@ -204,7 +204,7 @@ void chunk::ensure_scenery_buffers(scenery_scratch_buffers bufs) else len = std::bit_ceil(len_); - bufs.array = Array<entity_draw_order>{NoInit, len}; + bufs.array = Array<object_draw_order>{NoInit, len}; bufs.scenery_vertexes = Array<std::array<vertex, 4>>{NoInit, len}; bufs.scenery_indexes = Array<std::array<UnsignedShort, 6>>{NoInit, len}; } diff --git a/src/chunk-scenery.hpp b/src/chunk-scenery.hpp index 5648da5c..89e40078 100644 --- a/src/chunk-scenery.hpp +++ b/src/chunk-scenery.hpp @@ -9,7 +9,7 @@ struct chunk::topo_sort_data { enum m : uint8_t { mode_none, mode_static, mode_character, }; - entity* in = nullptr; + object* in = nullptr; Vector2i min, max, center; uint32_t in_mesh_idx; float slope = 0, ord; @@ -20,9 +20,9 @@ struct chunk::topo_sort_data bool intersects(const topo_sort_data& other) const; }; -struct chunk::entity_draw_order +struct chunk::object_draw_order { - entity *e; + object *e; uint32_t mesh_idx; float ord; topo_sort_data data; @@ -30,13 +30,13 @@ struct chunk::entity_draw_order struct chunk::scenery_mesh_tuple { GL::Mesh& mesh; - ArrayView<entity_draw_order> array; + ArrayView<object_draw_order> array; size_t size; }; struct chunk::scenery_scratch_buffers { - Array<entity_draw_order>& array; + Array<object_draw_order>& array; Array<std::array<vertex, 4>>& scenery_vertexes; Array<std::array<UnsignedShort, 6>>& scenery_indexes; }; diff --git a/src/chunk.cpp b/src/chunk.cpp index ff35f292..f8b823a8 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -9,7 +9,7 @@ namespace floormat { namespace { -constexpr auto entity_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; +constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; size_t _reload_no_ = 0; // NOLINT @@ -27,9 +27,9 @@ bool chunk::empty(bool force) const noexcept if (!force && !_maybe_empty) return false; for (auto i = 0uz; i < TILE_COUNT; i++) - if (!_entities.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1])) + if (!_objects.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1])) return _maybe_empty = false; - if (!_entities.empty()) + if (!_objects.empty()) return false; return true; } @@ -98,7 +98,7 @@ chunk::chunk(struct world& w) noexcept : _world{&w} chunk::~chunk() noexcept { _teardown = true; - _entities.clear(); + _objects.clear(); _rtree.RemoveAll(); } @@ -107,46 +107,46 @@ 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) +void chunk::add_object_unsorted(const std::shared_ptr<object>& e) { - _entities_sorted = false; + _objects_sorted = false; if (!e->is_dynamic()) mark_scenery_modified(); if (bbox bb; _bbox_for_scenery(*e, bb)) _add_bbox(bb); - _entities.push_back(e); + _objects.push_back(e); } -void chunk::sort_entities() +void chunk::sort_objects() { - if (_entities_sorted) + if (_objects_sorted) return; - _entities_sorted = true; + _objects_sorted = true; mark_scenery_modified(); - std::sort(_entities.begin(), _entities.end(), [](const auto& a, const auto& b) { + std::sort(_objects.begin(), _objects.end(), [](const auto& a, const auto& b) { return a->id < b->id; }); } -void chunk::add_entity(const std::shared_ptr<entity>& e) +void chunk::add_object(const std::shared_ptr<object>& e) { - fm_assert(_entities_sorted); + fm_assert(_objects_sorted); if (!e->is_dynamic()) mark_scenery_modified(); if (bbox bb; _bbox_for_scenery(*e, bb)) _add_bbox(bb); - auto& es = _entities; + auto& es = _objects; es.reserve(es.size() + 1); - auto it = std::lower_bound(es.cbegin(), es.cend(), e, entity_id_lessp); - _entities.insert(it, e); + auto it = std::lower_bound(es.cbegin(), es.cend(), e, object_id_lessp); + _objects.insert(it, e); } -void chunk::remove_entity(size_t i) +void chunk::remove_object(size_t i) { - fm_assert(_entities_sorted); - auto& es = _entities; + fm_assert(_objects_sorted); + auto& es = _objects; fm_debug_assert(i < es.size()); const auto& e = es[i]; if (!e->is_dynamic()) @@ -156,10 +156,10 @@ void chunk::remove_entity(size_t i) es.erase(es.cbegin() + ptrdiff_t(i)); } -const std::vector<std::shared_ptr<entity>>& chunk::entities() const +const std::vector<std::shared_ptr<object>>& chunk::objects() const { - fm_assert(_entities_sorted); - return _entities; + fm_assert(_objects_sorted); + return _objects; } } // namespace floormat diff --git a/src/chunk.hpp b/src/chunk.hpp index a41ed9a0..6a7e3fc9 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -13,8 +13,8 @@ namespace Corrade::Containers { template<typename T, typename D> class Array; } namespace floormat { struct anim_atlas; -struct entity; -struct entity_proto; +struct object; +struct object_proto; class tile_iterator; class tile_const_iterator; @@ -23,7 +23,7 @@ enum class collision : unsigned char { }; enum class collision_type : unsigned char { - none, entity, scenery, geometry, + none, object, scenery, geometry, }; struct collision_data final { @@ -35,7 +35,7 @@ struct collision_data final { struct chunk final { friend struct tile_ref; - friend struct entity; + friend struct object; friend struct world; tile_ref operator[](size_t idx) noexcept; @@ -82,7 +82,7 @@ struct chunk final const size_t size; }; struct topo_sort_data; - struct entity_draw_order; + struct object_draw_order; struct scenery_mesh_tuple; struct scenery_scratch_buffers; @@ -108,13 +108,13 @@ struct chunk final RTree* rtree() noexcept; struct world& world() noexcept { return *_world; } - [[nodiscard]] bool can_place_entity(const entity_proto& proto, local_coords pos); + [[nodiscard]] bool can_place_object(const object_proto& proto, local_coords pos); - void add_entity(const std::shared_ptr<entity>& e); - void add_entity_unsorted(const std::shared_ptr<entity>& e); - void sort_entities(); - void remove_entity(size_t i); - const std::vector<std::shared_ptr<entity>>& entities() const; + void add_object(const std::shared_ptr<object>& e); + void add_object_unsorted(const std::shared_ptr<object>& e); + void sort_objects(); + void remove_object(size_t i); + const std::vector<std::shared_ptr<object>>& objects() const; private: struct ground_stuff { @@ -131,7 +131,7 @@ private: Pointer<ground_stuff> _ground; Pointer<wall_stuff> _walls; - std::vector<std::shared_ptr<entity>> _entities; + std::vector<std::shared_ptr<object>> _objects; struct world* _world; GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate}; @@ -144,10 +144,10 @@ private: _scenery_modified : 1 = true, _pass_modified : 1 = true, _teardown : 1 = false, - _entities_sorted : 1 = true; + _objects_sorted : 1 = true; void ensure_scenery_buffers(scenery_scratch_buffers bufs); - static topo_sort_data make_topo_sort_data(entity& e, uint32_t mesh_idx); + static topo_sort_data make_topo_sort_data(object& e, uint32_t mesh_idx); struct bbox final // NOLINT(cppcoreguidelines-pro-type-member-init) { @@ -156,8 +156,8 @@ private: bool operator==(const bbox& other) const noexcept; }; - static bool _bbox_for_scenery(const entity& s, bbox& value) noexcept; - static bool _bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept; + static bool _bbox_for_scenery(const object& s, bbox& value) noexcept; + static bool _bbox_for_scenery(const object& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept; void _remove_bbox(const bbox& x); void _add_bbox(const bbox& x); void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b); diff --git a/src/light.cpp b/src/light.cpp index 58246d94..13f39b22 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -14,7 +14,7 @@ light_proto::light_proto() { atlas = loader.vobj("light"_s).atlas; pass = pass_mode::pass; - type = entity_type::light; + type = object_type::light; } light_proto::light_proto(const light_proto&) = default; @@ -23,7 +23,7 @@ light_proto::~light_proto() noexcept = default; bool light_proto::operator==(const light_proto&) const = default; light::light(object_id id, struct chunk& c, const light_proto& proto) : - entity{id, c, proto}, + object{id, c, proto}, max_distance{proto.max_distance}, color{proto.color}, falloff{proto.falloff}, @@ -46,7 +46,7 @@ Vector2 light::ordinal_offset(Vector2b) const light::operator light_proto() const { light_proto ret; - static_cast<entity_proto&>(ret) = entity_proto(*this); + static_cast<object_proto&>(ret) = object_proto(*this); ret.max_distance = max_distance; ret.color = color; ret.falloff = falloff; @@ -54,7 +54,7 @@ light::operator light_proto() const return ret; } -entity_type light::type() const noexcept { return entity_type::light; } +object_type light::type() const noexcept { return object_type::light; } void light::update(size_t, float) {} bool light::is_dynamic() const { return true; } bool light::is_virtual() const { return true; } diff --git a/src/light.hpp b/src/light.hpp index a58e0221..b5c4469a 100644 --- a/src/light.hpp +++ b/src/light.hpp @@ -1,13 +1,13 @@ #pragma once #include "src/light-falloff.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include <Magnum/Magnum.h> #include <Magnum/Math/Vector2.h> #include <Magnum/Math/Color.h> namespace floormat { -struct light_proto : entity_proto +struct light_proto : object_proto { light_proto(); light_proto(const light_proto&); @@ -21,7 +21,7 @@ struct light_proto : entity_proto uint8_t enabled : 1 = true; }; -struct light final : entity +struct light final : object { float max_distance; Color4ub color; @@ -32,7 +32,7 @@ struct light final : entity Vector2 ordinal_offset(Vector2b offset) const override; float depth_offset() const override; - entity_type type() const noexcept override; + object_type type() const noexcept override; void update(size_t i, float dt) override; bool is_dynamic() const override; bool is_virtual() const override; @@ -42,7 +42,7 @@ struct light final : entity friend struct world; }; -template<> struct entity_type_<struct light> : std::integral_constant<entity_type, entity_type::light> {}; -template<> struct entity_type_<struct light_proto> : std::integral_constant<entity_type, entity_type::light> {}; +template<> struct object_type_<struct light> : std::integral_constant<object_type, object_type::light> {}; +template<> struct object_type_<struct light_proto> : std::integral_constant<object_type, object_type::light> {}; } // namespace floormat diff --git a/src/entity-type.hpp b/src/object-type.hpp index 308bc8a6..1a1d1aff 100644 --- a/src/entity-type.hpp +++ b/src/object-type.hpp @@ -2,9 +2,9 @@ namespace floormat { -enum class entity_type : unsigned char { +enum class object_type : unsigned char { none, character, scenery, light, }; -constexpr inline size_t entity_type_BITS = 3; +constexpr inline size_t object_type_BITS = 3; } // namespace floormat diff --git a/src/entity.cpp b/src/object.cpp index 2a8c5aa1..1dc05f4b 100644 --- a/src/entity.cpp +++ b/src/object.cpp @@ -1,4 +1,4 @@ -#include "entity.hpp" +#include "object.hpp" #include "world.hpp" #include "rotation.inl" #include "anim-atlas.hpp" @@ -12,18 +12,18 @@ namespace floormat { namespace { -constexpr auto entity_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; +constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; } // namespace -bool entity_proto::operator==(const entity_proto&) const = default; -entity_proto& entity_proto::operator=(const entity_proto&) = default; -entity_proto::~entity_proto() noexcept = default; -entity_proto::entity_proto() = default; -entity_proto::entity_proto(const entity_proto&) = default; -entity_type entity_proto::type_of() const noexcept { return type; } +bool object_proto::operator==(const object_proto&) const = default; +object_proto& object_proto::operator=(const object_proto&) = default; +object_proto::~object_proto() noexcept = default; +object_proto::object_proto() = default; +object_proto::object_proto(const object_proto&) = default; +object_type object_proto::type_of() const noexcept { return type; } -entity::entity(object_id id, struct chunk& c, const entity_proto& proto) : +object::object(object_id id, struct chunk& c, const object_proto& proto) : id{id}, c{&c}, atlas{proto.atlas}, offset{proto.offset}, bbox_offset{proto.bbox_offset}, bbox_size{proto.bbox_size}, delta{proto.delta}, @@ -34,23 +34,23 @@ entity::entity(object_id id, struct chunk& c, const entity_proto& proto) : fm_soft_assert(frame < atlas->info().nframes); } -entity::~entity() noexcept +object::~object() noexcept { fm_debug_assert(id); if (c->_teardown || c->_world->_teardown) [[unlikely]] return; if (chunk::bbox bb; c->_bbox_for_scenery(*this, bb)) c->_remove_bbox(bb); - c->_world->do_kill_entity(id); + c->_world->do_kill_object(id); const_cast<object_id&>(id) = 0; } -float entity::ordinal() const +float object::ordinal() const { return ordinal(coord.local(), offset, atlas->group(r).z_offset); } -float entity::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const +float object::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const { constexpr auto inv_tile_size = 1.f/TILE_SIZE2; auto offset_ = ordinal_offset(offset); @@ -58,28 +58,28 @@ float entity::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const return vec[0] + vec[1] + Vector2(z_offset).sum(); } -struct chunk& entity::chunk() const +struct chunk& object::chunk() const { return *c; } -size_t entity::index() const +size_t object::index() const { auto& c = chunk(); const auto fn = [id = id](const auto& a, const auto&) { return a->id < id; }; - auto& es = c._entities; + auto& es = c._objects; auto it = std::lower_bound(es.cbegin(), es.cend(), nullptr, fn); fm_assert(it != es.cend()); fm_assert((*it)->id == id); return (size_t)std::distance(es.cbegin(), it); } -bool entity::is_virtual() const +bool object::is_virtual() const { return false; } -bool entity::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) +bool object::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) { if (bbox_offset.isZero() && bbox_size[0] == bbox_size[1]) return true; @@ -89,7 +89,7 @@ bool entity::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vec return can_move_to({}, coord, offset_, bbox_offset_, bbox_size_); } -bool entity::can_rotate(rotation new_r) +bool object::can_rotate(rotation new_r) { if (new_r == r) return true; @@ -97,7 +97,7 @@ bool entity::can_rotate(rotation new_r) return can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size); } -void entity::rotate(size_t, rotation new_r) +void object::rotate(size_t, rotation new_r) { fm_assert(atlas->check_rotation(new_r)); auto offset_ = !is_dynamic() ? rotate_point(offset, r, new_r) : offset; @@ -112,7 +112,7 @@ void entity::rotate(size_t, rotation new_r) template <typename T> constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); } -Pair<global_coords, Vector2b> entity::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i new_offset) +Pair<global_coords, Vector2b> object::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i new_offset) { auto off_tmp = Vector2i(cur_offset) + new_offset; auto off_new = off_tmp % iTILE_SIZE2; @@ -160,7 +160,7 @@ static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector return ret; } -bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) +bool object::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) { auto [coord_, offset_] = normalize_coords(coord2, offset, delta); @@ -182,17 +182,17 @@ bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, return true; } -bool entity::can_move_to(Vector2i delta) +bool object::can_move_to(Vector2i delta) { return can_move_to(delta, coord, offset, bbox_offset, bbox_size); } -size_t entity::move_to(size_t& i, Vector2i delta, rotation new_r) +size_t object::move_to(size_t& i, Vector2i delta, rotation new_r) { if (!can_rotate(new_r)) return i; - auto& es = c->_entities; + auto& es = c->_objects; fm_debug_assert(i < es.size()); auto e_ = es[i]; @@ -228,9 +228,9 @@ size_t entity::move_to(size_t& i, Vector2i delta, rotation new_r) if (!is_dynamic()) c2.mark_scenery_modified(); c2._add_bbox(bb1); - c->remove_entity(i); - auto& es = c2._entities; - auto it = std::lower_bound(es.cbegin(), es.cend(), e_, entity_id_lessp); + c->remove_object(i); + auto& es = c2._objects; + auto it = std::lower_bound(es.cbegin(), es.cend(), e_, object_id_lessp); const_cast<global_coords&>(coord) = coord_; set_bbox_(offset_, bb_offset, bb_size, pass); const_cast<rotation&>(r) = new_r; @@ -242,13 +242,13 @@ size_t entity::move_to(size_t& i, Vector2i delta, rotation new_r) return i; } -void entity::move_to(Magnum::Vector2i delta) +void object::move_to(Magnum::Vector2i delta) { auto i = index(); (void)move_to(i, delta, r); } -void entity::set_bbox_(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass_) +void object::set_bbox_(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass_) { const_cast<Vector2b&>(offset) = offset_; const_cast<Vector2b&>(bbox_offset) = bbox_offset_; @@ -256,9 +256,9 @@ void entity::set_bbox_(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_s const_cast<pass_mode&>(pass) = pass_; } -entity::operator entity_proto() const +object::operator object_proto() const { - entity_proto ret; + object_proto ret; ret.atlas = atlas; ret.offset = offset; ret.bbox_offset = bbox_offset; @@ -271,7 +271,7 @@ entity::operator entity_proto() const return ret; } -void entity::set_bbox(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass) +void object::set_bbox(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass) { if (offset != offset_) if (!is_dynamic()) @@ -284,15 +284,15 @@ void entity::set_bbox(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_si c->_replace_bbox(bb0, bb, b0, b); } -bool entity::can_activate(size_t) const { return false; } -bool entity::activate(size_t) { return false; } +bool object::can_activate(size_t) const { return false; } +bool object::activate(size_t) { return false; } -bool entity::is_dynamic() const +bool object::is_dynamic() const { return atlas->info().fps > 0; } -entity_type entity::type_of() const noexcept +object_type object::type_of() const noexcept { return type(); } diff --git a/src/entity.hpp b/src/object.hpp index e332c93a..f411a7ec 100644 --- a/src/entity.hpp +++ b/src/object.hpp @@ -3,43 +3,43 @@ #include "src/global-coords.hpp" #include "src/rotation.hpp" #include "src/pass-mode.hpp" -#include "src/entity-type.hpp" +#include "src/object-type.hpp" #include "src/object-id.hpp" #include <memory> #include <vector> namespace floormat { -template<typename T> struct entity_type_; +template<typename T> struct object_type_; struct anim_atlas; struct world; struct chunk; -struct entity_proto +struct object_proto { std::shared_ptr<anim_atlas> atlas; Vector2b offset, bbox_offset; Vector2ub bbox_size = Vector2ub(iTILE_SIZE2); uint16_t delta = 0, frame = 0; - entity_type type : 3 = entity_type::none; + object_type type : 3 = object_type::none; rotation r : rotation_BITS = rotation::N; pass_mode pass : pass_mode_BITS = pass_mode::see_through; - entity_proto& operator=(const entity_proto&); - entity_proto(); - entity_proto(const entity_proto&); + object_proto& operator=(const object_proto&); + object_proto(); + object_proto(const object_proto&); - virtual bool operator==(const entity_proto&) const; - bool operator!=(const entity_proto& o) const { return !operator==(o); } - virtual ~entity_proto() noexcept; + virtual bool operator==(const object_proto&) const; + bool operator!=(const object_proto& o) const { return !operator==(o); } + virtual ~object_proto() noexcept; - entity_type type_of() const noexcept; + object_type type_of() const noexcept; }; // todo rename to 'object' -struct entity +struct object { - fm_DECLARE_DELETED_COPY_ASSIGNMENT(entity); + fm_DECLARE_DELETED_COPY_ASSIGNMENT(object); const object_id id = 0; struct chunk* const c; @@ -51,7 +51,7 @@ struct entity const rotation r = rotation::N; const pass_mode pass = pass_mode::see_through; - virtual ~entity() noexcept; + virtual ~object() noexcept; virtual Vector2 ordinal_offset(Vector2b offset) const = 0; virtual float depth_offset() const = 0; @@ -61,9 +61,9 @@ struct entity size_t index() const; virtual bool is_virtual() const; - explicit operator entity_proto() const; + explicit operator object_proto() const; - virtual entity_type type() const noexcept = 0; + virtual object_type type() const noexcept = 0; virtual bool can_activate(size_t i) const; virtual bool activate(size_t i); virtual void update(size_t i, float dt) = 0; @@ -72,7 +72,7 @@ struct entity virtual bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); - entity_type type_of() const noexcept; + object_type type_of() const noexcept; static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); virtual bool is_dynamic() const; @@ -82,7 +82,7 @@ struct entity void move_to(Vector2i delta); protected: - entity(object_id id, struct chunk& c, const entity_proto& proto); + object(object_id id, struct chunk& c, const object_proto& proto); void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); }; diff --git a/src/scenery.cpp b/src/scenery.cpp index c5435137..22c7083a 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -11,7 +11,7 @@ namespace floormat { scenery_proto::scenery_proto() { - type = entity_type::scenery; + type = object_type::scenery; } scenery_proto& scenery_proto::operator=(const scenery_proto&) = default; @@ -129,12 +129,12 @@ bool scenery::activate(size_t) return false; } -bool scenery_proto::operator==(const entity_proto& e0) const +bool scenery_proto::operator==(const object_proto& e0) const { if (type != e0.type) return false; - if (!entity_proto::operator==(e0)) + if (!object_proto::operator==(e0)) return false; const auto& s0 = static_cast<const scenery_proto&>(e0); @@ -142,12 +142,12 @@ bool scenery_proto::operator==(const entity_proto& e0) const closing == s0.closing && interactive == s0.interactive; } -entity_type scenery::type() const noexcept { return entity_type::scenery; } +object_type scenery::type() const noexcept { return object_type::scenery; } scenery::operator scenery_proto() const { scenery_proto ret; - static_cast<entity_proto&>(ret) = entity::operator entity_proto(); + static_cast<object_proto&>(ret) = object::operator object_proto(); ret.sc_type = sc_type; ret.active = active; ret.closing = closing; @@ -156,7 +156,7 @@ scenery::operator scenery_proto() const } scenery::scenery(object_id id, struct chunk& c, const scenery_proto& proto) : - entity{id, c, proto}, sc_type{proto.sc_type}, active{proto.active}, + object{id, c, proto}, sc_type{proto.sc_type}, active{proto.active}, closing{proto.closing}, interactive{proto.interactive} { fm_debug_assert(atlas); // todo add placeholder graphic diff --git a/src/scenery.hpp b/src/scenery.hpp index 17fdad66..1c3b03ad 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -2,7 +2,7 @@ #include "pass-mode.hpp" #include "tile-defs.hpp" #include "rotation.hpp" -#include "entity.hpp" +#include "object.hpp" #include <type_traits> #include <Magnum/Math/Vector2.h> #include <Magnum/Magnum.h> @@ -18,7 +18,7 @@ enum class scenery_type : unsigned char { }; constexpr inline size_t scenery_type_BITS = 3; -struct scenery_proto : entity_proto +struct scenery_proto : object_proto { scenery_type sc_type : scenery_type_BITS = scenery_type::none; unsigned char active : 1 = false; @@ -29,11 +29,11 @@ struct scenery_proto : entity_proto scenery_proto(const scenery_proto&); ~scenery_proto() noexcept override; scenery_proto& operator=(const scenery_proto&); - bool operator==(const entity_proto& proto) const override; + bool operator==(const object_proto& proto) const override; operator bool() const; }; -struct scenery final : entity +struct scenery final : object { scenery_type sc_type : 3 = scenery_type::none; unsigned char active : 1 = false; @@ -46,7 +46,7 @@ struct scenery final : entity bool can_activate(size_t i) const override; bool activate(size_t i) override; - entity_type type() const noexcept override; + object_type type() const noexcept override; explicit operator scenery_proto() const; private: @@ -54,7 +54,7 @@ private: scenery(object_id id, struct chunk& c, const scenery_proto& proto); }; -template<> struct entity_type_<scenery> : std::integral_constant<entity_type, entity_type::scenery> {}; -template<> struct entity_type_<scenery_proto> : std::integral_constant<entity_type, entity_type::scenery> {}; +template<> struct object_type_<scenery> : std::integral_constant<object_type, object_type::scenery> {}; +template<> struct object_type_<scenery_proto> : std::integral_constant<object_type, object_type::scenery> {}; } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index e829ab5e..97cc4702 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,6 +1,6 @@ #include "world.hpp" #include "chunk.hpp" -#include "entity.hpp" +#include "object.hpp" #include "compat/int-hash.hpp" #include "compat/exception.hpp" @@ -47,10 +47,10 @@ world& world::operator=(world&& w) noexcept fm_debug_assert(w._unique_id == nullptr); _last_chunk = {}; _chunks = std::move(w._chunks); - _entities = std::move(w._entities); - _entity_counter = w._entity_counter; + _objects = std::move(w._objects); + _object_counter = w._object_counter; _current_frame = w._current_frame; - w._entity_counter = 0; + w._object_counter = 0; for (auto& [id, c] : _chunks) c._world = this; @@ -71,19 +71,19 @@ world::~world() noexcept v.mark_scenery_modified(); v.mark_passability_modified(); _last_chunk = {}; - v._entities.clear(); + v._objects.clear(); } _last_chunk = {}; _chunks.clear(); - _entities.clear(); + _objects.clear(); } world::world(size_t capacity) : _chunks{capacity} { _chunks.max_load_factor(max_load_factor); _chunks.reserve(initial_capacity); - _entities.max_load_factor(max_load_factor); - _entities.reserve(initial_capacity); + _objects.max_load_factor(max_load_factor); + _objects.reserve(initial_capacity); } chunk& world::operator[](chunk_coords_ coord) noexcept @@ -122,10 +122,10 @@ void world::clear() _last_collection = 0; _chunks.clear(); _chunks.rehash(initial_capacity); - _entities.clear(); - _entities.rehash(initial_capacity); + _objects.clear(); + _objects.rehash(initial_capacity); _collect_every = initial_collect_every; - _entity_counter = entity_counter_init; + _object_counter = object_counter_init; auto& [c, pos] = _last_chunk; c = nullptr; pos = chunk_tuple::invalid_coords; @@ -158,44 +158,44 @@ 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, bool sorted) +void world::do_make_object(const std::shared_ptr<object>& e, global_coords pos, bool sorted) { fm_assert(e->id > 0); fm_debug_assert(_unique_id && e->c->world()._unique_id == _unique_id); - fm_assert(!_entities.contains(e->id)); - fm_assert(e->type() != entity_type::none); + fm_assert(!_objects.contains(e->id)); + fm_assert(e->type() != object_type::none); const_cast<global_coords&>(e->coord) = pos; - _entities[e->id] = e; + _objects[e->id] = e; if (sorted) - e->c->add_entity(e); + e->c->add_object(e); else - e->c->add_entity_unsorted(e); + e->c->add_object_unsorted(e); } -void world::do_kill_entity(object_id id) +void world::do_kill_object(object_id id) { fm_debug_assert(id > 0); - auto cnt = _entities.erase(id); + auto cnt = _objects.erase(id); fm_debug_assert(cnt > 0); } -std::shared_ptr<entity> world::find_entity_(object_id id) +std::shared_ptr<object> world::find_object_(object_id id) { - auto it = _entities.find(id); - auto ret = it == _entities.end() ? nullptr : it->second.lock(); + auto it = _objects.find(id); + auto ret = it == _objects.end() ? nullptr : it->second.lock(); fm_debug_assert(!ret || &ret->c->world() == this); return ret; } -void world::set_entity_counter(object_id value) +void world::set_object_counter(object_id value) { - fm_assert(value >= _entity_counter); - _entity_counter = value; + fm_assert(value >= _object_counter); + _object_counter = value; } -void world::throw_on_wrong_entity_type(object_id id, entity_type actual, entity_type expected) +void world::throw_on_wrong_object_type(object_id id, object_type actual, object_type expected) { - fm_throw("object '{}' has wrong entity type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); + fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); } auto world::neighbors(floormat::chunk_coords_ coord) -> std::array<neighbor_pair, 8> 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)); } } |