diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-19 03:09:18 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-19 03:09:18 +0100 |
commit | 02e32261880cfd90c300907c01e2e87e59dc7343 (patch) | |
tree | e306672eaf8bc99d6ee135b947d55e8f73d9bf46 /src | |
parent | 67f029d1192f7baf881f6c1afd0bb2c301165ae9 (diff) |
src/entity: short-circuit in can_rotate()
Diffstat (limited to 'src')
-rw-r--r-- | src/character.cpp | 2 | ||||
-rw-r--r-- | src/entity.cpp | 22 | ||||
-rw-r--r-- | src/entity.hpp | 13 |
3 files changed, 24 insertions, 13 deletions
diff --git a/src/character.cpp b/src/character.cpp index f63b35e9..31376925 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -125,7 +125,7 @@ bool character::update(size_t i, float dt) offset_frac = Vector2s(Vector2(std::fmod(offset_[0], 1.f), std::fmod(offset_[1], 1.f)) * frac); auto off_i = Vector2i(offset_); if (can_move_to(off_i)) - i = move(i, off_i, new_r); + i = move_to(i, off_i, new_r); ++frame %= atlas->info().nframes; } //Debug{} << "pos" << Vector2i(pos.local()); diff --git a/src/entity.cpp b/src/entity.cpp index 8df9a9ce..20435f45 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -90,9 +90,17 @@ size_t entity::index() const bool entity::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) { - bbox_offset = rotate_point(bbox_offset, old_r, new_r); - bbox_size = rotate_size(bbox_size, old_r, new_r); - return can_move_to({}, coord, offset, bbox_offset, bbox_size); + const auto bbox_offset_ = rotate_point(bbox_offset, old_r, new_r); + const auto bbox_size_ = rotate_size(bbox_size, old_r, new_r); + if (bbox_offset_.isZero() && bbox_size_[0] == bbox_size_[1] || + bbox_offset_ == bbox_offset && bbox_size_ == bbox_size) + return true; + return can_move_to({}, coord, offset, bbox_offset_, bbox_size_); +} + +bool entity::can_rotate(rotation new_r) +{ + return can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size); } void entity::rotate(size_t, rotation new_r) @@ -152,17 +160,17 @@ bool entity::can_move_to(Vector2i delta) return can_move_to(delta, coord, offset, bbox_offset, bbox_size); } -size_t entity::move(size_t i, Vector2i delta, rotation new_r) +size_t entity::move_to(size_t i, Vector2i delta, rotation new_r) { + if (!can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size)) + return i; + auto& es = c->_entities; fm_debug_assert(i < es.size()); auto e_ = es[i]; auto& w = *c->_world; const auto [coord_, offset_] = normalize_coords(coord, offset, delta); - if (!can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size)) - return i; - if (coord_ == coord && offset_ == offset) return i; diff --git a/src/entity.hpp b/src/entity.hpp index 3f82842d..490f0b7d 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -62,14 +62,17 @@ struct entity virtual bool can_activate(size_t i) const; virtual bool activate(size_t i); virtual bool update(size_t i, float dt) = 0; - [[nodiscard]] virtual bool can_rotate(global_coords coord, rotation r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); virtual void rotate(size_t i, rotation r); + virtual bool can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); + virtual bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); + virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); - [[nodiscard]] virtual bool can_move_to(Vector2i delta); - [[nodiscard]] bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); - size_t move(size_t i, Vector2i delta, rotation new_r); - virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); + + bool can_rotate(rotation new_r); + bool can_move_to(Vector2i delta); + size_t move_to(size_t i, Vector2i delta, rotation new_r); + bool is_dynamic() const; friend struct world; |