Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../../src/console/console.h"
- #include <map>
- #include <string>
- #include <functional>
- #include <vector>
- #include <memory>
- class Circle{
- public:
- float x;
- float y;
- float radius;
- Circle(float _x, float _y, float _radius) : x{_x}, y{_y}, radius{_radius}{}
- };
- class Rectangle{
- public:
- float x;
- float y;
- float w;
- float h;
- Rectangle(float _x, float _y, float _w, float _h):
- x{_x},y{_y},w{_w},h{_h}{}
- };
- class ShapeBase{
- public:
- ShapeBase() = default;
- virtual void HandleUpdate(){}
- virtual void BigPoly(ShapeBase* _c){}
- virtual void Poly(Circle _c){
- }
- virtual void Poly(Rectangle _c){
- }
- };
- void FuncPoly(Rectangle _c, Rectangle _r){
- Console::Out("Rectangle and rectangle");
- }
- void FuncPoly(Circle _c, Rectangle _r){
- Console::Out("rectangle and circle");
- }
- void FuncPoly(Rectangle _c, Circle _r){
- Console::Out("circle and rectangle");
- }
- void FuncPoly(Circle _c, Circle _r){
- Console::Out("circle and circle");
- }
- template<typename tShape>
- class Shape : public ShapeBase{
- public:
- tShape shape;
- Shape(tShape _shape) : shape{_shape}{}
- std::vector<std::function<void (Shape<tShape>*)>> callbacks;
- void HandleUpdate(){
- for(auto&& i : callbacks){
- i(this);
- }
- }
- void BigPoly(ShapeBase* _c){
- _c->Poly(shape);
- }
- void Poly(Circle _c){
- FuncPoly(shape, _c);
- }
- void Poly(Rectangle _c){
- FuncPoly(shape, _c);
- }
- };
- class System{
- public:
- void Func(Shape<Circle>* _shape){
- Console::Out("Circle:", _shape->shape.x,_shape->shape.y,_shape->shape.radius);
- }
- void Func(Shape<Rectangle>* _shape){
- Console::Out("Rectangle:", _shape->shape.x,_shape->shape.y,_shape->shape.w, _shape->shape.h);
- }
- static System& Get(){
- static System system;
- return system;
- }
- };
- class CollisionBody{
- public:
- template<typename tShape, typename ...tArgs>
- void SetShape(tArgs ..._args){
- auto l_shape = std::make_unique<Shape<tShape>>(tShape(_args...));
- l_shape->callbacks.push_back(
- std::function<void(Shape<tShape>* _shape)>([](Shape<tShape>* _shape) -> void {
- System::Get().Func(_shape);
- })
- );
- shape = std::move(l_shape);
- }
- std::unique_ptr<ShapeBase> shape;
- CollisionBody(){
- }
- void HandleUpdate(){
- shape->HandleUpdate();
- }
- };
- class MyCollisionBody : public CollisionBody{
- public:
- MyCollisionBody() : CollisionBody(){
- SetShape<Circle>(5.0f, 1.0f, 2.0f);
- }
- };
- class MyOtherCollisionBody : public CollisionBody{
- public:
- MyOtherCollisionBody() : CollisionBody(){
- SetShape<Rectangle>(6.0f, 6.0f, 2.0f, 2.0f);
- }
- };
- int main(){
- MyCollisionBody cb;
- MyOtherCollisionBody cb2;
- MyOtherCollisionBody cb3;
- cb.HandleUpdate();
- cb2.HandleUpdate();
- cb.shape->BigPoly(cb2.shape.get());
- cb3.shape->BigPoly(cb2.shape.get());
- cb2.shape->BigPoly(cb.shape.get());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment