2016-03-13 17:33:47 +00:00
|
|
|
/* [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-16 09:17:28 +00:00
|
|
|
this->setType("Mario");
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
_left = false;
|
|
|
|
_right = false;
|
|
|
|
_up = false;
|
|
|
|
_down = false;
|
|
|
|
_jumps = 0;
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// Position frame par defaut
|
|
|
|
_pos = "NR"; // bottom-center
|
|
|
|
|
2016-03-16 10:49:11 +00:00
|
|
|
_gravity = 8;
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
// Constantes de mouvement sur X
|
2016-03-14 12:14:35 +00:00
|
|
|
_velocity[0] = 0.0;
|
|
|
|
_mult[0] = 10;
|
2016-03-15 10:54:48 +00:00
|
|
|
_dec[0] = .7;
|
2016-03-16 09:17:28 +00:00
|
|
|
_acc[0] = 1.6;
|
2016-03-14 12:14:35 +00:00
|
|
|
_min_vel[0] = 0.1;
|
2016-03-15 10:54:48 +00:00
|
|
|
_max_vel[0] = 10;
|
2016-03-14 12:14:35 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
// Constantes de mouvement sur Y
|
2016-03-14 12:14:35 +00:00
|
|
|
_velocity[1] = 0.0;
|
2016-03-16 11:08:22 +00:00
|
|
|
_mult[1] = 40;
|
2016-03-15 10:54:48 +00:00
|
|
|
_dec[1] = .3;
|
2016-03-16 09:17:28 +00:00
|
|
|
_acc[1] = 3;
|
2016-03-15 10:54:48 +00:00
|
|
|
_min_vel[1] = 0.2;
|
2016-03-14 15:10:21 +00:00
|
|
|
_max_vel[1] = 100;
|
2016-03-14 12:14:35 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
// On definit les frames de l'animation par defaut
|
2016-03-14 15:10:21 +00:00
|
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
2016-03-15 21:56:39 +00:00
|
|
|
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
|
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-15 21:56:39 +00:00
|
|
|
void xMarioMario::moveFromVelocity(){
|
|
|
|
|
|
|
|
/* (1) Si aucune collision, on deplace */
|
2016-03-16 09:17:28 +00:00
|
|
|
vector<int> after;
|
|
|
|
after = this->move(_velocity[0], _velocity[1]);
|
2016-03-15 21:56:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) On modifie la velocite en fonction des collisions */
|
2016-03-15 22:54:12 +00:00
|
|
|
_velocity[0] = (double) after[2];
|
|
|
|
_velocity[1] = (double) after[3];
|
2016-03-15 10:54:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Modification du sprite en fonction du mouvement */
|
|
|
|
this->turn();
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
/* (4) Gestion de touche encore enfoncee */
|
2016-03-14 15:10:21 +00:00
|
|
|
if( _left ) this->velocity(-1, 0);
|
|
|
|
else if( _right ) this->velocity(1, 0);
|
2016-03-15 21:56:39 +00:00
|
|
|
// diminution de l'acceleration quand on lache <- ET ->
|
2016-03-15 10:54:48 +00:00
|
|
|
else _velocity[0] *= ( 1 - _dec[0] );
|
2016-03-14 15:10:21 +00:00
|
|
|
|
|
|
|
// 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++;
|
|
|
|
}
|
2016-03-14 18:28:18 +00:00
|
|
|
}else
|
2016-03-14 15:10:21 +00:00
|
|
|
_jumps = 0;
|
2016-03-14 12:14:35 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
|
|
|
|
/* (5) On diminue la gravite */
|
2016-03-14 12:14:35 +00:00
|
|
|
_velocity[1] *= ( 1 - _dec[1] );
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
/* (6) Gestion de la gravite */
|
2016-03-16 09:17:28 +00:00
|
|
|
// TROUBLE
|
|
|
|
// TROUBLE
|
|
|
|
// TROUBLE
|
2016-03-14 15:10:21 +00:00
|
|
|
if( !this->onFloor() )
|
2016-03-16 10:49:11 +00:00
|
|
|
_velocity[1] += _gravity;
|
2016-03-16 09:17:28 +00:00
|
|
|
// TROUBLE
|
|
|
|
// TROUBLE
|
|
|
|
// TROUBLE
|
2016-03-15 21:56:39 +00:00
|
|
|
|
|
|
|
/* (7) Si velocite sous borne min, on met a 0 */
|
2016-03-15 10:54:48 +00:00
|
|
|
if( abs(_velocity[0]) < _min_vel[0] ) // sur x
|
2016-03-14 15:10:21 +00:00
|
|
|
_velocity[0] = 0;
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
if( abs(_velocity[1]) < _min_vel[1] ) // sur y
|
2016-03-14 15:10:21 +00:00
|
|
|
_velocity[1] = 0;
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
/* (8) Gestion du temps */
|
2016-03-15 22:54:12 +00:00
|
|
|
// if( abs(_velocity[0]) > 0 )
|
|
|
|
// cerr << "x -> " << _velocity[0] << endl;
|
2016-03-14 18:28:18 +00:00
|
|
|
|
2016-03-15 22:54:12 +00:00
|
|
|
// if( abs(_velocity[1]) > 0 )
|
|
|
|
// cerr << "y -> " << _velocity[1] << endl;
|
2016-03-14 18:28:18 +00:00
|
|
|
|
2016-03-15 10:54:48 +00:00
|
|
|
// cout << endl;
|
2016-03-15 21:56:39 +00:00
|
|
|
// usleep(20000);
|
2016-03-14 12:14:35 +00:00
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +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 18:28:18 +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] };
|
|
|
|
|
2016-03-15 10:54:48 +00:00
|
|
|
// Vrai si on change pas de sens sur X
|
|
|
|
bool sameway_x = (last[0]*x) > 0;
|
|
|
|
// Vrai si on change pas de sens sur y
|
|
|
|
bool sameway_y = (last[1]*y) > 0;
|
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
/* (1) Gestion velocite axe X */
|
2016-03-15 10:54:48 +00:00
|
|
|
if( sameway_x ) // Si meme sens, on accelere
|
2016-03-16 09:17:28 +00:00
|
|
|
_velocity[0] *= _acc[0];
|
2016-03-14 18:28:18 +00:00
|
|
|
else
|
2016-03-14 15:10:21 +00:00
|
|
|
_velocity[0] += x * _mult[0];
|
2016-03-14 12:14:35 +00:00
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
/* (2) Gestion velocite axe Y */
|
2016-03-15 10:54:48 +00:00
|
|
|
if( sameway_y ) // Si meme sens, on accelere
|
2016-03-16 09:17:28 +00:00
|
|
|
_velocity[1] *= _acc[1];
|
2016-03-14 15:10:21 +00:00
|
|
|
else
|
2016-03-16 09:17:28 +00:00
|
|
|
_velocity[1] += (y>0) ? y : y * _mult[1];
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
/* (3) On borne la velocite aux max */
|
|
|
|
if( abs(_velocity[0]) > _max_vel[0] ) // Si max x
|
2016-03-14 12:14:35 +00:00
|
|
|
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
if( abs(_velocity[1]) > _max_vel[1] ) // Si max y
|
2016-03-14 12:14:35 +00:00
|
|
|
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
|
2016-03-14 15:10:21 +00:00
|
|
|
|
2016-03-14 12:14:35 +00:00
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-14 12:14:35 +00:00
|
|
|
bool xMarioMario::onFloor(){
|
2016-03-15 23:15:18 +00:00
|
|
|
return _manager->hit((xSprite*)this, 0, 1);
|
2016-03-14 12:14:35 +00:00
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [TURN] Gestion des sprites en fonction du mouvement
|
2016-03-13 19:36:16 +00:00
|
|
|
=========================================================*/
|
2016-03-14 18:28:18 +00:00
|
|
|
void xMarioMario::turn(){
|
|
|
|
/* (0) Variables utiles */
|
|
|
|
bool left = _velocity[0] < 0; // si vers la gauche
|
|
|
|
bool up = _velocity[1] < 0; // si vers le haut
|
|
|
|
|
|
|
|
bool centerx = _velocity[0] == 0; // centre sur x
|
|
|
|
bool centery = _velocity[1] == 0; // centre sur y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Vers le haut */
|
2016-03-15 10:54:48 +00:00
|
|
|
if( up && !this->onFloor() ){
|
2016-03-14 18:28:18 +00:00
|
|
|
|
|
|
|
/* (1.2) HAUT - CENTRE */
|
|
|
|
if( centerx ){ // tc
|
|
|
|
if( _pos != "TC" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "TC";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (1.1) HAUT - GAUCHE */
|
|
|
|
}else if( left ){ // tl
|
|
|
|
if( _pos != "TL" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){31, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){31, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "TL";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (1.2) HAUT - DROITE */
|
|
|
|
}else{ // tr
|
|
|
|
if( _pos != "TR" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "TR";
|
|
|
|
return;
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
/* (2) Vers le bas */
|
|
|
|
}else if( !centery ){
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
/* (2.2) BAS - CENTRE */
|
2016-03-15 10:54:48 +00:00
|
|
|
if( centerx && !this->onFloor() ){ // bc
|
2016-03-14 18:28:18 +00:00
|
|
|
if( _pos != "BC" ){
|
2016-03-16 09:17:28 +00:00
|
|
|
// this->pull();
|
|
|
|
// this->clear();
|
|
|
|
// this->addFrame( (SDL_Rect){122, 160, 19, 29} );
|
|
|
|
// this->addFrame( (SDL_Rect){122, 160, 19, 29} );
|
|
|
|
// this->push(_index);
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-16 09:17:28 +00:00
|
|
|
// _pos = "BC";
|
2016-03-14 18:28:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2.1) BAS - GAUCHE */
|
|
|
|
}else if( left ){ // bl
|
|
|
|
if( _pos != "BL" ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2.2) BAS - DROITE */
|
|
|
|
}else{ // br
|
|
|
|
if( _pos != "BR" ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
/* (3) Droit */
|
|
|
|
}else{
|
|
|
|
|
|
|
|
/* (3.2) NORMAL - CENTRE */
|
|
|
|
if( centerx ){ // nc
|
|
|
|
if( _pos != "NR" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "NR";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3.1) NORMAL - GAUCHE */
|
|
|
|
}else if( left ){ // nl
|
|
|
|
if( _pos != "NL" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "NL";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (3.2) NORMAL - DROITE */
|
|
|
|
}else{ // nr
|
|
|
|
if( _pos != "NR" ){
|
|
|
|
this->pull();
|
|
|
|
this->clear();
|
|
|
|
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
|
|
|
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
|
|
|
this->push(_index);
|
|
|
|
|
|
|
|
_pos = "NR";
|
|
|
|
return;
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2016-03-16 09:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [ONCOLLIDE] Gestion des collisions
|
|
|
|
=========================================================*/
|
|
|
|
void xMarioMario::onCollide(vector<int> from, xSprite* by){
|
|
|
|
/* (1) Mort par carapace */
|
|
|
|
if( by->getType() == "green-shell" && from[0] != 0 )
|
|
|
|
cout << "MORT" << endl;
|
|
|
|
|
|
|
|
/* (2) Casse la carapace */
|
|
|
|
if( by->getType() == "green-shell" && from[1] == -1 )
|
|
|
|
by->pull();
|
2016-03-13 17:33:47 +00:00
|
|
|
}
|