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:
+64
-31
@@ -1,48 +1,81 @@
|
||||
#include <SFML/Audio.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() {
|
||||
// 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<sf::Event::Closed>())
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user