summaryrefslogtreecommitdiffhomepage
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
parent7c9c492f1555bb84a43fc68a56f69c25b54e7346 (diff)
downloadbullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.zip
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.gz
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.bz2
Added controller for player character
-rw-r--r--CMakeLists.txt1
-rw-r--r--Controller.h9
-rw-r--r--KeyboardController.cpp46
-rw-r--r--KeyboardController.h16
-rw-r--r--Player.cpp26
-rw-r--r--Player.h6
6 files changed, 99 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5b2d655..3c22511 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,6 +15,7 @@ add_executable(
ExampleGenerator.cpp
Game.cpp
GameScreen.cpp
+ KeyboardController.cpp
main.cpp
Player.cpp
TestStage.cpp
diff --git a/Controller.h b/Controller.h
new file mode 100644
index 0000000..4839656
--- /dev/null
+++ b/Controller.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <raylib.h>
+
+
+struct Controller
+{
+ virtual Vector2 direction() = 0;
+};
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;
+}
diff --git a/KeyboardController.h b/KeyboardController.h
new file mode 100644
index 0000000..98b5c6f
--- /dev/null
+++ b/KeyboardController.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <raylib.h>
+
+#include "Controller.h"
+
+
+class KeyboardController : public Controller
+{
+public:
+ KeyboardController();
+ Vector2 direction() override;
+private:
+ int x_axis;
+ int y_axis;
+};
diff --git a/Player.cpp b/Player.cpp
index 736f96a..c5be5a8 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -1,10 +1,21 @@
#include "Player.h"
+#include <algorithm>
+#include <memory>
+
#include <raylib.h>
+#include "KeyboardController.h"
+
+
+static constexpr float ACCELERATION {1900};
+static constexpr float MAX_SPEED {160};
+
Player::Player() :
- m_position {400, 450}
+ m_position {400, 450},
+ m_velocity {0, 0},
+ m_controller {std::make_unique<KeyboardController>()}
{
}
@@ -12,10 +23,15 @@ Player::Player() :
void
Player::update(const float dt)
{
- if (IsKeyDown(KEY_LEFT))
- m_position.x -= dt * 80;
- if (IsKeyDown(KEY_RIGHT))
- m_position.x += dt * 80;
+ const auto direction = m_controller->direction();
+ const Vector2 acceleration {
+ direction.x * ACCELERATION,
+ direction.y * ACCELERATION,
+ };
+ m_velocity.x = std::clamp(m_velocity.x + acceleration.x * dt, -MAX_SPEED, MAX_SPEED);
+ m_velocity.y = std::clamp(m_velocity.y + acceleration.y * dt, -MAX_SPEED, MAX_SPEED);
+ m_position.x += m_velocity.x * dt;
+ m_position.y += m_velocity.y * dt;
}
diff --git a/Player.h b/Player.h
index b138edd..59e8282 100644
--- a/Player.h
+++ b/Player.h
@@ -1,9 +1,12 @@
#pragma once
+#include <memory>
#include <vector>
#include <raylib.h>
+#include "Controller.h"
+
struct Player
{
@@ -12,6 +15,9 @@ struct Player
void draw();
template<typename T> bool collide(const std::vector<T>& bullets) const;
Vector2 m_position;
+ Vector2 m_velocity;
+ std::unique_ptr<Controller> m_controller;
};
+
#include "Player-inl.h"