summaryrefslogtreecommitdiffhomepage
path: root/Player.cpp
diff options
context:
space:
mode:
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;
}