diff --git a/include/player.hpp b/include/player.hpp index f901d98..ec2ae7e 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -14,6 +14,7 @@ public: float m_jump_strength; float m_thickness; bool m_jumping; + void handle_movment(); sf::Vector2 get_velocity(); bool get_jumping(); diff --git a/src/main.cpp b/src/main.cpp index 04b34a2..1038330 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,15 +5,6 @@ #include "globals.hpp" #include "player.hpp" -struct Player_Attributes { - sf::Vector2 m_size; - const float m_gravity; - const float jump_strength; - float m_thickness; - float m_velocityY; - bool jumping; -}; - int main() { float floor_height = 2.0f; bool jumping = false; @@ -40,25 +31,14 @@ int main() { if (event->is()) window.close(); } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { + window.close(); + } + player.handle_movment(); /* TODO * Move these to a player movement handler * * */ - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { - player.rect.move({2.5f, 0}); - } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { - player.rect.move({-2.5f, 0}); - } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { - player.rect.move({0, -2.5}); - } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) { - player.rect.move({0, 2.5}); - } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { - window.close(); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !player.m_jumping) { - player.m_jumping = true; - player.m_velocity.y = -player.m_jump_strength; // push up - } /* TODO * Move these to a player update loop to move diff --git a/src/player.cpp b/src/player.cpp index 6cb50f1..ae632aa 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,5 +1,7 @@ #include "player.hpp" + #include +#include Player::Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x) { rect.setSize(size); @@ -9,3 +11,21 @@ Player::Player(sf::Vector2 size, sf::Vector2 starting_position, fl m_thickness = thickness; m_velocity.x = velocity_x; } + +void Player::handle_movment() { + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { + rect.move({2.5f, 0}); + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { + rect.move({-2.5f, 0}); + } + // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { + // rect.move({0, -2.5}); + // } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) { + // rect.move({0, 2.5}); + // } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { + m_jumping = true; + m_velocity.y = -m_jump_strength; // push up + } + +}