252 lines
4.7 KiB
C++
252 lines
4.7 KiB
C++
/* [CONSTRUCTOR] Construction d'un xMarioMario
|
|
=========================================================*/
|
|
xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|
: xSpriteAnimation(
|
|
m,
|
|
"src/mario.png",
|
|
(SDL_Rect){
|
|
(int) (BLOC_SIZE*x+BLOC_SIZE*.1),
|
|
BLOC_SIZE*y,
|
|
(int) (BLOC_SIZE*.8),
|
|
BLOC_SIZE
|
|
}
|
|
){
|
|
|
|
_left = NULL;
|
|
_right = NULL;
|
|
_up = NULL;
|
|
_down = NULL;
|
|
|
|
|
|
// this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){33, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){62, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){122, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){122, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){153, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){182, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){213, 0, 19, 29} );
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){269, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){298, 0, 19, 29} );
|
|
// this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
// /* (1) On definit les clip de chaque frame */
|
|
// this->addFrame( (SDL_Rect){21, 0, 18, 32} );
|
|
// this->addFrame( (SDL_Rect){42, 0, 18, 32} );
|
|
// this->addFrame( (SDL_Rect){63, 0, 18, 32} );
|
|
// this->addFrame( (SDL_Rect){82, 0, 18, 32} );
|
|
// this->addFrame( (SDL_Rect){103, 0, 18, 32} );
|
|
// this->addFrame( (SDL_Rect){125, 0, 18, 32} );
|
|
}
|
|
|
|
|
|
|
|
/* [XLEFTMOVEPROCESS] Traitement async de mouvement
|
|
=========================================================*/
|
|
void xLeftMoveProcess(xMarioMario *m, bool *run){
|
|
bool hasMoved = false;
|
|
int step = 5;
|
|
|
|
|
|
while( *run ){
|
|
|
|
hasMoved = false;
|
|
step = 5;
|
|
|
|
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
|
while( !hasMoved && step > 0 ){
|
|
|
|
// Si aucune collision, on deplace
|
|
if( !m->manager()->hit("mario", -step, 0) ){
|
|
m->move(-step, 0);
|
|
hasMoved = true;
|
|
}
|
|
|
|
// on reduit la distance de mouvement
|
|
step--;
|
|
}
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
}
|
|
|
|
/* [MOVELEFT] Deplacement vers la gauche
|
|
=========================================================*/
|
|
void xMarioMario::moveLeft(bool *run){
|
|
if( _left != NULL ){
|
|
delete _left;
|
|
_left = NULL;
|
|
}
|
|
// On lance le thread
|
|
_left = new thread(xLeftMoveProcess, this, run);
|
|
_left->detach();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [XRIGHTMOVEPROCESS] Traitement async de mouvement
|
|
=========================================================*/
|
|
void xRightMoveProcess(xMarioMario *m, bool *run){
|
|
bool hasMoved = false;
|
|
int step = 5;
|
|
|
|
|
|
while( *run ){
|
|
|
|
hasMoved = false;
|
|
step = 5;
|
|
|
|
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
|
while( !hasMoved && step > 0 ){
|
|
|
|
// Si aucune collision, on deplace
|
|
if( !m->manager()->hit("mario", step, 0) ){
|
|
m->move(step, 0);
|
|
hasMoved = true;
|
|
}
|
|
|
|
// on reduit la distance de mouvement
|
|
step--;
|
|
}
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* [MOVERIGHT] Deplacement vers la droite
|
|
=========================================================*/
|
|
void xMarioMario::moveRight(bool *run){
|
|
if( _right != NULL ){
|
|
delete _right;
|
|
_right = NULL;
|
|
}
|
|
|
|
// On lance le thread
|
|
_right = new thread(xRightMoveProcess, this, run);
|
|
_right->detach();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [XUPMOVEPROCESS] Traitement async de mouvement
|
|
=========================================================*/
|
|
void xUpMoveProcess(xMarioMario *m, bool *run){
|
|
bool hasMoved = false;
|
|
int step = 5;
|
|
|
|
|
|
while( *run ){
|
|
|
|
hasMoved = false;
|
|
step = 5;
|
|
|
|
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
|
while( !hasMoved && step > 0 ){
|
|
|
|
// Si aucune collision, on deplace
|
|
if( !m->manager()->hit("mario", 0, -step) ){
|
|
m->move(0, -step);
|
|
hasMoved = true;
|
|
}
|
|
|
|
// on reduit la distance de mouvement
|
|
step--;
|
|
}
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/* [MOVEUP] Deplacement vers le haut
|
|
=========================================================*/
|
|
void xMarioMario::moveUp(bool *run){
|
|
if( _up != NULL ){
|
|
delete _up;
|
|
_up = NULL;
|
|
}
|
|
|
|
// On lance le thread
|
|
_up = new thread(xUpMoveProcess, this, run);
|
|
_up->detach();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [XDOWNMOVEPROCESS] Traitement async de mouvement
|
|
=========================================================*/
|
|
void xDownMoveProcess(xMarioMario *m, bool *run){
|
|
bool hasMoved = false;
|
|
int step = 5;
|
|
|
|
|
|
while( *run ){
|
|
|
|
hasMoved = false;
|
|
step = 5;
|
|
|
|
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
|
while( !hasMoved && step > 0 ){
|
|
|
|
// Si aucune collision, on deplace
|
|
if( !m->manager()->hit("mario", 0, step) ){
|
|
m->move(0, step);
|
|
hasMoved = true;
|
|
}
|
|
|
|
// on reduit la distance de mouvement
|
|
step--;
|
|
}
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* [MOVEDOWN] Deplacement vers le bas
|
|
=========================================================*/
|
|
void xMarioMario::moveDown(bool *run){
|
|
if( _down != NULL ){
|
|
delete _down;
|
|
_down = NULL;
|
|
}
|
|
|
|
// On lance le thread
|
|
_down = new thread(xDownMoveProcess, this, run);
|
|
_down->detach();
|
|
} |