lab.cpp/SDL#4/xMario/xMarioMario.cpp

181 lines
4.4 KiB
C++
Raw Normal View History

/* [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
}
){
2016-03-13 19:36:16 +00:00
_left = NULL;
_right = NULL;
_up = NULL;
_down = NULL;
2016-03-14 12:14:35 +00:00
// 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} );
2016-03-14 12:14:35 +00:00
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} );
2016-03-14 12:14:35 +00:00
/**///this->addFrame( (SDL_Rect){238, 0, 19, 29} );
// this->addFrame( (SDL_Rect){269, 0, 19, 29} );
// this->addFrame( (SDL_Rect){298, 0, 19, 29} );
2016-03-14 12:14:35 +00:00
/**/// 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} );
2016-03-13 19:36:16 +00:00
}
2016-03-14 12:14:35 +00:00
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
2016-03-13 19:36:16 +00:00
=========================================================*/
2016-03-14 12:14:35 +00:00
void xMarioMario::moveFromVelocity(){
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// Gestion de touche encore enfoncee
if( SDL_GetTicks() % 10 < 8 ){
if( _left ) this->velocity(-1, 0);
else if( _right ) this->velocity(1, 0);
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
if( _up ) this->velocity(0, -1);
else if( _down ) this->velocity(0, 1);
2016-03-13 19:36:16 +00:00
}
2016-03-14 12:14:35 +00:00
// 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] );
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// 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;
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
cout << floor(_velocity[0]) << " - " << floor(_velocity[1]) << endl;
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// _manager->update();
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// usleep(10000);
}
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
/* [VELOCITY] Retourne un pointeur sur la velocite
2016-03-13 19:36:16 +00:00
=========================================================*/
2016-03-14 12:14:35 +00:00
double xMarioMario::velocity(bool way){
// (way) ? HORIZONTAL : VERTICAL
return (way) ? _velocity[0] : _velocity[1];
2016-03-13 19:36:16 +00:00
}
2016-03-14 12:14:35 +00:00
/* [VELOCITY] Modifie la velocite
2016-03-13 19:36:16 +00:00
=========================================================*/
2016-03-14 12:14:35 +00:00
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();
2016-03-13 19:36:16 +00:00
}
2016-03-14 12:14:35 +00:00
if( last[1]*y > 0 )
_velocity[1] *= _acc[1];
else // sinon changement sens
_velocity[1] += y*_mult[1];
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// On retaille la velocite
if( abs(_velocity[0]) > _max_vel[0] )
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
if( abs(_velocity[1]) > _max_vel[1]*BLOC_SIZE )
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
}
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
bool xMarioMario::onFloor(){
return _manager->hit(_texture, 0, 1);
}
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
/* [TURNLEFT] Charge le sprite vers la gauche
2016-03-13 19:36:16 +00:00
=========================================================*/
2016-03-14 12:14:35 +00:00
void xMarioMario::turnLeft(){
this->stop();
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
for( int i = 0 ; i < _frames.size() ; i++ )
_frames.erase(_frames.begin()+i);
_frames.clear();
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// On ajoute au rendu
this->start(_index, _timeout, _flags);
2016-03-13 19:36:16 +00:00
}
2016-03-14 12:14:35 +00:00
/* [TURNRIGHT] Charge le sprite vers la droite
2016-03-13 19:36:16 +00:00
=========================================================*/
2016-03-14 12:14:35 +00:00
void xMarioMario::turnRight(){
this->stop();
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
for( int i = 0 ; i < _frames.size() ; i++ )
_frames.erase(_frames.begin()+i);
_frames.clear();
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
2016-03-13 19:36:16 +00:00
2016-03-14 12:14:35 +00:00
// On ajoute au rendu
this->start(_index, _timeout, _flags);
2016-03-13 19:36:16 +00:00
}