123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "Ogre.h"
- Ogre::Ogre(Input *input, Niveau *terrain, Controle controle): Joueur(input, terrain, controle), m_berzerk(false), m_timeBerzerk(0)
- {
- m_skin = IMG_Load("Skins/Ogre.png");
- if(m_skin == 0)
- std::cout << "Erreur de chargement du skin Skins/Ogre.png" << std::endl;
- m_skinBerzerk = IMG_Load("Skins/OgreBerzerk.png");
- if(m_skinBerzerk == 0)
- std::cout << "Erreur de chargement du skin Skins/OrgeBerzerk.png" << std::endl;
- }
- Ogre::~Ogre()
- {
- SDL_FreeSurface(m_skin);
- SDL_FreeSurface(m_skinBerzerk);
- }
- void Ogre::deplacer()
- {
- if(m_controle == local || m_controle == ordi)
- {
- if(m_input->getTouches(SDLK_w) && m_direction!=Bas)
- m_nouvDir = Haut;
- else if(m_input->getTouches(SDLK_d) && m_direction!=Gauche)
- m_nouvDir = Droite;
- else if(m_input->getTouches(SDLK_s) && m_direction!=Haut)
- m_nouvDir = Bas;
- else if(m_input->getTouches(SDLK_a) && m_direction!=Droite)
- m_nouvDir = Gauche;
- }
- for(int re = 0;re<VITESSE_BERZERK;re++)
- {
- int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
- int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
- int dirPossible = Arret;
- if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
- dirPossible |= Gauche;
- if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
- dirPossible |= Droite;
- if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
- dirPossible |= Haut;
- if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
- dirPossible |= Bas;
- if(dirPossible & m_nouvDir)
- m_direction = m_nouvDir;
- if(dirPossible & m_direction)
- {
- m_pos+=m_direction;
- posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
- posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
- }
- else if((!(dirPossible&Haut) && !(dirPossible&Bas) && (m_direction == Gauche || m_direction == Droite)) ||
- (!(dirPossible&Gauche) && !(dirPossible&Droite) && (m_direction == Haut || m_direction == Bas)))
- {
- m_direction = m_direction==Gauche?Droite:m_direction==Droite?Gauche:m_direction==Haut?Bas:Haut;
- m_nouvDir = m_direction;
- }
- if(SDL_GetTicks()-m_timeBerzerk>TEMPS_BERZERK)
- m_berzerk = false;
- if(m_terrain->takeItem(posX, posY, 1) == TONNEAU)
- {
- m_berzerk = true;
- m_timeBerzerk = SDL_GetTicks();
- }
- if(m_berzerk == false)
- break;
- }
- }
- bool Ogre::getBerzerk()
- {
- return m_berzerk;
- }
- void Ogre::afficher(SDL_Surface *ecran)
- {
- if(m_berzerk)
- SDL_BlitSurface(m_skinBerzerk, 0, ecran, &m_pos);
- else
- SDL_BlitSurface(m_skin, 0, ecran, &m_pos);
- }
- void Ogre::positionner(SDL_Rect position, bool coordonnee)
- {
- if(coordonnee)
- {
- m_pos.x = position.x;
- m_pos.y = position.y;
- }
- else
- {
- m_pos.x = position.x*LG_BLOC - (m_skin->w-LG_BLOC)/2;
- m_pos.y = position.y*LG_BLOC - (m_skin->h-LG_BLOC)/2;
- }
- m_direction = Arret;
- m_berzerk = false;
- m_timeBerzerk = 0;
- }
|