wall merging

This commit is contained in:
DarthMooMancer
2025-10-26 00:20:21 -04:00
parent b43f9972d7
commit 5e5f712a84
4 changed files with 58 additions and 30 deletions
+2
View File
@@ -2,7 +2,9 @@
#define MAPS_HPP #define MAPS_HPP
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp"
extern int map_data[MAX_ROWS][MAX_COLS]; extern int map_data[MAX_ROWS][MAX_COLS];
void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]);
#endif #endif
+2 -27
View File
@@ -56,10 +56,9 @@ CollisionDir Entity::find_intersection(Entity &entity) {
? CollisionDir::Right // a hit b from the left side ? CollisionDir::Right // a hit b from the left side
: CollisionDir::Left; // a hit b from the right side : CollisionDir::Left; // a hit b from the right side
} else { } else {
// Vertical collision
return (a.position.y < b.position.y) return (a.position.y < b.position.y)
? CollisionDir::Bottom // a hit b from above ? CollisionDir::Bottom
: CollisionDir::Top; // a hit b from below : CollisionDir::Top;
} }
} }
@@ -153,30 +152,7 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO
return false; 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() };
#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]);
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;
}
bool Player::get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) { bool Player::get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
if(!get_vertical_bottom_collision_by_offset(wall_list)) {
#ifdef __APPLE__ #ifdef __APPLE__
for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
#else #else
@@ -192,6 +168,5 @@ if(!get_vertical_bottom_collision_by_offset(wall_list)) {
return true; return true;
} }
} }
}
return false; return false;
} }
+6 -1
View File
@@ -23,7 +23,7 @@ int main() {
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f }; sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
sf::Vector2<float> pos { TILESIZE, TILESIZE }; sf::Vector2<float> pos { TILESIZE, TILESIZE };
float acceleration { 1.0f }; float acceleration { 1.0f };
float max_velocity_y { 20.0f }; float max_velocity_y { 15.0f };
float border_size { -1.0f }; float border_size { -1.0f };
float dash_distance { 20.0f }; float dash_distance { 20.0f };
sf::Vector2f velocity { 4.0f, 0 }; 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 }; 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))) { 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; if(wall_list[i][j] == nullptr) continue;
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); 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(); 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)) { if(!player.get_vertical_bottom_collision(wall_list)) {
player.get_vertical_top_collision(wall_list); player.get_vertical_top_collision(wall_list);
} }
+46
View File
@@ -1,4 +1,50 @@
#include "maps.hpp" #include "maps.hpp"
#include "globals.hpp"
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
#else
#include <ranges>
#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<float>(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<float>(merged_width), wall_list[i][start]->rect.getSize().y });
j = end;
}
}
int map_data[MAX_ROWS][MAX_COLS] = { 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 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },