lab.cpp/SDL#4/xMario/BreakableBloc/xMarioBrick.cpp

98 lines
1.9 KiB
C++
Raw Normal View History

/* [CONSTRUCTOR] Construction d'un xMarioBrick
=========================================================*/
xMarioBrick::xMarioBrick(xManager *m, int x, int y, int nb)
: xSprite(
m,
"src/blocs.png"
){
_manager = m;
// Note: le rect correspond a un nombre de bloc
// On convertit le tout en blocs reels
int xReal = x * BLOC_SIZE;
int yReal = y * BLOC_SIZE;
this->dimensions(
(SDL_Rect){xReal, yReal, BLOC_SIZE, BLOC_SIZE}, // On definit le viewport
(SDL_Rect){136, 0, 16, 16} // On definit le clip
);
_defaultrect = _dst;
_lastjump = SDL_GetTicks();
_jumps = 0;
_active = true;
_nb_jumps = nb;
}
/* [JUMP] Animation d'activation (quand saut)
=========================================================*/
void xMarioBrick::jump(){
if( !this->active() )
return;
if( _defaultrect.y == _dst.y && SDL_GetTicks()-_lastjump > 100 ){
this->move(0, -2);
_lastjump = SDL_GetTicks();
_jumps++;
if( _jumps >= _nb_jumps )
this->active(false);
}
}
/* [UNJUMP] Animation d'activation (quand saut)
=========================================================*/
void xMarioBrick::unjump(){
if( (_defaultrect.y-2) == _dst.y && SDL_GetTicks()-_lastjump > 100 ){
this->move(0, 2);
_lastjump = SDL_GetTicks();
}
}
/* [ACTIVE] Retourne l'etat du bouton
=========================================================*/
bool xMarioBrick::active(){
return _active;
}
/* [ACTIVE] Gestion du caractere "actif" du bouton
=========================================================*/
void xMarioBrick::active(bool active){
_active = active;
// actif, on affiche
if( active )
this->push(_index);
// sinon on enleve
else
this->pull();
}
/* [ONCOLLIDE] Gestion des collisions
=========================================================*/
void xMarioBrick::onCollide(vector<int> from, xSprite* by){
/* (1) Saut de mario */
if( by->getType() == "Mario" && from[1] == -1 )
this->jump();
}