summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-03-16 08:30:41 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-03-17 23:23:11 +0100
commitfd9298ef43136fa89dab034096690a08027ebf44 (patch)
tree312aefb9cddd02511ec09bbce9aa2bf495a42e8c /src
parentea37d1030186f76b88f189f6ea0cc2b2d57a985a (diff)
a
Diffstat (limited to 'src')
-rw-r--r--src/chunk-collision.cpp17
-rw-r--r--src/chunk.hpp3
-rw-r--r--src/entity.cpp43
-rw-r--r--src/entity.hpp6
4 files changed, 38 insertions, 31 deletions
diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp
index b7948501..b78f43a4 100644
--- a/src/chunk-collision.cpp
+++ b/src/chunk-collision.cpp
@@ -19,11 +19,11 @@ constexpr Vector2 tile_start(std::size_t k)
return TILE_SIZE2 * Vector2(coord) - half_tile;
}
-Pair<Vector2i, Vector2i> scenery_tile(const entity& sc)
+Pair<Vector2i, Vector2i> scenery_tile(local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size)
{
- auto center = iTILE_SIZE2 * Vector2i(sc.coord.local()) + Vector2i(sc.offset) + Vector2i(sc.bbox_offset);
- auto min = center - Vector2i(sc.bbox_size/2);
- auto size = Vector2i(sc.bbox_size);
+ auto center = iTILE_SIZE2 * Vector2i(local) + Vector2i(offset) + Vector2i(bbox_offset);
+ auto min = center - Vector2i(bbox_size/2);
+ auto size = Vector2i(bbox_size);
return { min, min + size, };
}
@@ -97,14 +97,19 @@ void chunk::ensure_passability() noexcept
}
}
-bool chunk::_bbox_for_scenery(const entity& s, bbox& value) noexcept
+bool chunk::_bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, bbox& value) noexcept
{
- auto [start, end] = scenery_tile(s);
+ auto [start, end] = scenery_tile(local, offset, s.bbox_offset, s.bbox_size);
auto id = make_id(collision_type::scenery, s.pass, s.id);
value = { .id = id, .start = start, .end = end };
return s.atlas && s.pass != pass_mode::pass;
}
+bool chunk::_bbox_for_scenery(const entity& s, bbox& value) noexcept
+{
+ return _bbox_for_scenery(s, s.coord.local(), s.offset, value);
+}
+
void chunk::_remove_bbox(const bbox& x)
{
if (_scenery_modified)
diff --git a/src/chunk.hpp b/src/chunk.hpp
index 15cb1dc0..497c0181 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -150,7 +150,8 @@ private:
bool operator==(const bbox& other) const noexcept;
};
- bool _bbox_for_scenery(const entity& s, bbox& value) noexcept;
+ static bool _bbox_for_scenery(const entity& s, bbox& value) noexcept;
+ static bool _bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, bbox& value) noexcept;
void _remove_bbox(const bbox& x);
void _add_bbox(const bbox& x);
void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b);
diff --git a/src/entity.cpp b/src/entity.cpp
index 3dc80fbc..09d4dfe2 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -43,20 +43,20 @@ entity::~entity() noexcept
w.do_kill_entity(id);
}
-std::uint32_t entity_proto::ordinal(local_coords local) const
+std::int32_t entity_proto::ordinal(local_coords local) const
{
return entity::ordinal(local, offset);
}
-std::uint32_t entity::ordinal() const
+std::int32_t entity::ordinal() const
{
return ordinal(coord.local(), offset);
}
-std::uint32_t entity::ordinal(local_coords xy, Vector2b offset)
+std::int32_t entity::ordinal(local_coords xy, Vector2b offset)
{
- constexpr auto x_size = (std::uint32_t)TILE_MAX_DIM * (std::uint32_t)iTILE_SIZE[0];
- auto vec = Vector2ui(xy) * Vector2ui(iTILE_SIZE2) + Vector2ui(offset);
+ constexpr auto x_size = (std::int32_t)TILE_MAX_DIM * (std::int32_t)iTILE_SIZE[0];
+ auto vec = Vector2i(xy) * Vector2i(iTILE_SIZE2) + Vector2i(offset);
return vec[1] * x_size + vec[0];
}
@@ -140,13 +140,13 @@ bool entity::can_move_to(Vector2i delta)
void entity::move(It it, Vector2i delta)
{
auto e_ = *it;
- auto& e = *e_;
+ const auto& e = *e_;
auto& c = e.c;
auto& w = *c._world;
- auto& coord = e.coord;
- auto& offset = e.offset;
+ const auto coord = e.coord;
+ const auto offset = e.offset;
auto& es = c._entities;
- auto [coord_, offset_] = normalize_coords(coord, offset, delta);
+ const auto [coord_, offset_] = normalize_coords(coord, offset, delta);
if (coord_ == coord && offset_ == offset)
return;
@@ -154,23 +154,24 @@ void entity::move(It it, Vector2i delta)
if (!e.is_dynamic())
c.mark_scenery_modified(false);
- bool same_chunk = coord_.chunk() == coord.chunk();
chunk::bbox bb0, bb1;
- bool b0 = c._bbox_for_scenery(e, bb0);
- coord = coord_; offset = offset_;
- bool b1 = c._bbox_for_scenery(e, bb1);
+ bool b0 = c._bbox_for_scenery(e, bb0),
+ b1 = c._bbox_for_scenery(e, coord_.local(), offset_, bb1);
+ const auto ord = e.ordinal(coord_.local(), offset_);
- if (same_chunk)
+ if (coord_.chunk() == coord.chunk())
{
c._replace_bbox(bb0, bb1, b0, b1);
- auto it_ = std::lower_bound(es.cbegin(), es.cend(), e_, [ord = e.ordinal()](const auto& a, const auto&) { return a->ordinal() < ord; });
- if (it_ != it)
+ auto it_ = std::lower_bound(es.cbegin(), es.cend(), e_, [ord](const auto& a, const auto&) { return a->ordinal() < ord; });
+ e_->coord = coord_;
+ e_->offset = offset_;
+ auto pos0 = std::distance(es.cbegin(), it), pos1 = std::distance(es.cbegin(), it_);
+ if (pos1 > pos0)
+ pos1--;
+ if (pos1 != pos0)
{
- auto pos0 = std::distance(es.cbegin(), it), pos1 = std::distance(es.cbegin(), it_);
- if (pos1 > pos0)
- pos1--;
es.erase(it);
- [[maybe_unused]] auto size = es.size();
+ //fm_debug("insert %td -> %zu", pos1, es.size());
es.insert(es.cbegin() + pos1, std::move(e_));
}
}
@@ -183,7 +184,7 @@ void entity::move(It it, Vector2i delta)
c2._add_bbox(bb1);
c.remove_entity(it);
auto it_ = std::lower_bound(c2._entities.cbegin(), c2._entities.cend(), e_,
- [ord = e.ordinal()](const auto& a, const auto&) { return a->ordinal() < ord; });
+ [ord](const auto& a, const auto&) { return a->ordinal() < ord; });
c2._entities.insert(it_, std::move(e_));
}
}
diff --git a/src/entity.hpp b/src/entity.hpp
index 6bfad752..a0a6ed53 100644
--- a/src/entity.hpp
+++ b/src/entity.hpp
@@ -24,7 +24,7 @@ struct entity_proto
rotation r : rotation_BITS = rotation::N;
pass_mode pass : pass_mode_BITS = pass_mode::see_through;
- std::uint32_t ordinal(local_coords coord) const;
+ std::int32_t ordinal(local_coords coord) const;
entity_proto& operator=(const entity_proto&);
entity_proto();
entity_proto(const entity_proto&);
@@ -51,8 +51,8 @@ struct entity
virtual ~entity() noexcept;
- std::uint32_t ordinal() const;
- static std::uint32_t ordinal(local_coords xy, Vector2b offset);
+ std::int32_t ordinal() const;
+ static std::int32_t ordinal(local_coords xy, Vector2b offset);
struct chunk& chunk() const;
It iter() const;