Guest User

Untitled

a guest
Sep 7th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1.  
  2. #include "../../src/console/console.h"
  3. #include <map>
  4. #include <string>
  5. #include <functional>
  6. #include <vector>
  7. #include <memory>
  8.  
  9. class Circle{
  10. public:
  11. float x;
  12. float y;
  13. float radius;
  14. Circle(float _x, float _y, float _radius) : x{_x}, y{_y}, radius{_radius}{}
  15. };
  16.  
  17. class Rectangle{
  18. public:
  19. float x;
  20. float y;
  21. float w;
  22. float h;
  23. Rectangle(float _x, float _y, float _w, float _h):
  24. x{_x},y{_y},w{_w},h{_h}{}
  25. };
  26.  
  27. class ShapeBase{
  28. public:
  29. ShapeBase() = default;
  30. virtual void HandleUpdate(){}
  31. virtual void BigPoly(ShapeBase* _c){}
  32.  
  33. virtual void Poly(Circle _c){
  34.  
  35. }
  36. virtual void Poly(Rectangle _c){
  37.  
  38. }
  39. };
  40.  
  41. void FuncPoly(Rectangle _c, Rectangle _r){
  42. Console::Out("Rectangle and rectangle");
  43. }
  44.  
  45. void FuncPoly(Circle _c, Rectangle _r){
  46. Console::Out("rectangle and circle");
  47. }
  48.  
  49. void FuncPoly(Rectangle _c, Circle _r){
  50. Console::Out("circle and rectangle");
  51. }
  52.  
  53. void FuncPoly(Circle _c, Circle _r){
  54. Console::Out("circle and circle");
  55. }
  56.  
  57. template<typename tShape>
  58. class Shape : public ShapeBase{
  59. public:
  60. tShape shape;
  61. Shape(tShape _shape) : shape{_shape}{}
  62. std::vector<std::function<void (Shape<tShape>*)>> callbacks;
  63. void HandleUpdate(){
  64. for(auto&& i : callbacks){
  65. i(this);
  66. }
  67. }
  68.  
  69. void BigPoly(ShapeBase* _c){
  70. _c->Poly(shape);
  71. }
  72. void Poly(Circle _c){
  73. FuncPoly(shape, _c);
  74. }
  75. void Poly(Rectangle _c){
  76. FuncPoly(shape, _c);
  77. }
  78. };
  79.  
  80. class System{
  81. public:
  82. void Func(Shape<Circle>* _shape){
  83. Console::Out("Circle:", _shape->shape.x,_shape->shape.y,_shape->shape.radius);
  84. }
  85.  
  86. void Func(Shape<Rectangle>* _shape){
  87. Console::Out("Rectangle:", _shape->shape.x,_shape->shape.y,_shape->shape.w, _shape->shape.h);
  88. }
  89.  
  90. static System& Get(){
  91. static System system;
  92. return system;
  93. }
  94. };
  95.  
  96. class CollisionBody{
  97. public:
  98. template<typename tShape, typename ...tArgs>
  99. void SetShape(tArgs ..._args){
  100. auto l_shape = std::make_unique<Shape<tShape>>(tShape(_args...));
  101. l_shape->callbacks.push_back(
  102. std::function<void(Shape<tShape>* _shape)>([](Shape<tShape>* _shape) -> void {
  103. System::Get().Func(_shape);
  104. })
  105. );
  106. shape = std::move(l_shape);
  107. }
  108. std::unique_ptr<ShapeBase> shape;
  109. CollisionBody(){
  110.  
  111. }
  112. void HandleUpdate(){
  113. shape->HandleUpdate();
  114. }
  115. };
  116.  
  117. class MyCollisionBody : public CollisionBody{
  118. public:
  119. MyCollisionBody() : CollisionBody(){
  120. SetShape<Circle>(5.0f, 1.0f, 2.0f);
  121. }
  122. };
  123.  
  124. class MyOtherCollisionBody : public CollisionBody{
  125. public:
  126. MyOtherCollisionBody() : CollisionBody(){
  127. SetShape<Rectangle>(6.0f, 6.0f, 2.0f, 2.0f);
  128. }
  129. };
  130.  
  131. int main(){
  132. MyCollisionBody cb;
  133. MyOtherCollisionBody cb2;
  134. MyOtherCollisionBody cb3;
  135. cb.HandleUpdate();
  136. cb2.HandleUpdate();
  137. cb.shape->BigPoly(cb2.shape.get());
  138. cb3.shape->BigPoly(cb2.shape.get());
  139. cb2.shape->BigPoly(cb.shape.get());
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment