123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include "Fantome.h"
- int Fantome::m_multiKill = 0;
- 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)
- {
- m_terrain = terrain;
- m_direction = Arret;
- m_nouvDir = Arret;
- m_skin = IMG_Load("Skins/Fantome.png");
- if(m_skin == 0)
- std::cout << "Erreur de chargement du skin Skins/Fantome.png" << std::endl;
- }
- Fantome::~Fantome()
- {
- SDL_FreeSurface(m_skin);
- }
- void Fantome::deplacer()
- {
- if(!m_vivant)
- {
- if(SDL_GetTicks()<m_tempsRestant)
- return;
- else
- {
- m_vivant = true;
- m_multiKill = 0;
- }
- }
- if(m_controle == local)
- {
- int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
- int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
- int FdirPossible = Arret;
- if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
- FdirPossible |= Haut;
- if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
- FdirPossible |= Droite;
- if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
- FdirPossible |= Bas;
- if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
- FdirPossible |= Gauche;
- int dirPerso = Arret;
- if(m_menestrel->getPosition().y < getPosition().y)
- dirPerso |= Haut;
- if(m_menestrel->getPosition().x > getPosition().x)
- dirPerso |= Droite;
- if(m_menestrel->getPosition().y > getPosition().y)
- dirPerso |= Bas;
- if(m_menestrel->getPosition().x < getPosition().x)
- dirPerso |= Gauche;
- std::vector<Direction> nouvDir;
- for(int i = 0;i<4;i++)
- {
- if((m_nouvDir = (Direction)(FdirPossible & (int)pow(2, i))))
- if(((m_direction<<2) != m_nouvDir) && ((m_direction>>2) != m_nouvDir))
- nouvDir.push_back((Direction)pow(2, i));
- }
- if(nouvDir.size()>0)
- {
- if(nouvDir.size()>1)
- {
- int nouvDirTaille = nouvDir.size();
- for(int i = 0;i<nouvDirTaille;i++)
- if(nouvDir[i] & dirPerso)
- for(int proba = 1;proba<10;proba++)
- nouvDir.push_back(nouvDir[i]);
- m_direction = nouvDir[rand()%nouvDir.size()];
- }
- else
- m_direction = nouvDir[0];
- m_pos += m_direction;
- }
- else
- m_direction = m_direction==Gauche?Droite:m_direction==Droite?Gauche:m_direction==Haut?Bas:Haut;
- }
- }
- void Fantome::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;
- }
- int posX = m_pos.x + (m_skin->w-LG_BLOC)/2;
- int posY = m_pos.y + (m_skin->h-LG_BLOC)/2;
- std::vector<Direction> dirPossible;
- if(m_terrain->typeBloc(posX-1, posY, 1) != MUR && m_terrain->typeBloc(posX-1, posY+LG_BLOC-1, 1) != MUR)
- dirPossible.push_back(Gauche);
- if(m_terrain->typeBloc(posX+LG_BLOC, posY, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC, posY+LG_BLOC-1, 1) != MUR)
- dirPossible.push_back(Droite);
- if(m_terrain->typeBloc(posX, posY-1, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY-1, 1) != MUR)
- dirPossible.push_back(Haut);
- if(m_terrain->typeBloc(posX, posY+LG_BLOC, 1) != MUR && m_terrain->typeBloc(posX+LG_BLOC-1, posY+LG_BLOC, 1) != MUR)
- dirPossible.push_back(Bas);
- if(dirPossible.size()>0)
- m_direction = dirPossible[rand()%dirPossible.size()];
- m_nouvDir = m_direction;
- m_tempsRestant = 0;
- }
- void Fantome::afficher(SDL_Surface *ecran)
- {
- if(m_vivant)
- SDL_BlitSurface(m_skin, 0, ecran, &m_pos);
- }
- void Fantome::tuer()
- {
- m_multiKill++;
- m_vivant = false;
- m_tempsRestant = SDL_GetTicks()+TEMPS_MORT;
- }
- bool Fantome::vivant()
- {
- return m_vivant;
- }
- bool Fantome::getMultiKill(int nbFantomes)
- {
- if(nbFantomes == m_multiKill)
- {
- m_multiKill = 0;
- return true;
- }
- else
- return false;
- }
|