158 lines
3.3 KiB
C++
158 lines
3.3 KiB
C++
/* [CONSTRUCTOR] Construction d'un xMarioMobile
|
|
=========================================================*/
|
|
xMarioMobile::xMarioMobile(
|
|
double gravity,
|
|
double multx, double multy,
|
|
double accx, double accy,
|
|
double decx, double decy,
|
|
double minx, double miny,
|
|
double maxx, double maxy){
|
|
|
|
_gravity = BLOC_SIZE * gravity;
|
|
|
|
// Constantes de mouvement sur X
|
|
_velocity[0] = 0.0;
|
|
_mult[0] = multx;
|
|
_dec[0] = decx;
|
|
_acc[0] = accx;
|
|
_min_vel[0] = minx;
|
|
_max_vel[0] = maxx;
|
|
|
|
// Constantes de mouvement sur Y
|
|
_velocity[1] = 0.0;
|
|
_mult[1] = multy;
|
|
_dec[1] = decy;
|
|
_acc[1] = accy;
|
|
_min_vel[1] = miny;
|
|
_max_vel[1] = maxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
|
=========================================================*/
|
|
void xMarioMobile::moveFromVelocity(){
|
|
|
|
/* (1) Si aucune collision, on deplace */
|
|
vector<int> after = this->spreadMove();
|
|
|
|
/* (2) On modifie la velocite en fonction des collisions */
|
|
_velocity[0] = (double) after[0];
|
|
_velocity[1] = (double) after[1];
|
|
|
|
|
|
/* (3) Modification du sprite en fonction du mouvement */
|
|
this->spreadTurn();
|
|
|
|
|
|
/* (4) Gestion de mouvement */
|
|
this->spreadUpdateVelocity();
|
|
|
|
|
|
/* (5) On diminue la gravite */
|
|
_velocity[1] *= ( 1 - _dec[1] );
|
|
|
|
|
|
/* (6) Gestion de la gravite */
|
|
this->spreadApplyGravity();
|
|
|
|
|
|
/* (7) Si velocite sous borne min, on met a 0 */
|
|
if( abs(_velocity[0]) < _min_vel[0] ) // sur x
|
|
_velocity[0] = 0;
|
|
|
|
if( abs(_velocity[1]) < _min_vel[1] ) // sur y
|
|
_velocity[1] = 0;
|
|
|
|
|
|
/* (8) Gestion du temps */
|
|
// if( abs(_velocity[0]) > 0 )
|
|
// cerr << "x -> " << _velocity[0] << endl;
|
|
|
|
// if( abs(_velocity[1]) > 0 )
|
|
// cerr << "y -> " << _velocity[1] << endl;
|
|
|
|
// cout << endl;
|
|
usleep(20000);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [VELOCITY] Retourne un pointeur sur la velocite
|
|
=========================================================*/
|
|
double xMarioMobile::velocity(bool way){
|
|
// (way) ? HORIZONTAL : VERTICAL
|
|
return (way) ? _velocity[0] : _velocity[1];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [VELOCITY] Modifie la velocite
|
|
=========================================================*/
|
|
void xMarioMobile::velocity(double x, double y){
|
|
double last[] = { _velocity[0], _velocity[1] };
|
|
|
|
// 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;
|
|
|
|
/* (1) Gestion velocite axe X */
|
|
if( sameway_x ) // Si meme sens, on accelere
|
|
_velocity[0] *= _acc[0];
|
|
else
|
|
_velocity[0] += x * _mult[0];
|
|
|
|
/* (2) Gestion velocite axe Y */
|
|
if( sameway_y ) // Si meme sens, on accelere
|
|
_velocity[1] *= _acc[1];
|
|
else
|
|
_velocity[1] += (y>0) ? y : 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]) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [SPREAD] Fonction a propager
|
|
=========================================================*/
|
|
vector<int> xMarioMobile::spreadMove(){
|
|
// To implement in children
|
|
cout << "PARENT" << endl;
|
|
}
|
|
|
|
|
|
void xMarioMobile::spreadTurn(){
|
|
// To implement in children
|
|
cout << "PARENT" << endl;
|
|
}
|
|
|
|
|
|
void xMarioMobile::spreadUpdateVelocity(){
|
|
// To implement in children
|
|
cout << "PARENT" << endl;
|
|
}
|
|
|
|
|
|
void xMarioMobile::spreadApplyGravity(){
|
|
// To implement in children
|
|
cout << "PARENT" << endl;
|
|
} |