181 lines
4.4 KiB
C++
181 lines
4.4 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;
|
|
|
|
// Constantes de velocite sur X
|
|
_velocity[0] = 0.0;
|
|
_mult[0] = 10;
|
|
_dec[0] = 0.9;
|
|
_acc[0] = 3.5;
|
|
_min_vel[0] = 0.1;
|
|
_max_vel[0] = 40;
|
|
|
|
// Constantes de velocite sur Y
|
|
_velocity[1] = 0.0;
|
|
_mult[1] = 20;
|
|
_dec[1] = 1;
|
|
_acc[1] = 1.9;
|
|
_min_vel[1] = 0.1;
|
|
_max_vel[1] = 50;
|
|
|
|
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} );
|
|
}
|
|
|
|
|
|
|
|
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
|
=========================================================*/
|
|
void xMarioMario::moveFromVelocity(){
|
|
|
|
// Gestion de touche encore enfoncee
|
|
if( SDL_GetTicks() % 10 < 8 ){
|
|
if( _left ) this->velocity(-1, 0);
|
|
else if( _right ) this->velocity(1, 0);
|
|
|
|
if( _up ) this->velocity(0, -1);
|
|
else if( _down ) this->velocity(0, 1);
|
|
}
|
|
|
|
|
|
|
|
// Si aucune collision, on deplace
|
|
if( !_manager->hit(_texture, _velocity[0], _velocity[1]) )
|
|
this->move(_velocity[0], _velocity[1]);
|
|
|
|
// On diminue la _velocite
|
|
_velocity[0] *= ( 1 - _dec[0] );
|
|
_velocity[1] *= ( 1 - _dec[1] );
|
|
|
|
// Gestion de velocite trop basse
|
|
if( abs(_velocity[0]) < _min_vel[0] ) _velocity[0] = 0;
|
|
if( abs(_velocity[1]) < _min_vel[1] ) _velocity[1] = 0;
|
|
|
|
cout << floor(_velocity[0]) << " - " << floor(_velocity[1]) << endl;
|
|
|
|
// _manager->update();
|
|
|
|
// usleep(10000);
|
|
}
|
|
|
|
|
|
/* [VELOCITY] Retourne un pointeur sur la velocite
|
|
=========================================================*/
|
|
double xMarioMario::velocity(bool way){
|
|
// (way) ? HORIZONTAL : VERTICAL
|
|
return (way) ? _velocity[0] : _velocity[1];
|
|
}
|
|
|
|
|
|
/* [VELOCITY] Modifie la velocite
|
|
=========================================================*/
|
|
void xMarioMario::velocity(double x, double y){
|
|
double last[] = { _velocity[0], _velocity[1] };
|
|
|
|
// Si meme sens, on accele
|
|
if( last[0]*x > 0 )
|
|
_velocity[0] *= _acc[0];
|
|
else{ // sinon changement sens
|
|
_velocity[0] += x*_mult[0];
|
|
|
|
// Gestion des frames
|
|
if( _velocity[0] > 0 ) this->turnRight();
|
|
else this->turnLeft();
|
|
}
|
|
|
|
if( last[1]*y > 0 )
|
|
_velocity[1] *= _acc[1];
|
|
else // sinon changement sens
|
|
_velocity[1] += y*_mult[1];
|
|
|
|
|
|
|
|
// On retaille la velocite
|
|
if( abs(_velocity[0]) > _max_vel[0] )
|
|
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
|
|
|
if( abs(_velocity[1]) > _max_vel[1]*BLOC_SIZE )
|
|
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
|
|
}
|
|
|
|
|
|
|
|
|
|
bool xMarioMario::onFloor(){
|
|
return _manager->hit(_texture, 0, 1);
|
|
}
|
|
|
|
|
|
|
|
/* [TURNLEFT] Charge le sprite vers la gauche
|
|
=========================================================*/
|
|
void xMarioMario::turnLeft(){
|
|
this->stop();
|
|
|
|
for( int i = 0 ; i < _frames.size() ; i++ )
|
|
_frames.erase(_frames.begin()+i);
|
|
_frames.clear();
|
|
|
|
|
|
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
|
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
|
|
|
// On ajoute au rendu
|
|
this->start(_index, _timeout, _flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [TURNRIGHT] Charge le sprite vers la droite
|
|
=========================================================*/
|
|
void xMarioMario::turnRight(){
|
|
this->stop();
|
|
|
|
for( int i = 0 ; i < _frames.size() ; i++ )
|
|
_frames.erase(_frames.begin()+i);
|
|
_frames.clear();
|
|
|
|
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
|
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
|
|
// On ajoute au rendu
|
|
this->start(_index, _timeout, _flags);
|
|
|
|
} |