summaryrefslogtreecommitdiffhomepage
path: root/Player.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 /Player.cpp
parent7c9c492f1555bb84a43fc68a56f69c25b54e7346 (diff)
downloadbullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.zip
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.gz
bullethell2022-97975da88cb25952af0c5ec75c130c66595d1807.tar.bz2
Added controller for player character
Diffstat (limited to 'Player.cpp')
-rw-r--r--Player.cpp26
1 files changed, 21 insertions, 5 deletions
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;
}