diff --git a/include/elements.hpp b/include/elements.hpp index e0f32cf..da83819 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,36 +1,34 @@ #ifndef PLAYER_HPP #define PLAYER_HPP +#include #include #include + +enum class CollisionDir { None, Left, Right, Top, Bottom }; class Entity { public: - Entity(sf::Vector2 size, sf::Vector2 starting_position, float thickness, sf::Color inside_color, sf::Color border_color); - bool find_intersection(Entity &entity); + 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); + CollisionDir find_intersection(Entity &entity); + sf::Vector2f velocity; sf::RectangleShape rect; protected: - sf::Color inside_color; - sf::Color border_color; - float top_left; - float bottom_left; - float top_right; - float bottom_right; - float border_thickness; + sf::Color _inside_color; + sf::Color _border_color; + float _border_thickness; }; class Player : public Entity { public: - Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x, sf::Color inside_color, sf::Color border_color); - sf::Vector2 m_velocity; + 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); float m_acceleration; float m_jump_strength; bool m_jumping; void handle_movment(); sf::Vector2 get_velocity(); bool get_jumping(); - }; #endif diff --git a/include/globals.hpp b/include/globals.hpp index b96b3a0..6b852ac 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,5 +1,5 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP -#define WIDTH 600 -#define HEIGHT 400 +#define WIDTH 608 +#define HEIGHT 416 #endif diff --git a/src/elements.cpp b/src/elements.cpp index 5eaead7..d3e2705 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,4 +1,6 @@ #include "elements.hpp" +#include +#include #include #include @@ -13,26 +15,54 @@ * */ -Entity::Entity(sf::Vector2 size, sf::Vector2 starting_position, float thickness, sf::Color inside_color, sf::Color border_color) { +// 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) { 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.setFillColor(inside_color); rect.setOutlineColor(border_color); - + velocity = initial_velocity; } -Player::Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x, sf::Color inside_color, sf::Color border_color) : Entity(size, starting_position, thickness, inside_color, border_color) { +CollisionDir Entity::find_intersection(Entity &entity) { + sf::FloatRect a = rect.getGlobalBounds(); + sf::FloatRect b = entity.rect.getGlobalBounds(); + auto inter = a.findIntersection(b); + if (!inter) { + return CollisionDir::None; + } + + const auto overlap = *inter; + + float overlapW = overlap.size.x; + float overlapH = overlap.size.y; + + // Compare which axis overlap is smaller + if (overlapW < overlapH) { + // Horizontal collision + return (a.position.x < b.position.x) + ? CollisionDir::Right // a hit b from the left side + : CollisionDir::Left; // a hit b from the right side + } else { + // Vertical collision + return (a.position.y < b.position.y) + ? CollisionDir::Bottom // a hit b from above + : CollisionDir::Top; // a hit b from below + } +} + +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; - m_velocity.x = velocity_x; } void Player::handle_movment() { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { - rect.move({m_velocity.x, 0}); + rect.move({velocity.x, 0}); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { - rect.move({-m_velocity.x, 0}); + rect.move({-velocity.x, 0}); } // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { // rect.move({0, -2.5}); @@ -41,7 +71,7 @@ void Player::handle_movment() { // } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { m_jumping = true; - m_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 5336814..6572177 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,39 +8,54 @@ #include #include #include +#include #include "globals.hpp" #include "elements.hpp" int main() { + int map = [[]]; + struct { + sf::Vector2 size { 16.0f, 16.0f }; + sf::Vector2 pos { 40, 40 }; + float acceleration { 1.0f }; + float max_velocity_y { 20.0f }; + float border_size { 1.0f }; + sf::Vector2f velocity { 4.0f, 0 }; + } player_data; + + struct { + sf::Vector2f size { 32.0f, 32.0f }; + sf::Vector2f pos { 0, 0 }; + sf::Vector2f velocity { 0, 0 }; + float border_size { 2.0f }; + } tile_data; + + struct { + + } simulated_object_data; + std::vector wall_list = {}; + for(int i = 0; i < 19; i++) { + wall_list.push_back(Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }); + } + for(int i = 0; i < wall_list.size(); i++) { + wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size }); + } float floor_height = 2.0f; sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default); window.setFramerateLimit(60); - struct { - sf::Vector2 size = { 16.0f, 16.0f }; - sf::Vector2 pos = { 40, 40 }; - float acceleration = 1.0f; - float max_velocity_y = 20.0f; - float border_size = 2.0f; - float velocity_x = 4.0f; - } player_data; Player player { player_data.size, player_data.pos, player_data.acceleration, - player_data.max_velocity_y, player_data.border_size, player_data.velocity_x, - sf::Color::Black, sf::Color::White + player_data.max_velocity_y, player_data.border_size, player_data.velocity, + sf::Color::Black }; - sf::RectangleShape line({ WIDTH, floor_height }); - line.setPosition({ 0, HEIGHT - 50}); + Entity line { { WIDTH, floor_height }, { 0, HEIGHT - 50 }, { 0, 0 }, 1.0f, sf::Color::White }; + // sf::RectangleShape line({ WIDTH, floor_height }); + // line.setPosition({ 0, HEIGHT - 50}); - sf::RectangleShape rect({ 4, 4 }); - rect.setFillColor(sf::Color::Black); - rect.setOutlineColor(sf::Color::White); - rect.setOutlineThickness(2.0f); - rect.setPosition({ 40, 40 }); - // float vel_x = 3.0f; - float vel_y = -20.0f; + Entity test_square { { 8, 8 }, { 100, 40 }, { 0, 0 }, 1.0f, sf::Color::Black }; while (window.isOpen()) { window.clear(sf::Color::Black); @@ -48,23 +63,35 @@ int main() { if (event->is()) window.close(); } - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { - vel_y = -10; - } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { window.close(); } + // FOR TESTING + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { + if(test_square.rect.getPosition().x < 30) { + test_square.velocity = { 20, 0 }; + } + if(test_square.rect.getPosition().x > WIDTH - 30) { + test_square.velocity = { -20, 0 }; + } + } player.handle_movment(); - player.m_velocity.y += player.m_acceleration; // always apply gravity - player.rect.move({0, player.m_velocity.y}); + player.velocity.y += player.m_acceleration; + player.rect.move({0, player.velocity.y}); - vel_y += 1.0f; - rect.move({ 0, vel_y }); + test_square.velocity.y++; + if(test_square.velocity.x > 0) { + test_square.velocity.x--; + } + if(test_square.velocity.x < 0) { + test_square.velocity.x++; + } + test_square.rect.move({ test_square.velocity.x, test_square.velocity.y }); - float groundY = line.getPosition().y - floor_height - player.rect.getSize().x; + float groundY = line.rect.getPosition().y - floor_height - player.rect.getSize().x; if (player.rect.getPosition().y >= groundY) { player.rect.setPosition({player.rect.getPosition().x, groundY}); - player.m_velocity.y = 0; + player.velocity.y = 0; player.m_jumping = false; } float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness(); @@ -74,47 +101,44 @@ int main() { if(player.rect.getPosition().x < player.rect.getOutlineThickness()) { player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y}); } - // if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) { - // vel_y = -20.0f; - // // vel_y = -20.0f; - // } - // if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) { - // if() + float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness(); + float groundY_rect = line.rect.getPosition().y - floor_height - (test_square.rect.getSize().y); + 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 }; + } + if(test_square.rect.getPosition().x < test_square.rect.getOutlineThickness()) { + test_square.rect.setPosition({ test_square.rect.getOutlineThickness(), test_square.rect.getPosition().y }); + test_square.velocity = { 20, 0 }; + } + if(test_square.rect.getPosition().y >= groundY_rect) { + test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect }); + } + auto collision = player.find_intersection(test_square); - float groundY_rect = line.getPosition().y - floor_height - (rect.getSize().y); - if(rect.getPosition().x > right_border) { - rect.setPosition({ right_border, rect.getPosition().y }); + switch (collision) { + case CollisionDir::Top: + test_square.velocity = { 0, -20 }; + break; + case CollisionDir::Bottom: + test_square.velocity = { 0, -20 }; + break; + case CollisionDir::Left: + test_square.velocity = { -20, 0 }; + break; + case CollisionDir::Right: + test_square.velocity = { 20, 0 }; + break; + default: break; } - if(rect.getPosition().x < rect.getPosition().x + rect.getOutlineThickness()) { - rect.setPosition({ rect.getOutlineThickness(), rect.getPosition().y }); + + for(int i = 0; i < wall_list.size(); i++) { + window.draw(wall_list[i].rect); } - if(rect.getPosition().y >= groundY_rect) { - rect.setPosition({ rect.getPosition().x, groundY_rect }); - vel_y = 0; - } - // - // auto playerBounds = player.rect.getGlobalBounds(); - // auto rectBounds = rect.getGlobalBounds(); - // - // // Create debug rectangles - // sf::RectangleShape playerBox(playerBounds.size); - // playerBox.setPosition(playerBounds.position); - // playerBox.setFillColor(sf::Color::Transparent); - // playerBox.setOutlineColor(sf::Color::Red); - // playerBox.setOutlineThickness(1.0f); - // - // sf::RectangleShape rectBox(rectBounds.size); - // rectBox.setPosition(rectBounds.position); - // rectBox.setFillColor(sf::Color::Transparent); - // rectBox.setOutlineColor(sf::Color::Red); - // rectBox.setOutlineThickness(1.0f); - // - // window.draw(playerBox); - // window.draw(rectBox); - window.draw(rect); window.draw(player.rect); - window.draw(line); + window.draw(test_square.rect); + window.draw(line.rect); window.display(); } }