diff options
-rw-r--r-- | editor/app.cpp | 4 | ||||
-rw-r--r-- | editor/imgui-inspect.cpp | 2 | ||||
-rw-r--r-- | editor/scenery-editor.cpp | 2 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 17 | ||||
-rw-r--r-- | src/character.cpp | 7 | ||||
-rw-r--r-- | src/character.hpp | 4 | ||||
-rw-r--r-- | src/entity.cpp | 17 | ||||
-rw-r--r-- | src/entity.hpp | 10 | ||||
-rw-r--r-- | src/scenery.cpp | 7 | ||||
-rw-r--r-- | src/scenery.hpp | 6 | ||||
-rw-r--r-- | src/world.cpp | 2 | ||||
-rw-r--r-- | src/world.hpp | 10 | ||||
-rw-r--r-- | test/serializer.cpp | 6 |
13 files changed, 54 insertions, 40 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index 71b34cb8..89f9ccf6 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -46,7 +46,7 @@ void app::reset_world() void app::ensure_player_character(world& w) { if (_character_id) - if (auto C = w.find_entity(_character_id); C && C->type == entity_type::character) + if (auto C = w.find_entity(_character_id); C && C->type() == entity_type::character) return; _character_id = 0; @@ -57,7 +57,7 @@ void app::ensure_player_character(world& w) for (const auto& e_ : c.entities()) { const auto& e = *e_; - if (e.type == entity_type::character) + if (e.type() == entity_type::character) { const auto& C = static_cast<const character&>(e); if (C.playable) diff --git a/editor/imgui-inspect.cpp b/editor/imgui-inspect.cpp index 45b42b7b..7ef24913 100644 --- a/editor/imgui-inspect.cpp +++ b/editor/imgui-inspect.cpp @@ -42,7 +42,7 @@ void app::draw_inspector() snformat(buf, "{} ({}x{} -> {}x{})"_cf, name, ch.x, ch.y, (int)pos.x, (int)pos.y); bool is_open = true; - if (s.type == entity_type::scenery) + if (s.type() == entity_type::scenery) { auto& s2 = static_cast<scenery&>(s); if (auto b2 = begin_window(buf, &is_open)) diff --git a/editor/scenery-editor.cpp b/editor/scenery-editor.cpp index 562ca04f..fd859db3 100644 --- a/editor/scenery-editor.cpp +++ b/editor/scenery-editor.cpp @@ -87,7 +87,7 @@ void scenery_editor::place_tile(world& w, global_coords pos, const scenery_& s) for (auto i = es.size()-1; i != (size_t)-1; i--) { const auto& e = *es[i]; - if (e.type != entity_type::scenery) + if (e.type() != entity_type::scenery) continue; auto center = Vector2(e.coord.local())*TILE_SIZE2 + Vector2(e.offset) + Vector2(e.bbox_offset), min = center - Vector2(e.bbox_size/2), max = min + Vector2(e.bbox_size); diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 99ae0b61..043e015a 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -184,8 +184,8 @@ void write_entity_flags(binary_writer<T>& s, const U& e) fm_assert((pass & pass_mask) == pass); flags |= pass; constexpr auto tag = entity_type_<U>::value; - if (e.type != tag) - fm_abort("invalid entity type '%d'", (int)e.type); + if (e.type_of() != tag) + fm_abort("invalid entity type '%d'", (int)e.type_of()); if constexpr(tag == entity_type::scenery) { flags |= (1 << 2) * e.active; @@ -327,8 +327,9 @@ void writer_state::serialize_new_scenery(const chunk& c, writer_t& s) object_id oid = e.id; fm_assert((oid & ((object_id)1 << 60)-1) == oid); static_assert(entity_type_BITS == 3); - fm_assert(((entity_type_i)e.type & (1 << entity_type_BITS)-1) == (entity_type_i)e.type); - oid |= (object_id)e.type << 64 - entity_type_BITS; + const auto type = e.type(); + fm_assert(((entity_type_i)type & (1 << entity_type_BITS)-1) == (entity_type_i)type); + oid |= (object_id)type << 64 - entity_type_BITS; s << oid; const auto local = e.coord.local(); s << local.to_index(); @@ -340,10 +341,10 @@ void writer_state::serialize_new_scenery(const chunk& c, writer_t& s) s << e.bbox_size[0]; s << e.bbox_size[1]; }; - switch (e.type) + switch (type) { default: - fm_abort("invalid entity type '%d'", (int)e.type); + fm_abort("invalid entity type '%d'", (int)type); case entity_type::character: { const auto& C = static_cast<const character&>(e); uint8_t id = 0; @@ -477,7 +478,7 @@ ArrayView<const char> writer_state::serialize_world() for (const auto& e_ : c.entities()) { const auto& e = *e_; - switch (e.type) + switch (e.type()) { case entity_type::scenery: intern_scenery(static_cast<const scenery&>(e), false); @@ -485,7 +486,7 @@ ArrayView<const char> writer_state::serialize_world() case entity_type::character: break; default: - fm_abort("invalid scenery type '%d'", (int)e.type); + fm_abort("invalid scenery type '%d'", (int)e.type()); } } } diff --git a/src/character.cpp b/src/character.cpp index 31376925..51c243c2 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -54,7 +54,6 @@ constexpr auto arrows_to_dir(bool L, bool R, bool U, bool D) character_proto::character_proto(const character_proto&) = default; character_proto::~character_proto() noexcept = default; character_proto& character_proto::operator=(const character_proto&) = default; -character::~character() = default; character_proto::character_proto() { @@ -132,6 +131,8 @@ bool character::update(size_t i, float dt) return true; } +entity_type character::type() const noexcept { return entity_type::character; } + character::operator character_proto() const { character_proto ret; @@ -141,8 +142,8 @@ character::operator character_proto() const return ret; } -character::character(object_id id, struct chunk& c, entity_type type, const character_proto& proto) : - entity{id, c, type, proto}, +character::character(object_id id, struct chunk& c, const character_proto& proto) : + entity{id, c, proto}, name{proto.name}, playable{proto.playable} { diff --git a/src/character.hpp b/src/character.hpp index f4c934fa..24e0e173 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -23,7 +23,7 @@ struct character_proto : entity_proto struct character final : entity { - ~character() override; + entity_type type() const noexcept override; explicit operator character_proto() const; void set_keys(bool L, bool R, bool U, bool D); @@ -39,7 +39,7 @@ private: static Vector2 move_vec(int left_right, int top_bottom); friend struct world; - character(object_id id, struct chunk& c, entity_type type, const character_proto& proto); + 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> {}; diff --git a/src/entity.cpp b/src/entity.cpp index cc7bc00d..8822326e 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -13,14 +13,14 @@ 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; } -entity::entity(object_id id, struct chunk& c, entity_type type, const entity_proto& proto) : +entity::entity(object_id id, struct chunk& c, const entity_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}, - frame{proto.frame}, type{type}, r{proto.r}, pass{proto.pass} + frame{proto.frame}, r{proto.r}, pass{proto.pass} { - fm_assert(type == proto.type); if (atlas) { fm_soft_assert(atlas->check_rotation(r)); @@ -60,7 +60,7 @@ float entity_proto::ordinal(local_coords local) const float entity::ordinal() const { - return ordinal(coord.local(), offset, type); + return ordinal(coord.local(), offset, type()); } float entity::ordinal(local_coords xy, Vector2b offset, entity_type type) @@ -181,7 +181,7 @@ size_t entity::move_to(size_t i, Vector2i delta, rotation new_r) const auto bb_size = rotate_size(bbox_size, r, new_r); bool b0 = c->_bbox_for_scenery(*this, bb0), b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1); - const auto ord = ordinal(coord_.local(), offset_, type); + const auto ord = ordinal(coord_.local(), offset_, type()); if (coord_.chunk() == coord.chunk()) { @@ -239,7 +239,7 @@ entity::operator entity_proto() const ret.bbox_size = bbox_size; ret.delta = delta; ret.frame = frame; - ret.type = type; + ret.type = type(); ret.r = r; ret.pass = pass; return ret; @@ -265,4 +265,9 @@ bool entity::is_dynamic() const return atlas->info().fps > 0; } +entity_type entity::type_of() const noexcept +{ + return type(); +} + } // namespace floormat diff --git a/src/entity.hpp b/src/entity.hpp index 490f0b7d..d759cbd1 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -32,6 +32,8 @@ struct entity_proto virtual bool operator==(const entity_proto&) const; virtual ~entity_proto() noexcept; + + entity_type type_of() const noexcept; }; struct entity @@ -45,7 +47,6 @@ struct entity const Vector2b offset, bbox_offset; const Vector2ub bbox_size; uint16_t delta = 0, frame = 0; - const entity_type type; const rotation r = rotation::N; const pass_mode pass = pass_mode::see_through; @@ -59,6 +60,7 @@ struct entity explicit operator entity_proto() const; + virtual entity_type type() const noexcept = 0; virtual bool can_activate(size_t i) const; virtual bool activate(size_t i); virtual bool update(size_t i, float dt) = 0; @@ -67,18 +69,18 @@ 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; static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); + bool is_dynamic() const; bool can_rotate(rotation new_r); bool can_move_to(Vector2i delta); size_t move_to(size_t i, Vector2i delta, rotation new_r); - bool is_dynamic() const; - friend struct world; protected: - entity(object_id id, struct chunk& c, entity_type type, const entity_proto& proto); + entity(object_id id, struct chunk& c, const entity_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 a424351f..bfa69142 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -100,6 +100,8 @@ 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; } + scenery::operator scenery_proto() const { scenery_proto ret; @@ -111,11 +113,10 @@ scenery::operator scenery_proto() const return ret; } -scenery::scenery(object_id id, struct chunk& c, entity_type type_, const scenery_proto& proto) : - entity{id, c, type_, proto}, sc_type{proto.sc_type}, active{proto.active}, +scenery::scenery(object_id id, struct chunk& c, const scenery_proto& proto) : + entity{id, c, proto}, sc_type{proto.sc_type}, active{proto.active}, closing{proto.closing}, interactive{proto.interactive} { - fm_assert(type == proto.type); } } // namespace floormat diff --git a/src/scenery.hpp b/src/scenery.hpp index a985d7d7..b52ddded 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -41,14 +41,16 @@ struct scenery final : entity unsigned char closing : 1 = false; unsigned char interactive : 1 = false; + bool update(size_t i, float dt) override; bool can_activate(size_t i) const override; bool activate(size_t i) override; - bool update(size_t i, float dt) override; + + entity_type type() const noexcept override; explicit operator scenery_proto() const; private: friend struct world; - scenery(object_id id, struct chunk& c, entity_type type, const scenery_proto& proto); + scenery(object_id id, struct chunk& c, const scenery_proto& proto); }; template<> struct entity_type_<scenery> : std::integral_constant<entity_type, entity_type::scenery> {}; diff --git a/src/world.cpp b/src/world.cpp index 07cfe196..8c564007 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -117,7 +117,7 @@ void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, fm_debug_assert(_unique_id && 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); + fm_assert(e->type() != entity_type::none); const_cast<global_coords&>(e->coord) = pos; _entities[e->id] = e; if (sorted) diff --git a/src/world.hpp b/src/world.hpp index 5d9b8eb6..d269acf6 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -68,11 +68,13 @@ public: size_t collect_threshold() const noexcept { return _collect_every; } template<typename T, bool sorted = true, typename... Xs> - requires requires(chunk& c) { T{object_id(), c, entity_type(), std::declval<Xs>()...}; } + requires requires(chunk& c) { + T{object_id(), c, std::declval<Xs>()...}; + std::is_base_of_v<entity, T>; + } std::shared_ptr<T> make_entity(object_id 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)...}); + auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), std::forward<Xs>(xs)...}); do_make_entity(std::static_pointer_cast<entity>(ret), pos, sorted); return ret; } @@ -110,7 +112,7 @@ std::shared_ptr<T> world::find_entity(object_id id) return ptr; else { - fm_soft_assert(ptr->type == entity_type_<T>::value); + fm_soft_assert(ptr->type() == entity_type_<T>::value); return std::static_pointer_cast<T>(std::move(ptr)); } } diff --git a/test/serializer.cpp b/test/serializer.cpp index 62c3365e..236003cf 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -59,8 +59,8 @@ void assert_chunks_equal(const chunk& a, const chunk& b) { const auto& ae = *a.entities()[i]; const auto& be = *b.entities()[i]; - fm_assert(ae.type == be.type); - switch (ae.type) + fm_assert(ae.type() == be.type()); + switch (ae.type()) { case entity_type::character: { const auto& e1 = static_cast<const character&>(ae); @@ -77,7 +77,7 @@ void assert_chunks_equal(const chunk& a, const chunk& b) break; } default: - fm_abort("invalid entity type '%d'", (int)ae.type); + fm_abort("invalid entity type '%d'", (int)ae.type()); } } } |