#include "Player.h" #include #include #include #include "KeyboardController.h" static constexpr float ACCELERATION {1800}; static constexpr float MAX_SPEED {180}; static constexpr float DAMP {2200}; static constexpr float MARGIN {0.3f}; static float damped_velocity(float dt, float direction, float velocity); Player::Player() : m_position {400, 450}, m_velocity {0, 0}, m_controller {std::make_unique()} { } void Player::update(const float dt) { const auto direction = m_controller->direction(); m_velocity.x = std::clamp(damped_velocity(dt, direction.x, m_velocity.x), -MAX_SPEED, MAX_SPEED); m_velocity.y = std::clamp(damped_velocity(dt, direction.y, m_velocity.y), -MAX_SPEED, MAX_SPEED); m_position.x += m_velocity.x * dt; m_position.y += m_velocity.y * dt; } void Player::draw() { DrawCircle(m_position.x, m_position.y, 10, LIGHTGRAY); } float damped_velocity(const float dt, const float direction, const float velocity) { if (direction > 0 || direction < 0) return velocity + direction * ACCELERATION * dt; if (velocity > 0) { if (velocity > MARGIN) { const float next = velocity - dt * DAMP; if (next < MARGIN) return 0; else return next; } return 0; } if (velocity < 0) { if (velocity < MARGIN) { const float next = velocity + dt * DAMP; if (next > MARGIN) return 0; else return next; } return 0; } return 0; }