TinyWorld.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. #include "TinyWorld.h"
  5. #include "ScullingQuery.h"
  6. #include "Wall.h"
  7. #define MAX_BLOCS_NUMBER 350
  8. #define MIN_BLOCS_NUMBER 6
  9. #define MAX_WIDTH 150
  10. #define MIN_WIDTH 50
  11. #define MAX_HEIGHT 60
  12. #define MIN_HEIGHT 10
  13. TinyWorld::TinyWorld(const b2Vec2 &gravity) : b2World(gravity) {}
  14. void TinyWorld::createProceduralWorld() {
  15. // Determine number of blocs
  16. int numberOfBlocs(MIN_BLOCS_NUMBER + rand() % (MAX_BLOCS_NUMBER - MIN_BLOCS_NUMBER));
  17. // Dims
  18. float width(MIN_WIDTH + rand() % (MAX_WIDTH - MIN_WIDTH));
  19. float height(MIN_HEIGHT + rand() % (MAX_HEIGHT - MIN_HEIGHT));
  20. // Bloc sizes
  21. WallShape shapeTab[4] = {TINY, HIGH, WIDE, BIG};
  22. // Generate
  23. for (int k(0); k < numberOfBlocs; k++) {
  24. addEntity(new Wall(b2Vec2(width * rand() / RAND_MAX, height * rand() / RAND_MAX), this, shapeTab[rand() % 4]));
  25. }
  26. }
  27. void TinyWorld::addEntity(Entity *newcomer) {
  28. m_entities.push_back(newcomer);
  29. }
  30. void TinyWorld::clearEveryEntity() {
  31. auto it(m_entities.begin());
  32. while (it != m_entities.end()) {
  33. delete (*it);
  34. it = m_entities.erase(it);
  35. }
  36. }
  37. void TinyWorld::updateAll() {
  38. for (auto it(m_entities.begin()); it != m_entities.end(); it++) {
  39. // Clean dead entities
  40. while (it != m_entities.end() && !(*it)->isExist()) {
  41. delete (*it);
  42. it = m_entities.erase(it);
  43. }
  44. // Update living entity
  45. if (it != m_entities.end())
  46. (*it)->update();
  47. }
  48. }
  49. void TinyWorld::collectVisuals(std::vector<Visual *> &scope, b2Vec2 center, b2Vec2 diago) {
  50. /*for (auto it(m_entities.begin()); it != m_entities.end(); it++) {
  51. scope.push_back((*it)->makeVisual());
  52. }*/
  53. ScullingQuery callback;
  54. b2AABB aabb;
  55. aabb.lowerBound = center - diago;
  56. aabb.upperBound = center + diago;
  57. QueryAABB(&callback, aabb);
  58. Entity *currentEntity;
  59. for (auto it(callback.getTab().begin()); it != callback.getTab().end(); it++) {
  60. currentEntity = (Entity *) (*it)->GetUserData().pointer;
  61. scope.push_back(currentEntity->makeVisual());
  62. }
  63. }