mini optimization to make making entities easier
This commit is contained in:
@@ -23,8 +23,6 @@ FetchContent_MakeAvailable(SFML)
|
||||
|
||||
add_executable(SFML_game_engine ${SOURCES})
|
||||
|
||||
# target_link_libraries(SFML_game_engine PRIVATE SFML::Graphics)
|
||||
# target_include_directories(SFML_game_engine PRIVATE include)
|
||||
target_link_libraries(SFML_game_engine
|
||||
PRIVATE
|
||||
SFML::Graphics
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,27 +5,26 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
class Entity {
|
||||
protected:
|
||||
public:
|
||||
Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, float thickness, sf::Color inside_color, sf::Color border_color);
|
||||
bool find_intersection(Entity &entity);
|
||||
sf::RectangleShape rect;
|
||||
protected:
|
||||
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 Entity {
|
||||
public:
|
||||
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::Vector2<float> m_size;
|
||||
sf::Vector2<float> m_velocity;
|
||||
float m_acceleration;
|
||||
float m_jump_strength;
|
||||
float m_thickness;
|
||||
bool m_jumping;
|
||||
void handle_movment();
|
||||
sf::Vector2<float> get_velocity();
|
||||
|
||||
+6
-2
@@ -12,16 +12,20 @@
|
||||
*
|
||||
* */
|
||||
|
||||
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) {
|
||||
|
||||
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, float thickness, 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);
|
||||
|
||||
}
|
||||
|
||||
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) : Entity(size, starting_position, thickness, inside_color, border_color) {
|
||||
m_acceleration = acceleration;
|
||||
m_jump_strength = initial_acceleration;
|
||||
m_velocity.x = velocity_x;
|
||||
m_thickness = thickness;
|
||||
}
|
||||
|
||||
void Player::handle_movment() {
|
||||
|
||||
+3
-3
@@ -67,12 +67,12 @@ int main() {
|
||||
player.m_velocity.y = 0;
|
||||
player.m_jumping = false;
|
||||
}
|
||||
float right_border = WIDTH - player.rect.getSize().x - player.m_thickness;
|
||||
float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness();
|
||||
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(player.rect.getPosition().x < player.rect.getOutlineThickness()) {
|
||||
player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y});
|
||||
}
|
||||
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
|
||||
// vel_y = -20.0f;
|
||||
|
||||
Reference in New Issue
Block a user