From 5f9791f6aa35a5a5e16ab6ad25e27d83d53db29c Mon Sep 17 00:00:00 2001 From: milang Date: Sat, 29 Feb 2020 19:01:17 +0100 Subject: [PATCH] optimized ennemi behaviour (to be tested) replace ennemies specific behaviours by a switch => means less duplicated code --- src/ennemi.c | 202 +++++++++++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 113 deletions(-) diff --git a/src/ennemi.c b/src/ennemi.c index 65b180c..9b3bb9a 100644 --- a/src/ennemi.c +++ b/src/ennemi.c @@ -175,45 +175,14 @@ void ennemiMove(ennemi_t *e) } } + if (e->b.y<0) e->type=NONE; + const bool mario_fatal_hit = (mario.p.last_vy<=-2 || mario.p.vy<=-2); - if (e->type==GOOMBA_ID) - { - boxMove(&e->b); - - if (e->b.vx==0) - { - if (e->p1==0) - { - e->b.vx=-1; - e->p1=-1; - } - else - { - e->p1*=-1; - e->b.vx=e->p1; - } - } - if (e->b.y<0) - e->type=NONE; - if (e_hitMario) - { - if (mario_fatal_hit) - { - e->life=DEAD; - e->p1=0; - scoreAdd(KILL_ENNEMI); - mario.p.vy=4; - mario.p.y=e->b.y+ennemi_heights[GOOMBA_ID]+1; - } - else - hurtMario(); - } - return; - } - - if (e->type==KOOPA_V_ID) + switch (e->type) // move { + case GOOMBA_ID: + case KOOPA_V_ID: boxMove(&e->b); if (e->b.vx==0) { @@ -228,28 +197,9 @@ void ennemiMove(ennemi_t *e) e->b.vx=e->p1; } } - if (e->b.y<0) - e->type=NONE; - if (e_hitMario) - { - if (mario_fatal_hit) - { - e->type=CARAPACE_VERTE; - e->b.h=ennemi_heights[CARAPACE_VERTE]; - e->p1=2; - e->b.vx=0; - scoreAdd(KILL_ENNEMI); - mario.p.vy=4; - mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1; - } - else - hurtMario(); - } - return; - } + break; - if (e->type==KOOPA_R_ID) - { + case KOOPA_R_ID: boxMove(&e->b); if (e->b.vx==0) { @@ -267,8 +217,7 @@ void ennemiMove(ennemi_t *e) else { // demi tour automatique int s=-1; - if (e->b.vx>0) - s=e->b.w; + if (e->b.vx>0) s=e->b.w; if (worldGetCellCategory(e->b.x+s, e->b.y-1)==CTG_EMPTY && sgn(e->b.vx)==sgn(s)) { @@ -276,30 +225,11 @@ void ennemiMove(ennemi_t *e) e->b.vx=e->p1; } } + break; - - if (e->b.y<0) - e->type=NONE; - if (e_hitMario) - { - if (mario_fatal_hit) - { - e->type=CARAPACE_ROUGE; - e->b.h=ennemi_heights[CARAPACE_ROUGE]; - e->p1=2; - e->b.vx=0; - scoreAdd(KILL_ENNEMI); - mario.p.vy=4; - mario.p.y=e->b.y+ennemi_heights[CARAPACE_ROUGE]+1; - } - else - hurtMario(); - } - return; - } - - if (e->type==CARAPACE_VERTE || e->type==CARAPACE_ROUGE) - { + case CARAPACE_VERTE: + case CARAPACE_ROUGE: + boxMove(&e->b); if (e->b.vx==0) { if (e->p1<2) @@ -308,53 +238,99 @@ void ennemiMove(ennemi_t *e) e->b.vx=6*e->p1; } } - if (e->b.y<0) e->type=NONE; if (e->p1>=2) { - e->p1++; - if (e->p1==80) + if ((e->p1++)==80) { // transformation carapace->koopa e->type--; e->b.h=ennemi_heights[e->type]; e->p1=e->b.vx=0; } } + } - if (e_hitMario) + if (e_hitMario && mario_fatal_hit) switch (e->type) // mario attacks + { + case GOOMBA_ID: + e->life=DEAD; + e->p1=0; + scoreAdd(KILL_ENNEMI); + mario.p.vy=4; + mario.p.y=e->b.y+ennemi_heights[e->type]+1; + break; + + case KOOPA_V_ID: + case KOOPA_R_ID: + e->type++; + e->b.h=ennemi_heights[e->type]; + e->p1=2; + e->b.vx=0; + scoreAdd(KILL_ENNEMI); + mario.p.vy=4; + mario.p.y=e->b.y+ennemi_heights[e->type]+1; + break; + + case CARAPACE_VERTE: + case CARAPACE_ROUGE: + if (e->p1==0 || e->p1>=2) { - if (e->p1==0 || e->p1>=2) + if (mario.p.x>=e->b.x) e->p1=-1; + if (mario.p.x<=e->b.x) e->p1=1; + e->b.vx=6*e->p1; + if (mario_fatal_hit) { - if (mario.p.x>=e->b.x) - e->p1=-1; - if (mario.p.x<=e->b.x) - e->p1=1; - e->b.vx=6*e->p1; - if (mario_fatal_hit) - { - mario.p.vy=4; - mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]; - } - else - { // mario bounce - if (mario.p.x>=e->b.x) mario.p.x=e->b.x+e->b.w; - if (mario.p.x<=e->b.x) mario.p.x=e->b.x-mario.p.w; - } + mario.p.vy=4; + mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]; } else - { - if (mario_fatal_hit) - { - e->p1=e->b.vx=0; - mario.p.vy=4; - mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1; - } - else hurtMario(); + { // mario bounce + if (mario.p.x>=e->b.x) mario.p.x=e->b.x+e->b.w; + if (mario.p.x<=e->b.x) mario.p.x=e->b.x-mario.p.w; } - + } - boxMove(&e->b); - return; + else + { + e->p1=e->b.vx=0; + mario.p.vy=4; + mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1; + } + break; + } + else if (e_hitMario) switch (e->type) // hurt mario + { + case GOOMBA_ID: + case KOOPA_V_ID: + case KOOPA_R_ID: + hurtMario(); + break; + + case CARAPACE_VERTE: + case CARAPACE_ROUGE: + if (e->p1==0 || e->p1>=2) + { + if (mario.p.x>=e->b.x) e->p1=-1; + if (mario.p.x<=e->b.x) e->p1=1; + e->b.vx=6*e->p1; + if (mario_fatal_hit) + { + mario.p.vy=4; + mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]; + } + else + { // mario bounce + if (mario.p.x>=e->b.x) mario.p.x=e->b.x+e->b.w; + if (mario.p.x<=e->b.x) mario.p.x=e->b.x-mario.p.w; + } + + } + else hurtMario(); + break; + + } + + }