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

Update Physic and Graphic Resolvers

parent af9ae943
No related branches found
No related tags found
No related merge requests found
Showing
with 154 additions and 25 deletions
......@@ -32,6 +32,16 @@ namespace megu {
return {};
}
std::optional<std::reference_wrapper<const Renderable>> GraphicEngine::get(const Identifiable & id) const {
for(const auto & [p, layer] : this->_layers) {
auto renderable = layer->get(id);
if(renderable.has_value()) {
return renderable;
}
}
return {};
}
void GraphicEngine::step() {
if(this->_window.isOpen()) {
// Draw Layers
......
......@@ -30,6 +30,7 @@ namespace megu {
void remove(Priority);
std::optional<std::reference_wrapper<const Layer>> get(Priority) const;
std::optional<std::reference_wrapper<const Renderable>> get(const Identifiable &) const;
inline bool empty() const {return this->_layers.empty();}
inline const std::map<Priority, std::unique_ptr<Layer>> & layers() const {return this->_layers;}
......
......@@ -37,6 +37,14 @@ namespace megu {
this->_objects[priority] = renderable;
}*/
std::optional<std::reference_wrapper<const Renderable>> get(const Identifiable & id) const {
for(const auto & [priority, object] : this->_objects) {
if(object.id() == id.id()) {
return object;
}
}
}
const Texture & draw(const Window & w, const TextureArray & a) {
this->_frameBuffer.bind();
this->_renderer.clear();
......
......@@ -43,4 +43,24 @@ namespace megu {
}
}
}
std::optional<std::reference_wrapper<const Tangible>> PhysicEngine::get(const Identifiable & id) const {
for(auto & [priotiy, objects] : this->_statics) {
for(auto & object : objects) {
if(object.get().id() == id.id()) {
return object.get();
}
}
}
for(auto & [priotiy, objects] : this->_dynamic) {
for(auto & object : objects) {
if(object.get().id() == id.id()) {
return object.get();
}
}
}
return {};
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
#include <vector>
#include <map>
#include <set>
#include <optional>
#include <engine/physic/front/object/Tangible.hpp>
#include <engine/physic/front/object/TangibleStatic.hpp>
......@@ -24,6 +25,8 @@ namespace megu {
void step(double);
void step(double, Priority);
std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const;
inline void clearCollision() {this->_collisions.clear();}
const std::set<Collision, reference_sorter<Collision>> & collision() const {return this->_collisions;}
......
......@@ -4,7 +4,7 @@ namespace megu::kernel {
class Props;
class Kernel;
template <class T, class C>
template <class T, class C, class O>
class Engine {
public:
virtual void boot(Kernel &) = 0;
......@@ -12,6 +12,7 @@ namespace megu::kernel {
virtual void step(Kernel &, double) = 0;
virtual T & get() = 0;
virtual const O & get(const C &) const = 0;
virtual void add(Kernel &, C &) = 0;
};
......
......@@ -4,12 +4,10 @@
namespace megu::kernel {
GraphicEngine::GraphicEngine(Window & window)
: _engine(window), _renderer(360, 360), _tmp_ta("assets/textures/Tile_Test_3.png", 1, 1, 32.f) {}
: _engine(window), _renderer(360, 360) {}
void GraphicEngine::boot(Kernel &) {
this->_engine.push(0, this->_renderer);
//this->_engine.push(0, 0, this->_tmp_ta, &this->_tmp_mod);
}
void GraphicEngine::stop(Kernel &) {
......@@ -23,4 +21,12 @@ namespace megu::kernel {
void GraphicEngine::add(Kernel & kernel, Graphical<GraphicEngine> & graphical) {
graphical.apply(kernel, *this);
}
const Renderable & GraphicEngine::get(const Graphical<GraphicEngine> & graphical) const {
auto object = this->_engine.get(graphical);
if(object.has_value()) {
return object.value();
}
throw std::runtime_error("Cannot find in physic object : " + graphical.id());
}
}
\ No newline at end of file
......@@ -13,17 +13,19 @@
#include <kernel/back/props/Graphical.hpp>
namespace megu::kernel {
class GraphicEngine : public Engine<megu::GraphicEngine, Graphical<GraphicEngine>> {
class GraphicEngine : public Engine<megu::GraphicEngine, Graphical<GraphicEngine>, Renderable> {
public:
GraphicEngine(Window &);
inline megu::GraphicEngine & get() {return this->_engine;}
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void add(Kernel &, Graphical<GraphicEngine> &) override;
inline megu::GraphicEngine & get() {return this->_engine;}
const megu::Renderable & get(const Graphical<GraphicEngine> &) const override;
inline const megu::GraphicEngine & engine() const {return this->_engine;}
inline const megu::Renderer & renderer() const {return this->_renderer;}
......@@ -31,8 +33,5 @@ namespace megu::kernel {
private:
megu::GraphicEngine _engine;
megu::Renderer _renderer;
megu::TileArray _tmp_ta;
megu::TileArray_Module _tmp_mod;
};
}
\ No newline at end of file
......@@ -21,4 +21,13 @@ namespace megu::kernel {
void PhysicEngine::add(Kernel & kernel, Physical<PhysicEngine> & props) {
props.apply(kernel, *this);
}
const megu::Tangible & PhysicEngine::get(const Physical<PhysicEngine> & props) const {
auto tangible = this->_engine.get(props);
if(tangible.has_value()) {
return tangible.value();
}
throw std::runtime_error("Cannot find in physic object : " + props.id());
}
}
\ No newline at end of file
......@@ -6,17 +6,19 @@
#include <kernel/back/props/Physical.hpp>
namespace megu::kernel {
class PhysicEngine : public Engine<megu::PhysicEngine, Physical<PhysicEngine>> {
class PhysicEngine : public Engine<megu::PhysicEngine, Physical<PhysicEngine>, Tangible> {
public:
PhysicEngine();
inline megu::PhysicEngine & get() override {return this->_engine;}
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void add(Kernel &, Physical<PhysicEngine> &) override;
inline megu::PhysicEngine & get() override {return this->_engine;}
const megu::Tangible & get(const Physical<PhysicEngine> &) const override;
private:
megu::PhysicEngine _engine;
......
#pragma once
#include <kernel/back/engine/Engine.hpp>
#include <utility/Identifiable.hpp>
namespace megu::kernel {
class Kernel;
template <class E>
class Component {
class Component : public virtual Identifiable {
public:
virtual void apply(Kernel & k, E &) = 0;
};
......
......@@ -9,8 +9,8 @@ namespace megu::kernel {
template <class Pe>
class Physical : public Component<Pe> {
public:
virtual void on_collide(double, const Identifiable &, Physical &) = 0;
virtual void on_collide(const Kernel &, const Pe &, Physical &, double) = 0;
using CollideLambda = std::function<void(double, const Identifiable &, Physical &)>;
using CollideLambda = std::function<void(const Kernel &, const Pe &, const Physical &, double)>;
};
}
\ No newline at end of file
......@@ -13,15 +13,15 @@ namespace megu::kernel {
}
void Kernel::step() {
double delta = Window::Time();
double time = Window::Time();
if(this->_window.isOpen()) {
this->_gEngine.step(*this, delta);
this->_gResolver.resolve(delta, this->_gEngine, this->_props);
this->_gEngine.step(*this, time);
this->_gResolver.resolve(*this, this->_gEngine, time);
}
this->_pEngine.step(*this, delta);
this->_pResolver.resolve(delta, this->_pEngine, this->_props);
this->_pEngine.step(*this, time);
this->_pResolver.resolve(*this, this->_pEngine, time);
}
void Kernel::add(Props * props) {
......@@ -29,11 +29,13 @@ namespace megu::kernel {
auto * pComponent = props->getPhysicComponent();
if(pComponent != nullptr) {
this->_pEngine.add(*this, *pComponent);
this->_pResolver.add(*pComponent);
}
auto * gComponent = props->getGraphicComponent();
if(gComponent != nullptr) {
this->_gEngine.add(*this, *gComponent);
this->_gResolver.add(*gComponent);
}
}
}
\ No newline at end of file
......@@ -22,6 +22,9 @@ namespace megu::kernel {
inline Identifiable_Map<Props> & props() {return this->_props;}
inline PhysicEngine & getPhysicEngine() {return this->_pEngine;}
inline GraphicEngine & getGraphicEngine() {return this->_gEngine;}
private:
Window & _window;
......
......@@ -12,9 +12,9 @@ namespace megu::kernel {
}
}
void Fixed::on_collide(double time, const Identifiable & identifiable, Physical<PhysicEngine> & physical) {
void Fixed::on_collide(const Kernel & kernel, const PhysicEngine & engine, Physical & physical, double time) {
if(this->_collide != nullptr) {
this->_collide(time, identifiable, physical);
this->_collide(kernel, engine, physical, time);
}
}
......
......@@ -13,7 +13,7 @@ namespace megu::kernel {
Fixed(float x, float y, float w, float h);
void update_physic(double) const override;
void on_collide(double, const Identifiable &, Physical<PhysicEngine> &) override;
void on_collide(const Kernel &, const PhysicEngine &, Physical &, double) override;
void apply(Kernel & k, PhysicEngine &) override;
void setCollideLambda(const CollideLambda &);
......
#include "FixedArray.hpp"
#include <kernel/front/Kernel.hpp>
namespace megu::kernel {
std::optional<std::reference_wrapper<const TangibleStatic>> FixedArray::at(const Position & position) const {
if(this->_tangibles.contains(position)) {
return this->_tangibles.at(position);
}
return {};
}
void FixedArray::push(const Fixed & tangible) {
this->_tangibles.insert({tangible.getPosition(), tangible});
}
void FixedArray::erase(const Fixed & tangible) {
this->_tangibles.erase(tangible.getPosition());
}
void FixedArray::erase(const Position & position) {
this->_tangibles.erase(position);
}
void FixedArray::on_collide(const Kernel & kernel, const PhysicEngine & engine, Physical & physical, double time) {
auto & tangible = engine.get(physical);
for(auto & [position, fixed] : this->_tangibles) {
if(fixed.isColliding(tangible)) {
physical.on_collide(kernel, engine, fixed, time);
}
}
}
void FixedArray::apply(Kernel & kernel, PhysicEngine & engine) {
engine.get().push(0, *this);
}
}
\ No newline at end of file
#pragma once
#include <kernel/back/props/Physical.hpp>
#include <kernel/back/engine/PhysicEngine.hpp>
#include <vector>
#include <functional>
#include "Fixed.hpp"
namespace megu::kernel {
class FixedArray : public Physical<PhysicEngine>, public TangibleStatic {
public:
std::optional<std::reference_wrapper<const TangibleStatic>> at(const Position &) const;
void push(const Fixed &);
void erase(const Fixed &);
void erase(const Position &);
void on_collide(const Kernel &, const PhysicEngine &, Physical &, double) override;
void apply(Kernel & k, PhysicEngine &) override;
private:
std::map<Position, Fixed> _tangibles;
CollideLambda _collide;
UpdateLambda _update;
};
}
\ No newline at end of file
......@@ -10,9 +10,9 @@ namespace megu::kernel {
}
}
void Movable::on_collide(double time, const Identifiable & identifiable, Physical<PhysicEngine> & physical) {
void Movable::on_collide(const Kernel & kernel, const PhysicEngine & engine, Physical & physical, double time) {
if(this->_collide != nullptr) {
this->_collide(time, identifiable, physical);
this->_collide(kernel, engine, physical, time);
}
}
......
......@@ -13,7 +13,7 @@ namespace megu::kernel {
Movable(float x, float y, float w, float h);
void update_physic(double) override;
void on_collide(double, const Identifiable &, Physical<PhysicEngine> &) override;
void on_collide(const Kernel &, const PhysicEngine &, Physical &, double) override;
void apply(Kernel & k, PhysicEngine &) override;
void setCollideLambda(CollideLambda &);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment