Menestrel.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "Menestrel.h"
  2. Menestrel::Menestrel(Input *input, Niveau *terrain, Controle controle): Joueur(input, terrain, controle)
  3. {
  4. m_skin = IMG_Load("Skins/Menestrel.png");
  5. if(m_skin == 0)
  6. std::cout << "Erreur de chargement du skin Skins/Menestrel.png" << std::endl;
  7. }
  8. Menestrel::~Menestrel()
  9. {
  10. SDL_FreeSurface(m_skin);
  11. }
  12. void Menestrel::deplacer()
  13. {
  14. if(m_controle == local)
  15. {
  16. if(m_input->getTouches(SDLK_UP))
  17. m_nouvDir = Haut;
  18. else if(m_input->getTouches(SDLK_RIGHT))
  19. m_nouvDir = Droite;
  20. else if(m_input->getTouches(SDLK_DOWN))
  21. m_nouvDir = Bas;
  22. else if(m_input->getTouches(SDLK_LEFT))
  23. m_nouvDir = Gauche;
  24. }
  25. int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  26. int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  27. int dirPossible = Arret;
  28. if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
  29. dirPossible |= Gauche;
  30. if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
  31. dirPossible |= Droite;
  32. if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
  33. dirPossible |= Haut;
  34. if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
  35. dirPossible |= Bas;
  36. if(dirPossible & m_nouvDir)
  37. m_direction = m_nouvDir;
  38. if(dirPossible & m_direction)
  39. {
  40. m_pos+=m_direction;
  41. posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
  42. posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
  43. }
  44. else if((!(dirPossible&Haut) && !(dirPossible&Bas) && (m_direction == Gauche || m_direction == Droite)) ||
  45. (!(dirPossible&Gauche) && !(dirPossible&Droite) && (m_direction == Haut || m_direction == Bas)))
  46. {
  47. m_direction = m_direction==Gauche?Droite:m_direction==Droite?Gauche:m_direction==Haut?Bas:Haut;
  48. m_nouvDir = m_direction;
  49. }
  50. }