fantome.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "Fantome.h"
  2. int Fantome::m_multiKill = 0;
  3. Fantome::Fantome(Niveau *terrain, Ogre *ogre, Menestrel *menestrel, Controle controle): Personnage(), m_controle(controle), m_ogre(ogre), m_menestrel(menestrel), m_tempsRestant(0), m_vivant(true)
  4. {
  5. m_terrain = terrain;
  6. m_direction = Arret;
  7. m_nouvDir = Arret;
  8. m_skin = IMG_Load("Skins/Fantome.png");
  9. if(m_skin == 0)
  10. std::cout << "Erreur de chargement du skin Skins/Fantome.png" << std::endl;
  11. }
  12. Fantome::~Fantome()
  13. {
  14. SDL_FreeSurface(m_skin);
  15. }
  16. void Fantome::deplacer()
  17. {
  18. if(!m_vivant)
  19. {
  20. if(SDL_GetTicks()<m_tempsRestant)
  21. return;
  22. else
  23. {
  24. m_vivant = true;
  25. m_multiKill = 0;
  26. }
  27. }
  28. if(m_controle == local)
  29. {
  30. int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  31. int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  32. int FdirPossible = Arret;
  33. if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
  34. FdirPossible |= Haut;
  35. if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
  36. FdirPossible |= Droite;
  37. if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
  38. FdirPossible |= Bas;
  39. if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
  40. FdirPossible |= Gauche;
  41. int dirPerso = Arret;
  42. if(m_menestrel->getPosition().y < getPosition().y)
  43. dirPerso |= Haut;
  44. if(m_menestrel->getPosition().x > getPosition().x)
  45. dirPerso |= Droite;
  46. if(m_menestrel->getPosition().y > getPosition().y)
  47. dirPerso |= Bas;
  48. if(m_menestrel->getPosition().x < getPosition().x)
  49. dirPerso |= Gauche;
  50. std::vector<Direction> nouvDir;
  51. for(int i = 0;i<4;i++)
  52. {
  53. if((m_nouvDir = (Direction)(FdirPossible & (int)pow(2, i))))
  54. if(((m_direction<<2) != m_nouvDir) && ((m_direction>>2) != m_nouvDir))
  55. nouvDir.push_back((Direction)pow(2, i));
  56. }
  57. if(nouvDir.size()>0)
  58. {
  59. if(nouvDir.size()>1)
  60. {
  61. int nouvDirTaille = nouvDir.size();
  62. for(int i = 0;i<nouvDirTaille;i++)
  63. if(nouvDir[i] & dirPerso)
  64. for(int proba = 1;proba<10;proba++)
  65. nouvDir.push_back(nouvDir[i]);
  66. m_direction = nouvDir[rand()%nouvDir.size()];
  67. }
  68. else
  69. m_direction = nouvDir[0];
  70. m_pos += m_direction;
  71. }
  72. else
  73. m_direction = m_direction==Gauche?Droite:m_direction==Droite?Gauche:m_direction==Haut?Bas:Haut;
  74. }
  75. }
  76. void Fantome::positionner(SDL_Rect position, bool coordonnee)
  77. {
  78. if(coordonnee)
  79. {
  80. m_pos.x = position.x;
  81. m_pos.y = position.y;
  82. }
  83. else
  84. {
  85. m_pos.x = position.x*LG_BLOC - (m_skin->w-LG_BLOC)/2;
  86. m_pos.y = position.y*LG_BLOC - (m_skin->h-LG_BLOC)/2;
  87. }
  88. int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  89. int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  90. std::vector<Direction> dirPossible;
  91. if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
  92. dirPossible.push_back(Gauche);
  93. if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
  94. dirPossible.push_back(Droite);
  95. if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
  96. dirPossible.push_back(Haut);
  97. if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
  98. dirPossible.push_back(Bas);
  99. if(dirPossible.size()>0)
  100. m_direction = dirPossible[rand()%dirPossible.size()];
  101. m_nouvDir = m_direction;
  102. m_tempsRestant = 0;
  103. }
  104. void Fantome::afficher(SDL_Surface *ecran)
  105. {
  106. if(m_vivant)
  107. SDL_BlitSurface(m_skin, 0, ecran, &m_pos);
  108. }
  109. void Fantome::tuer()
  110. {
  111. m_multiKill++;
  112. m_vivant = false;
  113. m_tempsRestant = SDL_GetTicks()+TEMPS_MORT;
  114. }
  115. bool Fantome::vivant()
  116. {
  117. return m_vivant;
  118. }
  119. bool Fantome::getMultiKill(int nbFantomes)
  120. {
  121. if(nbFantomes == m_multiKill)
  122. {
  123. m_multiKill = 0;
  124. return true;
  125. }
  126. else
  127. return false;
  128. }