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

This commit is contained in:
DarthMooMancer
2025-10-15 20:42:26 -04:00
parent c0377bb9f9
commit 399ccc8617
5 changed files with 104 additions and 31 deletions
+1
View File
@@ -9,6 +9,7 @@ include_directories(include)
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
src/player.cpp
) )
include(FetchContent) include(FetchContent)
+5
View File
@@ -0,0 +1,5 @@
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
#define WIDTH 600
#define HEIGHT 400
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
class Player {
public:
Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x);
sf::RectangleShape rect;
sf::Vector2<float> m_size;
sf::Vector2<float> m_velocity;
float m_acceleration;
float m_jump_strength;
float m_thickness;
bool m_jumping;
sf::Vector2<float> get_velocity();
bool get_jumping();
// private:
};
#endif
+64 -31
View File
@@ -1,48 +1,81 @@
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
#include "globals.hpp"
#include "player.hpp"
struct Player_Attributes {
sf::Vector2<float> m_size;
const float m_gravity;
const float jump_strength;
float m_thickness;
float m_velocityY;
bool jumping;
};
int main() { int main() {
// sf::RenderWindow window(sf::VideoMode({600, 400}), "SFML window"); float floor_height = 2.0f;
sf::RenderWindow window(sf::VideoMode({600, 400}), "SFML window"); bool jumping = false;
// window.setVerticalSyncEnabled(true); 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); window.setFramerateLimit(60);
// sf::RectangleShape player({ player_size, player_size });
sf::RectangleShape line({ WIDTH, floor_height });
// Load a sprite to display /* TODO
// const sf::Texture texture("original.jpeg"); * Move these functions to the player
// sf::Sprite sprite(texture); */
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 line.setPosition({ 0, HEIGHT - 50});
// const sf::Font font("MapleMono-Regular.ttf");
// sf::Text text(font, "Hello SFML", 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()) { while (window.isOpen()) {
// Process events window.clear(sf::Color::Black);
while (const std::optional event = window.pollEvent()) while (const std::optional event = window.pollEvent()) {
{
// Close window: exit
if (event->is<sf::Event::Closed>()) if (event->is<sf::Event::Closed>())
window.close(); window.close();
} }
/* TODO
// Clear screen * Move these to a player movement handler
// window.clear(); *
* */
// Draw the sprite if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
window.clear(sf::Color::Black); player.rect.move({2.5f, 0});
if(player.getPosition().x > 400) { } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
player.setPosition({40, 40}); 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}); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !player.m_jumping) {
window.draw(player); 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(); window.display();
} }
} }
+11
View File
@@ -0,0 +1,11 @@
#include "player.hpp"
#include <SFML/System/Vector2.hpp>
Player::Player(sf::Vector2<float> size, sf::Vector2<float> 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;
}