corrections en cours + sprites animes
This commit is contained in:
parent
8671bdd9a5
commit
18d07bef77
|
@ -76,10 +76,10 @@ int main(int argc, char* argv[]) {
|
|||
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
|
||||
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
|
||||
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
||||
left_move = false;
|
||||
right_move = false;
|
||||
up_move = false;
|
||||
down_move = false;
|
||||
mario->_left = false;
|
||||
mario->_right = false;
|
||||
mario->_up = false;
|
||||
mario->_down = false;
|
||||
|
||||
// Boucle de traitement
|
||||
mgr->update(); mgr->update();
|
||||
|
@ -92,15 +92,16 @@ int main(int argc, char* argv[]) {
|
|||
mgr->manageEvents(&event);
|
||||
|
||||
// Gestion de la gravite
|
||||
if( !mgr->hit("mario", 0, 5) )
|
||||
mario->move(0, 5);
|
||||
if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
|
||||
mario->velocity(0, 1.0);
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 7) )
|
||||
mb.jump();
|
||||
else
|
||||
mb.unjump();
|
||||
// Deplacement
|
||||
mario->moveFromVelocity();
|
||||
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 7) ) mb.jump();
|
||||
else mb.unjump();
|
||||
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
mgr->update(); // Mise a jour du rendu
|
||||
}
|
||||
|
@ -156,23 +157,31 @@ void keydownEventHandler(SDL_Event *e){
|
|||
|
||||
switch( (*e).key.keysym.sym ){
|
||||
case SDLK_UP:
|
||||
up_move = true;
|
||||
mario->moveUp(&up_move);
|
||||
if( !mario->_up ){
|
||||
mario->_up = true;
|
||||
// mario->velocity(0, -40.0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
left_move = true;
|
||||
mario->moveLeft(&left_move);
|
||||
if( !mario->_left ){
|
||||
mario->_left = true;
|
||||
// mario->velocity(-15.0, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
right_move = true;
|
||||
mario->moveRight(&right_move);
|
||||
if( !mario->_right ){
|
||||
mario->_right = true;
|
||||
// mario->velocity(15.0, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
// down_move = true;
|
||||
// mario->moveDown(&down_move);
|
||||
if( !mario->_down ){
|
||||
mario->_down = true;
|
||||
// mario->velocity(0, 1.0);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -204,23 +213,19 @@ void keyupEventHandler(SDL_Event *e){
|
|||
|
||||
switch( (*e).key.keysym.sym ){
|
||||
case SDLK_UP:
|
||||
up_move = false;
|
||||
|
||||
mario->_up = false;
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
left_move = false;
|
||||
|
||||
mario->_left = false;
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
right_move = false;
|
||||
|
||||
mario->_right = false;
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
// down_move = false;
|
||||
|
||||
mario->_down = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
BIN
SDL#4/main.o
BIN
SDL#4/main.o
Binary file not shown.
|
@ -6,6 +6,11 @@ A FAIRE
|
|||
- [ ] Gestion de la gravite
|
||||
|
||||
|
||||
EN COURS
|
||||
========
|
||||
- [ ] Erreur modification de frames d'une xAnimation, restent tjs les anciennes
|
||||
-> creer methode dans xAnimation
|
||||
- [ ] Gestion velocite pour deplacement
|
||||
|
||||
FAIT
|
||||
====
|
||||
|
|
|
@ -17,20 +17,35 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
_up = NULL;
|
||||
_down = NULL;
|
||||
|
||||
// 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;
|
||||
|
||||
// this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
// 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} );
|
||||
// this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
||||
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} );
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
/**///this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){269, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){298, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
/**/// this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
// /* (1) On definit les clip de chaque frame */
|
||||
// this->addFrame( (SDL_Rect){21, 0, 18, 32} );
|
||||
|
@ -43,210 +58,124 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
|
||||
|
||||
|
||||
/* [XLEFTMOVEPROCESS] Traitement async de mouvement
|
||||
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
||||
=========================================================*/
|
||||
void xLeftMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 3;
|
||||
void xMarioMario::moveFromVelocity(){
|
||||
|
||||
// Gestion de touche encore enfoncee
|
||||
if( SDL_GetTicks() % 10 < 8 ){
|
||||
if( _left ) this->velocity(-1, 0);
|
||||
else if( _right ) this->velocity(1, 0);
|
||||
|
||||
if( _up ) this->velocity(0, -1);
|
||||
else if( _down ) this->velocity(0, 1);
|
||||
}
|
||||
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 3;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
||||
// Si aucune collision, on deplace
|
||||
if( !m->manager()->hit("mario", -step, 0) ){
|
||||
m->move(-step, 0);
|
||||
hasMoved = true;
|
||||
}
|
||||
if( !_manager->hit(_texture, _velocity[0], _velocity[1]) )
|
||||
this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
// on reduit la distance de mouvement
|
||||
step--;
|
||||
}
|
||||
// On diminue la _velocite
|
||||
_velocity[0] *= ( 1 - _dec[0] );
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
usleep(10000);
|
||||
// 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;
|
||||
|
||||
}
|
||||
cout << floor(_velocity[0]) << " - " << floor(_velocity[1]) << endl;
|
||||
|
||||
// _manager->update();
|
||||
|
||||
// usleep(10000);
|
||||
}
|
||||
|
||||
/* [MOVELEFT] Deplacement vers la gauche
|
||||
|
||||
/* [VELOCITY] Retourne un pointeur sur la velocite
|
||||
=========================================================*/
|
||||
void xMarioMario::moveLeft(bool *run){
|
||||
if( _left != NULL ){
|
||||
delete _left;
|
||||
_left = NULL;
|
||||
}
|
||||
// On lance le thread
|
||||
_left = new thread(xLeftMoveProcess, this, run);
|
||||
_left->detach();
|
||||
double xMarioMario::velocity(bool way){
|
||||
// (way) ? HORIZONTAL : VERTICAL
|
||||
return (way) ? _velocity[0] : _velocity[1];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [XRIGHTMOVEPROCESS] Traitement async de mouvement
|
||||
/* [VELOCITY] Modifie la velocite
|
||||
=========================================================*/
|
||||
void xRightMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 3;
|
||||
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];
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 3;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
||||
// Si aucune collision, on deplace
|
||||
if( !m->manager()->hit("mario", step, 0) ){
|
||||
m->move(step, 0);
|
||||
hasMoved = true;
|
||||
// Gestion des frames
|
||||
if( _velocity[0] > 0 ) this->turnRight();
|
||||
else this->turnLeft();
|
||||
}
|
||||
|
||||
// on reduit la distance de mouvement
|
||||
step--;
|
||||
}
|
||||
if( last[1]*y > 0 )
|
||||
_velocity[1] *= _acc[1];
|
||||
else // sinon changement sens
|
||||
_velocity[1] += y*_mult[1];
|
||||
|
||||
usleep(10000);
|
||||
|
||||
}
|
||||
|
||||
// On retaille la velocite
|
||||
if( abs(_velocity[0]) > _max_vel[0] )
|
||||
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
||||
|
||||
if( abs(_velocity[1]) > _max_vel[1]*BLOC_SIZE )
|
||||
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [MOVERIGHT] Deplacement vers la droite
|
||||
|
||||
bool xMarioMario::onFloor(){
|
||||
return _manager->hit(_texture, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [TURNLEFT] Charge le sprite vers la gauche
|
||||
=========================================================*/
|
||||
void xMarioMario::moveRight(bool *run){
|
||||
if( _right != NULL ){
|
||||
delete _right;
|
||||
_right = NULL;
|
||||
}
|
||||
void xMarioMario::turnLeft(){
|
||||
this->stop();
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin()+i);
|
||||
_frames.clear();
|
||||
|
||||
|
||||
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_index, _timeout, _flags);
|
||||
|
||||
// On lance le thread
|
||||
_right = new thread(xRightMoveProcess, this, run);
|
||||
_right->detach();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [XUPMOVEPROCESS] Traitement async de mouvement
|
||||
/* [TURNRIGHT] Charge le sprite vers la droite
|
||||
=========================================================*/
|
||||
void xUpMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 7;
|
||||
void xMarioMario::turnRight(){
|
||||
this->stop();
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin()+i);
|
||||
_frames.clear();
|
||||
|
||||
|
||||
while( *run ){
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
hasMoved = false;
|
||||
step = 7;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
// On ajoute au rendu
|
||||
this->start(_index, _timeout, _flags);
|
||||
|
||||
// Si aucune collision, on deplace
|
||||
if( !m->manager()->hit("mario", 0, -step) ){
|
||||
m->move(0, -step);
|
||||
hasMoved = true;
|
||||
}
|
||||
|
||||
// on reduit la distance de mouvement
|
||||
step--;
|
||||
}
|
||||
|
||||
usleep(10000);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [MOVEUP] Deplacement vers le haut
|
||||
=========================================================*/
|
||||
void xMarioMario::moveUp(bool *run){
|
||||
if( _up != NULL ){
|
||||
delete _up;
|
||||
_up = NULL;
|
||||
}
|
||||
|
||||
// On lance le thread
|
||||
_up = new thread(xUpMoveProcess, this, run);
|
||||
_up->detach();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [XDOWNMOVEPROCESS] Traitement async de mouvement
|
||||
=========================================================*/
|
||||
void xDownMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 5;
|
||||
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 5;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
||||
// Si aucune collision, on deplace
|
||||
if( !m->manager()->hit("mario", 0, step) ){
|
||||
m->move(0, step);
|
||||
hasMoved = true;
|
||||
}
|
||||
|
||||
// on reduit la distance de mouvement
|
||||
step--;
|
||||
}
|
||||
|
||||
usleep(10000);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [MOVEDOWN] Deplacement vers le bas
|
||||
=========================================================*/
|
||||
void xMarioMario::moveDown(bool *run){
|
||||
if( _down != NULL ){
|
||||
delete _down;
|
||||
_down = NULL;
|
||||
}
|
||||
|
||||
// On lance le thread
|
||||
_down = new thread(xDownMoveProcess, this, run);
|
||||
_down->detach();
|
||||
}
|
|
@ -2,31 +2,49 @@
|
|||
|
||||
#define DEF_XMARIOMARIO_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 xMarioMario : public xSpriteAnimation{
|
||||
|
||||
public:
|
||||
xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||
|
||||
void moveLeft(bool *run); // Deplacement a gauche
|
||||
void moveRight(bool *run); // Deplacement a droite
|
||||
void moveUp(bool *run); // Deplacement en haut
|
||||
void moveDown(bool *run); // Deplacement en bas
|
||||
void moveFromVelocity();
|
||||
|
||||
// Changement de frames
|
||||
void turnLeft();
|
||||
void turnRight();
|
||||
|
||||
// GETTER
|
||||
double velocity(bool way=XMARIO_VEL_HOR); // Recupere velocite
|
||||
bool onFloor(); // Si mario est sur le sol
|
||||
|
||||
// SETTER
|
||||
void velocity(double x=0.0, double y=0.0); // Modification de velocite
|
||||
|
||||
|
||||
// Gestion du suivi du deplacement
|
||||
bool _left;
|
||||
bool _right;
|
||||
bool _up;
|
||||
bool _down;
|
||||
|
||||
|
||||
private:
|
||||
Uint32 _lastmove;
|
||||
float _acceleration;
|
||||
double _velocity[2];
|
||||
|
||||
// Gestion du deplacement
|
||||
thread *_left;
|
||||
thread *_right;
|
||||
thread *_up;
|
||||
thread *_down;
|
||||
|
||||
// Fontions du thread
|
||||
friend void xLeftMoveProcess(xMarioMario *m, bool *run);
|
||||
friend void xRightMoveProcess(xMarioMario *m, bool *run);
|
||||
friend void xUpMoveProcess(xMarioMario *m, bool *run);
|
||||
friend void xDownMoveProcess(xMarioMario *m, bool *run);
|
||||
double _mult[2];
|
||||
double _min_vel[2];
|
||||
double _max_vel[2];
|
||||
double _acc[2];
|
||||
double _dec[2];
|
||||
|
||||
|
||||
};
|
||||
|
|
|
@ -51,9 +51,6 @@ void xMarioMysteryBloc::jump(){
|
|||
/* [UNJUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::unjump(){
|
||||
// if( !this->active() )
|
||||
// return;
|
||||
|
||||
if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){
|
||||
|
||||
_viewport.x = _defaultrect.x;
|
||||
|
@ -82,29 +79,40 @@ bool xMarioMysteryBloc::active(){
|
|||
void xMarioMysteryBloc::active(bool active){
|
||||
_active = active;
|
||||
|
||||
this->stop();
|
||||
|
||||
if( active ){
|
||||
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->pull();
|
||||
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 0, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 16, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 32, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 48, 16, 16} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_index, _timeout, _flags);
|
||||
|
||||
}else{
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->stop();
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->push(_index);
|
||||
|
||||
}
|
||||
|
||||
this->start(_index, _timeout, _flags);
|
||||
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <cmath>
|
||||
#include <unistd.h>
|
||||
|
||||
/* [DEF] Definitions basiques
|
||||
|
|
|
@ -147,14 +147,14 @@ void xSpriteAnimation::pull(){
|
|||
=========================================================*/
|
||||
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
||||
int timer = 0;
|
||||
int step = 1;
|
||||
bool way = true;
|
||||
int start = 0;
|
||||
int stop = xSA->_frames.size();
|
||||
|
||||
while( flags&SPRITE_ANIM_INFINITE ){
|
||||
|
||||
/* (1) Pour chaque sprite */
|
||||
for( int i = start ; i != xSA->_frames.size() ; i+=step ){
|
||||
for( int i = start ; i != xSA->_frames.size() ; i+=(way?1:-1) ){
|
||||
timer = SDL_GetTicks();
|
||||
|
||||
|
||||
|
@ -172,9 +172,9 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
|
||||
// SPRITE_ANIM_REVERSE
|
||||
if( flags&SPRITE_ANIM_REVERSE ){
|
||||
step *= -1;
|
||||
start = (step==1) ? 0 : xSA->_frames.size()-1;
|
||||
stop = (step==1) ? xSA->_frames.size()-1 : 0;
|
||||
way = !way;
|
||||
start = (way) ? 0 : xSA->_frames.size()-1;
|
||||
stop = (way) ? xSA->_frames.size()-1 : 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue