player bounding box almost there

This commit is contained in:
Andrew Haynes Macbook
2026-01-23 09:22:39 -05:00
parent 06d3f27b1e
commit a3589fd93d
12 changed files with 679 additions and 490 deletions
+47 -38
View File
@@ -1,47 +1,56 @@
--- ---
# Base Style # Base Style
BasedOnStyle: LLVM BasedOnStyle: Google
Standard: Latest Standard: Latest
ColumnLimit: 120 ColumnLimit: 100
ReflowComments: true
SortIncludes: true
IncludeBlocks: Regroup
# Indentation
UseTab: Always UseTab: Always
TabWidth: 8 TabWidth: 8
IndentWidth: 8 IndentWidth: 8
ContinuationIndentWidth: 8 BreakBeforeBraces: Allman
AlignAfterOpenBracket: Align # BreakBeforeBraces: Stroustrup
PointerAlignment: Left
ReferenceAlignment: Left
# Braces
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: false
AfterClass: false
AfterControlStatement: false
AfterNamespace: false
IndentBraces: false
# Containers & lists
SpacesInContainerLiterals: true
Cpp11BracedListStyle: false
SpaceBeforeCpp11BracedList: true SpaceBeforeCpp11BracedList: true
# Control statements & loops
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
SpaceBeforeRangeBasedForLoopColon: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
RemoveEmptyLinesInUnwrappedLines: true
SeparateDefinitionBlocks: Always SeparateDefinitionBlocks: Always
Cpp11BracedListStyle: false
BreakAfterReturnType: TopLevel
AlignTrailingComments:
Kind: Always
# OverEmptyLines: 2
ReflowComments: Always
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
# RemoveEmptyLinesInUnwrappedLines: true
# Preprocessor # ReflowComments: true
IndentPPDirectives: AfterHash # SortIncludes: true
PPIndentWidth: 0 # 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
-101
View File
@@ -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)
+14 -8
View File
@@ -10,8 +10,7 @@
#define DEFAULT 1 #define DEFAULT 1
#define PLATFORM1 2 #define PLATFORM1 2
#define CHECKERED 3 #define CHECKERED 3
#define BASE 4
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
@@ -19,7 +18,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
struct ObjectData { #include "globals.hpp"
struct ObjectData
{
sf::Vector2<float> size { TILESIZE, TILESIZE }; sf::Vector2<float> size { TILESIZE, TILESIZE };
sf::Vector2<float> pos { TILESIZE, TILESIZE }; sf::Vector2<float> pos { TILESIZE, TILESIZE };
sf::Color interior_color { sf::Color::Black }; sf::Color interior_color { sf::Color::Black };
@@ -27,20 +29,24 @@ struct ObjectData {
float edge { -2.0f }; float edge { -2.0f };
}; };
struct EntityData : ObjectData { struct EntityData : ObjectData
{
sf::Vector2<float> velocity { 0, 0 }; sf::Vector2<float> velocity { 0, 0 };
}; };
struct PlayerData : EntityData { struct PlayerData : EntityData
float dash_length { 0.0f }; {
float max_dash_acceleration { 0.0f };
float max_vertical_acceleration { 0.0f }; float max_vertical_acceleration { 0.0f };
}; };
struct LevelData { struct LevelData
{
std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {}; std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {};
}; };
struct TimerData { struct TimerData
{
std::string name { "NULL" }; std::string name { "NULL" };
float max_time { 1.f }; float max_time { 1.f };
}; };
+43 -18
View File
@@ -1,28 +1,39 @@
#ifndef PLAYER_HPP #ifndef PLAYER_HPP
#define PLAYER_HPP #define PLAYER_HPP
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
#include <memory>
#include "data.hpp" #include "data.hpp"
#include "globals.hpp" #include "globals.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include <SFML/Graphics/Color.hpp> enum class CollisionDir
#include <SFML/Graphics/RectangleShape.hpp> {
#include <SFML/System/Vector2.hpp> None,
#include <SFML/Graphics/CircleShape.hpp> Left,
Right,
enum class CollisionDir { None, Left, Right, Top, Bottom }; Top,
Bottom
};
class Entity; class Entity;
class Object; class Object;
class Player; class Player;
class Object { class Object
{
public: public:
Object(ObjectData data); Object(ObjectData data);
CollisionDir find_intersection(Object& entity); CollisionDir find_intersection(Object& entity);
float find_horizontal_difference_from_interesection(Object& entity);
sf::RectangleShape rect; sf::RectangleShape rect;
}; };
class Entity : public Object { class Entity : public Object
{
public: public:
Entity(EntityData data) : Object(data), _velocity(data.velocity) {}; Entity(EntityData data) : Object(data), _velocity(data.velocity) {};
void set_velocity(sf::Vector2f velocity); void set_velocity(sf::Vector2f velocity);
@@ -34,30 +45,44 @@ class Entity : public Object {
sf::Vector2f _velocity; sf::Vector2f _velocity;
}; };
class Player : public Entity { class Player : public Entity
{
public: public:
Player(PlayerData data, float gravity) : Entity(data), dash_length(data.dash_length), max_vertical_acceleration(data.max_vertical_acceleration), gravity(gravity) {}; 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_vertical_acceleration;
float dash_length; float max_dash_acceleration;
float gravity; float gravity;
bool m_jumping { false }; bool m_jumping { false };
bool m_dash { false }; std::unique_ptr<Object> bounding_box;
sf::RectangleShape bounding_box { { 0, 100 } };
void initialize_bounding_box(); void initialize_bounding_box();
void update_bounding_box(); void update_bounding_box();
void check_bounding_box(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list); int check_bounding_box(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
void handle_movment(Timer& dash); void handle_movment(Timer& dash);
void handle_gravity(); void handle_gravity();
bool get_jumping(); bool get_jumping();
void horizontal_no_out_of_bounds(); void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds(Entity& base); void vertical_no_out_of_bounds(Entity& base);
bool get_horizontal_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>); bool get_horizontal_collision(
bool get_vertical_top_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
bool get_vertical_bottom_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>); bool get_vertical_top_collision(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
bool get_vertical_bottom_collision(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
private: private:
bool get_vertical_bottom_collision_by_offset(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
sf::Vector2f _min_velocity { 0, 0 }; sf::Vector2f _min_velocity { 0, 0 };
std::string direction;
}; };
float
total_distance(int k);
float
distance_to_acceleration(int target);
#endif #endif
+9 -6
View File
@@ -1,22 +1,25 @@
#ifndef MAPS_HPP #ifndef MAPS_HPP
#define MAPS_HPP #define MAPS_HPP
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
#include <memory>
#include "data.hpp"
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp" #include "globals.hpp"
#include <SFML/Graphics/RenderWindow.hpp> class Manager
#include <array> {
class Manager {
public: public:
void clear(); void clear();
void load(const std::array<std::array<char, MAX_COLS>, MAX_ROWS> map_data); void load(LevelData level);
void draw(sf::RenderWindow& window); void draw(sf::RenderWindow& window);
void set_next(); void set_next();
void set_previous(); void set_previous();
int get_active(); int get_active();
void set_active(int level); void set_active(int level);
std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list = {}; std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS> tilemap = {};
private: private:
int _active = 1; int _active = 1;
+2 -1
View File
@@ -3,6 +3,7 @@
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <atomic>
#include <vector> #include <vector>
#include "data.hpp" #include "data.hpp"
@@ -24,7 +25,7 @@ private:
struct Scheduler { struct Scheduler {
Scheduler(); Scheduler();
std::vector<Timer> timers; std::vector<Timer> timers;
void update(bool& running); void update(std::atomic<bool>& running);
int pull(std::string name); int pull(std::string name);
}; };
-40
View File
@@ -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)
+145 -76
View File
@@ -1,12 +1,17 @@
#include "data.hpp" #include "data.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <vector> #include <vector>
#include "globals.hpp"
std::vector<ObjectData> object_data { std::vector<ObjectData> object_data {
{}, {},
{
.interior_color = sf::Color::Transparent,
.exterior_color = sf::Color::White,
.edge = -1,
},
}; };
/* /*
@@ -28,7 +33,7 @@ std::vector<TimerData> timer_data {
{}, {},
{ {
.name = "Dash", .name = "Dash",
.max_time = .5, .max_time = 1,
}, },
{ {
.name = "Level_Switch", .name = "Level_Switch",
@@ -36,90 +41,154 @@ std::vector<TimerData> timer_data {
}, },
}; };
PlayerData player_data = [] { PlayerData player_data = []
{
PlayerData data; PlayerData data;
data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f }; data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f };
data.edge = -1.0f; data.edge = -1.0f;
data.velocity = { 3.0f, 0.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; data.max_vertical_acceleration = 12.5f;
return data; return data;
}(); }();
std::vector<LevelData> level_data = { std::vector<LevelData> 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' }, .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, '-', '-', '-', '2' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2',
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', ' ', ' ', ' ', '|' },
'2' } } }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ',
' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ',
' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '2' } } },
}, },
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, {
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, .data = { { { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0',
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, ' ', '0', ' ', '0' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ',
{ '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, '0', ' ', '0', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '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', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, ' ', '0', ' ', '0' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, { ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ',
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '0', ' ', '0', ' ' },
'2' } } }, { '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0',
' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ',
'0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0',
' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ',
'0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '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', ' ' }, .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' } } }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
}, ' ', ' ', ' ', ' ' },
{ .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, '-', '-', '-', '2' } } },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
}, },
}; };
+211 -68
View File
@@ -1,12 +1,16 @@
#include "elements.hpp" #include "elements.hpp"
#include "data.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <cmath>
#include <iostream>
#include <memory>
#include "data.hpp"
#include "globals.hpp"
#ifdef __APPLE__ #ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp> #include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp> #include <range/v3/view/iota.hpp>
@@ -16,7 +20,8 @@ namespace rv = ranges::views;
namespace rv = std::views; namespace rv = std::views;
#endif #endif
Object::Object(ObjectData data) { Object::Object(ObjectData data)
{
rect.setSize(data.size); rect.setSize(data.size);
rect.setPosition(data.pos); rect.setPosition(data.pos);
rect.setOutlineThickness(data.edge); rect.setOutlineThickness(data.edge);
@@ -24,19 +29,38 @@ Object::Object(ObjectData data) {
rect.setOutlineColor(data.exterior_color); rect.setOutlineColor(data.exterior_color);
} }
void Entity::set_velocity(sf::Vector2<float> velocity) { _velocity = velocity; } void
Entity::set_velocity(sf::Vector2<float> 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<float> Entity::get_velocity() { return _velocity; } sf::Vector2<float>
Entity::get_velocity()
{
return _velocity;
}
CollisionDir Object::find_intersection(Object& entity) { CollisionDir
Object::find_intersection(Object& entity)
{
sf::FloatRect a = rect.getGlobalBounds(); sf::FloatRect a = rect.getGlobalBounds();
sf::FloatRect b = entity.rect.getGlobalBounds(); sf::FloatRect b = entity.rect.getGlobalBounds();
auto inter = a.findIntersection(b); auto inter = a.findIntersection(b);
if (!inter) { if (!inter)
{
return CollisionDir::None; return CollisionDir::None;
} }
@@ -45,106 +69,213 @@ CollisionDir Object::find_intersection(Object& entity) {
float overlapW = overlap.size.x; float overlapW = overlap.size.x;
float overlapH = overlap.size.y; float overlapH = overlap.size.y;
if (overlapW < overlapH) { if (overlapW < overlapH)
{
// Horizontal collision // Horizontal collision
return (a.position.x < b.position.x) ? CollisionDir::Right // a hit b from the left side 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 : CollisionDir::Left; // a hit b from the right side
} else { }
else
{
return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top; 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 /* Bounding Box
* Create on dash in front of player and check if the box collides with a wall, * 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 * if so find the difference of the wall and the player and set the dash_distance
* to difference. * to difference.
* */ * */
void Player::initialize_bounding_box() { float
bounding_box.setOutlineColor(sf::Color::White); total_distance(int k)
bounding_box.setFillColor(sf::Color::Transparent); {
bounding_box.setOutlineThickness(-1); if (k > 0) return k + total_distance(k - 1);
bounding_box.setSize({ dash_length, rect.getSize().y }); else return 0;
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() { float
distance_to_acceleration(int target)
// Player and box positive distances are 108 in both x and y {
bounding_box.setOrigin( return roundf(-1 + sqrtf(1 + 8 * target) / 2);
{ -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<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list) { void
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Player::initialize_bounding_box()
if (wall_list[i][j] == nullptr) continue; {
bounding_box = std::make_unique<Object>(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) { void
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { Player::update_bounding_box()
{
bounding_box->rect.setPosition({ rect.getPosition().x, rect.getPosition().y });
}
int
Player::check_bounding_box(
std::array<std::array<std::unique_ptr<Object>, 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 }); 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 }); rect.move({ -_velocity.x, 0 });
} }
if (dash.get_time() <= 0) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0)
m_dash = false; {
} // Dash order: spawn hitbox, check distance and set max_dash_acceleration
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && dash.get_time() <= 0) { // Outside of this set max_dash_acceleration back to 15 { default }
m_dash = true; max_dash_acceleration = player_data.max_dash_acceleration;
_velocity.x = dash_length; initialize_bounding_box();
_velocity.x = max_dash_acceleration;
dash.reset(); 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; m_jumping = true;
_velocity.y = -max_vertical_acceleration; // push up _velocity.y = -max_vertical_acceleration; // push up
} }
} }
void Player::handle_gravity() { void
if (get_velocity().x > player_data.velocity.x) { Player::handle_gravity()
{
if (get_velocity().x > player_data.velocity.x)
{
set_velocity_x(get_velocity().x - gravity); 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); set_velocity_y(get_velocity().y + gravity);
rect.move({ 0, get_velocity().y }); 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; 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 }); 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 }); 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 - float groundY = base.rect.getPosition().y - base.rect.getSize().y - rect.getSize().y -
base.rect.getOutlineThickness() - rect.getOutlineThickness(); base.rect.getOutlineThickness() - rect.getOutlineThickness();
if (rect.getPosition().y >= groundY) { if (rect.getPosition().y >= groundY)
{
rect.setPosition({ rect.getPosition().x, groundY }); rect.setPosition({ rect.getPosition().x, groundY });
set_velocity_y(0); set_velocity_y(0);
m_jumping = false; m_jumping = false;
} }
} }
bool Player::get_horizontal_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list) { bool
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Player::get_horizontal_collision(
if (wall_list[i][j] == nullptr) continue; std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
auto collision = wall_list[i][j]->find_intersection(*this); {
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::None) continue;
if (collision == CollisionDir::Left) { if (collision == CollisionDir::Left)
rect.setPosition( {
{ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y }); rect.setPosition({ tilemap[i][j]->rect.getPosition().x - rect.getSize().x,
rect.getPosition().y });
return true; return true;
} }
if (collision == CollisionDir::Right) { if (collision == CollisionDir::Right)
rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, {
rect.setPosition(
{ tilemap[i][j]->rect.getPosition().x + tilemap[i][j]->rect.getSize().x,
rect.getPosition().y }); rect.getPosition().y });
return true; return true;
} }
@@ -152,14 +283,20 @@ bool Player::get_horizontal_collision(std::array<std::array<Object*, MAX_COLS>,
return false; return false;
} }
bool Player::get_vertical_top_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list) { bool
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Player::get_vertical_top_collision(
if (wall_list[i][j] == nullptr) continue; std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
auto collision = find_intersection(*wall_list[i][j]); {
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::None) continue;
if (collision == CollisionDir::Top) { if (collision == CollisionDir::Top)
rect.setPosition({ rect.getPosition().x, {
wall_list[i][j]->rect.getSize().y + wall_list[i][j]->rect.getPosition().y }); rect.setPosition(
{ rect.getPosition().x, tilemap[i][j]->rect.getSize().y +
tilemap[i][j]->rect.getPosition().y });
set_velocity_y(0); set_velocity_y(0);
return true; return true;
} }
@@ -167,14 +304,20 @@ bool Player::get_vertical_top_collision(std::array<std::array<Object*, MAX_COLS>
return false; return false;
} }
bool Player::get_vertical_bottom_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list) { bool
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Player::get_vertical_bottom_collision(
if (wall_list[i][j] == nullptr) continue; std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
auto collision = find_intersection(*wall_list[i][j]); {
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::None) continue;
if (collision == CollisionDir::Bottom && get_velocity().y > 0) { if (collision == CollisionDir::Bottom && get_velocity().y > 0)
{
rect.setPosition( 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; m_jumping = false;
set_velocity_y(0); set_velocity_y(0);
return true; return true;
+52 -39
View File
@@ -1,5 +1,3 @@
#include "data.hpp"
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
@@ -31,85 +29,100 @@ std::string assets = "/home/andrew/personal/Projects/CPP/engine/assets/";
#include "maps.hpp" #include "maps.hpp"
#include "scheduler.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; Scheduler scheduler;
Manager map_manager; Manager map_manager;
sf::Font font(assets + "pixel.ttf"); map_manager.load(level_data[BASE]);
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);
std::atomic<bool> running { true };
sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default);
window.setFramerateLimit(FPS); window.setFramerateLimit(FPS);
player.initialize_bounding_box(); Player player { player_data, GRAVITY };
map_manager.load(level_data[4].data); // 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) }); 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()) { while (window.isOpen())
std::cout << {
window.clear(sf::Color::Black); 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())); text.setString(std::to_string(scheduler.timers.at(0).get_time()));
while (const std::optional event = window.pollEvent()) { while (const std::optional event = window.pollEvent())
if (event->is<sf::Event::Closed>()) { {
if (event->is<sf::Event::Closed>())
{
running = false; running = false;
window.close(); window.close();
} }
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
{
running = false; running = false;
window.close(); window.close();
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::C))
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) { {
for (int i = 0; i < player.bounding_box.getPointCount(); i++) { player.bounding_box->rect.setSize(
std::cout << "(" << player.bounding_box.getPoint(i).x << ", " << player.bounding_box.getPoint(i).y { total_distance(player.max_dash_acceleration),
<< ")\n"; player.bounding_box->rect.getSize().y });
}
std::cout << "\n";
// sf::FloatRect a = player.bounding_box.getGlobalBounds();
} }
if (map_manager.get_active() >= level_data.size()) { if (map_manager.get_active() >= level_data.size())
{
map_manager.set_active(DEFAULT); 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(); map_manager.set_next();
scheduler.timers.at(1).reset(); scheduler.timers.at(1).reset();
} }
player.update_bounding_box();
player.handle_movment(scheduler.timers.at(scheduler.pull("Dash"))); 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(); 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); player.set_velocity_y((TILESIZE / 2) - GRAVITY);
} }
if (!player.get_vertical_bottom_collision(map_manager.wall_list)) { if (!player.get_vertical_bottom_collision(map_manager.tilemap))
player.get_vertical_top_collision(map_manager.wall_list); {
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); map_manager.draw(window);
window.draw(player.rect); 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(text);
window.draw(test_object.rect);
window.display(); window.display();
} }
scheduler_thread.join();
map_manager.clear(); map_manager.clear();
return 0; return 0;
+75 -47
View File
@@ -1,11 +1,12 @@
#include "maps.hpp" #include "maps.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
#include <memory>
#include "data.hpp" #include "data.hpp"
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp" #include "globals.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
#ifdef __APPLE__ #ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp> #include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp> #include <range/v3/view/iota.hpp>
@@ -15,79 +16,106 @@ namespace rv = ranges::views;
namespace rv = std::views; namespace rv = std::views;
#endif #endif
void Manager::clear() { void
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Manager::clear()
delete wall_list[i][j]; {
wall_list[i][j] = nullptr; 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++; _active++;
clear(); clear();
load(level_data[_active].data); load(level_data[_active]);
} }
void Manager::set_previous() { void
Manager::set_previous()
{
_active--; _active--;
clear(); clear();
load(level_data[_active].data); load(level_data[_active]);
} }
void Manager::draw(sf::RenderWindow& window) { void
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Manager::draw(sf::RenderWindow& window)
if (wall_list[i][j] == nullptr) continue; {
window.draw(wall_list[i][j]->rect); 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<std::array<char, MAX_COLS>, MAX_ROWS> map_data) { void
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) { Manager::load(LevelData level)
{
const std::array<std::array<char, MAX_COLS>, 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] == ' ') continue;
if (map_data[i][j] == '0') { 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, tilemap[i][j] = std::make_unique<Object>(object_data[0]);
wall_list[i][j]->rect.getPosition().y * i }); // 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; int start = j, end = j + 1;
wall_list[i][j] = new Object { object_data[0] }; tilemap[i][j] = std::make_unique<Object>(object_data[0]);
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1), tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1),
wall_list[i][j]->rect.getSize().y }); tilemap[i][j]->rect.getSize().y });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.setPosition(
wall_list[i][j]->rect.getPosition().y * i }); { 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; int start = i, end = i + 1;
wall_list[i][j] = new Object { object_data[0] }; tilemap[i][j] = std::make_unique<Object>(object_data[0]);
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x, tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x,
wall_list[i][j]->rect.getSize().y * float(end - start + 1) }); tilemap[i][j]->rect.getSize().y * float(end - start + 1) });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.setPosition(
wall_list[i][j]->rect.getPosition().y * i }); { 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; int start = j, end = j;
while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++; while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++;
if (map_data[i][end + 1] == '2') end++; if (map_data[i][end + 1] == '2') end++;
wall_list[i][j] = new Object { object_data[0] }; tilemap[i][j] = std::make_unique<Object>(object_data[0]);
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1), tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x * float(end - start + 1),
wall_list[i][j]->rect.getSize().y }); tilemap[i][j]->rect.getSize().y });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.setPosition(
wall_list[i][j]->rect.getPosition().y * i }); { 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; int start = i, end = i;
while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++; while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++;
if (map_data[end + 1][j] == '2') end++; if (map_data[end + 1][j] == '2') end++;
wall_list[i][j] = new Object { object_data[0] }; tilemap[i][j] = std::make_unique<Object>(object_data[0]);
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x, tilemap[i][j]->rect.setSize({ tilemap[i][j]->rect.getSize().x,
wall_list[i][j]->rect.getSize().y * float(end - start + 1) }); tilemap[i][j]->rect.getSize().y * float(end - start + 1) });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.setPosition(
wall_list[i][j]->rect.getPosition().y * i }); { tilemap[i][j]->rect.getPosition().x * j, tilemap[i][j]->rect.getPosition().y * i });
} }
} }
} }
+47 -14
View File
@@ -1,36 +1,65 @@
#include "scheduler.hpp" #include "scheduler.hpp"
#include "data.hpp"
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <vector> #include <vector>
Scheduler::Scheduler() { #include "data.hpp"
Scheduler::Scheduler()
{
timers.push_back(timer_data[DASH]); timers.push_back(timer_data[DASH]);
timers.push_back(timer_data[LEVEL_SWITCH]); timers.push_back(timer_data[LEVEL_SWITCH]);
} }
bool Timer::below_or_zero() { bool
if(time <= 0) { return true; } Timer::below_or_zero()
{
if (time <= 0) {
return true;
}
return false; 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 { try {
for(int i = 0; i < timers.size(); i++) { for (int i = 0; i < timers.size(); i++) {
if(timers[i].get_name() == name) { return i; } if (timers[i].get_name() == name) {
return i;
}
} }
throw -1; throw -1;
} catch (int x) { }
catch (int x) {
std::cout << "No valid timer with name: (" << name << ") as a value\n"; std::cout << "No valid timer with name: (" << name << ") as a value\n";
std::cout << "Using defualt timer values\n"; std::cout << "Using defualt timer values\n";
return -1; return -1;
@@ -39,9 +68,13 @@ int Scheduler::pull(std::string name) {
return 0; return 0;
} }
void Scheduler::update(bool& running) { void
Scheduler::update(std::atomic<bool>& running)
{
while (running) { while (running) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
for(Timer &val : timers) { val.update(); } for (Timer& val : timers) {
val.update();
}
} }
} }