From ef6a5bf50761e8479948196d55a92b0d32ffac45 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Macbook Date: Sun, 25 Jan 2026 21:58:14 -0500 Subject: [PATCH] remade the collision system --- include/data.hpp | 16 +-- include/elements.hpp | 32 ++---- src/data.cpp | 228 ++++++++++++++-------------------------- src/elements.cpp | 241 +++++++++++++------------------------------ src/main.cpp | 34 +++--- src/maps.cpp | 38 ++++--- 6 files changed, 199 insertions(+), 390 deletions(-) diff --git a/include/data.hpp b/include/data.hpp index 7b28f39..c11f30d 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -9,8 +9,7 @@ #define EMPTY 0 #define DEFAULT 1 #define PLATFORM1 2 -#define CHECKERED 3 -#define BASE 4 +#define BASE 3 #include #include @@ -42,7 +41,8 @@ struct PlayerData : EntityData struct LevelData { - std::array, MAX_ROWS> data {}; + // std::array, MAX_ROWS> data {}; + const char data[MAX_ROWS][MAX_COLS + 1]; }; struct TimerData @@ -51,10 +51,10 @@ struct TimerData float max_time { 1.f }; }; -extern std::vector object_data; -extern std::vector entity_data; -extern std::vector level_data; -extern std::vector timer_data; -extern PlayerData player_data; +extern const std::vector object_data; +extern const std::vector entity_data; +extern const std::vector level_data; +extern const std::vector timer_data; +extern const PlayerData player_data; #endif diff --git a/include/elements.hpp b/include/elements.hpp index 6a01fbd..98cc0a1 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -3,22 +3,16 @@ #include #include +#include #include #include #include +#include #include "data.hpp" #include "globals.hpp" #include "scheduler.hpp" -enum class CollisionDir -{ - None, - Left, - Right, - Top, - Bottom -}; class Entity; class Object; class Player; @@ -27,9 +21,6 @@ class Object { public: Object(ObjectData data); - // CollisionDir find_intersection(Object& entity); - float find_horizontal_difference_from_interesection(Object& entity); - float find_intersection(Object &entity); sf::RectangleShape rect; }; @@ -53,30 +44,25 @@ class Player : public Entity : Entity(data), max_dash_acceleration(data.max_vertical_acceleration), max_vertical_acceleration(data.max_vertical_acceleration), - gravity(gravity), _min_velocity(data.velocity) {}; + gravity(gravity) {}; float max_vertical_acceleration; float max_dash_acceleration; float gravity; bool m_jumping { false }; - std::unique_ptr bounding_box; - void initialize_bounding_box(); - void update_bounding_box(); - int check_bounding_box( - std::array, MAX_COLS>, MAX_ROWS>& tilemap); - void handle_movment(Timer& dash, std::array, 19>, 13> &tilemap); + void handle_movment(Timer& dash, + std::array, 19>, 13>& tilemap); void handle_gravity(); bool get_jumping(); void horizontal_no_out_of_bounds(); void vertical_no_out_of_bounds(); - // bool get_horizontal_collision( - // std::array, MAX_COLS>, MAX_ROWS>& tilemap); - // bool get_vertical_top_collision( - // std::array, MAX_COLS>, MAX_ROWS>& tilemap); + bool get_horizontal_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap); + bool get_vertical_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap); // bool get_vertical_bottom_collision( // std::array, MAX_COLS>, MAX_ROWS>& tilemap); private: - sf::Vector2f _min_velocity { 0, 0 }; std::string direction; }; diff --git a/src/data.cpp b/src/data.cpp index d1e50dd..1a6934a 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -5,7 +5,7 @@ #include "globals.hpp" -std::vector object_data { +const std::vector object_data { {}, { .interior_color = sf::Color::Transparent, @@ -25,11 +25,22 @@ std::vector object_data { return e; }(), */ -std::vector entity_data { +const std::vector entity_data { {}, }; -std::vector timer_data { +const PlayerData player_data = [] +{ + PlayerData data; + data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f }; + data.edge = -1.0f; + data.velocity = { 3.0f, 0.0f }; + data.max_dash_acceleration = 30.0f; // 15 + data.max_vertical_acceleration = 12.5f; // 12.5 default + return data; +}(); + +const std::vector timer_data { {}, { .name = "Dash", @@ -41,154 +52,65 @@ std::vector timer_data { }, }; -PlayerData player_data = [] -{ - PlayerData data; - data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f }; - data.edge = -1.0f; - data.velocity = { 3.0f, 0.0f }; - data.max_dash_acceleration = 30.0f; // 15 - data.max_vertical_acceleration = 12.5f; - return data; -}(); - -std::vector level_data = { - { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' } } } }, - { - .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', - ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', - ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '-', '-', '-', '2' } } }, - }, - { - .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', - ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', - ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '-', '-', '-', '2' } } }, - }, - { - .data = { { { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', - ' ', '0', ' ', '0' } } }, - }, - { - .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '-', '-', '-', '2' } } }, - }, +const std::vector level_data = { + { .data = { + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + }}, + { .data = { + "1-----------------2", + "1---2 1", + "| |", + "| |", + "|12 |", + "| 1----2 |", + "| |", + "| |", + "| |", + "| |", + "| 1 1 |", + "2 20 2 2", + "1-----------------2", + }}, + { .data = { + "1-----------------2", + "1---2 1", + "| |", + "| |", + "|1----2 |", + "| 1----2 |", + "| |", + "| 0 |", + "| 0 |", + "| |", + "| 1 1 |", + "2 20 2 2", + "1-----------------2", + }}, + { .data = { + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " 0 ", + " ", + " 0 0", + "1-----------------2", + }}, }; diff --git a/src/elements.cpp b/src/elements.cpp index 91a0f16..259abe3 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -68,116 +68,6 @@ Entity::get_velocity() return _velocity; } -float -Object::find_intersection(Object& entity) -{ - std::optional intersection = - rect.getGlobalBounds().findIntersection(entity.rect.getGlobalBounds()); - return intersection->size.x; - // return false; -} - -// CollisionDir -// Object::find_intersection(Object& 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; -// -// 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 -// { -// return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top; -// } -// } - -// float -// Object::find_horizontal_difference_from_interesection(Object& entity) -// { -// sf::FloatRect a = rect.getGlobalBounds(); -// sf::FloatRect b = entity.rect.getGlobalBounds(); -// auto inter = a.findIntersection(b); -// if (!inter) -// { -// return 0; -// } -// -// const auto overlap = *inter; -// -// return (a.position.x < b.position.x) -// ? b.position.x - a.position.x // a hit b from the left side -// : a.position.x - b.position.x; // a hit b from the right side -// } - -// -// void -// Player::initialize_bounding_box() -// { -// bounding_box = std::make_unique(object_data[1]); -// float length = total_distance(max_dash_acceleration); -// if (direction == "left") -// { -// bounding_box->rect.setSize({ -length, rect.getSize().y }); -// } -// else -// { -// bounding_box->rect.setSize({ length, rect.getSize().y }); -// } -// } -// -// void -// Player::update_bounding_box() -// { -// bounding_box->rect.setPosition({ rect.getPosition().x, rect.getPosition().y }); -// } -// -// int -// Player::check_bounding_box( -// std::array, MAX_COLS>, MAX_ROWS>& tilemap) -// { -// int distance = 0; -// float max_dist = total_distance(player_data.max_dash_acceleration); -// float min_dist = max_dist; -// -// for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) -// { -// if (tilemap[i][j] == nullptr) continue; -// float current_dist = -// bounding_box->find_horizontal_difference_from_interesection(*tilemap[i][j]); -// -// if (current_dist > 0 && current_dist < min_dist) -// { -// min_dist = current_dist; -// } -// } -// float final_dist = min_dist; -// if (direction == "left") -// { -// bounding_box->rect.setSize({ -final_dist, bounding_box->rect.getSize().y }); -// } -// else -// { -// bounding_box->rect.setSize({ final_dist, bounding_box->rect.getSize().y }); -// } -// max_dash_acceleration = distance_to_acceleration(final_dist); -// return final_dist; -// } - void Player::handle_movment(Timer& dash, std::array, 19>, 13>& tilemap) @@ -192,18 +82,11 @@ Player::handle_movment(Timer& dash, direction = "left"; rect.move({ -_velocity.x, 0 }); } - // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0) - // { - // // Dash order: spawn hitbox, check distance and set max_dash_acceleration - // // Outside of this set max_dash_acceleration back to 15 { default } - // max_dash_acceleration = player_data.max_dash_acceleration; - // initialize_bounding_box(); - // update_bounding_box(); - // std::cout << check_bounding_box(tilemap) << "\n"; - // _velocity.x = max_dash_acceleration; - // // _velocity.x = 15; - // dash.reset(); - // } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0) + { + _velocity.x = max_dash_acceleration; + dash.reset(); + } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) { m_jumping = true; @@ -218,10 +101,6 @@ Player::handle_gravity() { set_velocity_x(get_velocity().x - gravity); } - if (get_velocity().x <= player_data.velocity.x) - { - bounding_box.reset(); - } set_velocity_y(get_velocity().y + gravity); rect.move({ 0, get_velocity().y }); @@ -252,52 +131,70 @@ Player::vertical_no_out_of_bounds() } } -// bool -// Player::get_horizontal_collision( -// std::array, MAX_COLS>, MAX_ROWS>& tilemap) -// { -// for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) -// { -// if (tilemap[i][j] == nullptr) continue; -// auto collision = tilemap[i][j]->find_intersection(*this); -// if (collision == CollisionDir::None) continue; -// if (collision == CollisionDir::Left) -// { -// rect.setPosition({ tilemap[i][j]->rect.getPosition().x - rect.getSize().x, -// rect.getPosition().y }); -// return true; -// } -// if (collision == CollisionDir::Right) -// { -// rect.setPosition( -// { tilemap[i][j]->rect.getPosition().x + tilemap[i][j]->rect.getSize().x, -// rect.getPosition().y }); -// return true; -// } -// } -// return false; -// } -// -// bool -// Player::get_vertical_top_collision( -// std::array, MAX_COLS>, MAX_ROWS>& tilemap) -// { -// for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) -// { -// if (tilemap[i][j] == nullptr) continue; -// auto collision = find_intersection(*tilemap[i][j]); -// if (collision == CollisionDir::None) continue; -// if (collision == CollisionDir::Top) -// { -// rect.setPosition( -// { rect.getPosition().x, tilemap[i][j]->rect.getSize().y + -// tilemap[i][j]->rect.getPosition().y }); -// set_velocity_y(0); -// return true; -// } -// } -// return false; -// } +bool +Player::get_horizontal_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + sf::FloatRect self = rect.getGlobalBounds(); + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + sf::FloatRect tile = tilemap[i][j]->rect.getGlobalBounds(); + auto intersection = self.findIntersection(tile); + if (!intersection) continue; + if (intersection->size.x < intersection->size.y) + { + if (self.position.x < tile.position.x) + { + rect.setPosition( + { tile.position.x - rect.getSize().x, rect.getPosition().y }); + return true; + } + else + { + rect.setPosition( + { tile.position.x + tilemap[i][j]->rect.getSize().x, + rect.getPosition().y }); + return true; + } + } + } + return false; +} + +bool +Player::get_vertical_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + sf::FloatRect self = rect.getGlobalBounds(); + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + sf::FloatRect tile = tilemap[i][j]->rect.getGlobalBounds(); + auto intersection = self.findIntersection(tile); + if (!intersection) continue; + if (intersection->position.y <= 0) continue; + if (intersection->size.y < intersection->size.x || intersection->size.y - intersection->size.x < rect.getSize().x) + { + if (self.position.y < tile.position.y) + { + rect.setPosition( + { rect.getPosition().x, tile.position.y - rect.getSize().y }); + set_velocity_y(0); + m_jumping = false; + return true; + } + else + { + rect.setPosition({ rect.getPosition().x, tile.position.y + tilemap[i][j]->rect.getSize().y }); + set_velocity_y(0); + return true; + } + } + } + return false; +} + // // bool // Player::get_vertical_bottom_collision( diff --git a/src/main.cpp b/src/main.cpp index b02a8cc..2aeaeac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -69,12 +69,6 @@ main() running = false; window.close(); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::C)) - { - player.bounding_box->rect.setSize( - { total_distance(player.max_dash_acceleration), - player.bounding_box->rect.getSize().y }); - } if (map_manager.get_active() >= level_data.size()) { @@ -89,10 +83,12 @@ main() scheduler.timers.at(1).reset(); } - player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")), map_manager.tilemap); - // player.get_horizontal_collision(map_manager.tilemap); + player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")), + map_manager.tilemap); + std::cout << player.get_horizontal_collision(map_manager.tilemap) << "\n"; player.handle_gravity(); + player.get_vertical_collision(map_manager.tilemap); player.vertical_no_out_of_bounds(); // if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) // { @@ -108,20 +104,20 @@ main() // player.update_bounding_box(); // // player.check_bounding_box(map_manager.tilemap); // } - for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { - if(map_manager.tilemap[i][j]) { - std::cout << player.find_intersection(*map_manager.tilemap[i][j]); - window.close(); - } - } + // for (auto [i, j] : + // rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + // { + // if (map_manager.tilemap[i][j] == nullptr) continue; + // if(player.find_intersection(*map_manager.tilemap[i][j]).x > 0) { + // std::cout << player.find_intersection(*map_manager.tilemap[i][j]).x << "\n"; + // // running = false; + // // window.close(); + // } + // } map_manager.draw(window); window.draw(player.rect); - // if (player.bounding_box != nullptr) - // { - // window.draw(player.bounding_box->rect); - // } - // window.draw(text); + window.draw(text); window.display(); } diff --git a/src/maps.cpp b/src/maps.cpp index 22a9bfb..24f5b1e 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -66,34 +66,38 @@ Manager::draw(sf::RenderWindow& window) void Manager::load(LevelData level) { - const std::array, MAX_ROWS> map_data = level.data; + const char (*map_data)[MAX_COLS + 1] = level.data; for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { if (map_data[i][j] == ' ') continue; if (map_data[i][j] == '0') { tilemap[i][j] = std::make_unique(object_data[0]); - // tilemap[i][j] = new Object { object_data[0] }; tilemap[i][j]->rect.setPosition( - { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); + { tilemap[i][j]->rect.getPosition().x * j, + tilemap[i][j]->rect.getPosition().y * i }); } if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '2') { int start = j, end = j + 1; tilemap[i][j] = std::make_unique(object_data[0]); - tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1), - tilemap[i][j]->rect.getSize().y }); + tilemap[i][j]->rect.setSize( + { tilemap[i][j]->rect.getSize().x * float(end - start + 1), + tilemap[i][j]->rect.getSize().y }); tilemap[i][j]->rect.setPosition( - { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); + { tilemap[i][j]->rect.getPosition().x * j, + tilemap[i][j]->rect.getPosition().y * i }); } if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '2') { int start = i, end = i + 1; tilemap[i][j] = std::make_unique(object_data[0]); - tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x, - tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); + tilemap[i][j]->rect.setSize( + { tilemap[i][j]->rect.getSize().x, + tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); tilemap[i][j]->rect.setPosition( - { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); + { tilemap[i][j]->rect.getPosition().x * j, + tilemap[i][j]->rect.getPosition().y * i }); } if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '-') { @@ -101,10 +105,12 @@ Manager::load(LevelData level) while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++; if (map_data[i][end + 1] == '2') end++; tilemap[i][j] = std::make_unique(object_data[0]); - tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1), - tilemap[i][j]->rect.getSize().y }); + tilemap[i][j]->rect.setSize( + { tilemap[i][j]->rect.getSize().x * float(end - start + 1), + tilemap[i][j]->rect.getSize().y }); tilemap[i][j]->rect.setPosition( - { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); + { tilemap[i][j]->rect.getPosition().x * j, + tilemap[i][j]->rect.getPosition().y * i }); } if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '|') { @@ -112,10 +118,12 @@ Manager::load(LevelData level) while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++; if (map_data[end + 1][j] == '2') end++; tilemap[i][j] = std::make_unique(object_data[0]); - tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x, - tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); + tilemap[i][j]->rect.setSize( + { tilemap[i][j]->rect.getSize().x, + tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); tilemap[i][j]->rect.setPosition( - { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); + { tilemap[i][j]->rect.getPosition().x * j, + tilemap[i][j]->rect.getPosition().y * i }); } } }