diff --git a/SDL#4/exe b/SDL#4/exe index 690c584..15cbcfc 100755 Binary files a/SDL#4/exe and b/SDL#4/exe differ diff --git a/SDL#4/main.cpp b/SDL#4/main.cpp index f5adedf..a518277 100644 --- a/SDL#4/main.cpp +++ b/SDL#4/main.cpp @@ -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 diff --git a/SDL#4/main.o b/SDL#4/main.o index 700522c..dc5ffc7 100644 Binary files a/SDL#4/main.o and b/SDL#4/main.o differ diff --git a/SDL#4/src/mario_crop.png b/SDL#4/src/mario_crop.png new file mode 100644 index 0000000..da7b81f Binary files /dev/null and b/SDL#4/src/mario_crop.png differ diff --git a/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.cpp b/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.cpp index 024690d..eed0df4 100644 --- a/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.cpp +++ b/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.cpp @@ -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(); } @@ -111,4 +102,15 @@ void xMarioMysteryBloc::active(bool active){ } +} + + + + +/* [ONCOLLIDE] Gestion des collisions +=========================================================*/ +void xMarioMysteryBloc::onCollide(vector from, xSprite* by){ + /* (1) Saut de mario */ + if( by->getType() == "Mario" && from[1] == 1 ) + cout << "JUMP JUMP" << endl; } \ No newline at end of file diff --git a/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.h b/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.h index 0de036e..9b3929c 100644 --- a/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.h +++ b/SDL#4/xMario/BreakableBloc/xMarioMysteryBloc.h @@ -7,6 +7,10 @@ public: xMarioMysteryBloc(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite + // Surcharge parent + void onCollide(vector 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 \ No newline at end of file diff --git a/SDL#4/xMario/xMarioGrass.cpp b/SDL#4/xMario/xMarioGrass.cpp index 2935f88..b1fc50f 100644 --- a/SDL#4/xMario/xMarioGrass.cpp +++ b/SDL#4/xMario/xMarioGrass.cpp @@ -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} ); diff --git a/SDL#4/xMario/xMarioGreenShell.cpp b/SDL#4/xMario/xMarioGreenShell.cpp index ec99f5a..f699195 100644 --- a/SDL#4/xMario/xMarioGreenShell.cpp +++ b/SDL#4/xMario/xMarioGreenShell.cpp @@ -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} ); } \ No newline at end of file diff --git a/SDL#4/xMario/xMarioMario.cpp b/SDL#4/xMario/xMarioMario.cpp index 132ff38..b574244 100644 --- a/SDL#4/xMario/xMarioMario.cpp +++ b/SDL#4/xMario/xMarioMario.cpp @@ -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 after = this->move(_velocity[0], _velocity[1]); + vector 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]) <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; } @@ -327,4 +328,21 @@ void xMarioMario::turn(){ } +} + + + + + + +/* [ONCOLLIDE] Gestion des collisions +=========================================================*/ +void xMarioMario::onCollide(vector 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(); } \ No newline at end of file diff --git a/SDL#4/xMario/xMarioMario.h b/SDL#4/xMario/xMarioMario.h index e1b6f7c..3e0bbf2 100644 --- a/SDL#4/xMario/xMarioMario.h +++ b/SDL#4/xMario/xMarioMario.h @@ -15,6 +15,9 @@ public: xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite + // Surcharge parent + void onCollide(vector from, xSprite* by); + void moveFromVelocity(); // Changement de frames en fonction du mouvement diff --git a/SDL#4/xSDL/xManager.cpp b/SDL#4/xSDL/xManager.cpp index e9f3070..6d196d2 100644 --- a/SDL#4/xSDL/xManager.cpp +++ b/SDL#4/xSDL/xManager.cpp @@ -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 collideFrom; + collideFrom.push_back( (movex!=0) ? (int)( movex / abs(movex) ) : 0 ); + collideFrom.push_back( (movey!=0) ? (int)( movey / abs(movey) ) : 0 ); + + vector 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; } diff --git a/SDL#4/xSDL/xSprite.cpp b/SDL#4/xSDL/xSprite.cpp index 0ce29dc..4cadfcf 100644 --- a/SDL#4/xSDL/xSprite.cpp +++ b/SDL#4/xSDL/xSprite.cpp @@ -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 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 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 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 diff --git a/SDL#4/xSDL/xSprite.h b/SDL#4/xSDL/xSprite.h index e96dac3..625f1a6 100644 --- a/SDL#4/xSDL/xSprite.h +++ b/SDL#4/xSDL/xSprite.h @@ -15,6 +15,12 @@ vector move(SDL_Rect newpos); // Deplace le sprite vector move(int x, int y); // Deplace le sprite + // Action en cas de collision + virtual void onCollide(vector 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;