From 399ccc86178fdec84152e4e5baac075e21fb8abb Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Wed, 15 Oct 2025 20:42:26 -0400 Subject: [PATCH] added player class to simplify player creation, need to make it a child of a rect so that way I can make groups of all elements --- CMakeLists.txt | 1 + include/globals.hpp | 5 +++ include/player.hpp | 23 +++++++++++ src/main.cpp | 95 ++++++++++++++++++++++++++++++--------------- src/player.cpp | 11 ++++++ 5 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 include/globals.hpp create mode 100644 include/player.hpp create mode 100644 src/player.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bed7863..da15b14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ include_directories(include) set(SOURCES src/main.cpp + src/player.cpp ) include(FetchContent) diff --git a/include/globals.hpp b/include/globals.hpp new file mode 100644 index 0000000..b96b3a0 --- /dev/null +++ b/include/globals.hpp @@ -0,0 +1,5 @@ +#ifndef GLOBALS_HPP +#define GLOBALS_HPP +#define WIDTH 600 +#define HEIGHT 400 +#endif diff --git a/include/player.hpp b/include/player.hpp new file mode 100644 index 0000000..f901d98 --- /dev/null +++ b/include/player.hpp @@ -0,0 +1,23 @@ +#ifndef PLAYER_HPP +#define PLAYER_HPP + +#include +#include + +class Player { +public: + Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x); + sf::RectangleShape rect; + sf::Vector2 m_size; + sf::Vector2 m_velocity; + float m_acceleration; + float m_jump_strength; + float m_thickness; + bool m_jumping; + sf::Vector2 get_velocity(); + bool get_jumping(); + +// private: +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 31ca5e0..04b34a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,48 +1,81 @@ #include #include +#include +#include +#include "globals.hpp" +#include "player.hpp" + +struct Player_Attributes { + sf::Vector2 m_size; + const float m_gravity; + const float jump_strength; + float m_thickness; + float m_velocityY; + bool jumping; +}; int main() { - // sf::RenderWindow window(sf::VideoMode({600, 400}), "SFML window"); - sf::RenderWindow window(sf::VideoMode({600, 400}), "SFML window"); - // window.setVerticalSyncEnabled(true); + float floor_height = 2.0f; + bool jumping = false; + sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default); + Player player { { 16.0f, 16.0f}, { 40, 40}, 1.0f, 20.0f, 2.0f, 2.5 }; window.setFramerateLimit(60); + // sf::RectangleShape player({ player_size, player_size }); + sf::RectangleShape line({ WIDTH, floor_height }); - // Load a sprite to display - // const sf::Texture texture("original.jpeg"); - // sf::Sprite sprite(texture); + /* TODO + * Move these functions to the player + */ + player.rect.setFillColor(sf::Color::Black); + player.rect.setOutlineThickness(2); // Needs to be the same as floor height to be properly on the line; else it will just show up above the line or in the line + player.rect.setOutlineColor(sf::Color::White); + player.rect.setPosition({40, 40}); - // Create a graphical text to display - // const sf::Font font("MapleMono-Regular.ttf"); - // sf::Text text(font, "Hello SFML", 50); + line.setPosition({ 0, HEIGHT - 50}); - // Load a music to play - sf::RectangleShape player({40, 40}); - player.setFillColor(sf::Color::Black); - player.setOutlineThickness(2); - player.setOutlineColor(sf::Color::White); - player.setPosition({40, 40}); - - // Start the game loop while (window.isOpen()) { - // Process events - while (const std::optional event = window.pollEvent()) - { - // Close window: exit + window.clear(sf::Color::Black); + while (const std::optional event = window.pollEvent()) { if (event->is()) window.close(); } - - // Clear screen - // window.clear(); - - // Draw the sprite - window.clear(sf::Color::Black); - if(player.getPosition().x > 400) { - player.setPosition({40, 40}); + /* TODO + * Move these to a player movement handler + * + * */ + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { + player.rect.move({2.5f, 0}); + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { + player.rect.move({-2.5f, 0}); + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { + player.rect.move({0, -2.5}); + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) { + player.rect.move({0, 2.5}); + } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { + window.close(); } - player.move({1.0f, 0}); - window.draw(player); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !player.m_jumping) { + player.m_jumping = true; + player.m_velocity.y = -player.m_jump_strength; // push up + } + + /* TODO + * Move these to a player update loop to move + * + * */ + player.m_velocity.y += player.m_acceleration; // always apply gravity + player.rect.move({0, player.m_velocity.y}); + + // float groundY = line.getPosition().y - floor_height - player.rect.m_size.x; + float groundY = line.getPosition().y - floor_height - 16; + if (player.rect.getPosition().y >= groundY) { + player.rect.setPosition({player.rect.getPosition().x, groundY}); + player.m_velocity.y = 0; + player.m_jumping = false; + } + window.draw(line); + window.draw(player.rect); window.display(); } } diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..6cb50f1 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,11 @@ +#include "player.hpp" +#include + +Player::Player(sf::Vector2 size, sf::Vector2 starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x) { + rect.setSize(size); + rect.setPosition(starting_position); + m_acceleration = acceleration; + m_jump_strength = initial_acceleration; + m_thickness = thickness; + m_velocity.x = velocity_x; +}