summaryrefslogtreecommitdiffhomepage
path: root/src/chunk.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-12-23 14:42:15 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-12-26 04:39:12 +0100
commit3370445a2e22ee00687ecef2e9dc88f47bb2b4c6 (patch)
treea99ec32a02fc91d729205fc26d7183b6b4214b28 /src/chunk.cpp
parentc141a9efcfaecadffe771bd0c37bea0e46a3f9aa (diff)
a
Diffstat (limited to 'src/chunk.cpp')
-rw-r--r--src/chunk.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp
index 17fe1b49..652bd63d 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -1,5 +1,6 @@
#include "chunk.hpp"
#include "object.hpp"
+#include "world.hpp"
#include "tile-iterator.hpp"
#include <algorithm>
#include <Magnum/GL/Context.h>
@@ -42,6 +43,34 @@ tile_proto chunk::operator[](size_t idx) const noexcept { return tile_proto(tile
tile_ref chunk::operator[](local_coords xy) noexcept { return operator[](xy.to_index()); }
tile_proto chunk::operator[](local_coords xy) const noexcept { return operator[](xy.to_index()); }
+tile_ref chunk::at_offset(local_coords pos, Vector2i off)
+{
+ const auto coord = global_coords{_coord, pos};
+ const auto coord2 = coord + off;
+ if (coord.chunk() == coord2.chunk()) [[likely]]
+ return operator[](coord2.local());
+ else
+ return (*_world)[coord2].t;
+}
+
+Optional<tile_ref> chunk::at_offset_(local_coords pos, Vector2i off)
+{
+ const auto coord = global_coords{_coord, pos};
+ const auto coord2 = coord + off;
+ if (coord.chunk() == coord2.chunk()) [[likely]]
+ return operator[](coord2.local());
+ else
+ {
+ if (auto* ch = _world->at({coord2}))
+ return (*ch)[coord2.local()];
+ else
+ return NullOpt;
+ }
+}
+
+tile_ref chunk::at_offset(tile_ref r, Vector2i off) { return at_offset(local_coords{r.index()}, off); }
+Optional<tile_ref> chunk::at_offset_(tile_ref r, Vector2i off) { return at_offset_(local_coords{r.index()}, off); }
+
auto chunk::begin() noexcept -> iterator { return iterator { *this, 0 }; }
auto chunk::end() noexcept -> iterator { return iterator { *this, TILE_COUNT }; }
auto chunk::cbegin() const noexcept -> const_iterator { return const_iterator { *this, 0 }; }