move towards ecs

main
Efertone 10 months ago
parent 9ea734261c
commit b217d29ac8

@ -3,7 +3,7 @@ project(playground)
set(CMAKE_CXX_STANDARD 14)
add_executable(playground src/main.cpp src/ds/Vector.cpp headers/ds/Vector.h src/game/actor/Base.cpp headers/game/actor/Base.h src/game/Game.cpp headers/game/Game.h headers/game/interface.h headers/game/type.h src/game/actor/Player.cpp headers/game/actor/Player.h src/game/actor/SimpleEnemy.cpp headers/game/actor/SimpleEnemy.h src/graphics/AnimatedSprite.cpp headers/graphics/AnimatedSprite.h)
add_executable(playground src/main.cpp src/ds/Vector.cpp headers/ds/Vector.h src/game/actor/Base.cpp headers/game/actor/Base.h src/game/Game.cpp headers/game/Game.h headers/game/interface.h headers/game/type.h src/game/actor/Player.cpp headers/game/actor/Player.h src/game/actor/SimpleEnemy.cpp headers/game/actor/SimpleEnemy.h src/game/component/AnimatedSprite.cpp headers/game/component/AnimatedSprite.h src/game/Entity.cpp headers/game/Entity.h headers/game/component/IComponent.h src/game/component/Sprite.cpp headers/game/component/Sprite.h)
set(SFML_ROOT "D:/Libraries/cpp/SFML/2.5.1-mingw")
@ -28,7 +28,6 @@ if (WIN32)
install(FILES "${DLLFile}" DESTINATION .)
endforeach()
add_dependencies(playground copy_sfml_dlls)
endif ()
install(TARGETS playground RUNTIME DESTINATION .)

@ -4,10 +4,10 @@
# Usage
# -----
#
# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main).
# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, component, network, audio, main).
# If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing.
# example:
# find_package(SFML COMPONENTS graphics window system) // find the graphics, window and system modules
# find_package(SFML COMPONENTS component window system) // find the component, window and system modules
#
# You can enforce a specific version, either MAJOR.MINOR or only MAJOR.
# If nothing is specified, the version won't be checked (i.e. any version will be accepted).
@ -38,7 +38,7 @@
# ------
#
# This script defines the following variables:
# - For each specified module XXX (system, window, graphics, network, audio, main):
# - For each specified module XXX (system, window, component, network, audio, main):
# - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found)
# - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found)
# - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary)
@ -50,7 +50,7 @@
# - SFML_DEPENDENCIES: the list of libraries SFML depends on, in case of static linking
#
# example:
# find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED)
# find_package(SFML 2 COMPONENTS system window component audio REQUIRED)
# include_directories(${SFML_INCLUDE_DIR})
# add_executable(myapp ...)
# target_link_libraries(myapp ${SFML_LIBRARIES})
@ -309,7 +309,7 @@ if(SFML_STATIC_LIBRARIES)
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})
endif()
# sfml-graphics
# sfml-component
list(FIND SFML_FIND_COMPONENTS "graphics" FIND_SFML_GRAPHICS_COMPONENT)
if(NOT ${FIND_SFML_GRAPHICS_COMPONENT} EQUAL -1)

@ -0,0 +1,21 @@
//
// Created by Efertone on 7/28/2022.
//
#ifndef PLAYGROUND_ENTITY_H
#define PLAYGROUND_ENTITY_H
#include <vector>
#include "game/interface.h"
namespace game {
class Entity {
virtual void init();
virtual void update(float);
virtual void render(sf::RenderTarget*);
};
}
#endif //PLAYGROUND_ENTITY_H

@ -8,7 +8,7 @@
#include <SFML/Graphics.hpp>
#include "ds/Vector.h"
#include "game/interface.h"
#include "graphics/AnimatedSprite.h"
#include "game/component/AnimatedSprite.h"
namespace game {
namespace actor {
@ -48,15 +48,15 @@ namespace game {
AnimationState m_animation_state;
graphics::AnimatedSprite m_animation_idle;
graphics::AnimatedSprite m_animation_special;
graphics::AnimatedSprite m_animation_walk;
graphics::AnimatedSprite* m_currentAnimation;
game::component::AnimatedSprite m_animation_idle;
game::component::AnimatedSprite m_animation_special;
game::component::AnimatedSprite m_animation_walk;
game::component::AnimatedSprite* m_currentAnimation;
void initHudElements();
void init(ds::Vector position, sf::RectangleShape shape, sf::Color color);
void initSprite(ds::Vector position, sf::Sprite sprite);
void updateAnimation(game::actor::AnimationState nextState);
void updateAnimation(float dt, game::actor::AnimationState nextState);
void updateTexture(game::actor::AnimationState nextState);
bool doesCollideWithAny(float top, float left, const std::vector<game::interface::CanCollide*>& others);
};

@ -6,7 +6,7 @@
#define PLAYGROUND_PLAYER_H
#include "Base.h"
#include "graphics/AnimatedSprite.h"
#include "game/component/AnimatedSprite.h"
namespace game {
namespace actor {

@ -0,0 +1,37 @@
//
// Created by Efertone on 7/28/2022.
//
#ifndef PLAYGROUND_ANIMATEDSPRITE_H
#define PLAYGROUND_ANIMATEDSPRITE_H
#include <SFML/Graphics.hpp>
#include "game/component/Sprite.h"
namespace game {
namespace component {
class AnimatedSprite : public game::component::Sprite {
public:
AnimatedSprite();
~AnimatedSprite();
void load(const std::basic_string<char> &filename, int width, int height, int length);
void setLength(int length);
void setSize(int width, int height) override;
void setSize(int width, int height, int crop_x, int crop_y) override;
void update(float) override;
void reset();
protected:
sf::Clock m_animationClock;
int m_length{};
int m_currentFrame{};;
};
}
}
#endif //PLAYGROUND_ANIMATEDSPRITE_H

@ -0,0 +1,20 @@
//
// Created by Efertone on 7/28/2022.
//
#ifndef PLAYGROUND_ICOMPONENT_H
#define PLAYGROUND_ICOMPONENT_H
namespace game {
namespace component {
class IComponent {
public:
virtual void update(float) = 0;
};
}
}
#endif //PLAYGROUND_ICOMPONENT_H

@ -0,0 +1,46 @@
//
// Created by Efertone on 7/28/2022.
//
#ifndef PLAYGROUND_SPRITE_H
#define PLAYGROUND_SPRITE_H
#include <SFML/Graphics.hpp>
#include "IComponent.h"
namespace game {
namespace component {
class Sprite : public IComponent {
public:
Sprite();
~Sprite();
void load(const std::basic_string<char> &filename, int width, int height);
void setTexture(const std::basic_string<char> &filename);
virtual void setSize(int width, int height);
virtual void setSize(int width, int height, int crop_x, int crop_y);
void setCrop(int x, int y);
void setPosition(float x, float y);
void setScale(float x, float y);
sf::Rect<float> getGlobalBounds();
void draw(sf::RenderTarget *target);
void update(float) override;
protected:
sf::Texture m_texture;
sf::Sprite m_sprite;
std::pair<int, int> m_size{};
std::pair<int, int> m_crop{};
int actualWidth() const;
int actualHeight() const;
};
}
}
#endif //PLAYGROUND_SPRITE_H

@ -1,48 +0,0 @@
//
// Created by Efertone on 7/28/2022.
//
#ifndef PLAYGROUND_ANIMATEDSPRITE_H
#define PLAYGROUND_ANIMATEDSPRITE_H
#include <SFML/Graphics.hpp>
namespace graphics {
class AnimatedSprite {
public:
AnimatedSprite();
~AnimatedSprite();
void load(const std::basic_string<char> &filename, int width, int height, int length);
void setTexture(const std::basic_string<char> &filename);
void setLength(int length);
void setSize(int width, int height);
void setSize(int width, int height, int crop_x, int crop_y);
void setPosition(float x, float y);
void setScale(float x, float y);
sf::Rect<float> getGlobalBounds();
void update();
void reset();
void draw(sf::RenderTarget* target);
protected:
sf::Texture m_texture;
sf::Sprite m_sprite;
sf::Clock m_animationClock;
int m_length{};
int m_currentFrame{};
int m_width{};
int m_height{};
std::pair<int, int> m_crop{};
int actualWidth() const;
int actualHeight() const;
};
}
#endif //PLAYGROUND_ANIMATEDSPRITE_H

@ -0,0 +1,9 @@
//
// Created by Efertone on 7/28/2022.
//
#include "game/Entity.h"
void game::Entity::init() {}
void game::Entity::update(float) {}
void game::Entity::render(sf::RenderTarget *) {}

@ -83,12 +83,12 @@ sf::Rect<float> game::actor::Base::getGlobalBounds() {
void game::actor::Base::collisionAlert(game::actor::Base *) {}
void game::actor::Base::updateAnimation(game::actor::AnimationState nextState) {
void game::actor::Base::updateAnimation(float dt, game::actor::AnimationState nextState) {
if (nextState != this->m_animation_state) {
this->updateTexture(nextState);
}
this->m_currentAnimation->update();
this->m_currentAnimation->update(dt);
}
void game::actor::Base::updateTexture(game::actor::AnimationState nextState) {

@ -67,7 +67,7 @@ void game::actor::Player::drawHUD_Health(sf::View *view, sf::RenderTarget *win)
void game::actor::Player::update(float dt, sf::RenderTarget* window, std::vector<game::interface::CanCollide*> others) {
if (this->m_specialAnimationLocked) {
if (this->m_animationLockClock.getElapsedTime().asMilliseconds() < 1000) {
this->updateAnimation(game::actor::Special);
this->updateAnimation(dt, game::actor::Special);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * PLAYER_SCALE, PLAYER_SCALE);
@ -117,7 +117,7 @@ void game::actor::Player::update(float dt, sf::RenderTarget* window, std::vector
}
}
this->updateAnimation(nextState);
this->updateAnimation(dt, nextState);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * PLAYER_SCALE, PLAYER_SCALE);
}

@ -79,7 +79,7 @@ void game::actor::SimpleEnemy::update(float dt, sf::RenderTarget* window, std::v
}
}
this->updateAnimation(nextState);
this->updateAnimation(dt, nextState);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * SIMPLE_ENEMY_SCALE, SIMPLE_ENEMY_SCALE);
}

@ -0,0 +1,51 @@
//
// Created by Efertone on 7/28/2022.
//
#include <iostream>
#include "game/component/AnimatedSprite.h"
#define DEBUG false
game::component::AnimatedSprite::AnimatedSprite() = default;
game::component::AnimatedSprite::~AnimatedSprite() = default;
void game::component::AnimatedSprite::load(const std::basic_string<char> &filename, int width, int height, int length) {
this->setTexture(filename);
this->setSize(width, height);
this->setLength(length);
}
void game::component::AnimatedSprite::setLength(int length) {
this->m_length = length;
}
void game::component::AnimatedSprite::setSize(int width, int height) {
game::component::Sprite::setSize(width, height);
this->m_sprite.setTextureRect(sf::IntRect(
this->m_currentFrame * this->m_size.first, 0,
this->actualWidth(), this->actualHeight()));
}
void game::component::AnimatedSprite::setSize(int width, int height, int crop_x, int crop_y) {
this->setCrop(crop_x, crop_y);
this->setSize(width, height);
}
void game::component::AnimatedSprite::update(float dt) {
if (this->m_animationClock.getElapsedTime().asMilliseconds() > 100) {
this->m_animationClock.restart();
sf::IntRect tile(
this->m_currentFrame * this->m_size.first, 0,
this->actualWidth(), this->actualHeight());
this->m_sprite.setTextureRect(tile);
this->m_currentFrame = (this->m_currentFrame + 1) % this->m_length;
}
}
void game::component::AnimatedSprite::reset() {
this->m_animationClock.restart();
this->m_currentFrame = 0;
}

@ -0,0 +1,91 @@
//
// Created by Efertone on 7/28/2022.
//
#include <iostream>
#include "game/component/Sprite.h"
#define DEBUG false
game::component::Sprite::Sprite() = default;
game::component::Sprite::~Sprite() = default;
void game::component::Sprite::load(const std::basic_string<char> &filename, int width, int height) {
this->setTexture(filename);
this->setSize(width, height);
}
void game::component::Sprite::setTexture(const std::basic_string<char> &filename) {
if (!this->m_texture.loadFromFile(filename)) {
std::cout << "Error loading texture: " << filename << std::endl;
return;
}
this->m_sprite.setTexture(this->m_texture);
}
void game::component::Sprite::setSize(int width, int height) {
this->m_size = {width, height};
this->m_sprite.setOrigin(float(this->actualWidth())/2, float(height));
}
void game::component::Sprite::setSize(int width, int height, int crop_x, int crop_y) {
this->setCrop(crop_x, crop_y);
this->setSize(width, height);
}
void game::component::Sprite::setCrop(int x, int y) {
this->m_crop = {x, y};
this->m_sprite.setOrigin(float(this->actualWidth())/2, float(y));
}
int game::component::Sprite::actualWidth() const {
if (this->m_crop.first > 0) {
return this->m_crop.first;
}
return this->m_size.first;
}
int game::component::Sprite::actualHeight() const {
if (this->m_crop.second > 0) {
return this->m_crop.second;
}
return this->m_size.second;
}
void game::component::Sprite::setPosition(float x, float y) {
this->m_sprite.setPosition(x, y);
}
void game::component::Sprite::setScale(float x, float y) {
this->m_sprite.setScale(x, y);
}
sf::Rect<float> game::component::Sprite::getGlobalBounds() {
return this->m_sprite.getGlobalBounds();
}
void game::component::Sprite::update(float dt) {}
void game::component::Sprite::draw(sf::RenderTarget *target) {
if (DEBUG) {
sf::CircleShape circle;
circle.setFillColor(sf::Color::Red);
circle.setRadius(5.f);
circle.setPosition(this->m_sprite.getPosition());
target->draw(circle);
sf::RectangleShape bound;
bound.setPosition(this->m_sprite.getGlobalBounds().left, this->m_sprite.getGlobalBounds().top);
bound.setSize(sf::Vector2<float>(this->m_sprite.getGlobalBounds().width, this->m_sprite.getGlobalBounds().height));
bound.setOutlineColor(sf::Color::Green);
bound.setOutlineThickness(2.f);
bound.setFillColor(sf::Color::Transparent);
target->draw(bound);
}
target->draw(this->m_sprite);
}

@ -1,110 +0,0 @@
//
// Created by Efertone on 7/28/2022.
//
#include <iostream>
#include <utility>
#include "graphics/AnimatedSprite.h"
#define DEBUG false
graphics::AnimatedSprite::AnimatedSprite() = default;
graphics::AnimatedSprite::~AnimatedSprite() = default;
void graphics::AnimatedSprite::load(const std::basic_string<char> &filename, int width, int height, int length) {
this->setTexture(filename);
this->setSize(width, height);
this->setLength(length);
}
void graphics::AnimatedSprite::setTexture(const std::basic_string<char> &filename) {
if (!this->m_texture.loadFromFile(filename)) {
std::cout << "Error loading texture: " << filename << std::endl;
return;
}
this->m_sprite.setTexture(this->m_texture);
}
void graphics::AnimatedSprite::setLength(int length) {
this->m_length = length;
}
void graphics::AnimatedSprite::setSize(int width, int height) {
this->m_width = width;
this->m_height = height;
this->m_sprite.setOrigin(float(this->actualWidth())/2, float(height));
this->m_sprite.setTextureRect(sf::IntRect(
this->m_currentFrame * this->m_width, 0,
this->actualWidth(), this->actualHeight()));
}
int graphics::AnimatedSprite::actualWidth() const {
if (this->m_crop.first > 0) {
return this->m_crop.first;
}
return this->m_width;
}
int graphics::AnimatedSprite::actualHeight() const {
if (this->m_crop.second > 0) {
return this->m_crop.second;
}
return this->m_width;
}
void graphics::AnimatedSprite::setSize(int width, int height, int crop_x, int crop_y) {
this->m_crop = {crop_x, crop_y};
this->setSize(width, height);
}
void graphics::AnimatedSprite::update() {
if (this->m_animationClock.getElapsedTime().asMilliseconds() > 100) {
this->m_animationClock.restart();
sf::IntRect tile(this->m_currentFrame * this->m_width, 0, this->actualWidth(), this->actualHeight());
this->m_sprite.setTextureRect(tile);
this->m_currentFrame = (this->m_currentFrame + 1) % this->m_length;
}
}
void graphics::AnimatedSprite::reset() {
this->m_animationClock.restart();
this->m_currentFrame = 0;
}
void graphics::AnimatedSprite::draw(sf::RenderTarget *target) {
if (DEBUG) {
sf::CircleShape circle;
circle.setFillColor(sf::Color::Red);
circle.setRadius(5.f);
circle.setPosition(this->m_sprite.getPosition());
target->draw(circle);
sf::RectangleShape bound;
bound.setPosition(this->m_sprite.getGlobalBounds().left, this->m_sprite.getGlobalBounds().top);
bound.setSize(sf::Vector2<float>(this->m_sprite.getGlobalBounds().width, this->m_sprite.getGlobalBounds().height));
bound.setOutlineColor(sf::Color::Green);
bound.setOutlineThickness(2.f);
bound.setFillColor(sf::Color::Transparent);
target->draw(bound);
}
target->draw(this->m_sprite);
}
void graphics::AnimatedSprite::setPosition(float x, float y) {
this->m_sprite.setPosition(x, y);
}
void graphics::AnimatedSprite::setScale(float x, float y) {
this->m_sprite.setScale(x, y);
}
sf::Rect<float> graphics::AnimatedSprite::getGlobalBounds() {
return this->m_sprite.getGlobalBounds();
}
Loading…
Cancel
Save