Liner.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "Liner.h"
  2. static SDL_Surface* s_linerSurface[4];
  3. static SDL_Surface* s_trainee[15];
  4. static SDL_Surface* s_mort(0);
  5. Liner::Liner(int x, int y, int orientation, int id, SDL_Surface *screen, SDL_Surface *pseudo, Collisions *collisioneur, TableauPower *powerGetter)
  6. : m_x(x), m_y(y), m_orientation(orientation), m_id(id), m_estVivant(false), m_fin(false), m_meurtrier(-1),
  7. m_collisioneur(collisioneur), m_screen(screen), m_liner(0), m_cercle(0), m_pseudo(pseudo),
  8. m_powerGetter(powerGetter), m_power(0)
  9. {
  10. if (HAUT <= orientation && orientation <= GAUCHE)
  11. m_liner = s_linerSurface[orientation];
  12. SDL_Surface* cercle = SDL_LoadBMP("Textures/Cercle.bmp");
  13. if (!m_liner)
  14. std::cout << "Image du liner inexistante. Attention à initPict(). " << SDL_GetError() << std::endl;
  15. if (!cercle)
  16. std::cout << "Impossible de charger une surface dans l'objet Liner : " << SDL_GetError() << std::endl;
  17. else
  18. {
  19. m_cercle = colorMightyObjet(cercle, id);
  20. SDL_FreeSurface(cercle);
  21. SDL_SetColorKey( m_cercle, SDL_SRCCOLORKEY, SDL_MapRGB(m_cercle->format, 0, 0, 0) );
  22. }
  23. }///Constructeur
  24. Liner::~Liner()
  25. {
  26. SDL_FreeSurface(m_cercle);
  27. m_trainee.clear();
  28. }///Destructeur
  29. void Liner::actualiser(int direction)
  30. {
  31. //[1] Calcule le changement de direction
  32. m_orientation += direction;
  33. if (m_orientation==4)
  34. m_orientation=HAUT;
  35. else if (m_orientation==-1)
  36. m_orientation=GAUCHE;
  37. if (m_estVivant)//Début d'éxécution vivant
  38. {
  39. //[2] Rajoute un mur
  40. m_collisioneur->ajouter(m_x, m_y, m_id);
  41. //[3] Avance
  42. avancer(1);
  43. }//Fin d'exécution vivant
  44. //[4] Misa à jour du pointeur de pouvoir
  45. if (m_power!=0)
  46. {
  47. if(m_power->estUtilise())
  48. m_power=0;
  49. }
  50. else
  51. m_power=m_powerGetter->chercherPouvoir(m_x, m_y);
  52. }///actualiser
  53. void Liner::collisioner()
  54. {
  55. if (m_collisioneur->tester(m_x, m_y) != VIDE)
  56. {
  57. if (m_power!=0)
  58. {
  59. m_power->postMortemPower(m_x, m_y, m_id);
  60. m_power = 0;
  61. }
  62. if (m_collisioneur->tester(m_x, m_y) != VIDE){
  63. m_estVivant = false;
  64. m_meurtrier = m_collisioneur->tester(m_x, m_y);
  65. if ( m_meurtrier == NEUTRE || m_meurtrier == m_id ) m_meurtrier = 5;
  66. }
  67. }
  68. }///collisioner
  69. void Liner::afficher()
  70. {
  71. //[1] Calculs
  72. m_position.x = m_x * 20;
  73. m_position.y = m_y * 20;
  74. if (m_estVivant)
  75. m_liner = s_linerSurface[m_orientation];
  76. else
  77. m_liner=s_mort;
  78. //[2] Affiche trainée
  79. for ( Uint16 i(1); i<m_trainee.size(); i++ )
  80. {
  81. SDL_BlitSurface( s_trainee[i-1], 0, m_screen, &m_trainee[i] );
  82. }
  83. //[3] Affiche liner
  84. SDL_BlitSurface(m_cercle, 0, m_screen, &m_position);
  85. SDL_BlitSurface(m_liner, 0, m_screen, &m_position);
  86. //[4] Affiche pseudo
  87. if ( m_fin )
  88. {
  89. m_position.y += 20;
  90. SDL_BlitSurface(m_pseudo, 0, m_screen, &m_position);
  91. }
  92. //[5] Affiche pouvoir
  93. if (m_power!=0)
  94. m_power->afficher(m_x, m_y);
  95. }///afficher
  96. void Liner::direEstFini(bool fin)
  97. {
  98. m_fin = fin;
  99. }///direEstFini
  100. bool Liner::estVivant() const
  101. {
  102. return m_estVivant;
  103. }///estVivant
  104. void Liner::avancer(int nbFois)
  105. {
  106. // Avancée
  107. for (int i(0); i<nbFois; i++)
  108. {
  109. switch (m_orientation)
  110. {
  111. case HAUT:
  112. m_y-=1;
  113. break;
  114. case BAS:
  115. m_y+=1;
  116. break;
  117. case DROITE:
  118. m_x+=1;
  119. break;
  120. case GAUCHE:
  121. m_x-=1;
  122. break;
  123. }//Switch
  124. }//For
  125. // Remise sur le terrain
  126. m_collisioneur->coorectPos(m_x, m_y);
  127. // Mise à jour de la trainée
  128. m_trainee.push_front( { m_x * 20, m_y * 20, 0, 0 } );
  129. if ( m_trainee.size() > 16 ) m_trainee.pop_back();
  130. }///avancer
  131. void Liner::utiliserPouvoir()
  132. {
  133. if (m_power!=0)
  134. {
  135. m_power->usePower(m_orientation, m_x, m_y, m_id);
  136. m_power=0;
  137. m_collisioneur->coorectPos(m_x, m_y);
  138. }
  139. }///utiliserPouvoir
  140. void Liner::reset( int x, int y, int orientation )
  141. {
  142. // Attributs
  143. m_x = x ;
  144. m_y = y ;
  145. m_orientation = orientation ;
  146. m_estVivant = true ;
  147. m_fin = false ;
  148. m_meurtrier = -1 ;
  149. m_power = 0x0 ;
  150. m_trainee.clear();
  151. // Surface
  152. if (HAUT <= orientation && orientation <= GAUCHE)
  153. m_liner = s_linerSurface[orientation];
  154. else
  155. m_orientation = BAS ;
  156. }///reset
  157. int Liner::getMeurtrier()
  158. {
  159. return m_meurtrier;
  160. }///getMeutrier
  161. /************************************Fonctions indépendantes************************************/
  162. bool initialisationPict()
  163. {
  164. /// Chargement
  165. s_linerSurface[HAUT] = SDL_LoadBMP("Textures/Haut.bmp");
  166. s_linerSurface[DROITE] = retournement(s_linerSurface[HAUT],1);
  167. s_linerSurface[BAS] = retournement(s_linerSurface[HAUT],2);
  168. s_linerSurface[GAUCHE] = retournement(s_linerSurface[HAUT],3);
  169. s_mort = SDL_LoadBMP("Textures/Mort.bmp");
  170. for ( int i(0); i<15; i++ )
  171. {
  172. s_trainee[i] = SDL_CreateRGBSurface( SDL_HWSURFACE, 20, 20, 32, 0, 0, 0, 0 );
  173. if ( !s_trainee[i] )
  174. return false;
  175. else
  176. {
  177. SDL_FillRect( s_trainee[i], 0, SDL_MapRGB( s_trainee[i]->format, 255, 255, 255 ) );
  178. SDL_SetAlpha( s_trainee[i], SDL_SRCALPHA, 255-12*i );
  179. }
  180. }
  181. /// Tests
  182. for (int i(0); i<4; i++)
  183. {
  184. if (!s_linerSurface[i])
  185. return false;
  186. else
  187. SDL_SetColorKey(s_linerSurface[i],SDL_SRCCOLORKEY,SDL_MapRGB(s_linerSurface[i]->format,255,0,0));
  188. }
  189. if (!s_mort)
  190. return false;
  191. return true;
  192. }///initialisationPict
  193. void fermeturePict()
  194. {
  195. for(int i(0); i<4; i++)
  196. {
  197. SDL_FreeSurface(s_linerSurface[i]);
  198. s_linerSurface[i]=0;
  199. }
  200. SDL_FreeSurface(s_mort);
  201. s_mort=0;
  202. for ( int i(0); i<15; i++ )
  203. {
  204. SDL_FreeSurface(s_trainee[i]);
  205. s_trainee[i]=0;
  206. }
  207. }///fermeturePict