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

107 lines
2.2 KiB
C++
Raw Normal View History

/* [CONSTRUCTOR] Construction d'un xMarioGreenShell
=========================================================*/
xMarioBreakableBloc::xMarioBreakableBloc(xManager *m, const char *url, int x, int y, int nb)
: xSpriteAnimation(
m,
url,
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}
){
this->setType("breakable-bloc");
/* (1) On initialise les attributs */
_auto_unjump = NULL;
_defaultrect = _dst;
_lastjump = SDL_GetTicks();
_jumps = 0;
_active = true;
_nb_jumps = nb;
}
/* [ANIMATIONTHREAD] Gestion de la fin de l'animation
=========================================================*/
void xUnjumpProcess(xMarioBreakableBloc *mb){
usleep(1000 * 100); // 100ms
mb->unjump();
}
/* [JUMP] Animation d'activation (quand saut)
=========================================================*/
void xMarioBreakableBloc::jump(){
if( !this->active() )
return;
if( _defaultrect.y == _dst.y && SDL_GetTicks()-_lastjump > 100 ){
this->move(0, -5);
// Gestion du thread pour fin d'animation
if( _auto_unjump != NULL)
delete _auto_unjump;
_auto_unjump = NULL;
_auto_unjump = new thread(xUnjumpProcess, this);
_auto_unjump->detach();
_lastjump = SDL_GetTicks();
_jumps++;
if( _jumps >= _nb_jumps )
this->active(false);
}
}
/* [UNJUMP] Animation d'activation (quand saut)
=========================================================*/
void xMarioBreakableBloc::unjump(){
if( (_defaultrect.y-5) == _dst.y ){
this->move(0, 5);
_lastjump = SDL_GetTicks();
}
}
/* [ACTIVE] Retourne l'etat du bouton
=========================================================*/
bool xMarioBreakableBloc::active(){
return _active;
}
/* [ACTIVE] Gestion du caractere "actif" du bouton
=========================================================*/
void xMarioBreakableBloc::active(bool active){
_active = active;
}
/* [ONCOLLIDE] Gestion des collisions
=========================================================*/
void xMarioBreakableBloc::onCollide(vector<bool> from, xSprite* by){
/* (1) Saut de mario */
if( by->getType() == "Mario" && from[3] )
this->jump();
/* (2) Collision laterale par objet mobile */
if( by->getType() == "green-shell" && (from[0]||from[1]) )
this->jump();
}