diff --git a/include/data.hpp b/include/data.hpp index 5b49cd8..30b7cf0 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -5,22 +5,38 @@ #include #include +#include +#include #include -class ObjectData { - public: - sf::Vector2 size = { TILESIZE, TILESIZE }; - sf::Vector2 pos = { TILESIZE, TILESIZE }; - sf::Color interior_color = sf::Color::Black; - sf::Color exterior_color = sf::Color::White; - float edge = -2.0f; +struct ObjectData { + sf::Vector2 size { TILESIZE, TILESIZE }; + sf::Vector2 pos { TILESIZE, TILESIZE }; + sf::Color interior_color { sf::Color::Black }; + sf::Color exterior_color { sf::Color::White }; + float edge { -2.0f }; }; -class EntityData : public ObjectData { +struct EntityData : ObjectData { + sf::Vector2 velocity { 0, 0 }; +}; + +struct PlayerData : EntityData { + ObjectData object; + EntityData entity; + float dash_length { 0.0f }; + float max_vertical_acceleration { 0.0f }; +}; + +class LevelData { public: - sf::Vector2 velocity; + std::array, MAX_ROWS> data = {}; + std::string name = "Null"; }; extern std::vector object_data; +extern std::vector entity_data; +extern std::vector level_data; +extern PlayerData player_data; #endif // DATA_HPP diff --git a/include/elements.hpp b/include/elements.hpp index 3b73999..59b6530 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,9 +1,9 @@ #ifndef PLAYER_HPP #define PLAYER_HPP +#include "data.hpp" #include "globals.hpp" #include "scheduler.hpp" -#include "data.hpp" #include #include @@ -11,74 +11,48 @@ enum class CollisionDir { None, Left, Right, Top, Bottom }; class Entity; -class Wall; +class Object; class Player; -class Wall { -public: - Wall(ObjectData data); - ObjectData data; - sf::Vector2 size; - sf::Vector2 pos; - sf::Color interior_color; - sf::Color exterior_color; - float edge; - - CollisionDir find_intersection(Entity& entity); - sf::RectangleShape rect; +class Object { + public: + Object(ObjectData data); + CollisionDir find_intersection(Object& entity); + sf::RectangleShape rect; }; -class Entity { -public: - Entity(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, - sf::Color inside_color = sf::Color::Black, sf::Color border_color = sf::Color::White); +class Entity : public Object { + public: + Entity(EntityData data) : Object(data), _velocity(data.velocity) {}; + void set_velocity(sf::Vector2f velocity); + void set_velocity_x(int velocity); + void set_velocity_y(int velocity); + sf::Vector2 get_velocity(); - CollisionDir find_intersection(Wall& entity); - void set_velocity(sf::Vector2f velocity); - void set_velocity_x(int velocity); - void set_velocity_y(int velocity); - sf::Vector2 get_velocity(); - sf::RectangleShape rect; - -protected: - sf::Vector2f _velocity; - sf::Color _inside_color; - sf::Color _border_color; - float _border_thickness; + protected: + sf::Vector2f _velocity; }; class Player : public Entity { -public: - Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, - float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, + public: + Player(PlayerData data, float gravity) : Entity(data), dash_length(data.dash_length), max_vertical_acceleration(data.max_vertical_acceleration), gravity(gravity) {}; + float max_vertical_acceleration; + float dash_length; + float gravity; + bool m_jumping { false }; + bool m_dash { false }; + void handle_movment(Timer& dash); + void handle_gravity(); + bool get_jumping(); + void horizontal_no_out_of_bounds(); + void vertical_no_out_of_bounds(Entity& base); + bool get_horizontal_collision(std::array, MAX_ROWS>); + bool get_vertical_top_collision(std::array, MAX_ROWS>); + bool get_vertical_bottom_collision(std::array, MAX_ROWS>); - sf::Color border_color); - float m_acceleration; - float m_jump_strength; - float m_dash_distance; - bool m_jumping; - bool m_dash; - void handle_movment(Timer &dash); - void handle_gravity(); - bool get_jumping(); - void horizontal_no_out_of_bounds(); - void vertical_no_out_of_bounds(Entity& base); - bool get_horizontal_collision(std::array, MAX_ROWS>); - bool get_vertical_top_collision(std::array, MAX_ROWS>); - bool get_vertical_bottom_collision(std::array, MAX_ROWS>); - -private: - bool get_vertical_bottom_collision_by_offset(std::array, MAX_ROWS>); - sf::Vector2f _min_velocity; -}; - -struct player_data { - sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; - sf::Vector2 pos { TILESIZE, TILESIZE }; - float max_velocity_y { 15.0f }; - float border_size { -1.0f }; - float dash_distance { 20.0f }; - sf::Vector2f velocity { 3.0f, 0 }; + private: + bool get_vertical_bottom_collision_by_offset(std::array, MAX_ROWS>); + sf::Vector2f _min_velocity; }; #endif diff --git a/include/maps.hpp b/include/maps.hpp index 4f11693..5f4013f 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -7,23 +7,15 @@ #include #include -struct Level { - std::array, MAX_ROWS> data; - std::string name; -}; - class Manager { public: void clear_map(); void load_map(const std::array, MAX_ROWS> map_data); int get_current_level(); void set_current_level(int level); - std::array, MAX_ROWS> wall_list = {}; + std::array, MAX_ROWS> wall_list = {}; private: int current_level = 1; - }; -extern std::vector levels; - #endif diff --git a/src/data.cpp b/src/data.cpp index 5cff490..b98e0f3 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,6 +1,85 @@ #include "data.hpp" +#include "globals.hpp" + #include std::vector object_data { {}, }; + +std::vector entity_data { + {}, +}; + +PlayerData player_data { + .object = { + .size = { TILESIZE / 2.0f, TILESIZE / 2.0f }, + .edge = -1.0f, + }, + .entity = { .velocity = { 3.0f, 0.0f } }, + .dash_length = 20.0f, + .max_vertical_acceleration = 15.0f +}; + +std::vector level_data = { + { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ' } } }, + .name = "Empty" }, + { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, + { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '2' } } }, + .name = "Default" }, + { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, + { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '2' } } }, + .name = "Plat1" }, + { .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' } } }, + .name = "Check" } +}; diff --git a/src/elements.cpp b/src/elements.cpp index b93877b..5f56f6f 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,5 +1,6 @@ #include "elements.hpp" +#include "data.hpp" #include "globals.hpp" #include @@ -15,14 +16,7 @@ namespace rv = ranges::views; namespace rv = std::views; #endif -Wall::Wall(ObjectData data) { - this->data = data; - // rect.setSize(data.size); - // pos = data.pos; - // interior_color = data.interior_color; - // exterior_color = data.exterior_color; - // edge = data.edge; - +Object::Object(ObjectData data) { rect.setSize(data.size); rect.setPosition(data.pos); rect.setOutlineThickness(data.edge); @@ -30,16 +24,6 @@ Wall::Wall(ObjectData data) { rect.setOutlineColor(data.exterior_color); } -Entity::Entity(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, - sf::Color inside_color, sf::Color border_color) { - rect.setSize(size); - rect.setPosition(pos); - rect.setOutlineThickness(border_thickness); - rect.setFillColor(inside_color); - rect.setOutlineColor(border_color); - _velocity = velocity; -} - void Entity::set_velocity(sf::Vector2 velocity) { _velocity = velocity; } void Entity::set_velocity_x(int velocity) { _velocity.x = velocity; } @@ -48,7 +32,7 @@ void Entity::set_velocity_y(int velocity) { _velocity.y = velocity; } sf::Vector2 Entity::get_velocity() { return _velocity; } -CollisionDir Wall::find_intersection(Entity& entity) { +CollisionDir Object::find_intersection(Object& entity) { sf::FloatRect a = rect.getGlobalBounds(); sf::FloatRect b = entity.rect.getGlobalBounds(); auto inter = a.findIntersection(b); @@ -71,39 +55,6 @@ CollisionDir Wall::find_intersection(Entity& entity) { } -CollisionDir Entity::find_intersection(Wall& 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; - } -} - -Player::Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, - float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, - sf::Color border_color) - : Entity(size, pos, velocity, border_thickness, inside_color, border_color) { - _min_velocity = velocity; - m_acceleration = gravity; - m_jump_strength = max_acceleration; - m_dash_distance = dash_distance; - m_jumping = m_dash = false; -} - void Player::handle_movment(Timer &dash) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { rect.move({ _velocity.x, 0 }); @@ -116,21 +67,21 @@ void Player::handle_movment(Timer &dash) { } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && dash.get_time() <= 0) { m_dash = true; - _velocity.x = m_dash_distance; + _velocity.x = dash_length; dash.reset(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { m_jumping = true; - _velocity.y = -m_jump_strength; // push up + _velocity.y = -max_vertical_acceleration; // push up } } void Player::handle_gravity() { - if (get_velocity().x > _min_velocity.x) { - set_velocity_x(get_velocity().x - m_acceleration); + if (get_velocity().x > player_data.velocity.x) { + set_velocity_x(get_velocity().x - gravity); } - set_velocity_y(get_velocity().y + m_acceleration); + set_velocity_y(get_velocity().y + gravity); rect.move({ 0, get_velocity().y }); } @@ -154,7 +105,7 @@ void Player::vertical_no_out_of_bounds(Entity& base) { } } -bool Player::get_horizontal_collision(std::array, MAX_ROWS> wall_list) { +bool Player::get_horizontal_collision(std::array, MAX_ROWS> wall_list) { for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { if (wall_list[i][j] == nullptr) continue; auto collision = wall_list[i][j]->find_intersection(*this); @@ -173,7 +124,7 @@ bool Player::get_horizontal_collision(std::array, MA return false; } -bool Player::get_vertical_top_collision(std::array, MAX_ROWS> wall_list) { +bool Player::get_vertical_top_collision(std::array, MAX_ROWS> wall_list) { for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { if (wall_list[i][j] == nullptr) continue; auto collision = find_intersection(*wall_list[i][j]); @@ -188,7 +139,7 @@ bool Player::get_vertical_top_collision(std::array, return false; } -bool Player::get_vertical_bottom_collision(std::array, MAX_ROWS> wall_list) { +bool Player::get_vertical_bottom_collision(std::array, MAX_ROWS> wall_list) { for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { if (wall_list[i][j] == nullptr) continue; auto collision = find_intersection(*wall_list[i][j]); diff --git a/src/main.cpp b/src/main.cpp index c7f6d62..f70f2b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "data.hpp" + #include #include #include @@ -6,10 +7,9 @@ #include #include #include -#include -#include #include #include +#include #ifdef __APPLE__ #include @@ -24,6 +24,7 @@ namespace rv = std::views; #include "globals.hpp" #include "maps.hpp" #include "scheduler.hpp" +#include "data.hpp" int main() { Scheduler time; @@ -33,23 +34,25 @@ int main() { Timer("level_switch", 1), }; - map_manager.load_map(levels[1].data); + map_manager.load_map(level_data[1].data); - player_data player_data; - Player player { player_data.size, player_data.pos, player_data.velocity, - player_data.border_size, GRAVITY, player_data.max_velocity_y, - player_data.dash_distance, sf::Color::Black, sf::Color::White }; + Player player { player_data, GRAVITY }; sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default); window.setFramerateLimit(FPS); +#ifdef __APPLE__ sf::Font font("/Users/andrew/personal/Projects/CPP/engine/assets/pixel.ttf"); +#else + sf::Font font("/home/andrew/personal/Projects/CPP/engine/assets/pixel.ttf"); +#endif sf::Text text(font, "0, 0", 16); text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5) }); bool running = true; std::thread scheduler([&] { time.update(running, timers); }); + while (window.isOpen()) { window.clear(sf::Color::Black); text.setString(std::to_string(timers.at(0).get_time())); @@ -71,39 +74,37 @@ int main() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && timers.at(1).below_or_zero()) { map_manager.clear_map(); if (map_manager.get_current_level() == 1) { - map_manager.load_map(levels[1].data); + map_manager.load_map(level_data[1].data); map_manager.set_current_level(2); } else if (map_manager.get_current_level() == 2) { - map_manager.load_map(levels[2].data); + map_manager.load_map(level_data[2].data); map_manager.set_current_level(3); } else if (map_manager.get_current_level() == 3) { - map_manager.load_map(levels[3].data); + map_manager.load_map(level_data[3].data); map_manager.set_current_level(1); } else { - map_manager.load_map(levels[1].data); + map_manager.load_map(level_data[1].data); map_manager.set_current_level(1); } timers.at(1).reset(); } - // player.handle_movment(timers.at(time.pull(timers, "dash"))); - // if (!player.get_horizontal_collision(map_manager.wall_list)) { - // std::cout << "\n"; - // } else { std::cout << "Yes horizontal collision\n"; } - // - // player.handle_gravity(); - // if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { - // player.set_velocity_y((TILESIZE / 2) - GRAVITY); - // } - // if (!player.get_vertical_bottom_collision(map_manager.wall_list)) { - // std::cout << "\n"; - // if (!player.get_vertical_top_collision(map_manager.wall_list)) { - // std::cout << "\n"; - // } else { - // std::cout << "Yes vertical top collision\n"; - // } - // } else { std::cout << "Yes vertical bottom collision\n"; } - // + player.handle_movment(timers.at(time.pull(timers, "dash"))); + player.get_horizontal_collision(map_manager.wall_list); + + player.handle_gravity(); + if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { + player.set_velocity_y((TILESIZE / 2) - GRAVITY); + } + if (!player.get_vertical_bottom_collision(map_manager.wall_list)) { + std::cout << "\n"; + if (!player.get_vertical_top_collision(map_manager.wall_list)) { + std::cout << "\n"; + } else { + std::cout << "Yes vertical top collision\n"; + } + } else { std::cout << "Yes vertical bottom collision\n"; } + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { if (map_manager.wall_list[i][j] == nullptr) { continue; diff --git a/src/maps.cpp b/src/maps.cpp index a6e4bab..01c6480 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -25,61 +25,61 @@ int Manager::get_current_level() { return current_level; } void Manager::set_current_level(int level) { current_level = level; } -bool is_tiles_clear_x(std::array, MAX_ROWS> draw_list, int y_level, int start_x, int end_x) { - for (int i : rv::iota(start_x, end_x)) { - if (draw_list[i][y_level] == ' ') continue; - return false; - } - return true; -} - -bool is_tiles_clear_y(std::array, MAX_ROWS> draw_list, int x_level, int start_y, int end_y) { - for (int i : rv::iota(start_y, end_y)) { - if (draw_list[i][x_level] == ' ') continue; - return false; - } - return true; -} +// bool is_tiles_clear_x(std::array, MAX_ROWS> draw_list, int y_level, int start_x, int end_x) { +// for (int i : rv::iota(start_x, end_x)) { +// if (draw_list[i][y_level] == ' ') continue; +// return false; +// } +// return true; +// } +// +// bool is_tiles_clear_y(std::array, MAX_ROWS> draw_list, int x_level, int start_y, int end_y) { +// for (int i : rv::iota(start_y, end_y)) { +// if (draw_list[i][x_level] == ' ') continue; +// return false; +// } +// return true; +// } void Manager::load_map(const std::array, MAX_ROWS> map_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') { - wall_list[i][j] = new Wall { object_data[0] }; + wall_list[i][j] = new Object { object_data[0] }; wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, wall_list[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && 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; - wall_list[i][j] = new Wall { object_data[0] }; + wall_list[i][j] = new Object { object_data[0] }; wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1), wall_list[i][j]->rect.getSize().y }); wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, wall_list[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && 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; - wall_list[i][j] = new Wall { object_data[0] }; + wall_list[i][j] = new Object { object_data[0] }; wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x, wall_list[i][j]->rect.getSize().y * float(end - start + 1) }); wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, wall_list[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && map_data[i][j + 1] == '-') { + if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '-') { int start = j, end = j; while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++; if (map_data[i][end + 1] == '2') end++; - wall_list[i][j] = new Wall { object_data[0] }; + wall_list[i][j] = new Object { object_data[0] }; wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1), wall_list[i][j]->rect.getSize().y }); wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, wall_list[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && map_data[i + 1][j] == '|') { + if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '|') { int start = i, end = i; while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++; if (map_data[end + 1][j] == '2') end++; - wall_list[i][j] = new Wall { object_data[0] }; + wall_list[i][j] = new Object { object_data[0] }; wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x, wall_list[i][j]->rect.getSize().y * float(end - start + 1) }); wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, @@ -87,66 +87,3 @@ void Manager::load_map(const std::array, MAX_ROWS> ma } } } - -std::vector levels = { - { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ' } } }, - .name = "Empty" }, - { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '2' } } }, - .name = "Default" }, - { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '2' } } }, - .name = "Plat1" }, - { .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' } } }, - .name = "Check" } -};