From 72782ef1298deabbae0598d0d50159210ed64b27 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 17 Mar 2023 15:31:57 +0100 Subject: buffer flush (wip) --- src/character.cpp | 4 +++- src/character.hpp | 31 ++++++++++++++++++++++++------- src/entity-type.hpp | 1 + src/entity.hpp | 2 +- src/scenery.hpp | 5 ++++- src/world.cpp | 13 ++++++++----- src/world.hpp | 18 +++++++++++++----- 7 files changed, 54 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/character.cpp b/src/character.cpp index e0645ddb..c16fce35 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -51,7 +51,9 @@ constexpr auto arrows_to_dir(bool L, bool R, bool U, bool D) } // namespace -character::character(std::uint64_t id, struct chunk& c, entity_type type, bool playable) : entity{id, c, type}, playable{playable} +character::character(std::uint64_t id, struct chunk& c, entity_type type, const character_proto& proto) : + entity{id, c, type}, + playable{proto.playable} { atlas = loader.anim_atlas("npc-walk", loader.ANIM_PATH); bbox_size = Vector2ub(iTILE_SIZE2/2); diff --git a/src/character.hpp b/src/character.hpp index 9e2617ae..9481c4ac 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -2,32 +2,49 @@ #include "src/global-coords.hpp" #include "src/rotation.hpp" #include "src/entity.hpp" -#include +#include namespace floormat { struct anim_atlas; struct world; +struct character_proto : entity_proto +{ + String name; + bool playable : 1 = false; + + character_proto(); + character_proto(const character_proto&); + ~character_proto() noexcept override; + character_proto& operator=(const character_proto&); + bool operator==(const entity_proto& proto) const override; + operator bool() const; +}; + struct character final : entity { ~character() override; + explicit operator character_proto() const; + void set_keys(bool L, bool R, bool U, bool D); bool update(std::size_t i, float dt) override; - bool is_playable() const { return playable; } + void update_bbox(Vector2b bbox_offset, Vector2ub bbox_size) override; + + String name; + Vector2s offset_frac; + bool b_L : 1 = false, b_R : 1 = false, b_U : 1 = false, b_D : 1 = false; + bool playable : 1 = false; private: int allocate_frame_time(float dt); static Vector2 move_vec(int left_right, int top_bottom); friend struct world; - character(std::uint64_t id, struct chunk& c, entity_type type, bool playable); - - Vector2s offset_frac; - bool b_L : 1 = false, b_R : 1 = false, b_U : 1 = false, b_D : 1 = false; - bool playable : 1 = false; + character(std::uint64_t id, struct chunk& c, entity_type type, const character_proto& proto); }; template<> struct entity_type_ : std::integral_constant {}; +template<> struct entity_type_ : std::integral_constant {}; } // namespace floormat diff --git a/src/entity-type.hpp b/src/entity-type.hpp index 625b53c0..e1ed8fcc 100644 --- a/src/entity-type.hpp +++ b/src/entity-type.hpp @@ -5,5 +5,6 @@ namespace floormat { enum class entity_type : unsigned char { none, character, scenery, }; +constexpr inline std::size_t entity_type_BITS = 3; } // namespace floormat diff --git a/src/entity.hpp b/src/entity.hpp index 396cb881..893c30e9 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -66,7 +66,7 @@ struct entity static Pair normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); [[nodiscard]] virtual bool can_move_to(Vector2i delta); std::size_t move(std::size_t i, Vector2i delta); - void update_bbox(Vector2b bbox_offset, Vector2ub bbox_size); // todo + virtual void update_bbox(Vector2b bbox_offset, Vector2ub bbox_size) = 0; bool is_dynamic() const; friend struct world; diff --git a/src/scenery.hpp b/src/scenery.hpp index 61024b55..55a13bd0 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -18,10 +18,11 @@ struct world; enum class scenery_type : unsigned char { none, generic, door, }; +constexpr inline std::size_t scenery_type_BITS = 3; struct scenery_proto : entity_proto { - scenery_type sc_type : 3 = scenery_type::none; + scenery_type sc_type : scenery_type_BITS = scenery_type::none; unsigned char active : 1 = false; unsigned char closing : 1 = false; unsigned char interactive : 1 = false; @@ -44,6 +45,7 @@ struct scenery final : entity bool can_activate(std::size_t i) const override; bool activate(std::size_t i) override; bool update(std::size_t i, float dt) override; + void update_bbox(Vector2b bbox_offset, Vector2ub bbox_size) override; explicit operator scenery_proto() const; private: @@ -52,5 +54,6 @@ private: }; template<> struct entity_type_ : std::integral_constant {}; +template<> struct entity_type_ : std::integral_constant {}; } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index b1f2e434..ee1ef459 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -106,12 +106,9 @@ void world::collect(bool force) fm_debug("world: collected %zu/%zu chunks", len, len0); } -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, global_coords pos) { - fm_debug_assert(e->id > min_id); + fm_debug_assert(e->id > 0); 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); @@ -122,7 +119,7 @@ void world::do_make_entity(const std::shared_ptr& e, global_coords pos) void world::do_kill_entity(std::uint64_t id) { - fm_debug_assert(id > min_id); + fm_debug_assert(id > 0); auto cnt = _entities.erase(id); fm_debug_assert(cnt > 0); } @@ -135,4 +132,10 @@ std::shared_ptr world::find_entity_(std::uint64_t id) return ret; } +void world::set_entity_counter(std::uint64_t value) +{ + fm_assert(value >= _entity_counter); + _entity_counter = value; +} + } // namespace floormat diff --git a/src/world.hpp b/src/world.hpp index 89a1624c..08e4d384 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -32,11 +32,9 @@ private: std::size_t _last_collection = 0; std::size_t _collect_every = 64; std::shared_ptr _unique_id = std::make_shared('A'); - + std::uint64_t _entity_counter = 0; bool _teardown : 1 = false; - static std::uint64_t entity_counter; - explicit world(std::size_t capacity); void do_make_entity(const std::shared_ptr& e, global_coords pos); @@ -71,16 +69,26 @@ public: template requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval()...}; } - std::shared_ptr make_entity(global_coords pos, Xs&&... xs) + std::shared_ptr make_entity(std::uint64_t id, global_coords pos, Xs&&... xs) { 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)...}); + auto ret = std::shared_ptr(new T{id, operator[](pos.chunk()), entity_type_::value, std::forward(xs)...}); do_make_entity(std::static_pointer_cast(ret), pos); return ret; } + template + requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval()...}; } + inline std::shared_ptr make_entity(global_coords pos, Xs&&... xs) + { + return make_entity(++_entity_counter, pos, std::forward(xs)...); + } + template std::shared_ptr find_entity(std::uint64_t id); + bool is_teardown() const { return _teardown; } + std::uint64_t entity_counter() const { return _entity_counter; } + void set_entity_counter(std::uint64_t value); world& operator=(world&& w) noexcept; world(world&& w) noexcept; -- cgit v1.2.3