droidworld.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "game.h"
  2. #include "droidworld.h"
  3. DroidWorld::DroidWorld( float w, float h)
  4. {
  5. objectList = 0;
  6. width = w;
  7. height = h;
  8. }
  9. DroidWorld::~DroidWorld()
  10. {
  11. releaseAll();
  12. }
  13. void DroidWorld::prepeare()
  14. {
  15. // Images
  16. imageBall = loadVGImage("images/planet.png");
  17. imageAsteroid = loadVGImage("images/asteroid.png");
  18. }
  19. void DroidWorld::release()
  20. {
  21. if (imageBall!=VG_INVALID_HANDLE) {
  22. vgDestroyImage(imageBall); imageBall=VG_INVALID_HANDLE;
  23. }
  24. if (imageAsteroid!=VG_INVALID_HANDLE) {
  25. vgDestroyImage(imageAsteroid); imageAsteroid=VG_INVALID_HANDLE;
  26. }
  27. }
  28. DroidObject* DroidWorld::addObject( DroidObject *toAdd )
  29. {
  30. SDObjectCapsule *ncap = new SDObjectCapsule;
  31. ncap->next = objectList;
  32. ncap->obj = toAdd;
  33. objectList = ncap;
  34. return toAdd;
  35. }
  36. void DroidWorld::releaseAll() {
  37. SDObjectCapsule *l = objectList;
  38. while (l) {
  39. SDObjectCapsule *n = l->next;
  40. if (l->obj) {
  41. delete l->obj;
  42. l->obj = 0;
  43. }
  44. delete l;
  45. l = n;
  46. }
  47. objectList = 0;
  48. }
  49. void DroidWorld::run( float secsPassed )
  50. {
  51. float sqDistance;
  52. QVector3D temp;
  53. SDObjectCapsule *prev = 0;
  54. SDObjectCapsule *l = objectList;
  55. while (l) {
  56. if (l->obj) {
  57. if (l->obj->isAlive()) {
  58. l->obj->run( secsPassed );
  59. // mass against other object
  60. SDObjectCapsule *gravl = objectList;
  61. while (gravl) {
  62. if (l!=gravl && gravl->obj && gravl->obj->isAlive()) { // ignore self, empty and dead.
  63. temp = l->obj->getPosition() - gravl->obj->getPosition();
  64. sqDistance = QVector3D::dotProduct( temp, temp );
  65. temp = temp/(sqDistance+0.1f) * GENERAL_GRAVITY_POWER;
  66. l->obj->getDirection() += -temp*gravl->obj->getMass() / l->obj->getMass() * secsPassed;
  67. gravl->obj->getDirection() += temp*l->obj->getMass() / gravl->obj->getMass() * secsPassed;
  68. }
  69. gravl = gravl->next;
  70. }
  71. } else {
  72. // should be dead, . remove the capsule
  73. }
  74. }
  75. prev = l;
  76. l = l->next;
  77. }
  78. }
  79. void DroidWorld::draw()
  80. {
  81. SDObjectCapsule *l = objectList;
  82. while (l) {
  83. if (l->obj) {
  84. if (l->obj->isAlive()) l->obj->draw();
  85. }
  86. l = l->next;
  87. }
  88. }
  89. void DroidWorld::setMyTransform( DroidObject *o ) {
  90. vgLoadIdentity();
  91. vgTranslate( width/2, height/2);
  92. vgRotate( o->getAngle() );
  93. vgTranslate( o->getPosition().x() * width - o->getDrawSize()/2,
  94. o->getPosition().y()*height - o->getDrawSize()/2.0f );
  95. //vgScale(); // scale here.
  96. }