summaryrefslogtreecommitdiffhomepage
path: root/KeyboardController.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-18 12:39:58 +0200
committerAki <please@ignore.pl>2022-04-18 12:39:58 +0200
commit97975da88cb25952af0c5ec75c130c66595d1807 (patch)
tree4ae958a28819cfbd55882451651fe4497e12b292 /KeyboardController.cpp
parent7c9c492f1555bb84a43fc68a56f69c25b54e7346 (diff)
downloadbullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.zip
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.gz
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.bz2
Added controller for player character
Diffstat (limited to 'KeyboardController.cpp')
-rw-r--r--KeyboardController.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/KeyboardController.cpp b/KeyboardController.cpp
new file mode 100644
index 0000000..2273008
--- /dev/null
+++ b/KeyboardController.cpp
@@ -0,0 +1,46 @@
+#include "KeyboardController.h"
+
+#include <cmath>
+
+#include <raylib.h>
+
+
+static constexpr float DIAGONAL {std::sin(M_PI / 4.0f)};
+
+
+static int check_axis(int negative_key, int positive_key, int last);
+
+
+
+KeyboardController::KeyboardController() :
+ x_axis {0},
+ y_axis {0}
+{
+}
+
+
+Vector2
+KeyboardController::direction()
+{
+ x_axis = check_axis(KEY_LEFT, KEY_RIGHT, x_axis);
+ y_axis = check_axis(KEY_UP, KEY_DOWN, y_axis);
+ if (x_axis != 0 && y_axis != 0)
+ return {x_axis * DIAGONAL, y_axis * DIAGONAL};
+ return {x_axis, y_axis};
+}
+
+
+int
+check_axis(const int negative_key, const int positive_key, const int last)
+{
+ int next = last;
+ if (last == 1 && IsKeyReleased(positive_key))
+ next = 0;
+ else if (last == -1 && IsKeyReleased(negative_key))
+ next = 0;
+ if (IsKeyPressed(positive_key))
+ next = 1;
+ else if (IsKeyPressed(negative_key))
+ next = -1;
+ return next;
+}