diff --git a/include/elements.hpp b/include/elements.hpp index da83819..e63e9af 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -5,16 +5,19 @@ #include #include - enum class CollisionDir { None, Left, Right, Top, Bottom }; class Entity { public: - Entity(sf::Vector2 size, sf::Vector2 starting_position, sf::Vector2f initial_velocity, float thickness, sf::Color inside_color, sf::Color border_color = sf::Color::White); + Entity(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, sf::Color inside_color = sf::Color::Black, sf::Color border_color = sf::Color::White); CollisionDir find_intersection(Entity &entity); - sf::Vector2f velocity; + void set_velocity(sf::Vector2f velocity); + void set_velocity_x(int velocity); + void set_velocity_y(int velocity); + sf::Vector2 get_velocity(); sf::RectangleShape rect; protected: + sf::Vector2f _velocity; sf::Color _inside_color; sf::Color _border_color; float _border_thickness; @@ -22,12 +25,11 @@ protected: class Player : public Entity { public: - Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, sf::Vector2f initial_velocity, sf::Color inside_color, sf::Color border_color = sf::Color::White); + Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, sf::Color inside_color, sf::Color border_color); float m_acceleration; float m_jump_strength; bool m_jumping; void handle_movment(); - sf::Vector2 get_velocity(); bool get_jumping(); }; diff --git a/src/elements.cpp b/src/elements.cpp index d3e2705..ac0cc76 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -14,16 +14,29 @@ * * */ - -// CollisionDir Entity::getCollisionDirection(const sf::FloatRect& a, const sf::FloatRect& b) { - -Entity::Entity(sf::Vector2 size, sf::Vector2 starting_position, sf::Vector2f initial_velocity, float thickness, sf::Color inside_color, sf::Color border_color) { +Entity::Entity(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, sf::Color inside_color, sf::Color border_color) { rect.setSize(size); - rect.setPosition(starting_position); - rect.setOutlineThickness(thickness); // Needs to be the same as floor height to be properly on the line; else it will just show up above the line or in the line + rect.setPosition(pos); + rect.setOutlineThickness(border_thickness); rect.setFillColor(inside_color); rect.setOutlineColor(border_color); - velocity = initial_velocity; + _velocity = velocity; +} + +void Entity::set_velocity(sf::Vector2 velocity) { + _velocity = velocity; +} + +void Entity::set_velocity_x(int velocity) { + _velocity.x = velocity; +} + +void Entity::set_velocity_y(int velocity) { + _velocity.y = velocity; +} + +sf::Vector2 Entity::get_velocity() { + return _velocity; } CollisionDir Entity::find_intersection(Entity &entity) { @@ -53,25 +66,20 @@ CollisionDir Entity::find_intersection(Entity &entity) { } } -Player::Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, sf::Vector2f initial_velocity, sf::Color inside_color, sf::Color border_color) : Entity(size, starting_position, initial_velocity, thickness, inside_color, border_color) { - m_acceleration = acceleration; - m_jump_strength = initial_acceleration; +Player::Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, sf::Color inside_color, sf::Color border_color) : Entity(size, pos, velocity, border_thickness, inside_color, border_color) { + m_acceleration = gravity; + m_jump_strength = max_acceleration; } void Player::handle_movment() { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { - rect.move({velocity.x, 0}); + rect.move({_velocity.x, 0}); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { - rect.move({-velocity.x, 0}); + rect.move({-_velocity.x, 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; - velocity.y = -m_jump_strength; // push up + _velocity.y = -m_jump_strength; // push up } } diff --git a/src/main.cpp b/src/main.cpp index 5039b2f..9db89e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,14 +8,11 @@ #include #include #include -#include -#include #include "globals.hpp" #include "elements.hpp" #include "maps.hpp" int main() { - // std::vector wall_list = {}; Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; struct { sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; @@ -54,6 +51,7 @@ int main() { } } } + for(int i = 0; i < MAX_ROWS; i++) { for(int j = 0; j < MAX_COLS; j++) { if(wall_list[i][j] != nullptr) { @@ -63,9 +61,9 @@ int main() { } Player player { - player_data.size, player_data.pos, player_data.acceleration, - player_data.max_velocity_y, player_data.border_size, player_data.velocity, - sf::Color::Black + player_data.size, player_data.pos, player_data.velocity, + player_data.border_size, player_data.acceleration, player_data.max_velocity_y, + sf::Color::Black, sf::Color::White }; Entity line { line_data.size, line_data.pos, line_data.velocity, line_data.border_size, sf::Color::White }; @@ -87,29 +85,29 @@ int main() { // FOR TESTING if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { if(test_square.rect.getPosition().x < 30) { - test_square.velocity = { 20, 0 }; + test_square.set_velocity({ 20, 0 }); } if(test_square.rect.getPosition().x > WIDTH - 30) { - test_square.velocity = { -20, 0 }; + test_square.set_velocity({ -20, 0 }); } } player.handle_movment(); - player.velocity.y += player.m_acceleration; - player.rect.move({0, player.velocity.y}); + player.set_velocity_y(player.get_velocity().y + player.m_acceleration); + player.rect.move({0, player.get_velocity().y}); - test_square.velocity.y++; - if(test_square.velocity.x > 0) { - test_square.velocity.x--; + test_square.set_velocity_y(player.get_velocity().y + 1); + if(test_square.get_velocity().x > 0) { + test_square.set_velocity_x(test_square.get_velocity().x - 1); } - if(test_square.velocity.x < 0) { - test_square.velocity.x++; + if(test_square.get_velocity().x < 0) { + test_square.set_velocity_x(test_square.get_velocity().x + 1); } - test_square.rect.move({ test_square.velocity.x, test_square.velocity.y }); + test_square.rect.move({ test_square.get_velocity().x, test_square.get_velocity().y }); float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().y - line_data.border_size - player_data.border_size; if (player.rect.getPosition().y >= groundY) { player.rect.setPosition({player.rect.getPosition().x, groundY}); - player.velocity.y = 0; + player.set_velocity_y(0); player.m_jumping = false; } float right_border = WIDTH - player.rect.getSize().x; @@ -124,11 +122,11 @@ int main() { float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - test_square.rect.getSize().y - line_data.border_size - simulated_data.border_size; if(test_square.rect.getPosition().x > right_border_rect) { test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y }); - test_square.velocity = { -20, 0 }; + test_square.set_velocity({ -20, 0 }); } if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) { test_square.rect.setPosition({ 0, test_square.rect.getPosition().y }); - test_square.velocity = { 20, 0 }; + test_square.set_velocity({ 20, 0 }); } if(test_square.rect.getPosition().y >= groundY_rect) { test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect }); @@ -137,39 +135,41 @@ int main() { auto collision = player.find_intersection(test_square); switch (collision) { case CollisionDir::Top: - test_square.velocity = { 0, -20 }; + test_square.set_velocity({ 0, -20 }); break; case CollisionDir::Bottom: - test_square.velocity = { 0, -20 }; + test_square.set_velocity({ 0, -20 }); break; case CollisionDir::Left: - test_square.velocity = { -20, 0 }; + test_square.set_velocity({ -20, 0 }); break; case CollisionDir::Right: - test_square.velocity = { 20, 0 }; + test_square.set_velocity({ 20, 0 }); break; default: break; } // for(int i = 0; i < MAX_ROWS; i++) { // for(int j = 0; j < MAX_COLS; j++) { - // auto collision = player.find_intersection(*wall_list[i][j]); + // if(wall_list[i][j] == nullptr) continue; + // auto collision = wall_list[i][j]->find_intersection(player); // switch (collision) { // case CollisionDir::Top: - // player.velocity = { player.velocity.x, -player.velocity.y }; + // player.velocity = { player.velocity.x, 0 }; + // player.velocity.y -= player.m_acceleration; // break; // case CollisionDir::Bottom: - // player.velocity = { player.velocity.x, -player.velocity.y }; - // break; - // case CollisionDir::Left: - // player.velocity = { player.velocity.x, 0 }; - // break; - // case CollisionDir::Right: - // player.velocity = { player.velocity.x, 0 }; - // break; - // default: break; - // } - // - // } + // player.velocity = { player.velocity.x, 0 }; + // break; + // case CollisionDir::Left: + // player.velocity = { player.velocity.x, 0 }; + // break; + // case CollisionDir::Right: + // player.velocity = { player.velocity.x, 0 }; + // break; + // default: break; + // } + // + // } // } for(int i = 0; i < MAX_ROWS; i++) {