- [x] Classe parente pour objets mobiles
- [x] Integrer a xMario
This commit is contained in:
parent
f6e8d0ec23
commit
1bff71cae4
|
@ -103,6 +103,9 @@ int main(int argc, char* argv[]) {
|
|||
// Deplacement de Mario
|
||||
mario->moveFromVelocity();
|
||||
|
||||
// Deplacement coquille verte
|
||||
gs.autoMove();
|
||||
|
||||
// Mise a jour du rendu
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
mgr->update(); // Mise a jour du rendu
|
||||
|
|
BIN
SDL#4/main.o
BIN
SDL#4/main.o
Binary file not shown.
|
@ -5,14 +5,18 @@ A FAIRE
|
|||
|
||||
EN COURS
|
||||
========
|
||||
- [ ] Verifier toute la trajectoire pour move()
|
||||
- [x] Refaire texture xGreenShell
|
||||
- [ ] Gestion arret animation + reprise (switch)
|
||||
- [ ] Gestion du deplacement xGreenShell
|
||||
|
||||
- [ ] Verifier toute la trajectoire pour move()
|
||||
|
||||
|
||||
|
||||
FAIT
|
||||
====
|
||||
- [x] Classe parente pour objets mobiles
|
||||
- [x] Integrer a xMario
|
||||
- [x] Gestion direction collision en fonction des verifs de collide()
|
||||
- [x] Erreur saut infini mario
|
||||
- [x] Erreur -> Gestion params velocite en fonction taille bloc
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
/* [BODIES] Inclusion des .cpp des sous-libs
|
||||
=========================================================*/
|
||||
#include "xMario/xMarioMobile/xMarioMobile.cpp"
|
||||
|
||||
#include "xMario/xMarioMario.cpp"
|
||||
#include "xMario/xMarioGrass.cpp"
|
||||
#include "xMario/xMarioGreenShell.cpp"
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
|
||||
/* [HEADERS] Inclusion des .h des sous-libs
|
||||
=========================================================*/
|
||||
#include "xMario/xMarioMobile/xMarioMobile.h"
|
||||
|
||||
#include "xMario/xMarioMario.h"
|
||||
#include "xMario/xMarioGrass.h"
|
||||
#include "xMario/xMarioGreenShell.h"
|
||||
|
|
|
@ -11,6 +11,7 @@ xMarioGreenShell::xMarioGreenShell(xManager *m, int x, int y)
|
|||
(int)( BLOC_SIZE*.7 )
|
||||
}
|
||||
){
|
||||
/* (1) Initialisation des attributs */
|
||||
this->setType("green-shell");
|
||||
_active = false;
|
||||
_intouch = false;
|
||||
|
@ -20,11 +21,20 @@ xMarioGreenShell::xMarioGreenShell(xManager *m, int x, int y)
|
|||
this->addFrame( (SDL_Rect){62, 90, 16, 15} );
|
||||
this->addFrame( (SDL_Rect){62, 90, 16, 15} );
|
||||
this->addFrame( (SDL_Rect){62, 90, 16, 15} );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [AUTOMOVE] Gestion du mouvement
|
||||
=========================================================*/
|
||||
void xMarioGreenShell::autoMove(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [ACTIVE] Retourne l'etat du bouton
|
||||
=========================================================*/
|
||||
|
@ -80,7 +90,7 @@ void xMarioGreenShell::active(bool active){
|
|||
=========================================================*/
|
||||
void xMarioGreenShell::onCollide(vector<int> from, xSprite* by){
|
||||
/* (1) Mario par le cote */
|
||||
if( by->getType() == "Mario" && from[0] != 0 && !_intouch ){
|
||||
if( by->getType() == "Mario" && (from[0]||from[1]) && !_intouch ){
|
||||
|
||||
// si en mvt
|
||||
if( this->active() ){
|
||||
|
@ -96,7 +106,7 @@ void xMarioGreenShell::onCollide(vector<int> from, xSprite* by){
|
|||
|
||||
|
||||
/* (2) Mario par le haut */
|
||||
if( by->getType() == "Mario" && from[1] != -1 ){
|
||||
if( by->getType() == "Mario" && from[2] ){
|
||||
if( !this->active() ) // a l'arret, on met en mvt
|
||||
this->active(true);
|
||||
else // si en mouvement, met a l'arret
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
void onCollide(vector<int> from, xSprite* by);
|
||||
|
||||
|
||||
|
||||
void autoMove();
|
||||
|
||||
bool active(); // Retourne si le bloc est actif ou non
|
||||
void active(bool active); // Active ou non le bloc
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* [CONSTRUCTOR] Construction d'un xMarioMario
|
||||
=========================================================*/
|
||||
xMarioMario::xMarioMario(xManager *m, int x, int y)
|
||||
/* (1) Constructeur animation */
|
||||
: xSpriteAnimation(
|
||||
m,
|
||||
"src/mario.png",
|
||||
|
@ -10,6 +11,16 @@ xMarioMario::xMarioMario(xManager *m, int x, int 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");
|
||||
|
||||
|
@ -22,24 +33,6 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
// Position frame par defaut
|
||||
_pos = "NR"; // bottom-center
|
||||
|
||||
_gravity = BLOC_SIZE * 0.25;
|
||||
|
||||
// Constantes de mouvement sur X
|
||||
_velocity[0] = BLOC_SIZE * 0.0;
|
||||
_mult[0] = BLOC_SIZE * 0.3125;
|
||||
_dec[0] = BLOC_SIZE * 0.021875;
|
||||
_acc[0] = BLOC_SIZE * 0.05;
|
||||
_min_vel[0] = BLOC_SIZE * 0.003125;
|
||||
_max_vel[0] = BLOC_SIZE * 0.3125;
|
||||
|
||||
// Constantes de mouvement sur Y
|
||||
_velocity[1] = BLOC_SIZE * 0.0;
|
||||
_mult[1] = BLOC_SIZE * 1.25;
|
||||
_dec[1] = BLOC_SIZE * 0.009375;
|
||||
_acc[1] = BLOC_SIZE * 0.09375;
|
||||
_min_vel[1] = BLOC_SIZE * 0.00625;
|
||||
_max_vel[1] = BLOC_SIZE * 3.125;
|
||||
|
||||
// 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} );
|
||||
|
@ -48,130 +41,6 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
|
||||
|
||||
|
||||
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
||||
=========================================================*/
|
||||
void xMarioMario::moveFromVelocity(){
|
||||
|
||||
/* (1) Si aucune collision, on deplace */
|
||||
vector<int> after = this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
|
||||
/* (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->turn();
|
||||
|
||||
|
||||
/* (4) Gestion de touche encore enfoncee */
|
||||
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;
|
||||
|
||||
|
||||
/* (5) On diminue la gravite */
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
|
||||
/* (6) Gestion de la gravite */
|
||||
// TROUBLE
|
||||
// TROUBLE
|
||||
// TROUBLE
|
||||
if( !this->onFloor() )
|
||||
_velocity[1] += _gravity;
|
||||
// TROUBLE
|
||||
// TROUBLE
|
||||
// TROUBLE
|
||||
|
||||
/* (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 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] };
|
||||
|
||||
// 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]) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool xMarioMario::onFloor(){
|
||||
|
@ -187,13 +56,17 @@ bool xMarioMario::onWall(){
|
|||
|
||||
|
||||
|
||||
|
||||
/* [TURN] Gestion des sprites en fonction du mouvement
|
||||
/* [SPREAD] Fonction a propager
|
||||
=========================================================*/
|
||||
void xMarioMario::turn(){
|
||||
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 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
|
||||
|
@ -335,6 +208,28 @@ void xMarioMario::turn(){
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -5,31 +5,27 @@
|
|||
|
||||
/* [CST] Constantes et enumeration
|
||||
=========================================================*/
|
||||
#define XMARIO_VEL_HOR true // velocite verticale
|
||||
#define XMARIO_VEL_VER false // velocite horizontale
|
||||
|
||||
|
||||
/* [DEF] Definition de la classe
|
||||
=========================================================*/
|
||||
class xMarioMario : public xSpriteAnimation{
|
||||
class xMarioMario : public xSpriteAnimation, public xMarioMobile{
|
||||
|
||||
public:
|
||||
xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||
|
||||
// Surcharge parent
|
||||
void onCollide(vector<int> from, xSprite* by);
|
||||
|
||||
void moveFromVelocity();
|
||||
|
||||
// Changement de frames en fonction du mouvement
|
||||
void turn();
|
||||
|
||||
// GETTER
|
||||
double velocity(bool way=XMARIO_VEL_HOR); // Recupere velocite
|
||||
// GETTERS
|
||||
bool onFloor(); // Si mario est sur le sol
|
||||
bool onWall(); // Si mario est contre un mur
|
||||
|
||||
// SETTER
|
||||
void velocity(double x=0.0, double y=0.0); // Modification de velocite
|
||||
// PROPAGATION AUX ENFANTS
|
||||
vector<int> spreadMove(int x, int y);
|
||||
void spreadTurn();
|
||||
void spreadUpdateVelocity();
|
||||
void spreadApplyGravity();
|
||||
|
||||
// Surcharge parent
|
||||
void onCollide(vector<int> from, xSprite* by);
|
||||
|
||||
|
||||
// Gestion du suivi du deplacement
|
||||
|
@ -46,14 +42,6 @@
|
|||
|
||||
private:
|
||||
Uint32 _lastmove;
|
||||
double _velocity[2];
|
||||
double _gravity;
|
||||
|
||||
double _mult[2];
|
||||
double _min_vel[2];
|
||||
double _max_vel[2];
|
||||
double _acc[2];
|
||||
double _dec[2];
|
||||
|
||||
|
||||
};
|
||||
|
|
|
@ -0,0 +1,158 @@
|
|||
/* [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] = BLOC_SIZE * 0.0;
|
||||
_mult[0] = BLOC_SIZE * multx;
|
||||
_dec[0] = BLOC_SIZE * decx;
|
||||
_acc[0] = BLOC_SIZE * accx;
|
||||
_min_vel[0] = BLOC_SIZE * minx;
|
||||
_max_vel[0] = BLOC_SIZE * maxx;
|
||||
|
||||
// Constantes de mouvement sur Y
|
||||
_velocity[1] = BLOC_SIZE * 0.0;
|
||||
_mult[1] = BLOC_SIZE * multy;
|
||||
_dec[1] = BLOC_SIZE * decy;
|
||||
_acc[1] = BLOC_SIZE * accy;
|
||||
_min_vel[1] = BLOC_SIZE * miny;
|
||||
_max_vel[1] = BLOC_SIZE * maxy;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
||||
=========================================================*/
|
||||
void xMarioMobile::moveFromVelocity(){
|
||||
|
||||
/* (1) Si aucune collision, on deplace */
|
||||
vector<int> after = this->spreadMove(_velocity[0], _velocity[1]);
|
||||
|
||||
/* (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(int x, int y){
|
||||
// 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;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#ifndef DEF_XMARIOMOBILE_H
|
||||
|
||||
#define DEF_XMARIOMOBILE_H
|
||||
|
||||
|
||||
/* [CST] Constantes et enumeration
|
||||
=========================================================*/
|
||||
#define XMARIO_VEL_HOR true // velocite verticale
|
||||
#define XMARIO_VEL_VER false // velocite horizontale
|
||||
|
||||
/* [DEF] Definition de la classe
|
||||
=========================================================*/
|
||||
class xMarioMobile{
|
||||
|
||||
public:
|
||||
xMarioMobile(
|
||||
double gravity,
|
||||
double multx,
|
||||
double multy,
|
||||
double accx,
|
||||
double accy,
|
||||
double decx,
|
||||
double decy,
|
||||
double minx,
|
||||
double miny,
|
||||
double maxx,
|
||||
double maxy
|
||||
); // Initialisation des params de mouvement
|
||||
|
||||
// Routine appelee dans la boucle principale
|
||||
virtual void moveFromVelocity();
|
||||
|
||||
// GETTER
|
||||
double velocity(bool way=XMARIO_VEL_HOR); // Recupere velocite
|
||||
|
||||
// SETTER
|
||||
void velocity(double x=0.0, double y=0.0); // Modification de velocite
|
||||
|
||||
// PROPAGATION AUX ENFANTS
|
||||
virtual vector<int> spreadMove(int x, int y);
|
||||
virtual void spreadTurn();
|
||||
virtual void spreadUpdateVelocity();
|
||||
virtual void spreadApplyGravity();
|
||||
|
||||
|
||||
// Etat physique
|
||||
string _pos;
|
||||
|
||||
|
||||
protected:
|
||||
Uint32 _lastmove;
|
||||
double _velocity[2];
|
||||
double _gravity;
|
||||
|
||||
double _mult[2];
|
||||
double _min_vel[2];
|
||||
double _max_vel[2];
|
||||
double _acc[2];
|
||||
double _dec[2];
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue