diff options
-rw-r--r-- | draw/anim.cpp | 7 | ||||
-rw-r--r-- | main/clickable.hpp | 3 | ||||
-rw-r--r-- | src/chunk-collision.cpp | 17 | ||||
-rw-r--r-- | src/chunk.hpp | 3 | ||||
-rw-r--r-- | src/entity.cpp | 43 | ||||
-rw-r--r-- | src/entity.hpp | 6 |
6 files changed, 47 insertions, 32 deletions
diff --git a/draw/anim.cpp b/draw/anim.cpp index 47d84a97..4af20b8f 100644 --- a/draw/anim.cpp +++ b/draw/anim.cpp @@ -79,11 +79,13 @@ void anim_mesh::draw(tile_shader& shader, chunk& c) auto& atlas = *e.atlas; if (last && &atlas != last.atlas) { + //Debug{} << "draw-static" << last.atlas->name() << e.ordinal() << Vector2i(e.coord.local()); do_draw(last.run_from, i, last.atlas); last = nullptr; } if (e.is_dynamic()) { + //Debug{} << "draw-dyn" << e.atlas->name() << e.ordinal() << Vector2i(e.coord.local()); draw(shader, atlas, e.r, e.frame, e.coord.local(), e.offset, tile_shader::scenery_depth_offset); last = nullptr; } @@ -95,7 +97,12 @@ void anim_mesh::draw(tile_shader& shader, chunk& c) } } if (last) + { + //Debug{} << "draw-last" << last.atlas->name() << es[es.size()-1]->ordinal() << Vector2i(es[es.size()-1]->coord.local()); do_draw(last.run_from, i, last.atlas); + } + + //Debug{} << "--" << i << "--"; //#define FM_DEBUG_DRAW_COUNT #ifdef FM_DEBUG_DRAW_COUNT diff --git a/main/clickable.hpp b/main/clickable.hpp index c7502f33..cd6dd895 100644 --- a/main/clickable.hpp +++ b/main/clickable.hpp @@ -13,7 +13,8 @@ struct clickable final { Math::Range2D<int> dest; BitArrayView bitmask; std::shared_ptr<entity> e; - std::uint32_t depth, stride; + std::int32_t depth; + std::uint32_t stride; bool mirrored; }; 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; |