From a3589fd93de3d1924461b279a21105f98a154d25 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Macbook Date: Fri, 23 Jan 2026 09:22:39 -0500 Subject: [PATCH] player bounding box almost there --- .clang-format | 85 +++++++------ compare | 101 --------------- include/data.hpp | 22 ++-- include/elements.hpp | 111 +++++++++------- include/maps.hpp | 19 +-- include/scheduler.hpp | 3 +- output | 40 ------ src/data.cpp | 227 +++++++++++++++++++++------------ src/elements.cpp | 285 +++++++++++++++++++++++++++++++----------- src/main.cpp | 91 ++++++++------ src/maps.cpp | 122 +++++++++++------- src/scheduler.cpp | 63 +++++++--- 12 files changed, 679 insertions(+), 490 deletions(-) delete mode 100644 compare delete mode 100644 output diff --git a/.clang-format b/.clang-format index 8601227..271cffa 100644 --- a/.clang-format +++ b/.clang-format @@ -1,47 +1,56 @@ --- # Base Style -BasedOnStyle: LLVM +BasedOnStyle: Google Standard: Latest -ColumnLimit: 120 -ReflowComments: true -SortIncludes: true -IncludeBlocks: Regroup - -# Indentation +ColumnLimit: 100 UseTab: Always TabWidth: 8 IndentWidth: 8 -ContinuationIndentWidth: 8 -AlignAfterOpenBracket: Align -PointerAlignment: Left -ReferenceAlignment: Left - -# Braces -BreakBeforeBraces: Custom -BraceWrapping: - AfterFunction: false - AfterClass: false - AfterControlStatement: false - AfterNamespace: false - IndentBraces: false - -# Containers & lists -SpacesInContainerLiterals: true -Cpp11BracedListStyle: false - +BreakBeforeBraces: Allman +# BreakBeforeBraces: Stroustrup SpaceBeforeCpp11BracedList: true - -# Control statements & loops -SpaceBeforeParens: Custom -SpaceBeforeParensOptions: - AfterControlStatements: true -SpaceBeforeRangeBasedForLoopColon: true -AllowShortFunctionsOnASingleLine: true -AllowShortIfStatementsOnASingleLine: true -AllowShortLoopsOnASingleLine: true -RemoveEmptyLinesInUnwrappedLines: true SeparateDefinitionBlocks: Always +Cpp11BracedListStyle: false +BreakAfterReturnType: TopLevel +AlignTrailingComments: + Kind: Always + # OverEmptyLines: 2 +ReflowComments: Always +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +# RemoveEmptyLinesInUnwrappedLines: true -# Preprocessor -IndentPPDirectives: AfterHash -PPIndentWidth: 0 +# ReflowComments: true +# SortIncludes: true +# IncludeBlocks: Regroup +# +# # Indentation +# ContinuationIndentWidth: 8 +# AlignAfterOpenBracket: true +# PointerAlignment: Left +# ReferenceAlignment: Left +# +# # Braces +# BreakBeforeBraces: Custom +# BraceWrapping: +# AfterFunction: false +# AfterClass: false +# AfterControlStatement: false +# AfterNamespace: false +# IndentBraces: false +# +# # Containers & lists +# +# SpaceBeforeCpp11BracedList: true +# BreakAfterOpenBracketBracedList: true +# +# # Control statements & loops +# SpaceBeforeParens: Custom +# SpaceBeforeParensOptions: +# AfterControlStatements: true +# SpaceBeforeRangeBasedForLoopColon: true +# +# # Preprocessor +# IndentPPDirectives: AfterHash +# PPIndentWidth: 0 diff --git a/compare b/compare deleted file mode 100644 index c438428..0000000 --- a/compare +++ /dev/null @@ -1,101 +0,0 @@ -(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 014b04d..7b28f39 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -10,8 +10,7 @@ #define DEFAULT 1 #define PLATFORM1 2 #define CHECKERED 3 - -#include "globals.hpp" +#define BASE 4 #include #include @@ -19,7 +18,10 @@ #include #include -struct ObjectData { +#include "globals.hpp" + +struct ObjectData +{ sf::Vector2 size { TILESIZE, TILESIZE }; sf::Vector2 pos { TILESIZE, TILESIZE }; sf::Color interior_color { sf::Color::Black }; @@ -27,20 +29,24 @@ struct ObjectData { float edge { -2.0f }; }; -struct EntityData : ObjectData { +struct EntityData : ObjectData +{ sf::Vector2 velocity { 0, 0 }; }; -struct PlayerData : EntityData { - float dash_length { 0.0f }; +struct PlayerData : EntityData +{ + float max_dash_acceleration { 0.0f }; float max_vertical_acceleration { 0.0f }; }; -struct LevelData { +struct LevelData +{ std::array, MAX_ROWS> data {}; }; -struct TimerData { +struct TimerData +{ std::string name { "NULL" }; float max_time { 1.f }; }; diff --git a/include/elements.hpp b/include/elements.hpp index ae7a33d..c2071b9 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,63 +1,88 @@ #ifndef PLAYER_HPP #define PLAYER_HPP +#include +#include +#include +#include +#include + #include "data.hpp" #include "globals.hpp" #include "scheduler.hpp" -#include -#include -#include -#include - -enum class CollisionDir { None, Left, Right, Top, Bottom }; +enum class CollisionDir +{ + None, + Left, + Right, + Top, + Bottom +}; class Entity; class Object; class Player; -class Object { - public: - Object(ObjectData data); - CollisionDir find_intersection(Object& entity); - sf::RectangleShape rect; +class Object +{ + public: + Object(ObjectData data); + CollisionDir find_intersection(Object& entity); + float find_horizontal_difference_from_interesection(Object& entity); + sf::RectangleShape rect; }; -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(); +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(); - protected: - sf::Vector2f _velocity; + protected: + sf::Vector2f _velocity; }; -class Player : public Entity { - 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 }; - 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(); - 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>); +class Player : public Entity +{ + public: + Player(PlayerData data, float gravity) + : Entity(data), + max_dash_acceleration(data.max_vertical_acceleration), + max_vertical_acceleration(data.max_vertical_acceleration), + gravity(gravity), _min_velocity(data.velocity) {}; + float max_vertical_acceleration; + float max_dash_acceleration; + float gravity; + bool m_jumping { false }; + std::unique_ptr bounding_box; + void initialize_bounding_box(); + void update_bounding_box(); + int check_bounding_box( + std::array, MAX_COLS>, MAX_ROWS>& tilemap); + 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_COLS>, MAX_ROWS>& tilemap); + bool get_vertical_top_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap); + bool get_vertical_bottom_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap); - private: - bool get_vertical_bottom_collision_by_offset(std::array, MAX_ROWS>); - sf::Vector2f _min_velocity { 0, 0 }; + private: + sf::Vector2f _min_velocity { 0, 0 }; + std::string direction; }; +float +total_distance(int k); + +float +distance_to_acceleration(int target); + #endif diff --git a/include/maps.hpp b/include/maps.hpp index 97ba12e..80ea818 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -1,24 +1,27 @@ #ifndef MAPS_HPP #define MAPS_HPP +#include +#include +#include + +#include "data.hpp" #include "elements.hpp" #include "globals.hpp" -#include -#include - -class Manager { - public: +class Manager +{ + public: void clear(); - void load(const std::array, MAX_ROWS> map_data); + void load(LevelData level); 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 = {}; + std::array, MAX_COLS>, MAX_ROWS> tilemap = {}; - private: + private: int _active = 1; }; diff --git a/include/scheduler.hpp b/include/scheduler.hpp index cc1662e..76dd6c2 100644 --- a/include/scheduler.hpp +++ b/include/scheduler.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "data.hpp" @@ -24,7 +25,7 @@ private: struct Scheduler { Scheduler(); std::vector timers; - void update(bool& running); + void update(std::atomic& running); int pull(std::string name); }; diff --git a/output b/output deleted file mode 100644 index 6bbb426..0000000 --- a/output +++ /dev/null @@ -1,40 +0,0 @@ -(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 9972765..045fb00 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,12 +1,17 @@ #include "data.hpp" -#include "globals.hpp" - #include #include +#include "globals.hpp" + std::vector object_data { {}, + { + .interior_color = sf::Color::Transparent, + .exterior_color = sf::Color::White, + .edge = -1, + }, }; /* @@ -27,99 +32,163 @@ std::vector entity_data { std::vector timer_data { {}, { - .name = "Dash", - .max_time = .5, + .name = "Dash", + .max_time = 1, }, { - .name = "Level_Switch", - .max_time = 1, + .name = "Level_Switch", + .max_time = 1, }, }; -PlayerData player_data = [] { +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_dash_acceleration = 15.0f; // 15 data.max_vertical_acceleration = 12.5f; return data; }(); std::vector level_data = { - { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ' } } } + { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' } } } }, + { + .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '-', '-', '-', '2' }, + { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', + ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', + ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '-', '-', '-', '2' } } }, }, - { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '2' } } }, + { + .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '-', '-', '-', '2' }, + { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', + ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', + ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '-', '-', '-', '2' } } }, }, - { .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, - { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '2' } } }, + { + .data = { { { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' }, + { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', + '0', ' ', '0', ' ' }, + { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', + ' ', '0', ' ', '0' } } }, }, - { .data = { { { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' }, - { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' }, - { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', - '0' } } }, - }, - { .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', - '2' } } }, + { + .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', + '-', '-', '-', '2' } } }, }, }; diff --git a/src/elements.cpp b/src/elements.cpp index bcb454c..8381667 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,12 +1,16 @@ #include "elements.hpp" -#include "data.hpp" -#include "globals.hpp" - #include #include +#include #include #include +#include +#include +#include + +#include "data.hpp" +#include "globals.hpp" #ifdef __APPLE__ #include #include @@ -16,7 +20,8 @@ namespace rv = ranges::views; namespace rv = std::views; #endif -Object::Object(ObjectData data) { +Object::Object(ObjectData data) +{ rect.setSize(data.size); rect.setPosition(data.pos); rect.setOutlineThickness(data.edge); @@ -24,19 +29,38 @@ Object::Object(ObjectData data) { rect.setOutlineColor(data.exterior_color); } -void Entity::set_velocity(sf::Vector2 velocity) { _velocity = velocity; } +void +Entity::set_velocity(sf::Vector2 velocity) +{ + _velocity = velocity; +} -void Entity::set_velocity_x(int velocity) { _velocity.x = velocity; } +void +Entity::set_velocity_x(int velocity) +{ + _velocity.x = velocity; +} -void Entity::set_velocity_y(int velocity) { _velocity.y = velocity; } +void +Entity::set_velocity_y(int velocity) +{ + _velocity.y = velocity; +} -sf::Vector2 Entity::get_velocity() { return _velocity; } +sf::Vector2 +Entity::get_velocity() +{ + return _velocity; +} -CollisionDir Object::find_intersection(Object& entity) { +CollisionDir +Object::find_intersection(Object& entity) +{ sf::FloatRect a = rect.getGlobalBounds(); sf::FloatRect b = entity.rect.getGlobalBounds(); auto inter = a.findIntersection(b); - if (!inter) { + if (!inter) + { return CollisionDir::None; } @@ -45,121 +69,234 @@ CollisionDir Object::find_intersection(Object& entity) { float overlapW = overlap.size.x; float overlapH = overlap.size.y; - if (overlapW < overlapH) { + 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.x < b.position.x) + ? CollisionDir::Right // a hit b from the left side + : CollisionDir::Left; // a hit b from the right side + } + else + { return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top; } } +float +Object::find_horizontal_difference_from_interesection(Object& entity) +{ + sf::FloatRect a = rect.getGlobalBounds(); + sf::FloatRect b = entity.rect.getGlobalBounds(); + auto inter = a.findIntersection(b); + if (!inter) + { + return 0; + } + + const auto overlap = *inter; + + return (a.position.x < b.position.x) + ? b.position.x - a.position.x // a hit b from the left side + : a.position.x - b.position.x; // a hit b from the right side +} + /* 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 }); +float +total_distance(int k) +{ + if (k > 0) return k + total_distance(k - 1); + else return 0; } -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) }); +float +distance_to_acceleration(int target) +{ + return roundf(-1 + sqrtf(1 + 8 * target) / 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::initialize_bounding_box() +{ + bounding_box = std::make_unique(object_data[1]); + float length = total_distance(max_dash_acceleration); + if (direction == "left") + { + bounding_box->rect.setSize({ -length, rect.getSize().y }); } + else if (direction == "right") + { + bounding_box->rect.setSize({ length, rect.getSize().y }); + } + bounding_box->rect.setPosition({ rect.getPosition().x, rect.getPosition().y }); } -void Player::handle_movment(Timer& dash) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { +void +Player::update_bounding_box() +{ + bounding_box->rect.setPosition({ rect.getPosition().x, rect.getPosition().y }); +} + +int +Player::check_bounding_box( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + int distance = 0; + /* + * The box checks the wall farthest into the hitbox and not the closest one to the player + * + * Best idea is to just check if there is a collision and set hitbox to that size and then + * see if that checks the one closest to the player + * + * Make a way to find the shortest distance on the collsion becuase the wall closest to the + * player will ahve a shorter difference_from_interesection than a one further from it will + * + * */ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + if (bounding_box->find_horizontal_difference_from_interesection(*tilemap[i][j]) > 0) + { + std::cout << "Intersection: " + << bounding_box->find_horizontal_difference_from_interesection( + *tilemap[i][j]) + << "\n"; + bounding_box->rect.setSize( + { bounding_box->find_horizontal_difference_from_interesection( + *tilemap[i][j]), + bounding_box->rect.getSize().y }); + distance = bounding_box->rect.getSize().x; + max_dash_acceleration = distance_to_acceleration(distance); + } + else if (bounding_box->find_horizontal_difference_from_interesection( + *tilemap[i][j]) <= 0 && + bounding_box->rect.getSize().x <= total_distance(15)) + { + bounding_box->rect.setSize( + { bounding_box->rect.getSize().x + 1, bounding_box->rect.getSize().y }); + max_dash_acceleration = + distance_to_acceleration(bounding_box->rect.getSize().x); + } + } + return distance; +} + +void +Player::handle_movment(Timer& dash) +{ + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) + { + direction = "right"; rect.move({ _velocity.x, 0 }); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) + { + direction = "left"; rect.move({ -_velocity.x, 0 }); } - if (dash.get_time() <= 0) { - m_dash = false; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && dash.get_time() <= 0) { - m_dash = true; - _velocity.x = dash_length; + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0) + { + // Dash order: spawn hitbox, check distance and set max_dash_acceleration + // Outside of this set max_dash_acceleration back to 15 { default } + max_dash_acceleration = player_data.max_dash_acceleration; + initialize_bounding_box(); + _velocity.x = max_dash_acceleration; dash.reset(); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) + { m_jumping = true; - _velocity.y = -max_vertical_acceleration; // push up + _velocity.y = -max_vertical_acceleration; // push up } } -void Player::handle_gravity() { - if (get_velocity().x > player_data.velocity.x) { +void +Player::handle_gravity() +{ + if (get_velocity().x > player_data.velocity.x) + { set_velocity_x(get_velocity().x - gravity); } + if (get_velocity().x <= player_data.velocity.x) + { + bounding_box.reset(); + } set_velocity_y(get_velocity().y + gravity); rect.move({ 0, get_velocity().y }); } -void Player::horizontal_no_out_of_bounds() { +void +Player::horizontal_no_out_of_bounds() +{ float right_border = WIDTH - rect.getSize().x; - if (rect.getPosition().x > right_border) { + if (rect.getPosition().x > right_border) + { rect.setPosition({ right_border, rect.getPosition().y }); } - if (rect.getPosition().x < std::abs(rect.getOutlineThickness())) { + if (rect.getPosition().x < std::abs(rect.getOutlineThickness())) + { rect.setPosition({ 0, rect.getPosition().y }); } } -void Player::vertical_no_out_of_bounds(Entity& base) { +void +Player::vertical_no_out_of_bounds(Entity& base) +{ float groundY = base.rect.getPosition().y - base.rect.getSize().y - rect.getSize().y - base.rect.getOutlineThickness() - rect.getOutlineThickness(); - if (rect.getPosition().y >= groundY) { + if (rect.getPosition().y >= groundY) + { rect.setPosition({ rect.getPosition().x, groundY }); set_velocity_y(0); m_jumping = false; } } -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); +bool +Player::get_horizontal_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + auto collision = tilemap[i][j]->find_intersection(*this); if (collision == CollisionDir::None) continue; - if (collision == CollisionDir::Left) { - rect.setPosition( - { wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y }); + if (collision == CollisionDir::Left) + { + rect.setPosition({ tilemap[i][j]->rect.getPosition().x - rect.getSize().x, + rect.getPosition().y }); return true; } - if (collision == CollisionDir::Right) { - rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, - rect.getPosition().y }); + if (collision == CollisionDir::Right) + { + rect.setPosition( + { tilemap[i][j]->rect.getPosition().x + tilemap[i][j]->rect.getSize().x, + rect.getPosition().y }); return true; } } return false; } -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]); +bool +Player::get_vertical_top_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + auto collision = find_intersection(*tilemap[i][j]); if (collision == CollisionDir::None) continue; - if (collision == CollisionDir::Top) { - rect.setPosition({ rect.getPosition().x, - wall_list[i][j]->rect.getSize().y + wall_list[i][j]->rect.getPosition().y }); + if (collision == CollisionDir::Top) + { + rect.setPosition( + { rect.getPosition().x, tilemap[i][j]->rect.getSize().y + + tilemap[i][j]->rect.getPosition().y }); set_velocity_y(0); return true; } @@ -167,14 +304,20 @@ bool Player::get_vertical_top_collision(std::array return false; } -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]); +bool +Player::get_vertical_bottom_collision( + std::array, MAX_COLS>, MAX_ROWS>& tilemap) +{ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + auto collision = find_intersection(*tilemap[i][j]); if (collision == CollisionDir::None) continue; - if (collision == CollisionDir::Bottom && get_velocity().y > 0) { + if (collision == CollisionDir::Bottom && get_velocity().y > 0) + { rect.setPosition( - { rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y }); + { rect.getPosition().x, + tilemap[i][j]->rect.getPosition().y - rect.getSize().y }); m_jumping = false; set_velocity_y(0); return true; diff --git a/src/main.cpp b/src/main.cpp index 1ea7b03..a533463 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,3 @@ -#include "data.hpp" - #include #include #include @@ -31,85 +29,100 @@ std::string assets = "/home/andrew/personal/Projects/CPP/engine/assets/"; #include "maps.hpp" #include "scheduler.hpp" -int main() { +int +main() +{ + // float dist = total_distance(12); + // std::cout << "Distance: " << dist << "\n"; + // float acc = distance_to_acceleration(total_distance(12)); + // std::cout << "Distance: " << acc << "\n"; Scheduler scheduler; Manager map_manager; - 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); + map_manager.load(level_data[BASE]); + std::atomic running { true }; + + sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default); window.setFramerateLimit(FPS); - player.initialize_bounding_box(); - map_manager.load(level_data[4].data); + Player player { player_data, GRAVITY }; + // player.initialize_bounding_box(player.get_velocity().x); + sf::Font font(assets + "pixel.ttf"); + sf::Text text(font, "0, 0", 16); text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5) }); - std::thread scheduler_thread([&] { scheduler.update(running); }); + std::jthread scheduler_thread([&] { scheduler.update(running); }); - while (window.isOpen()) { - std::cout << + while (window.isOpen()) + { window.clear(sf::Color::Black); - 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()) { + while (const std::optional event = window.pollEvent()) + { + if (event->is()) + { running = false; window.close(); } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) + { running = false; window.close(); } - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) { - 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"; - } - std::cout << "\n"; - // sf::FloatRect a = player.bounding_box.getGlobalBounds(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::C)) + { + player.bounding_box->rect.setSize( + { total_distance(player.max_dash_acceleration), + player.bounding_box->rect.getSize().y }); } - if (map_manager.get_active() >= level_data.size()) { + if (map_manager.get_active() >= level_data.size()) + { map_manager.set_active(DEFAULT); - map_manager.load(level_data[DEFAULT].data); + map_manager.load(level_data[DEFAULT]); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && scheduler.timers.at(1).below_or_zero()) { + 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.get_horizontal_collision(map_manager.tilemap); player.handle_gravity(); - if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { + std::cout << "Vel: " << player.get_velocity().x << ", " << player.get_velocity().y + << "\n"; + 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)) { - player.get_vertical_top_collision(map_manager.wall_list); + if (!player.get_vertical_bottom_collision(map_manager.tilemap)) + { + player.get_vertical_top_collision(map_manager.tilemap); + } + if (player.bounding_box != nullptr) + { + player.update_bounding_box(); + player.check_bounding_box(map_manager.tilemap); } map_manager.draw(window); window.draw(player.rect); - window.draw(player.bounding_box); + if (player.bounding_box != nullptr) + { + window.draw(player.bounding_box->rect); + } window.draw(text); - window.draw(test_object.rect); window.display(); } - scheduler_thread.join(); map_manager.clear(); return 0; diff --git a/src/maps.cpp b/src/maps.cpp index e8d4732..22a9bfb 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,11 +1,12 @@ #include "maps.hpp" +#include +#include +#include + #include "data.hpp" #include "elements.hpp" #include "globals.hpp" - -#include -#include #ifdef __APPLE__ #include #include @@ -15,79 +16,106 @@ namespace rv = ranges::views; namespace rv = std::views; #endif -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; +void +Manager::clear() +{ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + tilemap[i][j] = nullptr; } } -int Manager::get_active() { return _active; } +int +Manager::get_active() +{ + return _active; +} -void Manager::set_active(int level) { _active = level; } +void +Manager::set_active(int new_level) +{ + _active = new_level; +} -void Manager::set_next() { +void +Manager::set_next() +{ _active++; clear(); - load(level_data[_active].data); + load(level_data[_active]); } -void Manager::set_previous() { +void +Manager::set_previous() +{ _active--; clear(); - load(level_data[_active].data); + load(level_data[_active]); } -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::draw(sf::RenderWindow& window) +{ + for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) + { + if (tilemap[i][j] == nullptr) continue; + window.draw(tilemap[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))) { +void +Manager::load(LevelData level) +{ + const std::array, MAX_ROWS> map_data = level.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 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] == '0') + { + tilemap[i][j] = std::make_unique(object_data[0]); + // tilemap[i][j] = new Object { object_data[0] }; + tilemap[i][j]->rect.setPosition( + { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '2') { + if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '2') + { int start = j, end = j + 1; - 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 }); + tilemap[i][j] = std::make_unique(object_data[0]); + tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1), + tilemap[i][j]->rect.getSize().y }); + tilemap[i][j]->rect.setPosition( + { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '2') { + if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '2') + { int start = i, end = i + 1; - 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 }); + tilemap[i][j] = std::make_unique(object_data[0]); + tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x, + tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); + tilemap[i][j]->rect.setPosition( + { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '-') { + if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '-') + { 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 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 }); + tilemap[i][j] = std::make_unique(object_data[0]); + tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1), + tilemap[i][j]->rect.getSize().y }); + tilemap[i][j]->rect.setPosition( + { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); } - if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '|') { + if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '|') + { 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 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 }); + tilemap[i][j] = std::make_unique(object_data[0]); + tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x, + tilemap[i][j]->rect.getSize().y * float(end - start + 1) }); + tilemap[i][j]->rect.setPosition( + { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i }); } } } diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 1892f0e..f9c3141 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -1,47 +1,80 @@ #include "scheduler.hpp" -#include "data.hpp" #include #include #include #include -Scheduler::Scheduler() { +#include "data.hpp" + +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; } +bool +Timer::below_or_zero() +{ + if (time <= 0) { + return true; + } return false; } -float Timer::get_time() { return time; } +float +Timer::get_time() +{ + return time; +} -std::string Timer::get_name() { return name; } +std::string +Timer::get_name() +{ + return name; +} -void Timer::reset() { time = max_time; } +void +Timer::reset() +{ + time = max_time; +} -void Timer::update() { if(!below_or_zero()) { time--; } } +void +Timer::update() +{ + if (!below_or_zero()) { + time--; + } +} -int Scheduler::pull(std::string name) { +int +Scheduler::pull(std::string name) +{ try { - for(int i = 0; i < timers.size(); i++) { - if(timers[i].get_name() == name) { return i; } + for (int i = 0; i < timers.size(); i++) { + if (timers[i].get_name() == name) { + return i; + } } throw -1; - } catch (int x) { + } + 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 0; } -void Scheduler::update(bool& running) { +void +Scheduler::update(std::atomic& running) +{ while (running) { std::this_thread::sleep_for(std::chrono::seconds(1)); - for(Timer &val : timers) { val.update(); } + for (Timer& val : timers) { + val.update(); + } } }