move towards ecs
parent
9ea734261c
commit
b217d29ac8
@ -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
|
@ -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 *) {}
|
@ -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…
Reference in New Issue