Skip to content
Snippets Groups Projects
Commit 97171b7b authored by BATON Theau's avatar BATON Theau
Browse files

Refactoring code

parent bd4dfe88
No related branches found
No related tags found
No related merge requests found
Showing
with 163 additions and 145 deletions
......@@ -4,11 +4,10 @@
#include <engine/utility/Priority.hpp>
namespace megu::kernel {
template <class Ge>
class Graphical : public Component<Ge> {
template <class E>
class Graphical : public Component<E> {
public:
inline Graphical(Priority l = 0, Priority o = 0)
: _layer(l), _object(o) {}
inline Graphical(Priority = 0, Priority = 0);
inline Priority getLayerPriority() const {return this->_layer;}
inline Priority getObjectPriority() const {return this->_object;}
......@@ -24,3 +23,5 @@ namespace megu::kernel {
};
}
#include "Graphical.tpp"
\ No newline at end of file
#include "Graphical.hpp"
namespace megu::kernel {
template <class E>
Graphical<E>::Graphical(Priority layer, Priority object)
: _layer(layer), _object(object) {}
}
\ No newline at end of file
......@@ -7,19 +7,22 @@
#include <engine/utility/Priority.hpp>
namespace megu::kernel {
template <class Pe>
class Physical : public Component<Pe> {
template <class E>
class Physical : public Component<E> {
public:
inline Physical(Priority p = 0)
: _layer(p) {}
Physical(Priority p = 0);
virtual ~Physical() = default;
virtual void on_collide(Kernel &, Pe &, Identifiable &, Physical &, double) = 0;
using CollideLambda = std::function<void(Kernel &, Pe &, Identifiable &, Physical &, double)>;
inline void setLayer(Priority p) {this->_layer = p;}
inline Priority getLayer() const {return this->_layer;}
virtual void onCollide(Kernel &, E &, Identifiable &, Physical &, double) = 0;
using CollideLambda = std::function<void(Kernel &, E &, Identifiable &, Physical &, double)>;
private:
Priority _layer;
};
}
#include "Physical.tpp"
\ No newline at end of file
#include "Physical.hpp"
namespace megu::kernel {
template <class E>
Physical<E>::Physical(Priority layer)
: _layer(layer) {}
}
\ No newline at end of file
......@@ -7,14 +7,14 @@ namespace megu::kernel {
template <class T, class C, class O>
class Engine {
public:
virtual void boot(Kernel &) = 0;
virtual void stop(Kernel &) = 0;
virtual void step(Kernel &, double) = 0;
virtual void boot() = 0;
virtual void stop() = 0;
virtual void step(double) = 0;
virtual T & get() = 0;
virtual const O & get(const C &) const = 0;
virtual void add(Kernel &, C &) = 0;
virtual void remove(Kernel &, C &) = 0;
virtual void push(C &) = 0;
virtual void remove(C &) = 0;
};
}
\ No newline at end of file
......@@ -6,26 +6,26 @@ namespace megu::kernel {
GraphicEngine::GraphicEngine(Window & window)
: _engine(window), _renderer(360, 360) {}
void GraphicEngine::boot(Kernel &) {
void GraphicEngine::boot() {
this->_engine.push(0, this->_renderer);
this->_engine.push(1, this->_renderer);
this->_engine.push(2, this->_renderer);
}
void GraphicEngine::stop(Kernel &) {
void GraphicEngine::stop() {
}
void GraphicEngine::step(Kernel & kernel, double time) {
void GraphicEngine::step(double time) {
this->_engine.step();
}
void GraphicEngine::add(Kernel & kernel, Graphical<GraphicEngine> & graphical) {
graphical.apply(kernel, *this);
void GraphicEngine::push(Graphical<GraphicEngine> & graphical) {
graphical.apply(*this);
}
void GraphicEngine::remove(Kernel & kernel, Graphical<GraphicEngine> & graphical) {
graphical.unapply(kernel, *this);
void GraphicEngine::remove(Graphical<GraphicEngine> & graphical) {
graphical.unapply(*this);
}
const Renderable & GraphicEngine::get(const Graphical<GraphicEngine> & graphical) const {
......
......@@ -19,12 +19,12 @@ namespace megu::kernel {
inline megu::GraphicEngine & get() {return this->_engine;}
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void boot() override;
void stop() override;
void step(double) override;
void add(Kernel &, Graphical<GraphicEngine> &) override;
void remove(Kernel &, Graphical<GraphicEngine> &) override;
void push(Graphical<GraphicEngine> &) override;
void remove(Graphical<GraphicEngine> &) override;
const megu::Renderable & get(const Graphical<GraphicEngine> &) const override;
......
......@@ -6,24 +6,24 @@ namespace megu::kernel {
PhysicEngine::PhysicEngine()
: _engine() {}
void PhysicEngine::boot(Kernel &) {
void PhysicEngine::boot() {
}
void PhysicEngine::stop(Kernel &) {
void PhysicEngine::stop() {
}
void PhysicEngine::step(Kernel &, double time) {
void PhysicEngine::step(double time) {
this->_engine.step(time);
}
void PhysicEngine::add(Kernel & kernel, Physical<PhysicEngine> & props) {
props.apply(kernel, *this);
void PhysicEngine::push(Physical<PhysicEngine> & props) {
props.apply(*this);
}
void PhysicEngine::remove(Kernel & kernel, Physical<PhysicEngine> & props) {
props.unapply(kernel, *this);
void PhysicEngine::remove(Physical<PhysicEngine> & props) {
props.unapply(*this);
}
const megu::Tangible & PhysicEngine::get(const Physical<PhysicEngine> & props) const {
......
......@@ -12,12 +12,14 @@ namespace megu::kernel {
inline megu::PhysicEngine & get() override {return this->_engine;}
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void boot() override;
void stop() override;
void step(double) override;
void add(Kernel &, Physical<PhysicEngine> &) override;
void remove(Kernel &, Physical<PhysicEngine> &) override;
void push(Physical<PhysicEngine> &) override;
void remove(Physical<PhysicEngine> &) override;
inline std::optional<SquareBox> check(SquareBox box, Priority prio) const {return this->_engine.checkCollision(box, prio);}
const megu::Tangible & get(const Physical<PhysicEngine> &) const override;
......
......@@ -3,57 +3,57 @@
namespace megu::kernel {
Kernel::Kernel(Window & window)
: _window(window), _pEngine(), _gEngine(window) {
this->_pEngine.boot(*this);
this->_gEngine.boot(*this);
this->_pEngine.boot();
this->_gEngine.boot();
}
Kernel::~Kernel() {
this->_pEngine.stop(*this);
this->_pEngine.stop(*this);
this->_pEngine.stop();
this->_pEngine.stop();
}
void Kernel::step() {
double time = Window::Time();
if(this->_window.isOpen()) {
this->_gEngine.step(*this, time);
this->_gEngine.step(time);
this->_gResolver.resolve(*this, this->_gEngine, time);
}
this->_pEngine.step(*this, time);
this->_pEngine.step(time);
this->_pResolver.resolve(*this, this->_pEngine, time);
}
void Kernel::add(Prop * props) {
this->_props[props->id()] = props;
auto * pComponent = props->getPhysicComponent();
void Kernel::push(Prop & props) {
this->_props[props.id()] = &props;
auto * pComponent = props.getPhysicComponent();
if(pComponent != nullptr) {
this->_pEngine.add(*this, *pComponent);
this->_pResolver.add(*pComponent);
this->_pEngine.push(*pComponent);
this->_pResolver.push(*pComponent);
}
auto * gComponent = props->getGraphicComponent();
auto * gComponent = props.getGraphicComponent();
if(gComponent != nullptr) {
this->_gEngine.add(*this, *gComponent);
this->_gResolver.add(*gComponent);
this->_gEngine.push(*gComponent);
this->_gResolver.push(*gComponent);
}
}
void Kernel::remove(Prop * props) {
auto * pComponent = props->getPhysicComponent();
void Kernel::remove(Prop & props) {
auto * pComponent = props.getPhysicComponent();
if(pComponent != nullptr) {
this->_pEngine.remove(*this, *pComponent);
this->_pEngine.remove(*pComponent);
this->_pResolver.remove(*pComponent);
}
auto * gComponent = props->getGraphicComponent();
auto * gComponent = props.getGraphicComponent();
if(gComponent != nullptr) {
this->_gEngine.remove(*this, *gComponent);
this->_gEngine.remove(*gComponent);
this->_gResolver.remove(*gComponent);
}
this->_props.erase(props->id());
this->_props.erase(props.id());
}
Prop * Kernel::get(const Prop::Physical_Component & pc) {
......
......@@ -15,14 +15,13 @@ namespace megu::kernel {
class Kernel {
public:
Kernel() = delete;
Kernel(Window &);
~Kernel();
void step();
void add(Prop *);
void remove(Prop *);
inline Identifiable_Map<Prop> & props() {return this->_props;}
void push(Prop &);
void remove(Prop &);
inline PhysicEngine & getPhysicEngine() {return this->_pEngine;}
inline GraphicEngine & getGraphicEngine() {return this->_gEngine;}
......
#include "Sprite.hpp"
//this->_sprite.setFrame({0.f, 0.f, 51.f, 98.f});
//this->_sprite.setSize({50, 100});
namespace megu::kernel {
GraphicLinker<megu::Sprite> Sprite::_Linker = GraphicLinker<megu::Sprite>();
Sprite::Sprite(const std::filesystem::path & path)
: Sprite(path, {0, 0, 0, 0}) {}
Sprite::Sprite()
: megu::Sprite() {}
Sprite::Sprite(const std::filesystem::path & path, const Frame & frame)
: megu::Sprite(path, frame), _animation(0), _index(0) {
......@@ -16,52 +13,52 @@ namespace megu::kernel {
}
}
const Frame_List & Sprite::getAnimations(size_t name) const {
const Frame_List & Sprite::getCurrentAnimations(size_t name) const {
if(this->_frames.contains(name)) {
return this->_frames.at(name);
}
throw std::runtime_error("Cannot get inexisting animation");
throw std::runtime_error("Missing animation : " + name);
}
const Frame & Sprite::getFrame(size_t name, size_t index) const {
auto & frames = this->getAnimations(name);
const Frame & Sprite::getCurrentFrame(size_t name, size_t index) const {
auto & frames = this->getCurrentAnimations(name);
if(frames.size() > index) {
return frames.at(index);
}
throw std::runtime_error("Cannot get inexisting frame");
throw std::runtime_error("Missing frame for animation : " + name);
}
void Sprite::setAnimation(size_t name) {
void Sprite::setCurrentAnimation(size_t name) {
if(this->_frames.contains(name)) {
this->_animation = name;
this->_previous = 0.0;
}
else {
throw std::runtime_error("Cannot set inexisting animation");
throw std::runtime_error("Cannot set inexisting animation : " + name);
}
}
void Sprite::push(size_t name, const Frame_List & frames) {
void Sprite::pushAnimation(size_t name, const Frame_List & frames) {
for(auto & frame : frames) {
this->push(name, frame);
this->pushAnimation(name, frame);
}
}
void Sprite::push(size_t name, const Frame & frame) {
void Sprite::pushAnimation(size_t name, const Frame & frame) {
this->_frames[name].push_back(frame);
}
void Sprite::push(size_t name, float x, float y, float w, float h) {
void Sprite::pushAnimation(size_t name, float x, float y, float w, float h) {
this->_frames[name].push_back({x, y, w, h});
}
void Sprite::remove(size_t name) {
void Sprite::removeAnimation(size_t name) {
if(this->_frames.contains(name)) {
this->_frames.erase(name);
}
}
void Sprite::remove(size_t name, size_t index) {
void Sprite::removeAnimation(size_t name, size_t index) {
if(this->_frames.contains(name) && this->_frames[name].size() > index) {
this->_frames[name].erase(this->_frames[name].begin() + index);
}
......@@ -83,12 +80,12 @@ namespace megu::kernel {
}
}
void Sprite::apply(Kernel & kernel, GraphicEngine & engine) {
void Sprite::apply(GraphicEngine & engine) {
Sprite::_Linker.link(*this, this->getLayerPriority(), this->getObjectPriority());
Sprite::_Linker.link(engine);
}
void Sprite::unapply(Kernel & kernel, GraphicEngine & engine) {
void Sprite::unapply(GraphicEngine & engine) {
Sprite::_Linker.unlink(*this);
Sprite::_Linker.link(engine);
}
......
......@@ -18,27 +18,28 @@ namespace megu::kernel {
class Sprite : public Graphical<GraphicEngine>, public megu::Sprite {
public:
Sprite(const std::filesystem::path &);
Sprite(const std::filesystem::path &, const Frame &);
Sprite();
Sprite(const std::filesystem::path &, const Frame & = {0, 0, 0, 0});
virtual ~Sprite() = default;
inline size_t getCurrentFrameIndex() const {return this->_index;}
inline double getDuration() const {return this->_duration;}
inline void setDuration(double d) {this->_duration = d;}
inline size_t getCurrentFrameIndex() const {return this->_index;}
const Frame & getCurrentFrame(size_t, size_t) const;
const Frame_List & getCurrentAnimations(size_t) const;
const Frame_List & getAnimations(size_t) const;
const Frame & getFrame(size_t, size_t) const;
inline void setDuration(double d) {this->_duration = d;}
void setCurrentAnimation(size_t);
void setAnimation(size_t);
void push(size_t, const Frame_List & = {});
void push(size_t, const Frame &);
void push(size_t, float, float, float, float);
void pushAnimation(size_t, const Frame_List & = {});
void pushAnimation(size_t, const Frame &);
void pushAnimation(size_t, float, float, float, float);
void remove(size_t);
void remove(size_t, size_t);
void removeAnimation(size_t);
void removeAnimation(size_t, size_t);
void apply(GraphicEngine &) override;
void unapply(GraphicEngine &) override;
void update(double) override;
void apply(Kernel &, GraphicEngine &) override;
void unapply(Kernel &, GraphicEngine &) override;
static const GraphicLinker<megu::Sprite> & linker() {return Sprite::_Linker;}
......
......@@ -9,19 +9,19 @@ namespace megu::kernel {
throw std::runtime_error("Tilemap dimension not matching tiles size.");
}*/
size_t tileWidth = this->texture().width() / this->_tileSize;
size_t tileHeight = this->texture().height() / this->_tileSize;
size_t tileWidth = this->getTextureWidth() / lenght;
size_t tileHeight = this->getTextureHeight() / lenght;
for(size_t y = 0; y < tileHeight; ++y) {
for(size_t x = 0; x < tileWidth; ++x) {
this->_tilesPosition.push_back({x * this->_tileSize, y * this->_tileSize});
this->_tilesPosition.push_back({x * lenght, y * lenght});
}
}
size_t i = 0;
for(size_t x = 0; x < width * this->_tileSize; x += this->_tileSize) {
for(size_t x = 0; x < width * lenght; x += lenght) {
this->_tilesValue.push_back({});
for(size_t y = 0; y < height * this->_tileSize; y += this->_tileSize) {
for(size_t y = 0; y < height * lenght; y += lenght) {
this->_tilesValue[i].push_back(0);
}
++i;
......@@ -30,12 +30,10 @@ namespace megu::kernel {
if(!Tilemap::_Linker.haveModule()) {
Tilemap::_Linker.setModule(new TileArray_Module{});
}
//this->setSize({width * size, height * size});
}
void Tilemap::setValue(size_t x, size_t y, size_t value) {
if(this->width() <= x || this->height() <= y) {
if(this->row() <= x || this->collumn() <= y) {
throw std::runtime_error("Set Tilemap coords out of range.");
}
......@@ -50,7 +48,7 @@ namespace megu::kernel {
this->_tileSize,
this->_tileSize};
this->setUv(this->height() - 1 - y, x, uv);
this->setUv(this->collumn() - 1 - y, x, uv);
this->_tilesValue[x][y] = value;
}
......@@ -66,8 +64,8 @@ namespace megu::kernel {
throw std::runtime_error("Tilemap coords out of bound");
}
void Tilemap::addAnimation(size_t x, size_t y, const std::vector<size_t> & ids) {
if(x < this->width() && y < this->height()) {
void Tilemap::pushAnimation(size_t x, size_t y, const std::vector<size_t> & ids) {
if(x < this->row() && y < this->collumn()) {
this->_animations[{x, y}] = std::pair<std::vector<size_t>, size_t>(ids, 0);
}
}
......@@ -91,12 +89,12 @@ namespace megu::kernel {
}
}
void Tilemap::apply(Kernel & kernel, GraphicEngine & engine) {
void Tilemap::apply(GraphicEngine & engine) {
Tilemap::_Linker.link(*this, this->getLayerPriority(), this->getObjectPriority());
Tilemap::_Linker.link(engine);
}
void Tilemap::unapply(Kernel & kernel, GraphicEngine & engine) {
void Tilemap::unapply(GraphicEngine & engine) {
Tilemap::_Linker.unlink(*this);
Tilemap::_Linker.link(engine);
}
......
......@@ -16,28 +16,24 @@ namespace megu::kernel {
struct TilePosition {
size_t x = 0;
size_t y = 0;
bool operator<(const TilePosition & p) const {
if(this->x == p.x) {
return this->y < p.y;
}
return this->y < p.y;
}
};
Tilemap() = delete;
Tilemap(const std::filesystem::path &, size_t, size_t, float, size_t, double = 0.1);
virtual ~Tilemap() = default;
inline void setDuration(double duration) {this->_duration = duration;}
void setValue(size_t, size_t, size_t);
inline double getDuration() const {return this->_duration;}
size_t getValue(size_t, size_t) const;
void addAnimation(size_t, size_t, const std::vector<size_t> &);
void pushAnimation(size_t, size_t, const std::vector<size_t> &);
void removeAnimation(size_t, size_t);
const std::map<TilePosition, std::pair<std::vector<size_t>, size_t>, reference_sorter<TilePosition>> & animation() const {return this->_animations;}
void apply(GraphicEngine &) override;
void unapply(GraphicEngine &) override;
void update(double) override;
void apply(Kernel &, GraphicEngine &) override;
void unapply(Kernel &, GraphicEngine &) override;
private:
std::vector<TilePosition> _tilesPosition;
......
......@@ -6,23 +6,23 @@ namespace megu::kernel {
Fixed::Fixed(float x, float y, float w, float h, CollideLambda cl, UpdateLambda ul)
: TangibleStatic(Position(x, y), Dimension(w, h, 0.f)), _collide(cl), _update(ul) {}
void Fixed::update_physic(double time) const {
void Fixed::updatePhysic(double time) const {
if(this->_update != nullptr) {
this->_update(time);
}
}
void Fixed::on_collide(Kernel & kernel, PhysicEngine & engine, Identifiable & id, Physical & physical, double time) {
void Fixed::onCollide(Kernel & kernel, PhysicEngine & engine, Identifiable & id, Physical & physical, double time) {
if(this->_collide != nullptr) {
this->_collide(kernel, engine, id, physical, time);
}
}
void Fixed::apply(Kernel & kernel, PhysicEngine & engine) {
void Fixed::apply(PhysicEngine & engine) {
engine.get().push(this->getLayer(), *this);
}
void Fixed::unapply(Kernel & kernel, PhysicEngine & engine) {
void Fixed::unapply(PhysicEngine & engine) {
engine.get().remove(*this);
}
......
......@@ -10,12 +10,14 @@
namespace megu::kernel {
class Fixed : public TangibleStatic, public Physical<PhysicEngine> {
public:
Fixed() = delete;
Fixed(float x, float y, float w, float h, CollideLambda = nullptr, UpdateLambda = nullptr);
virtual ~Fixed() = default;
void update_physic(double) const override;
void on_collide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override;
void apply(Kernel & k, PhysicEngine &) override;
void unapply(Kernel & k, PhysicEngine &) override;
void updatePhysic(double) const override;
void onCollide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override;
void apply(PhysicEngine &) override;
void unapply(PhysicEngine &) override;
void setCollideLambda(CollideLambda);
void setUpdateLambda(UpdateLambda);
......
......@@ -4,23 +4,23 @@ namespace megu::kernel {
Movable::Movable(float x, float y, float w, float h, CollideLambda cl, UpdateLambda ul)
: TangibleMovable(Position(x, y), Dimension(w, h, 0.f)), _collide(cl), _update(ul) {}
void Movable::update_physic(double time) {
void Movable::updatePhysic(double time) {
if(this->_update != nullptr) {
this->_update(time);
}
}
void Movable::on_collide(Kernel & kernel, PhysicEngine & engine, Identifiable & id, Physical & physical, double time) {
void Movable::onCollide(Kernel & kernel, PhysicEngine & engine, Identifiable & id, Physical & physical, double time) {
if(this->_collide != nullptr) {
this->_collide(kernel, engine, id, physical, time);
}
}
void Movable::apply(Kernel & kernel, PhysicEngine & engine) {
void Movable::apply(PhysicEngine & engine) {
engine.get().push(this->getLayer(), *this);
}
void Movable::unapply(Kernel & kernel, PhysicEngine & engine) {
void Movable::unapply(PhysicEngine & engine) {
engine.get().remove(*this);
}
......
......@@ -10,12 +10,14 @@
namespace megu::kernel {
class Movable : public TangibleMovable, public Physical<PhysicEngine> {
public:
Movable() = delete;
Movable(float x, float y, float w, float h, CollideLambda = nullptr, UpdateLambda = nullptr);
virtual ~Movable() = default;
void update_physic(double) override;
void on_collide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override;
void apply(Kernel & k, PhysicEngine &) override;
void unapply(Kernel & k, PhysicEngine &) override;
void updatePhysic(double) override;
void onCollide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override;
void apply(PhysicEngine &) override;
void unapply(PhysicEngine &) override;
void setCollideLambda(CollideLambda);
void setUpdateLambda(UpdateLambda);
......
#include "Tile.hpp"
namespace megu::kernel {
Tile::Tile()
: Fixed(0, 0, 1, 1) {}
Tile::Tile(float x, float y, float d, Priority p)
: Fixed(x, y, d, d) {
this->setLayer(p);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment