HitManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "HitManager.h"
  2. #include "Shot/Gun.h"
  3. #include "Shot/Part.h"
  4. #include "Shot/Cutter.h"
  5. #include "Shot/Disper.h"
  6. #include "Shot/Photo.h"
  7. #include "Shot/Missile.h"
  8. #include "Shot/Brexit.h"
  9. #include "Shot/Hecto.h"
  10. #include "Shot/Sniper.h"
  11. HitManager::HitManager()
  12. :m_window( {0, 0, 1280, 720} )
  13. {
  14. //ctor
  15. }
  16. HitManager::~HitManager()
  17. {
  18. //dtor
  19. }
  20. void HitManager::draw( SDL_Surface* screen )
  21. {
  22. for ( unsigned int i(0); i < m_shots.size(); i++) {
  23. m_shots[i]->draw( screen );
  24. }
  25. }
  26. void HitManager::addShot( Shot* tir )
  27. {
  28. m_shots.push_back( tir );
  29. }
  30. void HitManager::triggerWeapon( int weapId, Sint16 x, Sint16 y, bool ally )
  31. {
  32. switch ( weapId )
  33. {
  34. case GUN_ID :
  35. addShot( new Gun( x, y, ally ) );
  36. break;
  37. case CUTTER_ID :
  38. addShot( new Cutter( x, y, ally ) );
  39. break;
  40. case DISPER_ID :
  41. for ( int vy(-4); vy <= 4; vy++ )
  42. addShot( new Disper( x, y, vy, ally ) );
  43. break;
  44. case PHOTO_ID :
  45. if ( ally )
  46. addShot( new Photo( x, y, m_nearPt.x, m_nearPt.y, ally ) );
  47. else
  48. addShot( new Photo( x, y, m_nearAlly.x, m_nearAlly.y, ally ) );
  49. break;
  50. case MISSILE_ID :
  51. if ( ally )
  52. addShot( new Missile( x, y, m_nearAxe.x, m_nearAxe.y, ally ) );
  53. else
  54. addShot( new Missile( x, y, m_nearAlly.x, m_nearAlly.y, ally ) );
  55. break;
  56. case BREXIT_ID :
  57. addShot( new Brexit(x, y, true, ally ) );
  58. addShot( new Brexit(x, y, false, ally ) );
  59. break;
  60. case HECTO_ID :
  61. addShot( new Hecto( x, y, ally ) );
  62. break;
  63. case SNIPER_ID :
  64. addShot( new Sniper( x, y, ally ) );
  65. break;
  66. default :
  67. break;
  68. }
  69. }
  70. Sint32 HitManager::absorb( bool isAlly, const Uint16 dimH, const Uint16 dimW, const SDL_Rect& hitbox )
  71. {
  72. Sint32 deg( 0 );
  73. for ( unsigned int i(0); i < m_shots.size(); i++) {
  74. if ( isAlly != m_shots[i]->getIsAlly() && m_shots[i]->isIn( hitbox ) )
  75. deg += m_shots[i]->takeDegat();
  76. }
  77. return deg;
  78. }
  79. bool HitManager::colide( bool isAlly, Uint8** solid, const Uint16 dimH, const Uint16 dimW, const SDL_Rect& hitbox )
  80. {
  81. bool rep( false );
  82. for ( unsigned int i(0); i < m_shots.size(); i++) {
  83. if ( isAlly != m_shots[i]->getIsAlly() && m_shots[i]->isIn( hitbox ) )
  84. rep = rep || m_shots[i]->damageSolid( solid, dimH, dimW, hitbox );
  85. }
  86. return rep;
  87. }
  88. void HitManager::update()
  89. {
  90. // Raccourcissement de la file
  91. while ( !m_shots.empty() && ( !m_shots.front()->isIn(m_window) || !m_shots.front()->getExist() ) ) {
  92. delete m_shots.front();
  93. m_shots.front() = 0x0;
  94. m_shots.pop_front();
  95. }
  96. // Déplacement des tirs
  97. for ( unsigned int i(0); i < m_shots.size(); i++) {
  98. m_shots[i]->update() ;
  99. // Destruction des tirs hors champs
  100. if ( !m_shots[i]->isIn( m_window ) )
  101. m_shots[i]->setExist( false );
  102. }
  103. }
  104. void HitManager::clean()
  105. {
  106. while ( !m_shots.empty() ) {
  107. delete m_shots.front();
  108. m_shots.front() = 0x0;
  109. m_shots.pop_front();
  110. }
  111. }
  112. unsigned int HitManager::getDecay( unsigned int weapId )
  113. {
  114. if ( weapId >= SHOT_NB )
  115. {
  116. std::cout << "getDecay : Bug cher ami !" << std::endl;
  117. return 16;
  118. }
  119. unsigned int decTab[ SHOT_NB ] = ACTOR_DECAY;
  120. return decTab[ weapId ];
  121. }
  122. void HitManager::setNearPt( SDL_Rect nearPt )
  123. {
  124. m_nearPt = nearPt;
  125. }
  126. void HitManager::setNearAxe( SDL_Rect nearAxe )
  127. {
  128. m_nearAxe = nearAxe;
  129. }
  130. void HitManager::setNearAlly( SDL_Rect nearAlly )
  131. {
  132. m_nearAlly = nearAlly;
  133. }