summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-03-15 18:31:47 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-03-15 18:31:47 +0100
commit05f6826cca6f644444ddb6aaede4955500d48f3b (patch)
tree4f6cceeb705dd7bf616a8317601fddb7cd496632
parent73abf154e82546256f4a860c666f1f652767bda9 (diff)
a
-rw-r--r--editor/app.cpp26
-rw-r--r--editor/app.hpp2
-rw-r--r--editor/save.cpp5
-rw-r--r--editor/update.cpp9
-rw-r--r--floormat/main.hpp2
-rw-r--r--main/main-impl.hpp2
-rw-r--r--main/setup.cpp2
-rw-r--r--src/chunk.hpp2
-rw-r--r--src/world.cpp30
-rw-r--r--src/world.hpp23
10 files changed, 76 insertions, 27 deletions
diff --git a/editor/app.cpp b/editor/app.cpp
index 9023b5ac..a452934f 100644
--- a/editor/app.cpp
+++ b/editor/app.cpp
@@ -28,22 +28,34 @@ app::app(fm_settings&& opts) :
_table{loader.anim_atlas("table", loader.SCENERY_PATH)},
_control_panel(loader.anim_atlas("control-panel", loader.SCENERY_PATH))
{
- world& w = M->world();
- chunk_coords coord{0 ,0};
+ reset_world();
+ auto& w = M->world();
+ constexpr chunk_coords coord{0, 0};
maybe_initialize_chunk_(coord, w[coord]);
reset_camera_offset();
inspectors.reserve(16);
- _character_id = w.make_entity<character>(global_coords{})->id;
}
app::~app() = default;
+void app::reset_world()
+{
+ reset_world(floormat::world{});
+}
+
+void app::reset_world(struct world&& w)
+{
+ _character_id = 0;
+ if (!M)
+ return;
+ auto& w2 = M->reset_world(std::move(w));
+ w2.collect(true);
+ _character_id = w2.make_entity<character>(global_coords{})->id;
+}
+
int app::exec()
{
- int ret = M->exec();
- if (M)
- M->reset_world();
- return ret;
+ return M->exec();
}
static const char* const true_values[] = { "1", "true", "yes", "y", "Y", "on", "ON", "enable", "enabled", };
diff --git a/editor/app.hpp b/editor/app.hpp
index abfe2418..4cb7305c 100644
--- a/editor/app.hpp
+++ b/editor/app.hpp
@@ -75,6 +75,8 @@ private:
void maybe_initialize_chunk(const chunk_coords& pos, chunk& c) override;
void maybe_initialize_chunk_(const chunk_coords& pos, chunk& c);
void update_character(float dt);
+ void reset_world();
+ void reset_world(struct world&& w);
void draw() override;
diff --git a/editor/save.cpp b/editor/save.cpp
index 8e38b2da..6d79bfce 100644
--- a/editor/save.cpp
+++ b/editor/save.cpp
@@ -45,13 +45,14 @@ void app::do_quickload()
return;
}
fputs("quickload... ", stderr); fflush(stderr);
- M->reset_world(world::deserialize(quicksave_file));
+ reset_world(world::deserialize(quicksave_file));
fputs("done\n", stderr); fflush(stderr);
}
void app::do_new_file()
{
- auto& w = M->reset_world();
+ reset_world();
+ auto& w = M->world();
maybe_initialize_chunk_(chunk_coords{}, w[chunk_coords{}]);
}
diff --git a/editor/update.cpp b/editor/update.cpp
index 07d703e0..6107cfa6 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -202,12 +202,9 @@ void app::update_world(float dt)
void app::update_character([[maybe_unused]] float dt)
{
auto& w = M->world();
- auto cptr = w.find_entity(_character_id);
- if (cptr)
- {
- fm_debug_assert(cptr->type == entity_type::character);
- static_cast<character&>(*cptr).set_keys(keys[key_left], keys[key_right], keys[key_up], keys[key_down]);
- }
+ auto c = w.find_entity<character>(_character_id);
+ if (c)
+ c->set_keys(keys[key_left], keys[key_right], keys[key_up], keys[key_down]);
}
void app::set_cursor()
diff --git a/floormat/main.hpp b/floormat/main.hpp
index 92882bae..3fbef522 100644
--- a/floormat/main.hpp
+++ b/floormat/main.hpp
@@ -66,7 +66,7 @@ struct floormat_main
virtual struct world& world() noexcept = 0;
virtual struct world& reset_world() noexcept = 0;
- virtual struct world& reset_world(struct world) noexcept = 0;
+ virtual struct world& reset_world(struct world&& w) noexcept = 0;
virtual SDL_Window* window() noexcept = 0;
Vector2 dpi_scale() const noexcept { return _dpi_scale; }
static int get_mods() noexcept;
diff --git a/main/main-impl.hpp b/main/main-impl.hpp
index ddb16e81..9e8527ad 100644
--- a/main/main-impl.hpp
+++ b/main/main-impl.hpp
@@ -34,7 +34,7 @@ struct main_impl final : Platform::Sdl2Application, floormat_main
struct world& world() noexcept override;
struct world& reset_world() noexcept override;
- struct world& reset_world(struct world) noexcept override;
+ struct world& reset_world(struct world&& w) noexcept override;
SDL_Window* window() noexcept override;
fm_settings& settings() noexcept override;
diff --git a/main/setup.cpp b/main/setup.cpp
index 1f0adcb4..0212d4fc 100644
--- a/main/setup.cpp
+++ b/main/setup.cpp
@@ -103,7 +103,7 @@ struct world& main_impl::reset_world() noexcept
return reset_world(floormat::world{});
}
-struct world& main_impl::reset_world(struct world w) noexcept
+struct world& main_impl::reset_world(struct world&& w) noexcept
{
_clickable_scenery.clear();
_world = std::move(w);
diff --git a/src/chunk.hpp b/src/chunk.hpp
index dc784685..15cb1dc0 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -50,7 +50,7 @@ struct chunk final
bool empty(bool force = false) const noexcept;
- chunk(struct world& w) noexcept;
+ explicit chunk(struct world& w) noexcept;
~chunk() noexcept;
chunk(const chunk&) = delete;
chunk& operator=(const chunk&) = delete;
diff --git a/src/world.cpp b/src/world.cpp
index f5c378cd..71f6c7a9 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -4,6 +4,27 @@
namespace floormat {
+world::world(world&& w) noexcept = default;
+
+world& world::operator=(world&& w) noexcept
+{
+ fm_assert(!w._teardown);
+ if (&w != this) [[likely]]
+ {
+ _last_collection = w._last_collection;
+ _collect_every = w._collect_every;
+ _unique_id = std::move(w._unique_id);
+ w._unique_id = std::make_shared<char>('D');
+ _last_chunk = {};
+ _chunks = std::move(w._chunks);
+ _entities = std::move(w._entities);
+
+ for (auto& [id, c] : _chunks)
+ c._world = this;
+ }
+ return *this;
+}
+
world::world() : world{initial_capacity}
{
}
@@ -88,14 +109,15 @@ void world::collect(bool force)
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, chunk& c, global_coords pos)
+void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos)
{
- fm_debug_assert(e->id > min_id && &c.world() == this);
+ fm_debug_assert(e->id > min_id);
+ 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);
e->coord = pos;
_entities[e->id] = e;
- c.add_entity(e);
+ e->c.add_entity(e);
}
void world::do_kill_entity(std::uint64_t id)
@@ -105,7 +127,7 @@ void world::do_kill_entity(std::uint64_t id)
fm_debug_assert(cnt > 0);
}
-std::shared_ptr<entity> world::find_entity(std::uint64_t id)
+std::shared_ptr<entity> world::find_entity_(std::uint64_t id)
{
auto it = _entities.find(id);
return it == _entities.end() ? nullptr : it->second.lock();
diff --git a/src/world.hpp b/src/world.hpp
index 039f3387..89a1624c 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -31,6 +31,7 @@ private:
std::unordered_map<std::uint64_t, std::weak_ptr<entity>> _entities;
std::size_t _last_collection = 0;
std::size_t _collect_every = 64;
+ std::shared_ptr<char> _unique_id = std::make_shared<char>('A');
bool _teardown : 1 = false;
@@ -38,8 +39,9 @@ private:
explicit world(std::size_t capacity);
- void do_make_entity(const std::shared_ptr<entity>& e, chunk& c, global_coords pos);
+ void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos);
void do_kill_entity(std::uint64_t id);
+ std::shared_ptr<entity> find_entity_(std::uint64_t id);
friend struct entity;
@@ -73,15 +75,17 @@ public:
{
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)...});
- do_make_entity(std::static_pointer_cast<entity>(ret), ret->c, pos);
+ do_make_entity(std::static_pointer_cast<entity>(ret), pos);
return ret;
}
- std::shared_ptr<entity> find_entity(std::uint64_t id);
+ template<typename T = entity, typename U = entity> std::shared_ptr<T> find_entity(std::uint64_t id);
bool is_teardown() const { return _teardown; }
+ world& operator=(world&& w) noexcept;
+ world(world&& w) noexcept;
+
fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world);
- fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(world);
};
template<typename Hash, typename Alloc, typename Pred>
@@ -92,4 +96,15 @@ world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks
operator[](coord) = std::move(c);
}
+template<typename T, typename U>
+std::shared_ptr<T> world::find_entity(std::uint64_t id)
+{
+ static_assert(std::is_base_of_v<entity, T>);
+ std::shared_ptr<U> ptr = find_entity_(id);
+ if (!ptr)
+ return nullptr;
+ fm_assert(ptr->type == entity_type_<T>::value);
+ return std::static_pointer_cast<T>(ptr);
+}
+
} // namespace floormat