remade the collision system

This commit is contained in:
Andrew Haynes Macbook
2026-01-25 21:58:14 -05:00
parent 3159efb257
commit ef6a5bf507
6 changed files with 199 additions and 390 deletions
+75 -153
View File
@@ -5,7 +5,7 @@
#include "globals.hpp"
std::vector<ObjectData> object_data {
const std::vector<ObjectData> object_data {
{},
{
.interior_color = sf::Color::Transparent,
@@ -25,11 +25,22 @@ std::vector<ObjectData> object_data {
return e;
}(),
*/
std::vector<EntityData> entity_data {
const std::vector<EntityData> entity_data {
{},
};
std::vector<TimerData> 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<TimerData> timer_data {
{},
{
.name = "Dash",
@@ -41,154 +52,65 @@ std::vector<TimerData> 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<LevelData> 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<LevelData> 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",
}},
};
+69 -172
View File
@@ -68,116 +68,6 @@ Entity::get_velocity()
return _velocity;
}
float
Object::find_intersection(Object& entity)
{
std::optional<sf::FloatRect> 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>(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<std::array<std::unique_ptr<Object>, 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<std::array<std::unique_ptr<Object>, 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<std::array<std::unique_ptr<Object>, 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<std::array<std::unique_ptr<Object>, 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<std::array<std::unique_ptr<Object>, 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<std::array<std::unique_ptr<Object>, 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(
+15 -19
View File
@@ -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();
}
+23 -15
View File
@@ -66,34 +66,38 @@ Manager::draw(sf::RenderWindow& window)
void
Manager::load(LevelData level)
{
const std::array<std::array<char, MAX_COLS>, 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>(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>(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>(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>(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>(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 });
}
}
}