Photo.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "Photo.h"
  2. Photo::Photo()
  3. :Shot( PHOTO_DEGAT ), m_vx( -PHOTO_SPEED ), m_vy( 0 )
  4. {
  5. //ctor 1
  6. }
  7. Photo::Photo( Sint16 xMe, Sint16 yMe, bool ally )
  8. :Shot( PHOTO_DEGAT, xMe, yMe, ally ), m_vx( PHOTO_SPEED ), m_vy( 0 )
  9. {
  10. if ( ally )
  11. m_vx = -m_vx;
  12. }
  13. Photo::Photo( Sint16 xMe, Sint16 yMe, Sint16 xHim, Sint16 yHim, bool ally )
  14. :Shot( PHOTO_DEGAT, xMe, yMe, ally )
  15. {
  16. m_vx = xHim - xMe ;
  17. m_vy = yHim - yMe ;
  18. Sint16 norme( std::sqrt( m_vx * m_vx + m_vy * m_vy ) ) ;
  19. xHim += norme / PHOTO_SPEED ; // Anticipe le mouvement de l'ennemi !
  20. m_vx = xHim - xMe ;
  21. m_vy = yHim - yMe ;
  22. norme = std::sqrt(m_vx * m_vx + m_vy * m_vy ) ;
  23. m_vx = m_vx * PHOTO_SPEED / norme ;
  24. m_vy = m_vy * PHOTO_SPEED / norme ;
  25. }
  26. Photo::~Photo()
  27. {
  28. //dtor
  29. }
  30. void Photo::update()
  31. {
  32. m_x += m_vx;
  33. m_y += m_vy;
  34. }
  35. void Photo::draw( SDL_Surface* screen )
  36. {
  37. if ( m_exist )
  38. stringRGBA(screen, m_x - 4, m_y - 4, "o", 0, 100, 255, 255);
  39. }
  40. bool Photo::damageSolid( Uint8** solid, const Uint16 dimH, const Uint16 dimW, const SDL_Rect& hitbox )
  41. {
  42. // Coordonnées impact
  43. Uint16 yImpct, xImpct;
  44. if ( m_vy == 0 )
  45. {
  46. // Pénétration axe horizontal
  47. yImpct = ( m_y - hitbox.y ) / 8;
  48. if ( m_ally )
  49. xImpct = dimW - 1;
  50. else
  51. xImpct = 0;
  52. while ( solid[yImpct][xImpct] == 0 )
  53. {
  54. if ( m_ally )
  55. xImpct --;
  56. else
  57. xImpct ++;
  58. if ( xImpct >= dimW )
  59. return false;
  60. }
  61. }
  62. else
  63. {
  64. // Pénétration verticale
  65. xImpct = ( m_x - hitbox.x ) / 8;
  66. Sint8 adder( 0 );
  67. if ( m_vy < 0 ) {
  68. yImpct = dimH - 1;
  69. adder = -1;
  70. }
  71. else {
  72. yImpct = 0;
  73. adder = 1;
  74. }
  75. while ( solid[yImpct][xImpct] == 0 )
  76. {
  77. yImpct += adder;
  78. if ( yImpct >= dimH )
  79. return false;
  80. }
  81. }
  82. // Déstruction du centre
  83. damageBloc( solid, dimH, dimW, yImpct, xImpct, 123 );
  84. // Déstruction du projectile
  85. m_exist = false;
  86. return true;
  87. }