summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-03-01 11:48:59 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-03-01 11:48:59 +0100
commit078c376b6255fb6fd24362b27862819444327265 (patch)
tree2c160075a1cd171a034fd475e82f5af5209291cf /src
parent51398dfed2c6615aada518d85bf2d6d04f307063 (diff)
src: make scenery aware of its chunk
Diffstat (limited to 'src')
-rw-r--r--src/chunk.cpp4
-rw-r--r--src/chunk.hpp3
-rw-r--r--src/scenery.cpp8
-rw-r--r--src/scenery.hpp24
-rw-r--r--src/tile.cpp2
5 files changed, 33 insertions, 8 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp
index 774a874c..b7f54e4d 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -15,7 +15,9 @@ bool chunk::empty(bool force) const noexcept
tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); }
tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); }
-anim_atlas* chunk::scenery_atlas_at(std::size_t i) const noexcept { return _scenery_atlases[i].get(); }
+
+std::shared_ptr<anim_atlas>& chunk::scenery_atlas_at(std::size_t i) noexcept { return _scenery_atlases[i]; }
+scenery& chunk::scenery_at(std::size_t i) noexcept { return _scenery_variants[i]; }
tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; }
tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); }
diff --git a/src/chunk.hpp b/src/chunk.hpp
index 6ba8ef5e..8d6f5a6b 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -82,7 +82,8 @@ struct chunk final
tile_atlas* wall_atlas_at(std::size_t i) const noexcept;
scenery_mesh_tuple ensure_scenery_mesh() noexcept;
- anim_atlas* scenery_atlas_at(std::size_t i) const noexcept;
+ std::shared_ptr<anim_atlas>& scenery_atlas_at(std::size_t i) noexcept;
+ scenery& scenery_at(std::size_t i) noexcept;
void ensure_passability() noexcept;
diff --git a/src/scenery.cpp b/src/scenery.cpp
index 90c33032..7c32248b 100644
--- a/src/scenery.cpp
+++ b/src/scenery.cpp
@@ -1,5 +1,6 @@
#include "scenery.hpp"
#include "anim-atlas.hpp"
+#include "chunk.hpp"
#include "compat/assert.hpp"
#include "rotation.inl"
#include <algorithm>
@@ -15,9 +16,14 @@ scenery_proto& scenery_proto::operator=(const scenery_proto&) noexcept = default
scenery_proto::scenery_proto(const scenery_proto&) noexcept = default;
scenery_proto::operator bool() const noexcept { return atlas != nullptr; }
-scenery_ref::scenery_ref(std::shared_ptr<anim_atlas>& atlas, scenery& frame) noexcept : atlas{atlas}, frame{frame} {}
+scenery_ref::scenery_ref(struct chunk& c, std::size_t i) noexcept :
+ atlas{c.scenery_atlas_at(i)}, frame{c.scenery_at(i)},
+ c{&c}, idx{std::uint8_t(i)}
+{}
scenery_ref::scenery_ref(const scenery_ref&) noexcept = default;
scenery_ref::scenery_ref(scenery_ref&&) noexcept = default;
+struct chunk& scenery_ref::chunk() noexcept { return *c; }
+std::uint8_t scenery_ref::index() const noexcept { return idx; }
scenery_ref& scenery_ref::operator=(const scenery_proto& proto) noexcept
{
diff --git a/src/scenery.hpp b/src/scenery.hpp
index 9e58f612..5fe2f043 100644
--- a/src/scenery.hpp
+++ b/src/scenery.hpp
@@ -10,6 +10,7 @@
namespace floormat {
+struct chunk;
struct anim_atlas;
enum class scenery_type : std::uint8_t {
@@ -82,15 +83,30 @@ struct scenery_proto final
};
struct scenery_ref final {
- std::shared_ptr<anim_atlas>& atlas;
- scenery& frame;
-
- scenery_ref(std::shared_ptr<anim_atlas>& atlas, scenery& frame) noexcept;
+ scenery_ref(struct chunk& c, std::size_t i) noexcept;
scenery_ref(const scenery_ref&) noexcept;
scenery_ref(scenery_ref&&) noexcept;
scenery_ref& operator=(const scenery_proto& proto) noexcept;
+
operator scenery_proto() const noexcept;
operator bool() const noexcept;
+
+ struct chunk& chunk() noexcept;
+ std::uint8_t index() const noexcept;
+
+ template<std::size_t N> std::tuple_element_t<N, scenery_ref>& get() & { if constexpr(N == 0) return atlas; else return frame; }
+ template<std::size_t N> std::tuple_element_t<N, scenery_ref>& get() && { if constexpr(N == 0) return atlas; else return frame; }
+
+ std::shared_ptr<anim_atlas>& atlas;
+ scenery& frame;
+
+private:
+ struct chunk* c;
+ std::uint8_t idx;
};
} // namespace floormat
+
+template<> struct std::tuple_size<floormat::scenery_ref> final : std::integral_constant<std::size_t, 2> {};
+template<> struct std::tuple_element<0, floormat::scenery_ref> final { using type = std::shared_ptr<floormat::anim_atlas>; };
+template<> struct std::tuple_element<1, floormat::scenery_ref> final { using type = floormat::scenery; };
diff --git a/src/tile.cpp b/src/tile.cpp
index 34d2a6e1..25b413a9 100644
--- a/src/tile.cpp
+++ b/src/tile.cpp
@@ -30,7 +30,7 @@ std::shared_ptr<const anim_atlas> tile_ref::scenery_atlas() const noexcept {
tile_image_ref tile_ref::ground() noexcept { return {_chunk->_ground_atlases[i], _chunk->_ground_variants[i] }; }
tile_image_ref tile_ref::wall_north() noexcept { return {_chunk->_wall_atlases[i*2+0], _chunk->_wall_variants[i*2+0] }; }
tile_image_ref tile_ref::wall_west() noexcept { return {_chunk->_wall_atlases[i*2+1], _chunk->_wall_variants[i*2+1] }; }
-scenery_ref tile_ref::scenery() noexcept { return {_chunk->_scenery_atlases[i], _chunk->_scenery_variants[i] }; }
+scenery_ref tile_ref::scenery() noexcept { return { *_chunk, i }; }
tile_image_proto tile_ref::ground() const noexcept { return { _chunk->_ground_atlases[i], _chunk->_ground_variants[i] }; }
tile_image_proto tile_ref::wall_north() const noexcept { return { _chunk->_wall_atlases[i*2+0], _chunk->_wall_variants[i*2+0] }; }