Power.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "Power.h"
  2. Power::Power(SDL_Surface *screen, Collisions *gestionnaireCollisons, Sounderer *mediaPlayer, SDL_Surface* apparence)
  3. : m_screen(screen), m_image(apparence), m_gestionnaireCollisions(gestionnaireCollisons), m_mediaPlayer(mediaPlayer),
  4. m_used(false), m_estPose(true)
  5. {
  6. }///Constructeur
  7. Power::~Power()
  8. {
  9. }///Destructeur
  10. //Fonctions pour le tableauPower
  11. void Power::attribuer()
  12. {
  13. m_estPose = false;
  14. //SDL_SetAlpha(m_image, SDL_SRCALPHA, 128);
  15. }///attribuer
  16. void Power::afficher(int x, int y)
  17. {
  18. m_position.x = x * 20;
  19. m_position.y = y * 20;
  20. SDL_BlitSurface(m_image, 0, m_screen, &m_position);
  21. }///afficher
  22. bool Power::estUtilise()
  23. {
  24. return m_used;
  25. }
  26. bool Power::estPose()
  27. {
  28. return m_estPose;
  29. }///estAttribue
  30. void Power::convertDir(int orientation, int &ajoutX, int &ajoutY) const
  31. {
  32. ajoutX = 0;
  33. ajoutY = 0;
  34. switch (orientation)
  35. {
  36. case HAUT:
  37. ajoutY=-1;
  38. break;
  39. case DROITE:
  40. ajoutX=1;
  41. break;
  42. case BAS:
  43. ajoutY=1;
  44. break;
  45. case GAUCHE:
  46. ajoutX=-1;
  47. break;
  48. }
  49. }
  50. /********************************************* class TableauPower *********************************************/
  51. TableauPower::TableauPower(SDL_Surface *screen, Collisions *gestionnaireCollisons, int nbPow)
  52. : m_screen(screen), m_gestionnaireCollisions(gestionnaireCollisons), m_spriteGet(0), m_mediaPlayer(0), m_nbPow(nbPow)
  53. {
  54. //Sprites loader
  55. m_spriteGet = new SpriteLoader("Textures/");
  56. SDL_SetColorKey(m_spriteGet->takeSprite("Missile"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  57. SDL_SetColorKey(m_spriteGet->takeSprite("Expansion"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  58. SDL_SetColorKey(m_spriteGet->takeSprite("Boost"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  59. SDL_SetColorKey(m_spriteGet->takeSprite("Teleport"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  60. SDL_SetColorKey(m_spriteGet->takeSprite("Bouclier"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  61. SDL_SetColorKey(m_spriteGet->takeSprite("Trender"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  62. SDL_SetColorKey(m_spriteGet->takeSprite("Tunnel"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  63. SDL_SetColorKey(m_spriteGet->takeSprite("Bomb"), SDL_SRCCOLORKEY ,SDL_MapRGB(screen->format,255,255,255));
  64. //Sounderer
  65. m_mediaPlayer = new Sounderer("Sons/");
  66. if (m_mediaPlayer->init())
  67. {
  68. m_mediaPlayer->preLoad("Mort", EXT_OGG);
  69. m_mediaPlayer->preLoad("Missile", EXT_OGG);
  70. m_mediaPlayer->preLoad("Expansion", EXT_OGG);
  71. m_mediaPlayer->preLoad("Boost", EXT_OGG);
  72. m_mediaPlayer->preLoad("Teleport", EXT_OGG);
  73. m_mediaPlayer->preLoad("Bouclier", EXT_OGG);
  74. m_mediaPlayer->preLoad("Trender", EXT_OGG);
  75. m_mediaPlayer->preLoad("Tunnel", EXT_OGG);
  76. m_mediaPlayer->preLoad("Bomb", EXT_OGG);
  77. //m_mediaPlayer->startMusic("musicMadagascar");
  78. }
  79. //Tableaux statiques
  80. m_tableau = new Power*[m_nbPow];
  81. for ( int i(0); i<2; i++ )
  82. m_positions[i] = new int[m_nbPow];
  83. // Powers
  84. for (int i(0); i<m_nbPow; i++)
  85. m_tableau[i] = 0;
  86. }///Constructeur
  87. TableauPower::~TableauPower()
  88. {
  89. //Sprites loader
  90. delete m_spriteGet;
  91. //Sounderer
  92. delete m_mediaPlayer;
  93. //Powers
  94. for (int i(0); i<m_nbPow; i++)
  95. {
  96. if (m_tableau[i] == 0)
  97. {}//Ne fait rien si le pointeur est déjà vide.
  98. else
  99. delete m_tableau[i];
  100. }
  101. // Tableaux
  102. for ( int i(0); i<2; i++ )
  103. delete[] m_positions[i];
  104. delete[] m_tableau;
  105. }///Destructeur
  106. Power* TableauPower::chercherPouvoir(int x, int y) const
  107. {
  108. Power *pouvoirPotentiel(0);
  109. for (int i(0); i<m_nbPow; i++)
  110. if (m_tableau[i]!=0)
  111. if (x == m_positions[0][i] && y == m_positions[1][i] && m_tableau[i]->estPose())
  112. {
  113. pouvoirPotentiel = m_tableau[i];
  114. m_tableau[i]->attribuer();
  115. }
  116. return pouvoirPotentiel;
  117. }///chercherPouvoir
  118. Power* TableauPower::randomPower()
  119. {
  120. //[1] Démarrage
  121. Power *pouvoirAlea(0);
  122. //[2] Calculs alétoires
  123. switch (rand() % 8)
  124. {
  125. case 0:
  126. pouvoirAlea = new Missile(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Missile"));
  127. break;
  128. case 1:
  129. pouvoirAlea = new Expansion(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Expansion"));
  130. break;
  131. case 2:
  132. pouvoirAlea = new Boost(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Boost"));
  133. break;
  134. case 3:
  135. pouvoirAlea = new Teleport(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Teleport"));
  136. break;
  137. case 4:
  138. pouvoirAlea = new Bouclier(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Bouclier"));
  139. break;
  140. case 5:
  141. pouvoirAlea = new Trender(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Trender"));
  142. break;
  143. case 6:
  144. pouvoirAlea = new Tunnel(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Tunnel"));
  145. break;
  146. case 7:
  147. pouvoirAlea = new Bomb(m_screen, m_gestionnaireCollisions, m_mediaPlayer, m_spriteGet->takeSprite("Bomb"));
  148. break;
  149. }
  150. //[3] Return
  151. return pouvoirAlea;
  152. }///randomPower
  153. void TableauPower::initialiser()
  154. {
  155. for (int i(0); i<m_nbPow; i++)
  156. {
  157. if (m_tableau[i] != 0)
  158. {
  159. delete m_tableau[i];
  160. m_tableau[i] = 0;
  161. }
  162. m_tableau[i] = randomPower();
  163. m_positions[0][i] = rand() % m_gestionnaireCollisions->getSize(0);// x
  164. m_positions[1][i] = rand() % m_gestionnaireCollisions->getSize(1);// y
  165. }//for
  166. }///initialiser
  167. void TableauPower::actualiser()
  168. {
  169. for (int i(0); i<m_nbPow; i++)
  170. if (m_tableau[i]!=0)
  171. if (m_tableau[i]->estUtilise())
  172. {
  173. delete m_tableau[i];
  174. m_tableau[i]=0;
  175. }
  176. }///actualiser
  177. void TableauPower::afficher()
  178. {
  179. for (int i(0); i<m_nbPow; i++)
  180. if (m_tableau[i]!=0)
  181. if (m_tableau[i]->estPose())
  182. m_tableau[i]->afficher(m_positions[0][i], m_positions[1][i]);
  183. }///afficher
  184. void TableauPower::bruiterMort()
  185. {
  186. m_mediaPlayer->play("Mort");
  187. }///bruiterMort