diff --git a/.clang-format b/.clang-format index c0094ca..8601227 100644 --- a/.clang-format +++ b/.clang-format @@ -15,7 +15,6 @@ ContinuationIndentWidth: 8 AlignAfterOpenBracket: Align PointerAlignment: Left ReferenceAlignment: Left -IndentAccessModifiers: true # Braces BreakBeforeBraces: Custom diff --git a/compare b/compare new file mode 100644 index 0000000..c438428 --- /dev/null +++ b/compare @@ -0,0 +1,101 @@ +(100, 0) +(106.279, 0.197327) +(112.533, 0.788528) +(118.738, 1.77127) +(124.869, 3.14169) +(130.902, 4.89435) +(136.812, 7.02235) +(142.578, 9.5173) +(148.175, 12.3693) +(153.583, 15.5672) +(158.779, 19.0983) +(163.742, 22.9487) +(168.455, 27.1031) +(172.897, 31.5453) +(177.051, 36.2576) +(180.902, 41.2215) +(184.433, 46.4173) +(187.631, 51.8246) +(190.483, 57.4221) +(192.978, 63.1875) +(195.106, 69.0983) +(196.858, 75.131) +(198.229, 81.2619) +(199.211, 87.4667) +(199.803, 93.7209) +(200, 100) +(199.803, 106.279) +(199.211, 112.533) +(198.229, 118.738) +(196.858, 124.869) +(195.106, 130.902) +(192.978, 136.812) +(190.483, 142.578) +(187.631, 148.175) +(184.433, 153.583) +(180.902, 158.779) +(177.051, 163.742) +(172.897, 168.455) +(168.455, 172.897) +(163.742, 177.051) +(158.779, 180.902) +(153.583, 184.433) +(148.175, 187.631) +(142.578, 190.483) +(136.812, 192.978) +(130.902, 195.106) +(124.869, 196.858) +(118.738, 198.229) +(112.533, 199.211) +(106.279, 199.803) +(100, 200) +(93.7209, 199.803) +(87.4667, 199.211) +(81.2619, 198.229) +(75.131, 196.858) +(69.0983, 195.106) +(63.1875, 192.978) +(57.4221, 190.483) +(51.8246, 187.631) +(46.4173, 184.433) +(41.2214, 180.902) +(36.2576, 177.051) +(31.5453, 172.897) +(27.1031, 168.455) +(22.9487, 163.742) +(19.0983, 158.779) +(15.5672, 153.583) +(12.3693, 148.175) +(9.51729, 142.578) +(7.02235, 136.812) +(4.89436, 130.902) +(3.14169, 124.869) +(1.77127, 118.738) +(0.788528, 112.533) +(0.197327, 106.279) +(0, 100) +(0.197327, 93.721) +(0.788528, 87.4667) +(1.77127, 81.2619) +(3.14169, 75.131) +(4.89435, 69.0983) +(7.02235, 63.1875) +(9.5173, 57.4221) +(12.3693, 51.8246) +(15.5672, 46.4174) +(19.0983, 41.2215) +(22.9487, 36.2576) +(27.1031, 31.5453) +(31.5453, 27.1031) +(36.2576, 22.9487) +(41.2215, 19.0983) +(46.4173, 15.5672) +(51.8247, 12.3693) +(57.4221, 9.5173) +(63.1875, 7.02235) +(69.0983, 4.89435) +(75.131, 3.14169) +(81.2619, 1.77126) +(87.4667, 0.788528) +(93.7209, 0.197327) + diff --git a/include/data.hpp b/include/data.hpp index 30b7cf0..014b04d 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -1,6 +1,16 @@ #ifndef DATA_HPP #define DATA_HPP +// Timers +#define DASH 1 +#define LEVEL_SWITCH 2 + +// Maps +#define EMPTY 0 +#define DEFAULT 1 +#define PLATFORM1 2 +#define CHECKERED 3 + #include "globals.hpp" #include @@ -22,21 +32,23 @@ struct EntityData : ObjectData { }; struct PlayerData : EntityData { - ObjectData object; - EntityData entity; float dash_length { 0.0f }; float max_vertical_acceleration { 0.0f }; }; -class LevelData { - public: - std::array, MAX_ROWS> data = {}; - std::string name = "Null"; +struct LevelData { + std::array, MAX_ROWS> data {}; +}; + +struct TimerData { + std::string name { "NULL" }; + float max_time { 1.f }; }; extern std::vector object_data; extern std::vector entity_data; extern std::vector level_data; +extern std::vector timer_data; extern PlayerData player_data; -#endif // DATA_HPP +#endif diff --git a/include/elements.hpp b/include/elements.hpp index 59b6530..ae7a33d 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -8,6 +8,7 @@ #include #include #include +#include enum class CollisionDir { None, Left, Right, Top, Bottom }; class Entity; @@ -41,6 +42,10 @@ class Player : public Entity { float gravity; bool m_jumping { false }; bool m_dash { false }; + sf::RectangleShape bounding_box { { 0, 100 } }; + void initialize_bounding_box(); + void update_bounding_box(); + void check_bounding_box(std::array, MAX_ROWS> wall_list); void handle_movment(Timer& dash); void handle_gravity(); bool get_jumping(); @@ -52,7 +57,7 @@ class Player : public Entity { private: bool get_vertical_bottom_collision_by_offset(std::array, MAX_ROWS>); - sf::Vector2f _min_velocity; + sf::Vector2f _min_velocity { 0, 0 }; }; #endif diff --git a/include/globals.hpp b/include/globals.hpp index 1782df7..74a60b4 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -8,6 +8,4 @@ #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 5f4013f..97ba12e 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -8,14 +8,18 @@ #include 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); + public: + void clear(); + void load(const std::array, MAX_ROWS> map_data); + void draw(sf::RenderWindow& window); + void set_next(); + void set_previous(); + int get_active(); + void set_active(int level); std::array, MAX_ROWS> wall_list = {}; -private: - int current_level = 1; + + private: + int _active = 1; }; #endif diff --git a/include/scheduler.hpp b/include/scheduler.hpp index c76836f..cc1662e 100644 --- a/include/scheduler.hpp +++ b/include/scheduler.hpp @@ -4,10 +4,11 @@ #include #include #include +#include "data.hpp" class Timer { public: - Timer(std::string name, float max_time) : name(name), max_time(max_time) {}; + Timer(TimerData data) : name(data.name), max_time(data.max_time) {}; bool below_or_zero(); float get_time(); std::string get_name(); @@ -21,8 +22,11 @@ private: }; struct Scheduler { + Scheduler(); std::vector timers; - void update(bool& running, std::vector& times); - int pull(std::vector& times, std::string name); + void update(bool& running); + int pull(std::string name); }; + + #endif diff --git a/output b/output new file mode 100644 index 0000000..6bbb426 --- /dev/null +++ b/output @@ -0,0 +1,40 @@ +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) +(0, 0) +(200, 0) +(200, 100) +(0, 100) diff --git a/src/data.cpp b/src/data.cpp index b98e0f3..9972765 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,26 +1,51 @@ #include "data.hpp" + #include "globals.hpp" #include +#include std::vector object_data { {}, }; +/* + Example EntityData + []() -> EntityData { + EntityData e; + e.size = { TILESIZE / 1.5f, TILESIZE / 1.5f }; + e.pos = { 400.0f, 300.0f }; + e.interior_color = sf::Color::Blue; + e.velocity = { 6.0f, 0.0f }; + return e; + }(), +*/ std::vector entity_data { {}, }; -PlayerData player_data { - .object = { - .size = { TILESIZE / 2.0f, TILESIZE / 2.0f }, - .edge = -1.0f, +std::vector timer_data { + {}, + { + .name = "Dash", + .max_time = .5, + }, + { + .name = "Level_Switch", + .max_time = 1, }, - .entity = { .velocity = { 3.0f, 0.0f } }, - .dash_length = 20.0f, - .max_vertical_acceleration = 15.0f }; +PlayerData player_data = [] { + PlayerData data; + data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f }; + data.edge = -1.0f; + data.velocity = { 3.0f, 0.0f }; + data.dash_length = 15.0f; // 15 + data.max_vertical_acceleration = 12.5f; + return data; +}(); + std::vector level_data = { { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, @@ -35,8 +60,8 @@ std::vector level_data = { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ' } } }, - .name = "Empty" }, + ' ' } } } + }, { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, @@ -51,7 +76,7 @@ std::vector level_data = { { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' } } }, - .name = "Default" }, + }, { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, @@ -66,7 +91,7 @@ std::vector level_data = { { '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' }, @@ -81,5 +106,20 @@ std::vector level_data = { { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' } } }, - .name = "Check" } + }, + { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '2' } } }, + }, }; diff --git a/src/elements.cpp b/src/elements.cpp index 5f56f6f..bcb454c 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -52,10 +52,38 @@ CollisionDir Object::find_intersection(Object& entity) { } else { return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top; } - } -void Player::handle_movment(Timer &dash) { +/* Bounding Box + * Create on dash in front of player and check if the box collides with a wall, + * if so find the difference of the wall and the player and set the dash_distance + * to difference. + * */ + +void Player::initialize_bounding_box() { + bounding_box.setOutlineColor(sf::Color::White); + bounding_box.setFillColor(sf::Color::Transparent); + bounding_box.setOutlineThickness(-1); + bounding_box.setSize({ dash_length, rect.getSize().y }); + bounding_box.setOrigin({ rect.getPosition().x, rect.getPosition().y / 2 }); + bounding_box.setPosition({ bounding_box.getOrigin().x, bounding_box.getOrigin().y }); +} + +void Player::update_bounding_box() { + + // Player and box positive distances are 108 in both x and y + bounding_box.setOrigin( + { -rect.getPosition().x + bounding_box.getGeometricCenter().x + (rect.getSize().x / 2), + -rect.getPosition().y + bounding_box.getGeometricCenter().y + (rect.getSize().y / 2) }); +} + +void Player::check_bounding_box(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; + } +} + +void Player::handle_movment(Timer& dash) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { rect.move({ _velocity.x, 0 }); } @@ -70,7 +98,7 @@ void Player::handle_movment(Timer &dash) { _velocity.x = dash_length; dash.reset(); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) { m_jumping = true; _velocity.y = -max_vertical_acceleration; // push up } diff --git a/src/main.cpp b/src/main.cpp index f70f2b7..1ea7b03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,60 +2,62 @@ #include #include +#include +#include +#include #include #include #include #include #include +#include #include #include -#include #ifdef __APPLE__ #include #include namespace rv = ranges::views; +std::string assets = "/Users/andrew/personal/Projects/CPP/engine/assets/"; #else #include namespace rv = std::views; +std::string assets = "/home/andrew/personal/Projects/CPP/engine/assets/"; #endif +#include "data.hpp" #include "elements.hpp" #include "globals.hpp" #include "maps.hpp" #include "scheduler.hpp" -#include "data.hpp" int main() { - Scheduler time; + Scheduler scheduler; Manager map_manager; - std::vector timers = { - Timer("dash", .5), - Timer("level_switch", 1), - }; - - map_manager.load_map(level_data[1].data); - + sf::Font font(assets + "pixel.ttf"); Player player { player_data, GRAVITY }; - + Object test_object { object_data[0] }; sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default); + bool running { true }; + sf::Text text(font, "0, 0", 16); + 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 + player.initialize_bounding_box(); + map_manager.load(level_data[4].data); - 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); }); + std::thread scheduler_thread([&] { scheduler.update(running); }); while (window.isOpen()) { + std::cout << window.clear(sf::Color::Black); - text.setString(std::to_string(timers.at(0).get_time())); + test_object.rect.setPosition( + { player.rect.getPosition().x + 108 - player.rect.getSize().x, player.rect.getPosition().y }); + test_object.rect.setPosition({ player.rect.getPosition().x - 108, player.rect.getPosition().y }); + + text.setString(std::to_string(scheduler.timers.at(0).get_time())); while (const std::optional event = window.pollEvent()) { if (event->is()) { running = false; @@ -67,57 +69,48 @@ int main() { running = false; window.close(); } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) { - map_manager.clear_map(); - } - - 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(level_data[1].data); - map_manager.set_current_level(2); - } else if (map_manager.get_current_level() == 2) { - 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(level_data[3].data); - map_manager.set_current_level(1); - } else { - map_manager.load_map(level_data[1].data); - map_manager.set_current_level(1); + for (int i = 0; i < player.bounding_box.getPointCount(); i++) { + std::cout << "(" << player.bounding_box.getPoint(i).x << ", " << player.bounding_box.getPoint(i).y + << ")\n"; } - timers.at(1).reset(); + std::cout << "\n"; + // sf::FloatRect a = player.bounding_box.getGlobalBounds(); } - player.handle_movment(timers.at(time.pull(timers, "dash"))); + if (map_manager.get_active() >= level_data.size()) { + map_manager.set_active(DEFAULT); + map_manager.load(level_data[DEFAULT].data); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && scheduler.timers.at(1).below_or_zero()) { + map_manager.set_next(); + scheduler.timers.at(1).reset(); + } + + player.update_bounding_box(); + player.handle_movment(scheduler.timers.at(scheduler.pull("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; - } - window.draw(map_manager.wall_list[i][j]->rect); + if (!player.get_vertical_bottom_collision(map_manager.wall_list)) { + player.get_vertical_top_collision(map_manager.wall_list); } + map_manager.draw(window); window.draw(player.rect); + window.draw(player.bounding_box); window.draw(text); + window.draw(test_object.rect); window.display(); } - scheduler.join(); + scheduler_thread.join(); - map_manager.clear_map(); + map_manager.clear(); return 0; } diff --git a/src/maps.cpp b/src/maps.cpp index 01c6480..e8d4732 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -4,6 +4,7 @@ #include "elements.hpp" #include "globals.hpp" +#include #include #ifdef __APPLE__ #include @@ -14,34 +15,37 @@ namespace rv = ranges::views; namespace rv = std::views; #endif -void Manager::clear_map() { +void Manager::clear() { 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; } +int Manager::get_active() { return _active; } -void Manager::set_current_level(int level) { current_level = level; } +void Manager::set_active(int level) { _active = 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; -// } +void Manager::set_next() { + _active++; + clear(); + load(level_data[_active].data); +} -void Manager::load_map(const std::array, MAX_ROWS> map_data) { +void Manager::set_previous() { + _active--; + clear(); + load(level_data[_active].data); +} + +void Manager::draw(sf::RenderWindow& window) { + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { + if (wall_list[i][j] == nullptr) continue; + window.draw(wall_list[i][j]->rect); + } +} + +void Manager::load(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') { diff --git a/src/scheduler.cpp b/src/scheduler.cpp index ea6a04a..1892f0e 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -1,7 +1,15 @@ #include "scheduler.hpp" +#include "data.hpp" #include +#include #include +#include + +Scheduler::Scheduler() { + timers.push_back(timer_data[DASH]); + timers.push_back(timer_data[LEVEL_SWITCH]); +} bool Timer::below_or_zero() { if(time <= 0) { return true; } @@ -16,17 +24,24 @@ 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; } +int Scheduler::pull(std::string name) { + try { + for(int i = 0; i < timers.size(); i++) { + if(timers[i].get_name() == name) { return i; } + } + throw -1; + } catch (int x) { + std::cout << "No valid timer with name: (" << name << ") as a value\n"; + std::cout << "Using defualt timer values\n"; + return -1; } - return -1; + return 0; } -void Scheduler::update(bool& running, std::vector& times) { +void Scheduler::update(bool& running) { while (running) { std::this_thread::sleep_for(std::chrono::seconds(1)); - for(Timer &val : times) { val.update(); } + for(Timer &val : timers) { val.update(); } } }