From cb391573e22c77a9bb258906f4c766f1b153c6df Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Fri, 24 Oct 2025 14:37:31 -0400 Subject: [PATCH] fixes --- .DS_Store | Bin 6148 -> 6148 bytes CMakeLists.txt | 1 + include/elements.hpp | 10 ++++++- include/scheduler.hpp | 7 +++++ src/elements.cpp | 15 ++++++++--- src/main.cpp | 61 +++++++++++++++++------------------------- src/maps.cpp | 26 +++++++++--------- src/scheduler.cpp | 11 ++++++++ 8 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 include/scheduler.hpp create mode 100644 src/scheduler.cpp diff --git a/.DS_Store b/.DS_Store index bdcd5c9918544041ef0d8bdef40f79ac98e4b1fa..a75289dbd119518ab2453a18ede031dc834043a5 100644 GIT binary patch delta 47 xcmZoMXffEJ#>&F~&ED9DHuF7ONE7FaImGZ-XX#vC6=hIjl0wJU0tAce7Rs0RWc25rzN& diff --git a/CMakeLists.txt b/CMakeLists.txt index 149a0e4..ea093fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES src/main.cpp src/elements.cpp src/maps.cpp + src/scheduler.cpp ) include(FetchContent) diff --git a/include/elements.hpp b/include/elements.hpp index 51785d0..2bd5f03 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -24,12 +24,20 @@ protected: float _border_thickness; }; +struct Player_timers { + float dash_cooldown { 0 }; + float dash_cooldown_max { 5 }; +}; + class Player : public Entity { public: - Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, sf::Color inside_color, sf::Color border_color); + 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 m_acceleration; float m_jump_strength; + float m_dash_distance; bool m_jumping; + bool m_dash; + Player_timers timers; void handle_movment(); void handle_gravity(); bool get_jumping(); diff --git a/include/scheduler.hpp b/include/scheduler.hpp new file mode 100644 index 0000000..0d43c19 --- /dev/null +++ b/include/scheduler.hpp @@ -0,0 +1,7 @@ +#ifndef SCHEDULER_HPP +#define SCHEDULER_HPP + +struct Timer { + void update_timers(bool &running, float &dash_timer); +}; +#endif diff --git a/src/elements.cpp b/src/elements.cpp index a0a48ff..332a49c 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -63,10 +63,12 @@ CollisionDir Entity::find_intersection(Entity &entity) { } } -Player::Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, sf::Color inside_color, sf::Color border_color) : Entity(size, pos, velocity, border_thickness, inside_color, border_color) { +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() { @@ -74,12 +76,16 @@ void Player::handle_movment() { rect.move({_velocity.x, 0}); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { rect.move({-_velocity.x, 0}); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { + } if(timers.dash_cooldown <= 0) { + m_dash = false; + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && timers.dash_cooldown <= 0) { + m_dash = true; + _velocity.x = m_dash_distance; + timers.dash_cooldown = timers.dash_cooldown_max; + } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { m_jumping = true; _velocity.y = -m_jump_strength; // push up } - } void Player::handle_gravity() { @@ -110,6 +116,7 @@ void Player::vertical_no_out_of_bounds(Entity &base) { } } +// bool Player::get_horizontal_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) { bool Player::get_horizontal_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) { #ifdef __APPLE__ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { diff --git a/src/main.cpp b/src/main.cpp index 237632e..ed8049c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,10 @@ -#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include #include +#include #ifdef __APPLE__ #include #include @@ -21,8 +14,10 @@ #include "globals.hpp" #include "elements.hpp" #include "maps.hpp" +#include "scheduler.hpp" int main() { + Timer time; Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; struct { sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; @@ -30,6 +25,7 @@ int main() { float acceleration { 1.0f }; float max_velocity_y { 20.0f }; float border_size { -1.0f }; + float dash_distance { 20.0f }; sf::Vector2f velocity { 4.0f, 0 }; } player_data; @@ -40,20 +36,6 @@ int main() { float border_size { -2.0f }; } tile_data; - struct { - sf::Vector2f size { WIDTH, 2.0f }; - sf::Vector2f pos { 0, HEIGHT - (TILESIZE * 1) }; - sf::Vector2f velocity { 0, 0 }; - float border_size { -1.0f }; - } line_data; - - struct { - sf::Vector2f size { TILESIZE / 4.0f, TILESIZE / 4.0f }; - sf::Vector2f pos { TILESIZE * 3, TILESIZE }; - sf::Vector2f velocity { 0, 0 }; - float border_size { -1.0f }; - } simulated_data; - #ifdef __APPLE__ for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { if(map_data[i][j] != 1) continue; @@ -64,6 +46,7 @@ int main() { if(wall_list[i][j] == nullptr) continue; wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); } + #else for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { if(map_data[i][j] != 1) continue; @@ -79,11 +62,9 @@ int main() { Player player { player_data.size, player_data.pos, player_data.velocity, player_data.border_size, player_data.acceleration, player_data.max_velocity_y, - sf::Color::Black, sf::Color::White + player_data.dash_distance, sf::Color::Black, sf::Color::White }; - Entity line { line_data.size, line_data.pos, line_data.velocity, line_data.border_size, sf::Color::White }; - sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default); window.setFramerateLimit(60); @@ -96,31 +77,38 @@ int main() { sf::Text text(font, "0, 0", 16); 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.dash_cooldown); } ); while (window.isOpen()) { window.clear(sf::Color::Black); - text.setString(std::to_string((int)player.rect.getPosition().x) + ", " + std::to_string((int)player.rect.getPosition().y)); + text.setString(std::to_string(player.timers.dash_cooldown)); while (const std::optional event = window.pollEvent()) { - if (event->is()) + if (event->is()) { + running = false; window.close(); + } } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { + running = false; window.close(); } - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { - player.set_velocity_x(20); - } // Handle Horizontal movement and collisions player.handle_movment(); player.get_horizontal_collision(wall_list); - player.horizontal_no_out_of_bounds(line); + + if(player.rect.getPosition().y > HEIGHT) { + player.rect.setPosition({ player.rect.getPosition().x, HEIGHT - TILESIZE - player.rect.getSize().y }); + player.set_velocity_y(0); + while(player.get_vertical_bottom_collision(wall_list) || player.get_vertical_top_collision(wall_list)) { + player.rect.setPosition({ player.rect.getPosition().x, player.rect.getPosition().y - TILESIZE - player.rect.getSize().y }); + } + } player.handle_gravity(); - if(!player.get_vertical_bottom_collision(wall_list)) { player.get_vertical_top_collision(wall_list); } - player.vertical_no_out_of_bounds(line); // Drawing Everything to window #ifdef __APPLE__ @@ -136,7 +124,6 @@ int main() { } #endif window.draw(player.rect); - window.draw(line.rect); window.draw(text); window.display(); } @@ -149,4 +136,6 @@ int main() { delete wall_list[i][j]; } #endif + player_dash_cooldown.join(); + return 0; } diff --git a/src/maps.cpp b/src/maps.cpp index edf41c4..bdb7673 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,17 +1,17 @@ #include "maps.hpp" int map_data[MAX_ROWS][MAX_COLS] = { - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 } + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, + { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; diff --git a/src/scheduler.cpp b/src/scheduler.cpp new file mode 100644 index 0000000..cf8b63e --- /dev/null +++ b/src/scheduler.cpp @@ -0,0 +1,11 @@ +// timer for player cooldown +// +#include "scheduler.hpp" +#include +#include +void Timer::update_timers(bool &running, float &dash_timer) { + while (running) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + dash_timer--; + } +}