From 8f11e24339fd80841074fad5e5950a77b8c986b1 Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Thu, 23 Oct 2025 21:39:52 -0400 Subject: [PATCH] wip --- include/elements.hpp | 3 +++ src/elements.cpp | 10 ++++++++++ src/main.cpp | 8 +------- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index 7e3ef29..51785d0 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -31,12 +31,15 @@ public: float m_jump_strength; bool m_jumping; void handle_movment(); + void handle_gravity(); bool get_jumping(); void horizontal_no_out_of_bounds(Entity &base); void vertical_no_out_of_bounds(Entity &base); bool get_horizontal_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]); bool get_vertical_top_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]); bool get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]); +private: + sf::Vector2f _min_velocity; }; #endif diff --git a/src/elements.cpp b/src/elements.cpp index b8aa571..355eb2d 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -59,6 +59,7 @@ CollisionDir Entity::find_intersection(Entity &entity) { } Player::Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, sf::Color inside_color, sf::Color border_color) : Entity(size, pos, velocity, border_thickness, inside_color, border_color) { + _min_velocity = velocity; m_acceleration = gravity; m_jump_strength = max_acceleration; } @@ -76,6 +77,15 @@ void Player::handle_movment() { } +void Player::handle_gravity() { + if(get_velocity().x > _min_velocity.x) { + set_velocity_x(get_velocity().x - m_acceleration); + } + + set_velocity_y(get_velocity().y + m_acceleration); + rect.move({0, get_velocity().y}); +} + void Player::horizontal_no_out_of_bounds(Entity &base) { float right_border = WIDTH - rect.getSize().x; if(rect.getPosition().x > right_border) { diff --git a/src/main.cpp b/src/main.cpp index e2697c5..11c6814 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,13 +100,7 @@ int main() { player.get_horizontal_collision(wall_list); player.horizontal_no_out_of_bounds(line); - if(player.get_velocity().x > 4) { - player.set_velocity_x(player.get_velocity().x - player.m_acceleration); - } - - // Handle Vertical movement and collisions - player.set_velocity_y(player.get_velocity().y + player.m_acceleration); - player.rect.move({0, player.get_velocity().y}); + player.handle_gravity(); if(!player.get_vertical_bottom_collision(wall_list)) { player.get_vertical_top_collision(wall_list);