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

241 lines
4.5 KiB
C++
Raw Normal View History

/* [CONSTRUCTOR] Construction d'un xMarioMario
=========================================================*/
xMarioMario::xMarioMario(xManager *m, int x, int y)
/* (1) Constructeur animation */
: xSpriteAnimation(
m,
"src/mario.png",
(SDL_Rect){
(int) (BLOC_SIZE*x+BLOC_SIZE*.1),
BLOC_SIZE*y,
(int) (BLOC_SIZE*.8),
BLOC_SIZE
}
),
/* (2) Constructeur objet mobile */
xMarioMobile(
.25, // gravite
0.3125, 1.25, // multiplicateur
0.05, .09375, // acceleration
0.021875, .009375, // decceleration
0.003125, .00625, // min
0.3125, 3.125 // max
){
this->setType("Mario");
_left = false;
_right = false;
_up = false;
_down = false;
_jumps = 0;
// Position frame par defaut
_pos = "NR"; // bottom-center
// On definit les frames de l'animation par defaut
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
bool xMarioMario::onFloor(){
return _manager->hit((xSprite*)this, 0, 1);
2016-03-14 12:14:35 +00:00
}
2016-03-13 19:36:16 +00:00
bool xMarioMario::onWall(){
return _manager->hit((xSprite*)this, 1, 0) || _manager->hit((xSprite*)this, -1, 0);
}
2016-03-13 19:36:16 +00:00
/* [SPREAD] Fonction a propager
2016-03-13 19:36:16 +00:00
=========================================================*/
vector<int> xMarioMario::spreadMove(int x, int y){
return this->move(x, y);
}
void xMarioMario::spreadTurn(){
/* (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 */
if( up && !this->onFloor() ){
/* (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-13 19:36:16 +00:00
/* (2) Vers le bas */
}else if( !centery ){
2016-03-13 19:36:16 +00:00
/* (2.2) BAS - CENTRE */
if( centerx && !this->onFloor() ){ // bc
if( _pos != "BC" ){
// 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
// _pos = "BC";
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
/* (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
}
}
}
void xMarioMario::spreadUpdateVelocity(){
if( _left ) this->velocity(-1, 0);
else if( _right ) this->velocity(1, 0);
// diminution de l'acceleration quand on lache <- ET ->
else _velocity[0] *= ( 1 - _dec[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
_jumps = 0;
}
void xMarioMario::spreadApplyGravity(){
if( !this->onFloor() )
_velocity[1] += _gravity;
}
/* [ONCOLLIDE] Gestion des collisions
=========================================================*/
void xMarioMario::onCollide(vector<int> from, xSprite* by){
}