Sniper.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "Sniper.h"
  2. Sniper::Sniper()
  3. :Shot( SNIPER_DEGAT ), m_velocity( -SNIPER_MIN_SPEED )
  4. {
  5. //ctor
  6. }
  7. Sniper::Sniper( Sint16 x, Sint16 y, bool ally )
  8. :Shot( SNIPER_DEGAT, x, y, ally ), m_velocity( SNIPER_MIN_SPEED )
  9. {
  10. if ( ally )
  11. m_velocity = -m_velocity;
  12. }
  13. Sniper::~Sniper()
  14. {
  15. //dtor
  16. }
  17. void Sniper::update()
  18. {
  19. m_x += m_velocity;
  20. if ( m_ally )
  21. {
  22. m_velocity -= SNIPER_ACC;
  23. if ( m_velocity < -SNIPER_MAX_SPEED )
  24. m_velocity = -SNIPER_MAX_SPEED;
  25. }
  26. else
  27. {
  28. m_velocity += SNIPER_ACC;
  29. if ( m_velocity > SNIPER_MAX_SPEED )
  30. m_velocity = SNIPER_MAX_SPEED;
  31. }
  32. }
  33. void Sniper::draw( SDL_Surface* screen )
  34. {
  35. if ( m_exist )
  36. stringRGBA(screen, m_x - 4, m_y - 4, "=",
  37. 255 - (abs( m_velocity ) - SNIPER_MIN_SPEED) * 255 / (SNIPER_MAX_SPEED - SNIPER_MIN_SPEED),
  38. 255,
  39. (abs( m_velocity ) - SNIPER_MIN_SPEED) * 255 / (SNIPER_MAX_SPEED - SNIPER_MIN_SPEED),
  40. 255);
  41. }
  42. bool Sniper::damageSolid( Uint8** solid, const Uint16 dimH, const Uint16 dimW, const SDL_Rect& hitbox )
  43. {
  44. // Déterminer l'ordonnée d'impact
  45. Uint16 yImpct( ( m_y - hitbox.y ) / 8 );
  46. // Détermination de l'abscisse d'impact
  47. Uint16 xImpct(0);
  48. if ( m_ally )
  49. xImpct = dimW - 1;
  50. while ( solid[yImpct][xImpct] == 0 )
  51. {
  52. if ( m_ally )
  53. xImpct --;
  54. else
  55. xImpct ++;
  56. if ( xImpct >= dimW )
  57. return false;
  58. }
  59. // Déstruction
  60. Uint8 power( abs( m_velocity ) * 255 / SNIPER_MAX_SPEED );
  61. while ( xImpct < dimW && power >= SNIPER_BREAK )
  62. {
  63. // Dommage central
  64. if ( damageBloc( solid, dimH, dimW, yImpct, xImpct, power ) )
  65. {
  66. // Baisse de puissance
  67. power -= SNIPER_BREAK;
  68. }
  69. // Dégâts latéraux
  70. damageBloc( solid, dimH, dimW, yImpct + 1, xImpct, power / 2 );
  71. damageBloc( solid, dimH, dimW, yImpct - 1, xImpct, power / 2 );
  72. // Mouvement
  73. if ( m_ally )
  74. xImpct --;
  75. else
  76. xImpct ++;
  77. }
  78. if ( xImpct < dimW && power > 0 )
  79. damageBloc( solid, dimH, dimW, yImpct, xImpct, power );
  80. // Déstruction du projectile
  81. m_exist = false;
  82. return true;
  83. }