Missile.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "Missile.h"
  2. Missile::Missile()
  3. :Shot( MISSILE_DEGAT ), m_xHim( 0 ), m_yHim( 0 ), m_hasTgt( false ), m_decay( 0 ), m_dir( MISSILE_BOOST )
  4. {
  5. //ctor 1
  6. }
  7. Missile::Missile( Sint16 xMe, Sint16 yMe, bool ally )
  8. :Shot( MISSILE_DEGAT, xMe, yMe, ally ), m_xHim( 0 ), m_yHim( 0 ), m_hasTgt( false ), m_decay( 0 ), m_dir( MISSILE_BOOST )
  9. {
  10. //ctor 2
  11. }
  12. Missile::Missile( Sint16 xMe, Sint16 yMe, Sint16 xHim, Sint16 yHim, bool ally )
  13. :Shot( MISSILE_DEGAT, xMe, yMe, ally ), m_xHim( xHim ), m_yHim( yHim ), m_hasTgt( true ), m_decay( 0 ), m_dir( MISSILE_BOOST )
  14. {
  15. //ctor 3
  16. if ( ally )
  17. m_decay = abs( yHim - yMe ) / MISSILE_TURN_SPEED;
  18. }
  19. Missile::~Missile()
  20. {
  21. //dtor
  22. }
  23. void Missile::update()
  24. {
  25. if ( m_dir == MISSILE_BOOST )
  26. {
  27. if ( m_ally )
  28. {
  29. m_x -= MISSILE_BOOST_SPEED;
  30. m_xHim += 1;
  31. if ( m_hasTgt && m_xHim >= m_x ) turn();
  32. }
  33. else
  34. {
  35. m_x += MISSILE_BOOST_SPEED;
  36. if ( m_hasTgt && m_xHim <= m_x ) turn();
  37. }
  38. }
  39. else
  40. {
  41. m_y += m_dir * MISSILE_TURN_SPEED;
  42. }
  43. }
  44. void Missile::draw( SDL_Surface* screen )
  45. {
  46. if ( !m_exist )
  47. return;
  48. switch ( m_dir )
  49. {
  50. case MISSILE_BOOST:
  51. if ( m_ally ) stringRGBA(screen, m_x - 8, m_y - 4, "<-", 255, 120, 0, 255);
  52. else stringRGBA(screen, m_x - 8, m_y - 4, "->", 255, 120, 0, 255);
  53. break;
  54. case MISSILE_DOWN:
  55. stringRGBA(screen, m_x - 4, m_y - 8, "|", 255, 120, 0, 255);
  56. stringRGBA(screen, m_x - 4, m_y, "v", 255, 120, 0, 255);
  57. break;
  58. case MISSILE_UP:
  59. stringRGBA(screen, m_x - 4, m_y - 8, "^", 255, 120, 0, 255);
  60. stringRGBA(screen, m_x - 4, m_y, "|", 255, 120, 0, 255);
  61. break;
  62. default:
  63. stringRGBA(screen, m_x - 4, m_y - 4, "X", 255, 120, 0, 255);
  64. break;
  65. }
  66. }
  67. bool Missile::damageSolid( Uint8** solid, const Uint16 dimH, const Uint16 dimW, const SDL_Rect& hitbox )
  68. {
  69. // Coordonnées impact
  70. Uint16 yImpct, xImpct;
  71. if ( m_dir == MISSILE_BOOST )
  72. {
  73. // Pénétration axe horizontal
  74. yImpct = ( m_y - hitbox.y ) / 8;
  75. if ( m_ally )
  76. xImpct = dimW - 1;
  77. else
  78. xImpct = 0;
  79. while ( solid[yImpct][xImpct] == 0 )
  80. {
  81. if ( m_ally )
  82. xImpct --;
  83. else
  84. xImpct ++;
  85. if ( xImpct >= dimW )
  86. return false;
  87. }
  88. }
  89. else
  90. {
  91. // Pénétration verticale
  92. xImpct = ( m_x - hitbox.x ) / 8;
  93. Sint8 adder( 0 );
  94. if ( m_dir == MISSILE_UP ) {
  95. yImpct = dimH - 1;
  96. adder = MISSILE_UP;
  97. }
  98. else {
  99. yImpct = 0;
  100. adder = MISSILE_DOWN;
  101. }
  102. while ( solid[yImpct][xImpct] == 0 )
  103. {
  104. yImpct += adder;
  105. if ( yImpct >= dimH )
  106. return false;
  107. }
  108. }
  109. // Déstruction du centre
  110. damageBloc( solid, dimH, dimW, yImpct, xImpct, 255 );
  111. // Forts dégats sur l'immédiat
  112. #define MISSILE_NEAREST_BAM 250
  113. damageBloc( solid, dimH, dimW, yImpct - 1, xImpct, MISSILE_NEAREST_BAM );
  114. damageBloc( solid, dimH, dimW, yImpct + 1, xImpct, MISSILE_NEAREST_BAM );
  115. damageBloc( solid, dimH, dimW, yImpct, xImpct - 1, MISSILE_NEAREST_BAM );
  116. damageBloc( solid, dimH, dimW, yImpct, xImpct + 1, MISSILE_NEAREST_BAM );
  117. damageBloc( solid, dimH, dimW, yImpct - 1, xImpct - 1, MISSILE_NEAREST_BAM );
  118. damageBloc( solid, dimH, dimW, yImpct + 1, xImpct - 1, MISSILE_NEAREST_BAM );
  119. damageBloc( solid, dimH, dimW, yImpct - 1, xImpct + 1, MISSILE_NEAREST_BAM );
  120. damageBloc( solid, dimH, dimW, yImpct + 1, xImpct + 1, MISSILE_NEAREST_BAM );
  121. // Faibles dégats sur l'adjacent
  122. #define MISSILE_FAR_BAM 200
  123. damageBloc( solid, dimH, dimW, yImpct - 2, xImpct, MISSILE_FAR_BAM );
  124. damageBloc( solid, dimH, dimW, yImpct + 2, xImpct, MISSILE_FAR_BAM );
  125. damageBloc( solid, dimH, dimW, yImpct, xImpct - 2, MISSILE_FAR_BAM );
  126. damageBloc( solid, dimH, dimW, yImpct, xImpct + 2, MISSILE_FAR_BAM );
  127. // Déstruction du projectile
  128. m_exist = false;
  129. return true;
  130. }
  131. void Missile::turn()
  132. {
  133. m_x = m_xHim + m_decay ;
  134. if ( m_yHim > m_y )
  135. m_dir = MISSILE_DOWN;
  136. else
  137. m_dir = MISSILE_UP;
  138. }