WaveManager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "WaveManager.h"
  2. WaveManager::WaveManager()
  3. :m_lvl(0), m_chrono( SDL_GetTicks() ), m_allDead( true ), m_complete( false ), m_gameLost( false ), m_planet("Sideral Space"),
  4. m_hiter( 0x0 )
  5. {
  6. //ctor
  7. }
  8. WaveManager::~WaveManager()
  9. {
  10. //dtor
  11. }
  12. void WaveManager::giveHitManager( HitManager* theHitManager )
  13. {
  14. m_hiter = theHitManager;
  15. }
  16. void WaveManager::draw( SDL_Surface* screen )
  17. {
  18. // Affiche le n° de vague
  19. std::string wave( "Vague numero ");
  20. wave += char( m_lvl + 48 );
  21. stringRGBA( screen, 12, 10, wave.c_str(), 255, 255, 255, 255 );
  22. // Affiche le nome de la planète
  23. stringRGBA( screen, 12, 20, m_planet.c_str(), 255, 255, 255, 255 );
  24. // Affiche si victoire !!!
  25. if ( m_complete )
  26. {
  27. stringRGBA( screen, 12, 30, "Mission accomplie !", 255, 255, 255, 255 );
  28. stringRGBA( screen, 12, 40, "Appuyer sur ECHAP pour rentrer a la base.", 255, 255, 255, 255 );
  29. }
  30. // Affiche vaisseau
  31. for ( unsigned int i(0); i < m_escadron.size(); i++ ) {
  32. m_escadron[i]->draw( screen );
  33. }
  34. }
  35. void WaveManager::update()
  36. {
  37. // Actionne les ennemis
  38. for ( unsigned int i(0); i < m_escadron.size(); i++ ) {
  39. m_escadron[i]->update();
  40. // Regarde s'il sort de l'écran
  41. if ( m_escadron[i]->getPos()->x > 1280 )
  42. m_gameLost = true;
  43. }
  44. // Vague suivante
  45. if ( !m_allDead && !m_complete ){
  46. m_allDead = allDead();
  47. if ( m_allDead )
  48. m_chrono = SDL_GetTicks();
  49. }
  50. else if ( SDL_GetTicks() - m_chrono > 3000 ){
  51. nextWave();
  52. }
  53. }
  54. void WaveManager::clean()
  55. {
  56. while ( !m_escadron.empty() ) {
  57. delete m_escadron.back();
  58. m_escadron.back() = 0x0;
  59. m_escadron.pop_back();
  60. }
  61. }
  62. void WaveManager::reset()
  63. {
  64. clean();
  65. m_lvl = 0;
  66. m_allDead = true;
  67. m_complete = false;
  68. }
  69. bool WaveManager::allDead()
  70. {
  71. for ( unsigned int i(0); i < m_escadron.size(); i++ ) {
  72. if ( m_escadron[i]->getIsAlive() )
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool WaveManager::hasWon()
  78. {
  79. return m_complete;
  80. }
  81. bool WaveManager::hasLost()
  82. {
  83. return m_gameLost;
  84. }
  85. void WaveManager::setPlanet( std::string planet )
  86. {
  87. m_planet = planet;
  88. }
  89. std::vector< Foe* >* WaveManager::getFoes()
  90. {
  91. return &m_escadron;
  92. }
  93. void WaveManager::nextWave()
  94. {
  95. // Réinitialisation de l'état de l'escadron
  96. m_allDead = false;
  97. m_gameLost = false;
  98. // Augmentation de la difficulté
  99. m_lvl ++;
  100. // Destruction des anciens
  101. clean();
  102. // Création des nouveaux de manière procédurale
  103. /*Sint16 disp( 0 );
  104. if ( m_lvl > 1 )
  105. disp = 540 / (m_lvl - 1);
  106. for ( unsigned int i(0); i < m_lvl; i++) {
  107. m_escadron.push_back( new Foe );
  108. m_escadron.back()->loadShape("Resources/Ships/Bloc10.txt");
  109. m_escadron.back()->giveHitManager( m_hiter );
  110. Uint16 dec( m_escadron.back()->getHitBox()->w );
  111. m_escadron.back()->setPos( -dec, 50 + i * disp );
  112. }*/
  113. // Par lecture
  114. std::string lvlPath("Resources/Levels/" + m_planet + "/w_" + (char) (m_lvl + 48) + ".txt");
  115. std::ifstream flux( lvlPath.c_str() );
  116. std::string shipName;
  117. Sint16 shipPos;
  118. Sint16 shipLate;
  119. while ( flux >> shipName )
  120. {
  121. // Lecture
  122. flux >> shipPos;
  123. flux >> shipLate;
  124. // Création
  125. m_escadron.push_back( new Foe );
  126. m_escadron.back()->loadShape("Resources/Levels/" + m_planet + "/" + shipName + ".txt");
  127. m_escadron.back()->giveHitManager( m_hiter );
  128. Uint16 dec( m_escadron.back()->getHitBox()->w );
  129. m_escadron.back()->setPos( -dec - shipLate, 720 * shipPos / 100 );
  130. }
  131. // Si la vague est vide, c'est que c'est fini !
  132. if ( m_escadron.empty() )
  133. {
  134. m_complete = true;
  135. m_lvl --;
  136. }
  137. }
  138. SDL_Rect WaveManager::nearestFromPoint( SDL_Rect pos_ref )
  139. {
  140. SDL_Rect pos_rep( pos_ref );
  141. pos_rep.x = -10000; // Tire devant si pas de target
  142. SDL_Rect pos_i;
  143. Sint32 dist_min( 2147483647 ); // Nombre maximal du Sint16
  144. Sint32 dist_i;
  145. Sint32 x_i;
  146. Sint32 y_i;
  147. for ( unsigned int i(0); i < m_escadron.size(); i++ )
  148. {
  149. pos_i = m_escadron[i]->getHeartPos();
  150. x_i = ( pos_i.x - pos_ref.x );
  151. y_i = ( pos_i.y - pos_ref.y );
  152. dist_i = x_i * x_i + y_i * y_i;
  153. if ( dist_i < dist_min && m_escadron[i]->getIsAlive() )
  154. {
  155. dist_min = dist_i;
  156. pos_rep = pos_i;
  157. }
  158. }
  159. //pos_rep.x += std::sqrt( dist_min ) / 20 ;
  160. return pos_rep;
  161. }
  162. SDL_Rect WaveManager::nearestFromAxe( SDL_Rect pos_ref )
  163. {
  164. SDL_Rect pos_rep( pos_ref );
  165. pos_rep.x = -10000; // Tire devant si pas de target
  166. SDL_Rect pos_i;
  167. Sint32 dist_min( 2147483647 ); // Nombre maximal du Sint16
  168. Sint32 dist_i;
  169. for ( unsigned int i(0); i < m_escadron.size(); i++ )
  170. {
  171. pos_i = m_escadron[i]->getHeartPos();
  172. dist_i = abs( pos_i.y - pos_ref.y );
  173. if ( dist_i < dist_min && m_escadron[i]->getIsAlive() )
  174. {
  175. dist_min = dist_i;
  176. pos_rep = pos_i;
  177. }
  178. }
  179. return pos_rep;
  180. }