diff --git a/include/maps.hpp b/include/maps.hpp index d6f854e..1b5a7be 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -2,7 +2,9 @@ #define MAPS_HPP #include "globals.hpp" +#include "elements.hpp" extern int map_data[MAX_ROWS][MAX_COLS]; +void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]); #endif diff --git a/src/elements.cpp b/src/elements.cpp index 83efd8f..6f4566d 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -56,10 +56,9 @@ CollisionDir Entity::find_intersection(Entity &entity) { ? 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 + ? CollisionDir::Bottom + : CollisionDir::Top; } } @@ -153,16 +152,14 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO return false; } -bool Player::get_vertical_bottom_collision_by_offset(Entity *wall_list[MAX_ROWS][MAX_COLS]) { -Entity bounding_box { { rect.getSize() }, { rect.getPosition().x, rect.getPosition().y + get_velocity().y }, { get_velocity() }, rect.getOutlineThickness() }; - +bool Player::get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) { #ifdef __APPLE__ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { #else for(auto [i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { #endif if(wall_list[i][j] == nullptr) continue; - auto collision = bounding_box.find_intersection(*wall_list[i][j]); + auto collision = find_intersection(*wall_list[i][j]); if(collision == CollisionDir::None) continue; if(collision == CollisionDir::Bottom && get_velocity().y > 0) { rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y }); @@ -173,25 +170,3 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO } return false; } - -bool Player::get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) { - -if(!get_vertical_bottom_collision_by_offset(wall_list)) { -#ifdef __APPLE__ - for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { - #else - for(auto [i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { - #endif - if(wall_list[i][j] == nullptr) continue; - auto collision = find_intersection(*wall_list[i][j]); - if(collision == CollisionDir::None) continue; - if(collision == CollisionDir::Bottom && get_velocity().y > 0) { - rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y }); - m_jumping = false; - set_velocity_y(0); - return true; - } - } - } - return false; -} diff --git a/src/main.cpp b/src/main.cpp index ea06ec4..5883444 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ int main() { sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; sf::Vector2 pos { TILESIZE, TILESIZE }; float acceleration { 1.0f }; - float max_velocity_y { 20.0f }; + float max_velocity_y { 15.0f }; float border_size { -1.0f }; float dash_distance { 20.0f }; sf::Vector2f velocity { 4.0f, 0 }; @@ -53,6 +53,8 @@ int main() { wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }; } + merged_walls(wall_list); + for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { if(wall_list[i][j] == nullptr) continue; wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); @@ -106,6 +108,9 @@ int main() { // } player.handle_gravity(); + if(player.get_velocity().y > ((float)TILESIZE / 2)) { + player.set_velocity_y(TILESIZE / 2); + } if(!player.get_vertical_bottom_collision(wall_list)) { player.get_vertical_top_collision(wall_list); } diff --git a/src/maps.cpp b/src/maps.cpp index bdb7673..6f219f3 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,4 +1,50 @@ #include "maps.hpp" +#include "globals.hpp" +#ifdef __APPLE__ +#include +#include +#else +#include +#endif + +void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) { +#ifdef __APPLE__ +for(auto[j, i] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { + #else + for(auto[j, i] : std::views::cartesian_product(std::views::iota(0, MAX_COLS), std::views::iota(0, MAX_ROWS))) { + #endif + if(wall_list[i][j] == nullptr) continue; + if(wall_list[i][j]->rect.getSize().x != TILESIZE) continue; + int start = i, end = i; + + while(end + 1 < MAX_ROWS && wall_list[end + 1][j] != nullptr) { + delete wall_list[end + 1][j]; + wall_list[end + 1][j] = nullptr; + end++; + } + int merged_height = (end - start + 1) * TILESIZE; + wall_list[start][j]->rect.setSize({ wall_list[start][j]->rect.getSize().x, static_cast(merged_height) }); + i = end; + } +#ifdef __APPLE__ +for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { + #else + for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { + #endif + if(wall_list[i][j] == nullptr) continue; + if(wall_list[i][j]->rect.getSize().y != TILESIZE) continue; + int start = j, end = j; + + while(end + 1 < MAX_COLS && wall_list[i][end + 1] != nullptr && wall_list[i][end + 1]->rect.getSize().y == TILESIZE) { + delete wall_list[i][end + 1]; + wall_list[i][end + 1] = nullptr; + end++; + } + int merged_width = (end - start + 1) * TILESIZE; + wall_list[i][start]->rect.setSize({ static_cast(merged_width), wall_list[i][start]->rect.getSize().y }); + j = end; + } +} int map_data[MAX_ROWS][MAX_COLS] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },