Ogre.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "Ogre.h"
  2. Ogre::Ogre(Input *input, Niveau *terrain, Controle controle): Joueur(input, terrain, controle), m_berzerk(false), m_timeBerzerk(0)
  3. {
  4. m_skin = IMG_Load("Skins/Ogre.png");
  5. if(m_skin == 0)
  6. std::cout << "Erreur de chargement du skin Skins/Ogre.png" << std::endl;
  7. m_skinBerzerk = IMG_Load("Skins/OgreBerzerk.png");
  8. if(m_skinBerzerk == 0)
  9. std::cout << "Erreur de chargement du skin Skins/OrgeBerzerk.png" << std::endl;
  10. }
  11. Ogre::~Ogre()
  12. {
  13. SDL_FreeSurface(m_skin);
  14. SDL_FreeSurface(m_skinBerzerk);
  15. }
  16. void Ogre::deplacer()
  17. {
  18. if(m_controle == local || m_controle == ordi)
  19. {
  20. if(m_input->getTouches(SDLK_w) && m_direction!=Bas)
  21. m_nouvDir = Haut;
  22. else if(m_input->getTouches(SDLK_d) && m_direction!=Gauche)
  23. m_nouvDir = Droite;
  24. else if(m_input->getTouches(SDLK_s) && m_direction!=Haut)
  25. m_nouvDir = Bas;
  26. else if(m_input->getTouches(SDLK_a) && m_direction!=Droite)
  27. m_nouvDir = Gauche;
  28. }
  29. for(int re = 0;re<VITESSE_BERZERK;re++)
  30. {
  31. int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  32. int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  33. int dirPossible = Arret;
  34. if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
  35. dirPossible |= Gauche;
  36. if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
  37. dirPossible |= Droite;
  38. if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
  39. dirPossible |= Haut;
  40. if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
  41. dirPossible |= Bas;
  42. if(dirPossible & m_nouvDir)
  43. m_direction = m_nouvDir;
  44. if(dirPossible & m_direction)
  45. {
  46. m_pos+=m_direction;
  47. posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  48. posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  49. }
  50. else if((!(dirPossible&Haut) && !(dirPossible&Bas) && (m_direction == Gauche || m_direction == Droite)) ||
  51. (!(dirPossible&Gauche) && !(dirPossible&Droite) && (m_direction == Haut || m_direction == Bas)))
  52. {
  53. m_direction = m_direction==Gauche?Droite:m_direction==Droite?Gauche:m_direction==Haut?Bas:Haut;
  54. m_nouvDir = m_direction;
  55. }
  56. if(SDL_GetTicks()-m_timeBerzerk>TEMPS_BERZERK)
  57. m_berzerk = false;
  58. if(m_terrain->takeItem(posX, posY, 1) == TONNEAU)
  59. {
  60. m_berzerk = true;
  61. m_timeBerzerk = SDL_GetTicks();
  62. }
  63. if(m_berzerk == false)
  64. break;
  65. }
  66. }
  67. bool Ogre::getBerzerk()
  68. {
  69. return m_berzerk;
  70. }
  71. void Ogre::afficher(SDL_Surface *ecran)
  72. {
  73. if(m_berzerk)
  74. SDL_BlitSurface(m_skinBerzerk, 0, ecran, &m_pos);
  75. else
  76. SDL_BlitSurface(m_skin, 0, ecran, &m_pos);
  77. }
  78. void Ogre::positionner(SDL_Rect position, bool coordonnee)
  79. {
  80. if(coordonnee)
  81. {
  82. m_pos.x = position.x;
  83. m_pos.y = position.y;
  84. }
  85. else
  86. {
  87. m_pos.x = position.x*LG_BLOC - (m_skin->w-LG_BLOC)/2;
  88. m_pos.y = position.y*LG_BLOC - (m_skin->h-LG_BLOC)/2;
  89. }
  90. m_direction = Arret;
  91. m_berzerk = false;
  92. m_timeBerzerk = 0;
  93. }