196 lines
4.7 KiB
C++
196 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 = false;
|
|
_right = false;
|
|
_up = false;
|
|
_down = false;
|
|
_jumps = 0;
|
|
|
|
_gravity = 13;
|
|
|
|
// Constantes de velocite sur X
|
|
_velocity[0] = 0.0;
|
|
_mult[0] = 10;
|
|
_dec[0] = 0.9;
|
|
_acc[0] = 5;
|
|
_min_vel[0] = 0.1;
|
|
_max_vel[0] = 40;
|
|
|
|
// Constantes de velocite sur Y
|
|
_velocity[1] = 0.0;
|
|
_mult[1] = 50;
|
|
_dec[1] = 1;
|
|
_acc[1] = 10;
|
|
_min_vel[1] = 0.1;
|
|
_max_vel[1] = 100;
|
|
|
|
/**/// 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(){
|
|
|
|
/* (1) Gestion de touche encore enfoncee */
|
|
if( _left ) this->velocity(-1, 0);
|
|
else if( _right ) this->velocity(1, 0);
|
|
|
|
// Si touche haut
|
|
if( _up ){
|
|
// Si au sol et premier saut ou autre saut (pour la hauteur)
|
|
if( this->onFloor() && _jumps == 0 || _jumps == 1 ){
|
|
this->velocity(0, -1);
|
|
_jumps++;
|
|
}
|
|
}else if( _down )
|
|
this->velocity(0, 1);
|
|
else
|
|
_jumps = 0;
|
|
|
|
|
|
/* (2) Si aucune collision, on deplace */
|
|
this->move(_velocity[0], _velocity[1]);
|
|
|
|
|
|
/* (3) On diminue la velocite (decceleration) */
|
|
_velocity[0] *= ( 1 - _dec[0] );
|
|
_velocity[1] *= ( 1 - _dec[1] );
|
|
|
|
|
|
/* (4) Gestion de la gravite */
|
|
if( !this->onFloor() )
|
|
this->move(0, 13);
|
|
|
|
/* (5) Si velocite sous borne min */
|
|
if( _velocity[0] < _min_vel[0] ) // sur x
|
|
_velocity[0] = 0;
|
|
|
|
if( _velocity[1] < _min_vel[1] ) // sur y
|
|
_velocity[1] = 0;
|
|
|
|
|
|
/* (6) Gestion du temps */
|
|
// if( _velocity[0]*_velocity[1] != 0 )
|
|
// cout << _velocity[0] << " - " << _velocity[1] << endl;
|
|
usleep(1);
|
|
}
|
|
|
|
|
|
/* [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] };
|
|
|
|
/* (1) Gestion velocite axe X */
|
|
if( last[0]*x > 0 ) // Si meme sens, on accelere
|
|
_velocity[0] *= _acc[0];
|
|
else{
|
|
_velocity[0] += x * _mult[0];
|
|
|
|
if( _velocity[0] > 0 ) // vers droite
|
|
this->turnRight();
|
|
else
|
|
this->turnLeft();
|
|
}
|
|
|
|
|
|
/* (2) Gestion velocite axe Y */
|
|
if( last[1]*y > 0 ) // Si meme sens, on accelere
|
|
_velocity[1] *= _acc[1];
|
|
else
|
|
_velocity[1] += y * _mult[1];
|
|
|
|
/* (3) On borne la velocite aux max */
|
|
if( abs(_velocity[0]) > _max_vel[0] ) // Si max x
|
|
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
|
|
|
if( abs(_velocity[1]) > _max_vel[1] ) // Si max y
|
|
_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();
|
|
this->pull();
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
|
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
|
|
|
// On ajoute au rendu
|
|
this->push(_index);
|
|
this->start(_timeout, _flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [TURNRIGHT] Charge le sprite vers la droite
|
|
=========================================================*/
|
|
void xMarioMario::turnRight(){
|
|
this->stop();
|
|
this->pull();
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
|
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
|
|
// On ajoute au rendu
|
|
this->push(_index);
|
|
this->start(_timeout, _flags);
|
|
|
|
} |