wip
This commit is contained in:
@@ -1,15 +1,27 @@
|
||||
#include "player.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
|
||||
Player::Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x) {
|
||||
/* Entitiy
|
||||
* Parent class
|
||||
* Holds all 4 corners of rect
|
||||
* Bounding checks for if a rect1_top_left is between rect2_top_right - rect2_top_left
|
||||
*
|
||||
*
|
||||
* */
|
||||
|
||||
Player::Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x, sf::Color inside_color, sf::Color border_color) {
|
||||
rect.setSize(size);
|
||||
rect.setPosition(starting_position);
|
||||
rect.setOutlineThickness(thickness); // 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
|
||||
rect.setFillColor(inside_color);
|
||||
rect.setOutlineColor(border_color);
|
||||
m_acceleration = acceleration;
|
||||
m_jump_strength = initial_acceleration;
|
||||
m_velocity.x = velocity_x;
|
||||
m_thickness = thickness;
|
||||
}
|
||||
|
||||
void Player::handle_movment() {
|
||||
+80
-5
@@ -1,30 +1,56 @@
|
||||
#include <SFML/Graphics/CircleShape.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/System/Angle.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <iostream>
|
||||
#include "globals.hpp"
|
||||
#include "player.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
int main() {
|
||||
float floor_height = 2.0f;
|
||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||
window.setFramerateLimit(60);
|
||||
|
||||
Player player { { 16.0f, 16.0f}, { 40, 40}, 1.0f, 20.0f, 2.0f, 4 };
|
||||
player.rect.setFillColor(sf::Color::Black);
|
||||
player.rect.setOutlineColor(sf::Color::White);
|
||||
struct {
|
||||
sf::Vector2<float> size = { 16.0f, 16.0f };
|
||||
sf::Vector2<float> pos = { 40, 40 };
|
||||
float acceleration = 1.0f;
|
||||
float max_velocity_y = 20.0f;
|
||||
float border_size = 2.0f;
|
||||
float velocity_x = 4.0f;
|
||||
} player_data;
|
||||
|
||||
Player player {
|
||||
player_data.size, player_data.pos, player_data.acceleration,
|
||||
player_data.max_velocity_y, player_data.border_size, player_data.velocity_x,
|
||||
sf::Color::Black, sf::Color::White
|
||||
};
|
||||
|
||||
sf::RectangleShape line({ WIDTH, floor_height });
|
||||
|
||||
line.setPosition({ 0, HEIGHT - 50});
|
||||
|
||||
sf::RectangleShape rect({ 4, 4 });
|
||||
rect.setFillColor(sf::Color::Black);
|
||||
rect.setOutlineColor(sf::Color::White);
|
||||
rect.setOutlineThickness(2.0f);
|
||||
rect.setPosition({ 40, 40 });
|
||||
// float vel_x = 3.0f;
|
||||
float vel_y = -20.0f;
|
||||
|
||||
while (window.isOpen()) {
|
||||
window.clear(sf::Color::Black);
|
||||
while (const std::optional event = window.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
window.close();
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
||||
vel_y = -10;
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
||||
window.close();
|
||||
}
|
||||
@@ -32,12 +58,61 @@ int main() {
|
||||
player.m_velocity.y += player.m_acceleration; // always apply gravity
|
||||
player.rect.move({0, player.m_velocity.y});
|
||||
|
||||
vel_y += 1.0f;
|
||||
rect.move({ 0, vel_y });
|
||||
|
||||
float groundY = line.getPosition().y - floor_height - player.rect.getSize().x;
|
||||
if (player.rect.getPosition().y >= groundY) {
|
||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
||||
player.m_velocity.y = 0;
|
||||
player.m_jumping = false;
|
||||
}
|
||||
float right_border = WIDTH - player.rect.getSize().x - player.m_thickness;
|
||||
if(player.rect.getPosition().x > right_border) {
|
||||
player.rect.setPosition({ right_border, player.rect.getPosition().y});
|
||||
}
|
||||
if(player.rect.getPosition().x < player.m_thickness) {
|
||||
player.rect.setPosition({ player.m_thickness, player.rect.getPosition().y});
|
||||
}
|
||||
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
|
||||
// vel_y = -20.0f;
|
||||
// // vel_y = -20.0f;
|
||||
// }
|
||||
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
|
||||
// if()
|
||||
|
||||
|
||||
float groundY_rect = line.getPosition().y - floor_height - (rect.getSize().y);
|
||||
if(rect.getPosition().x > right_border) {
|
||||
rect.setPosition({ right_border, rect.getPosition().y });
|
||||
}
|
||||
if(rect.getPosition().x < rect.getPosition().x + rect.getOutlineThickness()) {
|
||||
rect.setPosition({ rect.getOutlineThickness(), rect.getPosition().y });
|
||||
}
|
||||
if(rect.getPosition().y >= groundY_rect) {
|
||||
rect.setPosition({ rect.getPosition().x, groundY_rect });
|
||||
vel_y = 0;
|
||||
}
|
||||
//
|
||||
// auto playerBounds = player.rect.getGlobalBounds();
|
||||
// auto rectBounds = rect.getGlobalBounds();
|
||||
//
|
||||
// // Create debug rectangles
|
||||
// sf::RectangleShape playerBox(playerBounds.size);
|
||||
// playerBox.setPosition(playerBounds.position);
|
||||
// playerBox.setFillColor(sf::Color::Transparent);
|
||||
// playerBox.setOutlineColor(sf::Color::Red);
|
||||
// playerBox.setOutlineThickness(1.0f);
|
||||
//
|
||||
// sf::RectangleShape rectBox(rectBounds.size);
|
||||
// rectBox.setPosition(rectBounds.position);
|
||||
// rectBox.setFillColor(sf::Color::Transparent);
|
||||
// rectBox.setOutlineColor(sf::Color::Red);
|
||||
// rectBox.setOutlineThickness(1.0f);
|
||||
//
|
||||
// window.draw(playerBox);
|
||||
// window.draw(rectBox);
|
||||
window.draw(rect);
|
||||
window.draw(player.rect);
|
||||
window.draw(line);
|
||||
window.display();
|
||||
|
||||
Reference in New Issue
Block a user