wip
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ include_directories(include)
|
|||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/player.cpp
|
src/elements.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -4,20 +4,33 @@
|
|||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
class Entity {
|
||||||
|
protected:
|
||||||
|
sf::RectangleShape rect;
|
||||||
|
sf::Color inside_color;
|
||||||
|
sf::Color border_color;
|
||||||
|
float top_left;
|
||||||
|
float bottom_left;
|
||||||
|
float top_right;
|
||||||
|
float bottom_right;
|
||||||
|
float acceleration;
|
||||||
|
float border_thickness;
|
||||||
|
};
|
||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
public:
|
public:
|
||||||
Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x);
|
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);
|
||||||
sf::RectangleShape rect;
|
sf::RectangleShape rect;
|
||||||
sf::Vector2<float> m_size;
|
sf::Vector2<float> m_size;
|
||||||
sf::Vector2<float> m_velocity;
|
sf::Vector2<float> m_velocity;
|
||||||
float m_acceleration;
|
float m_acceleration;
|
||||||
float m_jump_strength;
|
float m_jump_strength;
|
||||||
|
float m_thickness;
|
||||||
bool m_jumping;
|
bool m_jumping;
|
||||||
void handle_movment();
|
void handle_movment();
|
||||||
sf::Vector2<float> get_velocity();
|
sf::Vector2<float> get_velocity();
|
||||||
bool get_jumping();
|
bool get_jumping();
|
||||||
|
|
||||||
// private:
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,15 +1,27 @@
|
|||||||
#include "player.hpp"
|
#include "elements.hpp"
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.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.setSize(size);
|
||||||
rect.setPosition(starting_position);
|
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.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_acceleration = acceleration;
|
||||||
m_jump_strength = initial_acceleration;
|
m_jump_strength = initial_acceleration;
|
||||||
m_velocity.x = velocity_x;
|
m_velocity.x = velocity_x;
|
||||||
|
m_thickness = thickness;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::handle_movment() {
|
void Player::handle_movment() {
|
||||||
+80
-5
@@ -1,30 +1,56 @@
|
|||||||
|
#include <SFML/Graphics/CircleShape.hpp>
|
||||||
#include <SFML/Audio.hpp>
|
#include <SFML/Audio.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
#include <SFML/System/Angle.hpp>
|
#include <SFML/System/Angle.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <iostream>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "player.hpp"
|
#include "elements.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
float floor_height = 2.0f;
|
float floor_height = 2.0f;
|
||||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
|
|
||||||
Player player { { 16.0f, 16.0f}, { 40, 40}, 1.0f, 20.0f, 2.0f, 4 };
|
struct {
|
||||||
player.rect.setFillColor(sf::Color::Black);
|
sf::Vector2<float> size = { 16.0f, 16.0f };
|
||||||
player.rect.setOutlineColor(sf::Color::White);
|
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 });
|
sf::RectangleShape line({ WIDTH, floor_height });
|
||||||
|
|
||||||
line.setPosition({ 0, HEIGHT - 50});
|
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()) {
|
while (window.isOpen()) {
|
||||||
window.clear(sf::Color::Black);
|
window.clear(sf::Color::Black);
|
||||||
while (const std::optional event = window.pollEvent()) {
|
while (const std::optional event = window.pollEvent()) {
|
||||||
if (event->is<sf::Event::Closed>())
|
if (event->is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
||||||
|
vel_y = -10;
|
||||||
|
}
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
@@ -32,12 +58,61 @@ int main() {
|
|||||||
player.m_velocity.y += player.m_acceleration; // always apply gravity
|
player.m_velocity.y += player.m_acceleration; // always apply gravity
|
||||||
player.rect.move({0, player.m_velocity.y});
|
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;
|
float groundY = line.getPosition().y - floor_height - player.rect.getSize().x;
|
||||||
if (player.rect.getPosition().y >= groundY) {
|
if (player.rect.getPosition().y >= groundY) {
|
||||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
||||||
player.m_velocity.y = 0;
|
player.m_velocity.y = 0;
|
||||||
player.m_jumping = false;
|
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(player.rect);
|
||||||
window.draw(line);
|
window.draw(line);
|
||||||
window.display();
|
window.display();
|
||||||
|
|||||||
Reference in New Issue
Block a user