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
+8 -8
View File
@@ -9,8 +9,7 @@
#define EMPTY 0 #define EMPTY 0
#define DEFAULT 1 #define DEFAULT 1
#define PLATFORM1 2 #define PLATFORM1 2
#define CHECKERED 3 #define BASE 3
#define BASE 4
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
@@ -42,7 +41,8 @@ struct PlayerData : EntityData
struct LevelData struct LevelData
{ {
std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {}; // std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {};
const char data[MAX_ROWS][MAX_COLS + 1];
}; };
struct TimerData struct TimerData
@@ -51,10 +51,10 @@ struct TimerData
float max_time { 1.f }; float max_time { 1.f };
}; };
extern std::vector<ObjectData> object_data; extern const std::vector<ObjectData> object_data;
extern std::vector<EntityData> entity_data; extern const std::vector<EntityData> entity_data;
extern std::vector<LevelData> level_data; extern const std::vector<LevelData> level_data;
extern std::vector<TimerData> timer_data; extern const std::vector<TimerData> timer_data;
extern PlayerData player_data; extern const PlayerData player_data;
#endif #endif
+9 -23
View File
@@ -3,22 +3,16 @@
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <memory> #include <memory>
#include <optional>
#include "data.hpp" #include "data.hpp"
#include "globals.hpp" #include "globals.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
enum class CollisionDir
{
None,
Left,
Right,
Top,
Bottom
};
class Entity; class Entity;
class Object; class Object;
class Player; class Player;
@@ -27,9 +21,6 @@ class Object
{ {
public: public:
Object(ObjectData data); Object(ObjectData data);
// CollisionDir find_intersection(Object& entity);
float find_horizontal_difference_from_interesection(Object& entity);
float find_intersection(Object &entity);
sf::RectangleShape rect; sf::RectangleShape rect;
}; };
@@ -53,30 +44,25 @@ class Player : public Entity
: Entity(data), : Entity(data),
max_dash_acceleration(data.max_vertical_acceleration), max_dash_acceleration(data.max_vertical_acceleration),
max_vertical_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_vertical_acceleration;
float max_dash_acceleration; float max_dash_acceleration;
float gravity; float gravity;
bool m_jumping { false }; bool m_jumping { false };
std::unique_ptr<Object> bounding_box; void handle_movment(Timer& dash,
void initialize_bounding_box(); std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap);
void update_bounding_box();
int check_bounding_box(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
void handle_movment(Timer& dash, std::array<std::array<std::unique_ptr<Object>, 19>, 13> &tilemap);
void handle_gravity(); void handle_gravity();
bool get_jumping(); bool get_jumping();
void horizontal_no_out_of_bounds(); void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds(); void vertical_no_out_of_bounds();
// bool get_horizontal_collision( bool get_horizontal_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
// bool get_vertical_top_collision( bool get_vertical_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
// bool get_vertical_bottom_collision( // bool get_vertical_bottom_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap); // std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
private: private:
sf::Vector2f _min_velocity { 0, 0 };
std::string direction; std::string direction;
}; };
+75 -153
View File
@@ -5,7 +5,7 @@
#include "globals.hpp" #include "globals.hpp"
std::vector<ObjectData> object_data { const std::vector<ObjectData> object_data {
{}, {},
{ {
.interior_color = sf::Color::Transparent, .interior_color = sf::Color::Transparent,
@@ -25,11 +25,22 @@ std::vector<ObjectData> object_data {
return e; 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", .name = "Dash",
@@ -41,154 +52,65 @@ std::vector<TimerData> timer_data {
}, },
}; };
PlayerData player_data = [] const std::vector<LevelData> level_data = {
{ { .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",
' ', ' ', ' ', ' ' }, "| |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| |",
' ', ' ', ' ', ' ' }, "|12 |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| 1----2 |",
' ', ' ', ' ', ' ' }, "| |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| |",
' ', ' ', ' ', ' ' }, "| |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| |",
' ', ' ', ' ', ' ' }, "| 1 1 |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "2 20 2 2",
' ', ' ', ' ', ' ' }, "1-----------------2",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', }},
' ', ' ', ' ', ' ' }, { .data = {
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "1-----------------2",
' ', ' ', ' ', ' ' }, "1---2 1",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| |",
' ', ' ', ' ', ' ' }, "| |",
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "|1----2 |",
' ', ' ', ' ', ' ' } } } }, "| 1----2 |",
{ "| |",
.data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', "| 0 |",
'-', '-', '-', '2' }, "| 0 |",
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "| |",
' ', ' ', ' ', '1' }, "| 1 1 |",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "2 20 2 2",
' ', ' ', ' ', '|' }, "1-----------------2",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', }},
' ', ' ', ' ', '|' }, { .data = {
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', " ",
' ', ' ', ' ', '|' }, " ",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', " ",
' ', ' ', ' ', '|' }, " ",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', " ",
' ', ' ', ' ', '|' }, " ",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', " ",
' ', ' ', ' ', '|' }, " ",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', " ",
' ', ' ', ' ', '|' }, " 0 ",
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', " ",
' ', ' ', ' ', '|' }, " 0 0",
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', "1-----------------2",
' ', ' ', ' ', '|' }, }},
{ '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' } } },
},
}; };
+69 -172
View File
@@ -68,116 +68,6 @@ Entity::get_velocity()
return _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 void
Player::handle_movment(Timer& dash, Player::handle_movment(Timer& dash,
std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap) std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap)
@@ -192,18 +82,11 @@ Player::handle_movment(Timer& dash,
direction = "left"; direction = "left";
rect.move({ -_velocity.x, 0 }); rect.move({ -_velocity.x, 0 });
} }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0)
// { {
// // Dash order: spawn hitbox, check distance and set max_dash_acceleration _velocity.x = max_dash_acceleration;
// // Outside of this set max_dash_acceleration back to 15 { default } dash.reset();
// 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::Space) && !m_jumping && _velocity.x <= 3) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3)
{ {
m_jumping = true; m_jumping = true;
@@ -218,10 +101,6 @@ Player::handle_gravity()
{ {
set_velocity_x(get_velocity().x - 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); set_velocity_y(get_velocity().y + gravity);
rect.move({ 0, get_velocity().y }); rect.move({ 0, get_velocity().y });
@@ -252,52 +131,70 @@ Player::vertical_no_out_of_bounds()
} }
} }
// bool bool
// Player::get_horizontal_collision( Player::get_horizontal_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap) 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))) 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; {
// auto collision = tilemap[i][j]->find_intersection(*this); if (tilemap[i][j] == nullptr) continue;
// if (collision == CollisionDir::None) continue; sf::FloatRect tile = tilemap[i][j]->rect.getGlobalBounds();
// if (collision == CollisionDir::Left) auto intersection = self.findIntersection(tile);
// { if (!intersection) continue;
// rect.setPosition({ tilemap[i][j]->rect.getPosition().x - rect.getSize().x, if (intersection->size.x < intersection->size.y)
// rect.getPosition().y }); {
// return true; if (self.position.x < tile.position.x)
// } {
// if (collision == CollisionDir::Right) rect.setPosition(
// { { tile.position.x - rect.getSize().x, rect.getPosition().y });
// rect.setPosition( return true;
// { tilemap[i][j]->rect.getPosition().x + tilemap[i][j]->rect.getSize().x, }
// rect.getPosition().y }); else
// return true; {
// } rect.setPosition(
// } { tile.position.x + tilemap[i][j]->rect.getSize().x,
// return false; rect.getPosition().y });
// } return true;
// }
// bool }
// Player::get_vertical_top_collision( }
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap) return false;
// { }
// for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
// { bool
// if (tilemap[i][j] == nullptr) continue; Player::get_vertical_collision(
// auto collision = find_intersection(*tilemap[i][j]); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
// if (collision == CollisionDir::None) continue; {
// if (collision == CollisionDir::Top) sf::FloatRect self = rect.getGlobalBounds();
// { for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
// rect.setPosition( {
// { rect.getPosition().x, tilemap[i][j]->rect.getSize().y + if (tilemap[i][j] == nullptr) continue;
// tilemap[i][j]->rect.getPosition().y }); sf::FloatRect tile = tilemap[i][j]->rect.getGlobalBounds();
// set_velocity_y(0); auto intersection = self.findIntersection(tile);
// return true; 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)
// return false; {
// } 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 // bool
// Player::get_vertical_bottom_collision( // Player::get_vertical_bottom_collision(
+15 -19
View File
@@ -69,12 +69,6 @@ main()
running = false; running = false;
window.close(); 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()) if (map_manager.get_active() >= level_data.size())
{ {
@@ -89,10 +83,12 @@ main()
scheduler.timers.at(1).reset(); scheduler.timers.at(1).reset();
} }
player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")), map_manager.tilemap); player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")),
// player.get_horizontal_collision(map_manager.tilemap); map_manager.tilemap);
std::cout << player.get_horizontal_collision(map_manager.tilemap) << "\n";
player.handle_gravity(); player.handle_gravity();
player.get_vertical_collision(map_manager.tilemap);
player.vertical_no_out_of_bounds(); player.vertical_no_out_of_bounds();
// if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) // if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY))
// { // {
@@ -108,20 +104,20 @@ main()
// player.update_bounding_box(); // player.update_bounding_box();
// // player.check_bounding_box(map_manager.tilemap); // // player.check_bounding_box(map_manager.tilemap);
// } // }
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { // for (auto [i, j] :
if(map_manager.tilemap[i][j]) { // rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
std::cout << player.find_intersection(*map_manager.tilemap[i][j]); // {
window.close(); // 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); map_manager.draw(window);
window.draw(player.rect); window.draw(player.rect);
// if (player.bounding_box != nullptr) window.draw(text);
// {
// window.draw(player.bounding_box->rect);
// }
// window.draw(text);
window.display(); window.display();
} }
+23 -15
View File
@@ -66,34 +66,38 @@ Manager::draw(sf::RenderWindow& window)
void void
Manager::load(LevelData level) 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))) 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] == ' ') continue;
if (map_data[i][j] == '0') if (map_data[i][j] == '0')
{ {
tilemap[i][j] = std::make_unique<Object>(object_data[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.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') if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '2')
{ {
int start = j, end = j + 1; int start = j, end = j + 1;
tilemap[i][j] = std::make_unique<Object>(object_data[0]); 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.setSize(
tilemap[i][j]->rect.getSize().y }); { 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.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') if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '2')
{ {
int start = i, end = i + 1; int start = i, end = i + 1;
tilemap[i][j] = std::make_unique<Object>(object_data[0]); 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.setSize(
tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); { 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.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] == '-') 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++; while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++;
if (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] = 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.setSize(
tilemap[i][j]->rect.getSize().y }); { 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.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] == '|') 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++; while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++;
if (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] = std::make_unique<Object>(object_data[0]);
tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x, tilemap[i][j]->rect.setSize(
tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); { 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.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 });
} }
} }
} }