wall merging
This commit is contained in:
@@ -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
|
||||
|
||||
+2
-27
@@ -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,30 +152,7 @@ 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() };
|
||||
|
||||
#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]) {
|
||||
|
||||
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
|
||||
@@ -192,6 +168,5 @@ if(!get_vertical_bottom_collision_by_offset(wall_list)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+6
-1
@@ -23,7 +23,7 @@ int main() {
|
||||
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
|
||||
sf::Vector2<float> 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);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,50 @@
|
||||
#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] = {
|
||||
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
|
||||
|
||||
Reference in New Issue
Block a user