diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-17 15:31:57 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-17 23:23:12 +0100 |
commit | 72782ef1298deabbae0598d0d50159210ed64b27 (patch) | |
tree | 6107108a7733b9beda77a36dea0209fe1367d8b9 /src | |
parent | 90742e5c5abd4fb996f548e0cff6661a950057c1 (diff) |
buffer flush (wip)
Diffstat (limited to 'src')
-rw-r--r-- | src/character.cpp | 4 | ||||
-rw-r--r-- | src/character.hpp | 31 | ||||
-rw-r--r-- | src/entity-type.hpp | 1 | ||||
-rw-r--r-- | src/entity.hpp | 2 | ||||
-rw-r--r-- | src/scenery.hpp | 5 | ||||
-rw-r--r-- | src/world.cpp | 13 | ||||
-rw-r--r-- | src/world.hpp | 18 |
7 files changed, 54 insertions, 20 deletions
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 <memory> +#include <Corrade/Containers/String.h> 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_<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> {}; } // 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<global_coords, Vector2b> 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_<scenery> : std::integral_constant<entity_type, entity_type::scenery> {}; +template<> struct entity_type_<scenery_proto> : std::integral_constant<entity_type, entity_type::scenery> {}; } // 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<entity>& 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<entity>& 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<entity> 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<char> _unique_id = std::make_shared<char>('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<entity>& e, global_coords pos); @@ -71,16 +69,26 @@ public: template<typename T, typename... Xs> requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval<Xs>()...}; } - std::shared_ptr<T> make_entity(global_coords pos, Xs&&... xs) + std::shared_ptr<T> make_entity(std::uint64_t id, global_coords pos, Xs&&... xs) { 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)...}); + auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), entity_type_<T>::value, std::forward<Xs>(xs)...}); do_make_entity(std::static_pointer_cast<entity>(ret), pos); return ret; } + template<typename T, typename... Xs> + requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval<Xs>()...}; } + inline std::shared_ptr<T> make_entity(global_coords pos, Xs&&... xs) + { + return make_entity<T, Xs...>(++_entity_counter, pos, std::forward<Xs>(xs)...); + } + template<typename T = entity, typename U = entity> std::shared_ptr<T> 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; |