Correction bugs + onCollide() gestion collisions sur chaque sprite

This commit is contained in:
xdrm-brackets 2016-03-16 10:17:28 +01:00
parent cb1dc22b9c
commit e23608b1a7
13 changed files with 162 additions and 68 deletions

BIN
SDL#4/exe

Binary file not shown.

View File

@ -40,17 +40,17 @@ int main(int argc, char* argv[]) {
btmcenter.push("bottom-center");
xMarioGrass floattcenter(mgr, (SDL_Rect){5, 2, 5, 5} );
floattcenter.push("float-center");
// xMarioGrass floattcenter(mgr, (SDL_Rect){14, 20-5, 1, 1} );
// floattcenter.push("float-top");
/* [3] Gestion des animations (blocs animes)
=========================================================*/
// On cree une coquille verte
// xMarioGreenShell gs(mgr, 5, 20-3);
// gs.push("green-sheel");
// gs.start(100, SPRITE_ANIM_INFINITE);
xMarioGreenShell gs(mgr, 5, 20-3);
gs.push("green-sheel");
gs.start(100, SPRITE_ANIM_INFINITE);
// On cree une brique
xMarioBrick mbr1(mgr, 4, 20-6);
@ -126,18 +126,17 @@ int main(int argc, char* argv[]) {
mario->moveFromVelocity();
if( mgr->hit("mystery-bloc", 0, 1) ) mb.jump();
else mb.unjump();
// if( mgr->hit("mystery-bloc", 0, 1) ) mb.jump();
// else mb.unjump();
if( mgr->hit("mystery-bloc2", 0, 1) ) mb1.jump();
else mb1.unjump();
// if( mgr->hit("mystery-bloc2", 0, 1) ) mb1.jump();
// else mb1.unjump();
if( mgr->hit("mystery-bloc3", 0, 1) ) mb2.jump();
else mb2.unjump();
// if( mgr->hit("mystery-bloc3", 0, 1) ) mb2.jump();
// else mb2.unjump();
if( mgr->hit("mystery-bloc4", 0, 1) ) mb3.jump();
else mb3.unjump();
// if( mgr->hit("mystery-bloc4", 0, 1) ) mb3.jump();
// else mb3.unjump();
mgr->manageFps(); // Gestion des FPS (speed)
mgr->update(); // Mise a jour du rendu

Binary file not shown.

BIN
SDL#4/src/mario_crop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

View File

@ -6,6 +6,7 @@ xMarioMysteryBloc::xMarioMysteryBloc(xManager *m, int x, int y)
"src/myst_bloc.png",
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}
){
this->setType("mystery-bloc");
/* (2) On definit les clip de chaque frame */
this->addFrame( (SDL_Rect){0, 0, 16, 16} );
@ -31,12 +32,7 @@ void xMarioMysteryBloc::jump(){
if( _defaultrect.y == _dst.y && SDL_GetTicks()-_lastjump > 300 ){
_dst.x = _defaultrect.x;
_dst.y = _defaultrect.y-10;
_dst.w = _defaultrect.w;
_dst.h = _defaultrect.h;
this->move(_dst);
this->move(0, -10);
_lastjump = SDL_GetTicks();
@ -51,14 +47,9 @@ void xMarioMysteryBloc::jump(){
/* [UNJUMP] Animation d'activation (quand saut)
=========================================================*/
void xMarioMysteryBloc::unjump(){
if( _defaultrect.y != _dst.y && SDL_GetTicks()-_lastjump > 100 ){
if( (_defaultrect.y-10) == _dst.y && SDL_GetTicks()-_lastjump > 100 ){
_dst.x = _defaultrect.x;
_dst.y = _defaultrect.y;
_dst.w = _defaultrect.w;
_dst.h = _defaultrect.h;
this->move(_dst);
this->move(0, 10);
_lastjump = SDL_GetTicks();
}
@ -112,3 +103,14 @@ void xMarioMysteryBloc::active(bool active){
}
/* [ONCOLLIDE] Gestion des collisions
=========================================================*/
void xMarioMysteryBloc::onCollide(vector<int> from, xSprite* by){
/* (1) Saut de mario */
if( by->getType() == "Mario" && from[1] == 1 )
cout << "JUMP JUMP" << endl;
}

View File

@ -7,6 +7,10 @@
public:
xMarioMysteryBloc(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
// Surcharge parent
void onCollide(vector<int> from, xSprite* by);
void jump(); // Effectue l'animation d'activation
void unjump(); // Effectue l'animation d'activation
@ -19,6 +23,7 @@
Uint32 _lastjump;
SDL_Rect _defaultrect;
};
#endif

View File

@ -24,10 +24,13 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// On cree une copie du spritesheet
this->add( new xSprite(_manager, _spritesheet) );
// On definit le tyoe
_sprites[index]->setType("Grass");
// TOP-LEFT
if( x == xMin && y == yMin ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){137, 99, 16, 16}
);
@ -35,7 +38,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// TOP RIGHT
else if( x == xMax && y == yMin ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){171, 99, 16, 16}
);
@ -43,7 +46,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// BOTTOM LEFT
else if( x == xMin && y == yMax ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){137, 133, 16, 16}
);
@ -51,7 +54,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// BOTTOM RIGHT
else if( x == xMax && y == yMax ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){171, 133, 16, 16}
);
@ -59,7 +62,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// LEFT
else if( x == xMin ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){137, 116, 16, 16}
);
@ -67,7 +70,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// RIGHT
else if( x == xMax ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){171, 116, 16, 16}
);
@ -75,7 +78,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// TOP
else if( y == yMin ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){154, 99, 16, 16}
);
@ -83,7 +86,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// BOTTOM
else if( y == yMax ){
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){137, 184, 16, 16}
);
@ -91,7 +94,7 @@ xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
// INSIDE
else{
this->get(index)->dimensions(
_sprites[index]->dimensions(
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
(SDL_Rect){137, 167, 16, 16}
);

View File

@ -4,12 +4,18 @@ xMarioGreenShell::xMarioGreenShell(xManager *m, int x, int y)
: xSpriteAnimation(
m,
"src/mario_crop.png",
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}
(SDL_Rect){
(int)( BLOC_SIZE*x+BLOC_SIZE*.1 ),
(int)( BLOC_SIZE*y+BLOC_SIZE*.3 ),
(int)( BLOC_SIZE*.8 ),
(int)( BLOC_SIZE*.7 )
}
){
_type = "green-shell";
/* (2) On definit les clip de chaque frame */
this->addFrame( (SDL_Rect){4*19, 210, 20, 20} );
this->addFrame( (SDL_Rect){5*19, 210, 20, 20} );
this->addFrame( (SDL_Rect){6*19, 210, 20, 20} );
this->addFrame( (SDL_Rect){7*19, 210, 20, 20} );
this->addFrame( (SDL_Rect){79, 213, 16, 16} );
this->addFrame( (SDL_Rect){98, 213, 16, 16} );
this->addFrame( (SDL_Rect){116, 213, 16, 16} );
this->addFrame( (SDL_Rect){135, 213, 16, 16} );
}

View File

@ -11,6 +11,7 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
BLOC_SIZE
}
){
this->setType("Mario");
_left = false;
_right = false;
@ -27,7 +28,7 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
_velocity[0] = 0.0;
_mult[0] = 10;
_dec[0] = .7;
_acc[0] = .6;
_acc[0] = 1.6;
_min_vel[0] = 0.1;
_max_vel[0] = 10;
@ -35,7 +36,7 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
_velocity[1] = 0.0;
_mult[1] = 50;
_dec[1] = .3;
_acc[1] = 2;
_acc[1] = 3;
_min_vel[1] = 0.2;
_max_vel[1] = 100;
@ -52,15 +53,10 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
void xMarioMario::moveFromVelocity(){
/* (1) Si aucune collision, on deplace */
vector<int> after = this->move(_velocity[0], _velocity[1]);
vector<int> after;
after = this->move(_velocity[0], _velocity[1]);
// if( _velocity[0] != after[2] && after[0] != 0 )
// cerr << "collision from " << ((after[0]==1)?"right":"left") << " at " << (_dst.x+after[2]) << endl;
// if( _velocity[1] != after[3] && after[1] != 0 )
// cerr << "collision from " << ((after[1]==1)?"bottom":"top") << " at " << (_dst.y+after[3]) <<endl;
/* (2) On modifie la velocite en fonction des collisions */
_velocity[0] = (double) after[2];
_velocity[1] = (double) after[3];
@ -92,9 +88,14 @@ void xMarioMario::moveFromVelocity(){
/* (6) Gestion de la gravite */
// TROUBLE
// TROUBLE
// TROUBLE
if( !this->onFloor() )
this->move(0, _gravity);
// TROUBLE
// TROUBLE
// TROUBLE
/* (7) Si velocite sous borne min, on met a 0 */
if( abs(_velocity[0]) < _min_vel[0] ) // sur x
@ -146,15 +147,15 @@ void xMarioMario::velocity(double x, double y){
/* (1) Gestion velocite axe X */
if( sameway_x ) // Si meme sens, on accelere
_velocity[0] *= (1+_acc[0]);
_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] *= (1+_acc[1]);
_velocity[1] *= _acc[1];
else
_velocity[1] += y * _mult[1];
_velocity[1] += (y>0) ? y : y * _mult[1];
/* (3) On borne la velocite aux max */
@ -250,13 +251,13 @@ void xMarioMario::turn(){
/* (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);
// this->pull();
// this->clear();
// this->addFrame( (SDL_Rect){122, 160, 19, 29} );
// this->addFrame( (SDL_Rect){122, 160, 19, 29} );
// this->push(_index);
_pos = "BC";
// _pos = "BC";
return;
}
@ -328,3 +329,20 @@ void xMarioMario::turn(){
}
}
/* [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();
}

View File

@ -15,6 +15,9 @@
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

View File

@ -227,6 +227,15 @@ bool xManager::hit(xSprite* current, int movex, int movey){
a.x += movex;
a.y += movey;
// Contiendra le sens de collision
vector<int> collideFrom;
collideFrom.push_back( (movex!=0) ? (int)( movex / abs(movex) ) : 0 );
collideFrom.push_back( (movey!=0) ? (int)( movey / abs(movey) ) : 0 );
vector<int> collideTo;
collideTo.push_back( -collideFrom[0] );
collideTo.push_back( -collideFrom[1] );
/* (2) On regarde si en dehors de la fenetre */
if( (a.x < _winrect.x ) // Inclus a droite
@ -234,6 +243,7 @@ bool xManager::hit(xSprite* current, int movex, int movey){
|| (a.y < _winrect.y ) // Inclus en haut
|| (a.y+a.h > _winrect.y+_winrect.h ) // Inclus en bas
){
cerr << _indexes[xIndex] << " collide with WINDOW" << endl;
_mutex_hit.unlock();
return true;
}
@ -248,14 +258,17 @@ bool xManager::hit(xSprite* current, int movex, int movey){
// On verifie que le sprite n'entre pas en collision
if( this->collide(a, *(_sprites[i])->dst()) ){
// DEBUG
// if( i != 35 )
// cerr << "locked by sprite " << i << endl;
// On lance les listeners de collision
_sprites[i]->onCollide(collideFrom, current);
current->onCollide(collideTo, _sprites[i]);
_debug = *(_sprites[i])->dst();
// _debug = (SDL_Rect){547-1, 531-1, 2, 2};
// cout << "YES collision" << endl;
// if( i != 35 )
// cerr << "COLLISION with" << _indexes[i] << " with under " << (*(_sprites[i])->dst()).y+(*(_sprites[i])->dst()).h-a.y << endl;
_mutex_hit.unlock();
return true;
}

View File

@ -10,6 +10,8 @@ xSprite::~xSprite(){
/* [CONSTRUCTOR] Construction de la surface vide
=========================================================*/
xSprite::xSprite(xManager *m){
_type = "basic";
_manager = m;
SDL_DestroyTexture(_texture);
@ -21,6 +23,8 @@ xSprite::xSprite(xManager *m){
/* [CONSTRUCTOR] Construction de la surface avec couleur
=========================================================*/
xSprite::xSprite(xManager *m, const int rgb[]){
_type = "basic";
_manager = m;
_texture = NULL;
@ -45,6 +49,7 @@ xSprite::xSprite(xManager *m, const int rgb[]){
/* [CONSTRUCTOR] Construction de la surface avec image
=========================================================*/
xSprite::xSprite(xManager *m, const char *url){
_type = "basic";
_manager = m;
_texture = NULL;
@ -60,6 +65,8 @@ xSprite::xSprite(xManager *m, const char *url){
/* [CONSTRUCTOR] Constructions avec copie de texture
=========================================================*/
xSprite::xSprite(xManager *m, SDL_Texture *t){
_type = "basic";
_manager = m;
_texture = t;
@ -132,6 +139,10 @@ vector<int> xSprite::move(int x, int y){
/* (2) Tant qu'on peut bouger (ni x ni y ne vaut 0) */
while( incrx!=0 || incry!=0 ){
result[2] = incrx;
result[3] = incry;
/* (3) Si on peut aller a la destination */
if( !_manager->hit(this, incrx, incry) ){
_dst.x += incrx;
@ -171,14 +182,40 @@ vector<int> xSprite::move(int x, int y){
}
// retour
result[2] = incrx;
result[3] = incry;
_mutex_move.unlock();
return result;
}
/* [ONCOLLIDE] Action en cas de collision
=========================================================*/
void xSprite::onCollide(vector<int> from, xSprite* by){
if( from[0] == 0 && from[1] == 0 )
return;
cerr << _type << " collided with " << by->_type << " from ";
if( from[0] != 0 )
cerr << ((from[0]==1)?"right":"left") << endl;
if( from[1] != 0 )
cerr << ((from[1]==1)?"bottom":"top") << endl;
}
/* [GETTYPE] Retourne le type de sprite
=========================================================*/
string xSprite::getType(){ return _type; }
/* [SETTYPE] Modifie le type de sprite
=========================================================*/
void xSprite::setType(string newtype){ _type = newtype; }
/* [DIMENSIONS] Definition des dimensions par defaut

View File

@ -15,6 +15,12 @@
vector<int> move(SDL_Rect newpos); // Deplace le sprite
vector<int> move(int x, int y); // Deplace le sprite
// Action en cas de collision
virtual void onCollide(vector<int> from, xSprite* by);
string getType(); // renvoie le type de sprite
void setType(string newtype); // modifie le type de sprite
void dimensions(); // Dimensions par defaut
void dimensions(SDL_Rect r); // Dimensions sortie
void dimensions(SDL_Rect r, SDL_Rect clip); // Dimensions in/out
@ -33,8 +39,10 @@
SDL_Rect *src();
protected:
xManager *_manager;
SDL_Texture *_texture;
string _type;
xManager *_manager;
SDL_Texture *_texture;
SDL_Rect _dst;
SDL_Rect _src;