diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-29 00:14:27 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-29 00:14:27 +0100 |
commit | 3b03a82fb6db0a3fda4d711456fc89e7d21bfe26 (patch) | |
tree | b8d1f691a8c3ccaeca1ca9f7bc000f7f86ca1212 /src | |
parent | 3a4fb937342c5bc57fce0f48b81b81ed06b32bf9 (diff) |
prefer array over jump table
Diffstat (limited to 'src')
-rw-r--r-- | src/critter.cpp | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/critter.cpp b/src/critter.cpp index 4068c782..8be3678b 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -49,24 +49,21 @@ constexpr auto arrows_to_dir(bool left, bool right, bool up, bool down) constexpr Vector2 rotation_to_vec(rotation r) { constexpr double c = move_speed * frame_time; + constexpr double d = c / Vector2d{1, 1}.length(); - CORRADE_ASSUME(r < rotation_COUNT); + constexpr Vector2 array[8] = { + Vector2(Vector2d{ 0, -1} * c), + Vector2(Vector2d{ 1, -1} * d), + Vector2(Vector2d{ 1, 0} * c), + Vector2(Vector2d{ 1, 1} * d), + Vector2(Vector2d{ 0, 1} * c), + Vector2(Vector2d{-1, 1} * d), + Vector2(Vector2d{-1, 0} * c), + Vector2(Vector2d{-1, -1} * d), + }; - constexpr double d = c / Vector2d{1, 1}.length(); - switch (r) - { - using enum rotation; - case NE: return Vector2(Vector2d{ 1, -1} * d); - case SE: return Vector2(Vector2d{ 1, 1} * d); - case SW: return Vector2(Vector2d{-1, 1} * d); - case NW: return Vector2(Vector2d{-1, -1} * d); - case N: return Vector2(Vector2d{ 0, -1} * c); - case E: return Vector2(Vector2d{ 1, 0} * c); - case S: return Vector2(Vector2d{ 0, 1} * c); - case W: return Vector2(Vector2d{-1, 0} * c); - } - std::unreachable(); - fm_assert(false); + CORRADE_ASSUME(r < rotation_COUNT); + return array[(size_t)r]; } constexpr std::array<rotation, 3> rotation_to_similar(rotation r) |