PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/GameLogics/World.cpp

https://bitbucket.org/maartenvandenheuvel/tyrian
C++ | 187 lines | 153 code | 28 blank | 6 comment | 30 complexity | 835c0d840afabf6d042b8ba205cbbd45 MD5 | raw file
  1. /*
  2. * World.cpp
  3. *
  4. * Created on: Dec 27, 2011
  5. * Author: maarten
  6. */
  7. #include "World.h"
  8. #include "Factory.h"
  9. #include "Entity.h"
  10. #include <list>
  11. #include <iostream>
  12. #include <stdlib.h>
  13. using namespace Tyrian;
  14. using namespace std;
  15. World::World( Factory* fact)
  16. : thisFactory(fact),thisKeyboard(thisFactory->CreateKeyboard()), thisStopwatch(thisFactory->CreateStopwatch()), lastFrame(thisStopwatch->GetTime())
  17. {
  18. srand( this->lastFrame );
  19. thisFactory->AddWorld(this);
  20. this->ObjectList.push_back( thisFactory->CreatePlayer());
  21. }
  22. void World::AddToList( Entity* src)
  23. {
  24. this->ObjectList.push_back( src );
  25. }
  26. World::~World()
  27. {
  28. std::list<Entity*>::iterator it = this->ObjectList.begin();
  29. while ( it != this->ObjectList.end() )
  30. {
  31. (*it)->~Entity();
  32. this->ObjectList.erase(it);
  33. }
  34. }
  35. unsigned long World::GetFrameDelay()
  36. {
  37. unsigned long passedTime;
  38. unsigned long temp = thisStopwatch->GetTime();
  39. passedTime = temp - lastFrame;
  40. lastFrame = temp;
  41. cout << "Last frame: " << lastFrame << endl;
  42. cout << "Passed time: " << passedTime << endl;
  43. return passedTime;
  44. }
  45. void World::Run()
  46. {
  47. while ( !this->thisKeyboard->KeyEscapePressed() )
  48. {
  49. this->Collision();
  50. this->Move();
  51. this->CreateEnemy();
  52. }
  53. }
  54. void World::Move()
  55. {
  56. list<Entity*>::iterator it = this->ObjectList.begin();
  57. unsigned long frameDelay = (this->GetFrameDelay() );
  58. while ( it != this->ObjectList.end() )
  59. {
  60. cout << "Got here 1" << endl;
  61. frameDelay += (this->GetFrameDelay() );
  62. (*it)->Move( frameDelay );
  63. it++;
  64. }
  65. }
  66. void World::OutOfBounds()
  67. {
  68. list<Entity*>::iterator it = this->ObjectList.begin();
  69. while( it != this->ObjectList.end())
  70. {
  71. if( ((*it)->GetCoord()->GetX()) > (XAXIS/2 + (*it)->GetXSize()))
  72. {
  73. delete *it;
  74. it = this->ObjectList.erase(it);
  75. }
  76. else if( ((*it)->GetCoord()->GetX()) < -(XAXIS/2 + (*it)->GetXSize()))
  77. {
  78. delete *it;
  79. it = this->ObjectList.erase(it);
  80. }
  81. else if( ((*it)->GetCoord()->GetY()) > (YAXIS/2 + (*it)->GetYSize()))
  82. {
  83. delete *it;
  84. it = this->ObjectList.erase(it);
  85. }
  86. else if( ((*it)->GetCoord()->GetY()) < -(YAXIS/2 + (*it)->GetYSize()))
  87. {
  88. delete *it;
  89. it = this->ObjectList.erase(it);
  90. }
  91. else
  92. {
  93. it++;
  94. }
  95. }
  96. cout << "List size: " << this->ObjectList.size() << endl;
  97. }
  98. void World::Collision()
  99. {
  100. this->OutOfBounds();
  101. cout << "Got to Collision" << endl;
  102. list<Entity*>::iterator it = this->ObjectList.begin();
  103. while ( it != this->ObjectList.end())
  104. {
  105. list<Entity*>::iterator it2 = this->ObjectList.begin();
  106. while ( it2 != this->ObjectList.end())
  107. {
  108. if (((*it2)->GetCoord()->GetX() <= ((*it)->GetCoord()->GetX() + (*it)->GetXSize())
  109. && ((*it2)->GetCoord()->GetX() >= ((*it)->GetCoord()->GetX()))
  110. && ((*it2)->GetCoord()->GetY() <= ((*it)->GetCoord()->GetY() + (*it)->GetYSize()))
  111. && ((*it2)->GetCoord()->GetY() >= ((*it)->GetCoord()->GetY())))
  112. || (((*it)->GetCoord()->GetX() <= ((*it2)->GetCoord()->GetX() + (*it2)->GetXSize())
  113. && ((*it)->GetCoord()->GetX() >= ((*it2)->GetCoord()->GetX()))
  114. && ((*it)->GetCoord()->GetY() <= ((*it2)->GetCoord()->GetY() + (*it2)->GetYSize()))
  115. && ((*it)->GetCoord()->GetY() >= ((*it2)->GetCoord()->GetY())))))
  116. {
  117. cout << "Collided" << endl;
  118. if((*it)->CollidesWith((*it2)))
  119. {
  120. if ((*it2)->CollidesWith((*it)))
  121. {
  122. delete *it2;
  123. delete *it;
  124. this->ObjectList.erase(it2);
  125. it = this->ObjectList.erase(it);
  126. it2 = this->ObjectList.end();
  127. }
  128. else
  129. {
  130. delete *it;
  131. it = this->ObjectList.erase(it);
  132. it2 = this->ObjectList.end();
  133. }
  134. }
  135. else if ((*it2)->CollidesWith((*it)))
  136. {
  137. delete *it2;
  138. it2 = this->ObjectList.erase(it2);
  139. }
  140. else
  141. {
  142. it2++;
  143. }
  144. }
  145. else
  146. {
  147. it2++;
  148. }
  149. }
  150. it++;
  151. }
  152. }
  153. void World::CreateEnemy()
  154. {
  155. if ( ((rand() % 100)) > SPAWNCHANCE )
  156. {
  157. this->AddToList(this->thisFactory->CreateEnemy( (rand() % XAXIS) - (XAXIS/2) ));
  158. }
  159. }