fork download
  1. #include <veritas/utilities/strong_typedef.hpp>
  2. #include <veritas/veritas.hpp>
  3. #include <SFML/System.hpp>
  4. #include <SFML/Graphics.hpp>
  5. #include <cmath>
  6.  
  7. veritas_strong_typedef(sf::Vector2f, Velocity);
  8. veritas_strong_typedef(sf::Vector2f, Position);
  9. veritas_strong_typedef(sf::SphereShape, Graphics);
  10. struct Collider { float radius; };
  11.  
  12. namespace vt = veritas;
  13.  
  14. //***************************
  15.  
  16. auto& movement_group = vt::create_entity_group<Velocity, Position>();
  17. auto& collision_group = vt::create_entity_group<Collider, Position>();
  18. auto& graphics_group = vt::create_entity_group<Graphics, Position>();
  19.  
  20. //***************************
  21.  
  22. void movement_system(float dt)
  23. {
  24. for (auto& entity : movement_group)
  25. {
  26. auto& position = entity.get_component<Position>();
  27. auto& velocity = entity.get_component<Velocity>();
  28. position += dt * velocity;
  29. }
  30. }
  31.  
  32. void collision_system()
  33. {
  34. for (auto& entity1 : collision_group)
  35. {
  36. auto& position1 = entity1.get_component<Position>();
  37. auto& collider1 = entity1.get_component<Collider>();
  38. for (auto& entity2 : collision_group)
  39. {
  40. auto& position2 = entity2.get_component<Position>();
  41. auto& collider2 = entity2.get_component<Collider>();
  42.  
  43. auto distance = [](Position pos1, Position pos2){
  44. Position diff = pos1 - pos2;
  45. return sqrt(diff.x*diff.x + diff.y*diff.y);};
  46.  
  47. if (entity1.id != entity2.id &&
  48. distance(position1, position2) < collider1.radius + collider2.radius)
  49. {
  50. vt::event_bus::store(CollisionEvent{entity1, entity2});
  51. }
  52. }
  53. }
  54. vt::event_bus::emit_stored<CollisionEvent>();
  55. }
  56.  
  57. void graphics_system(sf::RenderWindow& window)
  58. {
  59. window.clear();
  60. for (auto& entity : graphics_group)
  61. {
  62. auto& position = entity.get_component<Position>();
  63. auto& graphics = entity.get_component<Graphics>();
  64. graphics.setPosition(position);
  65. window.draw(graphics);
  66. }
  67. window.display();
  68. }
  69.  
  70. //****************************
  71.  
  72. void destroy_entities(const CollisionEvent& event)
  73. {
  74. vt::destroy_entity(event.e1);
  75. vt::destroy_entity(event.e2);
  76. }
  77.  
  78. //****************************
  79.  
  80. void create_sphere(Position pos, Velocity vel)
  81. {
  82. auto entity = vt::create_entity();
  83. entity.add_component<Position>(pos);
  84. entity.add_component<Velocity>(vel);
  85. auto& collider = entity.add_component<Collider>();
  86. collider.radius = 5;
  87. auto& graphics = entity.add_component<Graphics>();
  88. graphics.setSize(5);
  89. graphics.setFillColor(sf::Color::Red);
  90. }
  91.  
  92. int main()
  93. {
  94. vt::event_bus::subscribe<CollisionEvent>(destroy_entities);
  95.  
  96. sf::RenderWindow window(sf::VideoMode(800, 600), "veritas example");
  97.  
  98. sf::Clock clock;
  99.  
  100. float duration = 0;
  101.  
  102. for (int i = 0; i < 100; ++i)
  103. {
  104. create_sphere({rand() % 795, rand() % 595}, {rand() % 50, rand() % 50});
  105. }
  106.  
  107. while (window.isOpen())
  108. {
  109. duration += clock.restart().asSeconds();
  110. while (duration > 1/60.f)
  111. {
  112. vt::update_entity_groups();
  113. duration -= 1/60.f;
  114. movement_system(1/60.f);
  115. collision_system();
  116. }
  117. graphics_system();
  118. }
  119.  
  120. return 0;
  121. }
  122.  
  123.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:48: fatal error: veritas/utilities/strong_typedef.hpp: No such file or directory
 #include <veritas/utilities/strong_typedef.hpp>
                                                ^
compilation terminated.
stdout
Standard output is empty