diff --git a/.clang-format b/.clang-format index b6a510e..c0094ca 100644 --- a/.clang-format +++ b/.clang-format @@ -14,6 +14,8 @@ IndentWidth: 8 ContinuationIndentWidth: 8 AlignAfterOpenBracket: Align PointerAlignment: Left +ReferenceAlignment: Left +IndentAccessModifiers: true # Braces BreakBeforeBraces: Custom diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e54e9d..7464c00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES src/main.cpp src/elements.cpp src/maps.cpp + src/data.cpp src/scheduler.cpp ) diff --git a/include/data.hpp b/include/data.hpp new file mode 100644 index 0000000..5b49cd8 --- /dev/null +++ b/include/data.hpp @@ -0,0 +1,26 @@ +#ifndef DATA_HPP +#define DATA_HPP + +#include "globals.hpp" + +#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; +}; + +class EntityData : public ObjectData { + public: + sf::Vector2 velocity; +}; + +extern std::vector object_data; + +#endif // DATA_HPP diff --git a/include/elements.hpp b/include/elements.hpp index 514981f..3b73999 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -3,25 +3,44 @@ #include "globals.hpp" #include "scheduler.hpp" +#include "data.hpp" #include #include #include enum class CollisionDir { None, Left, Right, Top, Bottom }; +class Entity; +class Wall; +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 Entity { - public: +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); - CollisionDir find_intersection(Entity& entity); + sf::Color inside_color = sf::Color::Black, sf::Color border_color = sf::Color::White); + + 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: +protected: sf::Vector2f _velocity; sf::Color _inside_color; sf::Color _border_color; @@ -29,26 +48,26 @@ class Entity { }; class Player : public Entity { - public: +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, - sf::Color border_color); + float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, + + sf::Color border_color); float m_acceleration; float m_jump_strength; float m_dash_distance; bool m_jumping; bool m_dash; - Times timers; - void handle_movment(); + void handle_movment(Timer &dash); void handle_gravity(); bool get_jumping(); - void horizontal_no_out_of_bounds(Entity& base); + 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>); + 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: +private: bool get_vertical_bottom_collision_by_offset(std::array, MAX_ROWS>); sf::Vector2f _min_velocity; }; diff --git a/include/globals.hpp b/include/globals.hpp index 700ceac..1782df7 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,10 +1,13 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP #define TILESIZE 32 -#define MAX_COLS 8 // 19 cols -#define MAX_ROWS 8 // 13 rows +#define MAX_COLS 19 // 19 cols +#define MAX_ROWS 13 // 13 rows #define WIDTH TILESIZE * MAX_COLS #define HEIGHT TILESIZE * MAX_ROWS #define FPS 60 #define GRAVITY 1 + +#define dash_cooldown .5 +#define level_switch_cooldown_max 1 #endif diff --git a/include/maps.hpp b/include/maps.hpp index 481f94f..4f11693 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -8,13 +8,22 @@ #include struct Level { - char data[MAX_ROWS][MAX_COLS]; + std::array, MAX_ROWS> data; std::string name; }; -extern Level levels[]; -extern const char map[MAX_ROWS][MAX_COLS]; +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 = {}; +private: + int current_level = 1; + +}; + +extern std::vector levels; -void clear_map(std::array, MAX_ROWS>& wall_list); -void load_map(std::array, MAX_ROWS>& wall_list, const char map_data[MAX_ROWS][MAX_COLS]); #endif diff --git a/include/scheduler.hpp b/include/scheduler.hpp index 8014e2e..c76836f 100644 --- a/include/scheduler.hpp +++ b/include/scheduler.hpp @@ -1,14 +1,28 @@ #ifndef SCHEDULER_HPP #define SCHEDULER_HPP -struct Times { - float dash_cooldown { 0 }; - float dash_cooldown_max { .5 }; - float level_switch_cooldown { 0 }; - float level_switch_cooldown_max { 1 }; +#include +#include +#include + +class Timer { +public: + Timer(std::string name, float max_time) : name(name), max_time(max_time) {}; + bool below_or_zero(); + float get_time(); + std::string get_name(); + void reset(); + void update(); + +private: + float time = 0; + const float max_time; + const std::string name; }; -struct Timer { - void update_timers(bool& running, Times& times); +struct Scheduler { + std::vector timers; + void update(bool& running, std::vector& times); + int pull(std::vector& times, std::string name); }; #endif diff --git a/src/data.cpp b/src/data.cpp new file mode 100644 index 0000000..5cff490 --- /dev/null +++ b/src/data.cpp @@ -0,0 +1,6 @@ +#include "data.hpp" +#include + +std::vector object_data { + {}, +}; diff --git a/src/elements.cpp b/src/elements.cpp index 91cbb1a..b93877b 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -15,6 +15,21 @@ 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; + + rect.setSize(data.size); + rect.setPosition(data.pos); + rect.setOutlineThickness(data.edge); + rect.setFillColor(data.interior_color); + 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); @@ -33,7 +48,30 @@ void Entity::set_velocity_y(int velocity) { _velocity.y = velocity; } sf::Vector2 Entity::get_velocity() { return _velocity; } -CollisionDir Entity::find_intersection(Entity& entity) { +CollisionDir Wall::find_intersection(Entity& 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; + } + +} + +CollisionDir Entity::find_intersection(Wall& entity) { sf::FloatRect a = rect.getGlobalBounds(); sf::FloatRect b = entity.rect.getGlobalBounds(); auto inter = a.findIntersection(b); @@ -66,20 +104,20 @@ Player::Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f vel m_jumping = m_dash = false; } -void Player::handle_movment() { +void Player::handle_movment(Timer &dash) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { rect.move({ _velocity.x, 0 }); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { rect.move({ -_velocity.x, 0 }); } - if (timers.dash_cooldown <= 0) { + if (dash.get_time() <= 0) { m_dash = false; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && timers.dash_cooldown <= 0) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && dash.get_time() <= 0) { m_dash = true; _velocity.x = m_dash_distance; - timers.dash_cooldown = timers.dash_cooldown_max; + dash.reset(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { m_jumping = true; @@ -96,7 +134,7 @@ void Player::handle_gravity() { rect.move({ 0, get_velocity().y }); } -void Player::horizontal_no_out_of_bounds(Entity& base) { +void Player::horizontal_no_out_of_bounds() { float right_border = WIDTH - rect.getSize().x; if (rect.getPosition().x > right_border) { rect.setPosition({ right_border, rect.getPosition().y }); @@ -116,7 +154,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); @@ -135,7 +173,7 @@ bool Player::get_horizontal_collision(std::array, 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]); @@ -150,7 +188,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 5665909..c7f6d62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "data.hpp" #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include #include #ifdef __APPLE__ @@ -24,17 +26,18 @@ namespace rv = std::views; #include "scheduler.hpp" int main() { - Timer time; - std::array, MAX_ROWS> wall_list = {}; - bool dragging = false; - bool prev_dragging = false; - int current_level = 0; + Scheduler time; + Manager map_manager; + std::vector timers = { + Timer("dash", .5), + Timer("level_switch", 1), + }; - load_map(wall_list, levels[3].data); + map_manager.load_map(levels[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 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 }; sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default); @@ -46,10 +49,10 @@ int main() { text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5) }); bool running = true; - std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); }); + std::thread scheduler([&] { time.update(running, timers); }); while (window.isOpen()) { window.clear(sf::Color::Black); - text.setString("Test"); + text.setString(std::to_string(timers.at(0).get_time())); while (const std::optional event = window.pollEvent()) { if (event->is()) { running = false; @@ -62,64 +65,58 @@ int main() { window.close(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) { - clear_map(wall_list); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { - dragging = false; + map_manager.clear_map(); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) { - clear_map(wall_list); - if (current_level == 0) { - load_map(wall_list, levels[1].data); - current_level = 1; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } else if (current_level == 1) { - load_map(wall_list, levels[2].data); - current_level = 2; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } else if (current_level == 2) { - load_map(wall_list, levels[3].data); - current_level = 0; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } - } - - player.handle_movment(); - if (!player.get_horizontal_collision(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(wall_list)) { - std::cout << "\n"; - if (!player.get_vertical_top_collision(wall_list)) { - std::cout << "\n"; + 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.set_current_level(2); + } else if (map_manager.get_current_level() == 2) { + map_manager.load_map(levels[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.set_current_level(1); } else { - std::cout << "Yes vertical top collision\n"; + map_manager.load_map(levels[1].data); + map_manager.set_current_level(1); } - } else { - std::cout << "Yes vertical bottom collision\n"; + 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"; } + // for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { - if (wall_list[i][j] == nullptr) { + if (map_manager.wall_list[i][j] == nullptr) { continue; } - window.draw(wall_list[i][j]->rect); + window.draw(map_manager.wall_list[i][j]->rect); } window.draw(player.rect); window.draw(text); window.display(); } + scheduler.join(); - clear_map(wall_list); - player_dash_cooldown.join(); + map_manager.clear_map(); return 0; } diff --git a/src/maps.cpp b/src/maps.cpp index ff4815e..a6e4bab 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,10 +1,10 @@ #include "maps.hpp" +#include "data.hpp" #include "elements.hpp" #include "globals.hpp" #include -#include #ifdef __APPLE__ #include #include @@ -14,13 +14,17 @@ namespace rv = ranges::views; namespace rv = std::views; #endif -void clear_map(std::array, MAX_ROWS>& wall_list) { +void Manager::clear_map() { for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { delete wall_list[i][j]; wall_list[i][j] = nullptr; } } +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; @@ -37,139 +41,112 @@ bool is_tiles_clear_y(std::array, MAX_ROWS> draw_list return true; } -void load_map(std::array, MAX_ROWS>& wall_list, const char map_data[MAX_ROWS][MAX_COLS]) { +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 Entity { { TILESIZE, TILESIZE }, - { float(j * TILESIZE), float(i * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black }; + wall_list[i][j] = new Wall { 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') { int start = j, end = j + 1; - wall_list[i][j] = new Entity { { float(end - start + 1) * TILESIZE, TILESIZE }, - { float(j * TILESIZE), float(i * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black }; + wall_list[i][j] = new Wall { 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') { int start = i, end = i + 1; - wall_list[i][j] = new Entity { { TILESIZE, float(end - start + 1) * TILESIZE }, - { float(j * TILESIZE), float(i * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black }; + wall_list[i][j] = new Wall { 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] == '-') { 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 Entity { { float(end - start + 1) * TILESIZE, TILESIZE }, - { float(j * TILESIZE), float(i * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black }; + wall_list[i][j] = new Wall { 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] == '|') { 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 Entity { { TILESIZE, float(end - start + 1) * TILESIZE }, - { float(j * TILESIZE), float(i * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black }; + wall_list[i][j] = new Wall { 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 }); } } } -Level levels[] = { - { - .data = { - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, - {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, }, - .name = "Empty" - } +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" } }; -// Level 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 = "1" -// }, -// { -// .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 = "3" -// } -// }; diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 01e6c93..ea6a04a 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -1,14 +1,32 @@ -// timer for player cooldown - #include "scheduler.hpp" #include #include -void Timer::update_timers(bool& running, Times& times) { +bool Timer::below_or_zero() { + if(time <= 0) { return true; } + return false; +} + +float Timer::get_time() { return time; } + +std::string Timer::get_name() { return name; } + +void Timer::reset() { time = max_time; } + +void Timer::update() { if(!below_or_zero()) { time--; } } + +int Scheduler::pull(std::vector& times, std::string name) { + for(int i = 0; i < times.size(); i++) { + if(times[i].get_name() == name) { return i; } + } + + return -1; +} + +void Scheduler::update(bool& running, std::vector& times) { while (running) { std::this_thread::sleep_for(std::chrono::seconds(1)); - times.dash_cooldown--; - times.level_switch_cooldown--; + for(Timer &val : times) { val.update(); } } }