clean mario.c source code

This commit is contained in:
Milang 2020-02-17 14:49:03 +01:00
parent 27a22209e3
commit 72d1ce0255
6 changed files with 124 additions and 174 deletions

Binary file not shown.

View File

@ -2,7 +2,7 @@
[_1]
Type=5
Order=1
Order=0
Top=15
Left=2235
Height=4740
@ -13,11 +13,11 @@ OptionA=0
[_2]
Type=1
Order=0
Order=1
Top=15
Left=7800
Height=4740
Width=5535
Height=6885
Width=8400
State=0
Flags=00000001
OptionA=15
@ -26,8 +26,8 @@ OptionB=15
[_3]
Type=6
Order=2
Top=4740
Left=7800
Top=4035
Left=2715
Height=6390
Width=5535
State=0

Binary file not shown.

View File

@ -30,15 +30,8 @@ extern pnj mario;
extern int coins;
void marioDraw();
//void mario_physics();
void marioJump();
void resetMarioJump(); // resets coyote time & jump buffering (used by teleporters)
void marioResetJump(); // resets coyote time & jump buffering (used by teleporters)
void marioMove();
void marioBigger();
void marioSmaller();
@ -46,7 +39,6 @@ extern int global_quit;
extern int numero_frame;
extern int mario_xMax;
extern int mario_dead;
extern int id_frame;
extern int mario_immunity;
extern int mario_has_bullets;
extern int last_vx_sign;

View File

@ -11,9 +11,6 @@
#include <camera.h>
#include <base.h>
int id_frame=0;
pnj mario=
{
{26,17,6,8,0,0,0,1},
@ -22,8 +19,10 @@ pnj mario=
0
};
// if mario is able to throw fire bullets
int mario_has_bullets=0;
static int mario_timeAccel=0;
// If mario is dead
int mario_dead=0;
// Used to draw mario (mario direction)
@ -32,7 +31,7 @@ int last_vx_sign=1;
// After he becomes little
int mario_immunity=0;
// Time during mario is invincible
int mario_starMode=0;
void marioBigger()
@ -49,7 +48,6 @@ void marioSmaller()
mario_has_bullets=0;
if (mario_immunity==0)
mario_immunity=1;
//mario.p.vy=5;
}
void marioDraw()
@ -73,33 +71,11 @@ void marioDraw()
}
}
int test_sol_le_plus_proche(int x, int y)
{
int distance=0;
int t=0;
while (t==0)
{
int type_sol = worldGetCellCategory(x, y-1);
if (type_sol==CTG_SOIL)
t=1;
if (type_sol==CTG_DEATH)
return distance+1;
type_sol = worldGetCellCategory(x+7, y-1);
if (type_sol==CTG_SOIL)
t=1;
if (type_sol==CTG_DEATH)
return distance+1;
y--;
distance++;
}
return distance;
}
static int jump_buffering=0;
static int coyote_time=5;
void resetMarioJump()
{
void marioResetJump()
{ // disables jump buffering & coyote time until the next time mario hits the ground
jump_buffering=0;
coyote_time=5;
}
@ -107,154 +83,136 @@ void resetMarioJump()
int global_quit=0;
void marioMove()
{
id_frame++;
{ // Mario star mode & immunity counters
if (mario_starMode==200) mario_starMode=0;
else if (mario_starMode) mario_starMode++;
if (mario_starMode==200) mario_starMode=0;
else if (mario_starMode) mario_starMode++;
if (mario_immunity==60) mario_immunity=0;
else if (mario_immunity) mario_immunity++;
if (mario_immunity==60) mario_immunity=0;
else if (mario_immunity) mario_immunity++;
}
if (mario_has_bullets==1 && MKB_getKeyState(MK_RUN)==2) bulletThrow();
if (MKB_getKeyState(MK_JUMP)==2 || jump_buffering) //|| keys[MK_JUMP2]==2)
{
if (mario.p.vx*sgn(mario.p.vx)>=6) boxJump(&mario.p, 9);
else boxJump(&mario.p, 8);
if (mario.p.vy<=0 && MKB_getKeyState(MK_JUMP)==2 && coyote_time<4) // n'a pas sauté alors que la touche était enfoncée et coyote time =4 frames = 0.2 sec
{ // Jump (with coyote time & jump buffering)
if (MKB_getKeyState(MK_JUMP)==2 || jump_buffering) //|| keys[MK_JUMP2]==2)
{
if (mario.p.vx*sgn(mario.p.vx)>=6) mario.p.vy=9;
else mario.p.vy=8;
if (mario.p.vx*sgn(mario.p.vx)>=6) boxJump(&mario.p, 9);
else boxJump(&mario.p, 8);
if (mario.p.vy<=0 && MKB_getKeyState(MK_JUMP)==2 && coyote_time<4) // n'a pas sauté alors que la touche était enfoncée et coyote time =4 frames = 0.2 sec
{
if (mario.p.vx*sgn(mario.p.vx)>=6) mario.p.vy=9;
else mario.p.vy=8;
}
// n'a pas sauté alors que la touche était enfoncée
else if (mario.p.vy<=0 && MKB_getKeyState(MK_JUMP)==2) jump_buffering=5; // Jump buffering during 5-1 frames = 0.2 sec
}
// n'a pas sauté alors que la touche était enfoncée
else if (mario.p.vy<=0 && MKB_getKeyState(MK_JUMP)==2) jump_buffering=5; // Jump buffering during 5-1 frames = 0.2 sec
if (jump_buffering) jump_buffering--;
for (int i=0; i<mario.p.w; i++)
{
if (worldGetCellCategory(mario.p.x+i, mario.p.y-1)==CTG_SOIL)
{
coyote_time=0;
break;
}
}
coyote_time++;
if (mario.p.vy>=2 && MKB_getKeyState(MK_JUMP)==0) mario.p.vy-=2; // Custom jump height
}
if (jump_buffering) jump_buffering--;
for (int i=0; i<mario.p.w; i++)
{
if (worldGetCellCategory(mario.p.x+i, mario.p.y-1)==CTG_SOIL)
{ // Lateral move
int vx=sgn(MKB_getKeyState(MK_RIGHT)-MKB_getKeyState(MK_LEFT)); // Mario wanted dir (arrows)
// cells next to mario
int c1=worldGetCellCategory(mario.p.x, mario.p.y-1/*+mario.p.h*/);
int c2=worldGetCellCategory(mario.p.x+mario.p.w-1, mario.p.y-1/*+mario.p.h*/);
static int mario_timeAccel=0;
mario_timeAccel=1-mario_timeAccel; // increments & %2
if (vx!=sgn(mario.p.vx)) mario_timeAccel=1; // reverse direction means deceleration which is 2 times faster
if (mario_timeAccel)
{
coyote_time=0;
break;
if (vx)
{
// sprinte et est sur le sol
if (MKB_getKeyState(MK_RUN) && (c1==CTG_SOIL || c2==CTG_SOIL) && (abs(mario.p.vx)<=7 || sgn(mario.p.vx)!=vx)) mario.p.vx+=vx;
else if (abs(mario.p.vx+vx) <= 4) mario.p.vx+=vx;
// ralentissement si au dela de la vitesse sans sprint
else if (MKB_getKeyState(MK_RUN)==0 && abs(mario.p.vx)>4) mario.p.vx-=sgn(mario.p.vx);
}
else mario.p.vx-=sgn(mario.p.vx);
}
{ // last vx sign determination
if (vx>0 && (c1==CTG_SOIL || c2==CTG_SOIL)) last_vx_sign=1;
if (vx<0 && (c1==CTG_SOIL || c2==CTG_SOIL)) last_vx_sign=0;
}
if (mario.p.x+mario.p.vx<cameraX(mario.p.x)) // security: avoid mario to go to the left of the screen
{
mario.p.vx=0;
mario.p.x=cameraX(mario.p.x);
}
}
coyote_time++;
if (mario.p.vy>=2 && MKB_getKeyState(MK_JUMP)==0) mario.p.vy-=2; // Custom jump height
boxMove(&mario.p); // apply gravity
int vx=sgn(MKB_getKeyState(MK_RIGHT)-MKB_getKeyState(MK_LEFT)); // Mario wanted dir (arrows)
if (mario.p.y<0) mario_dead=1; // die if out of the map
int c1=worldGetCellCategory(mario.p.x, mario.p.y-1/*+mario.p.h*/);
int c2=worldGetCellCategory(mario.p.x+mario.p.w-1, mario.p.y-1/*+mario.p.h*/);
{// take coins that mario touchs
coin_t* c;
c=(coin_t*)worldGetCell(mario.p.x, mario.p.y);
if (c->type==COIN && c->taken==0) {coinAdd();c->taken=1;}
mario_timeAccel=1-mario_timeAccel;
if (vx==-sgn(mario.p.vx)) mario_timeAccel=1;
if (mario_timeAccel)
{
if (vx)
c=(coin_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y);
if (c->type==COIN && c->taken==0) {coinAdd();c->taken=1;}
c=(coin_t*)worldGetCell(mario.p.x, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0) {coinAdd();c->taken=1;}
c=(coin_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0) {coinAdd();c->taken=1;}
}
{// End level flag detection
end_level_t *e1=(end_level_t*)worldGetCell(mario.p.x, mario.p.y), *e2=(end_level_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y);
if (e1->type==END_LEVEL)
{
// sprinte et est sur le sol
if (MKB_getKeyState(MK_RUN) && (c1==CTG_SOIL || c2==CTG_SOIL) && (abs(mario.p.vx)<=7 || sgn(mario.p.vx)!=vx)) mario.p.vx+=vx;
else if (abs(mario.p.vx+vx) <= 4) mario.p.vx+=vx;
// ralentissement si au dela de la vitesse sans sprint
else if (MKB_getKeyState(MK_RUN)==0 && abs(mario.p.vx)>4) mario.p.vx-=sgn(mario.p.vx);
finish_level=1;
scoreAdd(400*e1->bonus);
}
else mario.p.vx-=sgn(mario.p.vx);
}
if (vx>0 && (c1==CTG_SOIL || c2==CTG_SOIL))
last_vx_sign=1;
if (vx<0 && (c1==CTG_SOIL || c2==CTG_SOIL))
last_vx_sign=0;
if (mario.p.x+mario.p.vx<cameraX(mario.p.x))
{
mario.p.vx=0;
mario.p.x=cameraX(mario.p.x);
}
//teleporteurs_check();
boxMove(&mario.p); // <-> + gravity
if (mario.p.y<0)
mario_dead=1;
if (mario.p.vx==0)
mario_timeAccel=0;
// coins
coin_t* c;
c=(coin_t*)worldGetCell(mario.p.x, mario.p.y);
if (c->type==COIN && c->taken==0)
{coinAdd();c->taken=1;}
c=(coin_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y);
if (c->type==COIN && c->taken==0)
{coinAdd();c->taken=1;}
c=(coin_t*)worldGetCell(mario.p.x, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0)
{coinAdd();c->taken=1;}
c=(coin_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0)
{coinAdd();c->taken=1;}
end_level_t *e1=(end_level_t*)worldGetCell(mario.p.x, mario.p.y), *e2=(end_level_t*)worldGetCell(mario.p.x+mario.p.w-1, mario.p.y);
if (e1->type==END_LEVEL)
{
finish_level=1;
scoreAdd(400*e1->bonus);
}
if (e2->type==END_LEVEL)
{
finish_level=1;
scoreAdd(400*e2->bonus);
}
//if ((mario.p.x+mario.p.w/2)/8==(mario.p.x+mario.p.w/2-1)/8)
//{
gift_t* t=(gift_t*)worldGetCell((mario.p.x+mario.p.w/2),mario.p.y+mario.p.h);
if (t->type==GIFT || t->type==BRICK)
{
brick_t *c=(brick_t*)t;
if (c->time_hit_id==0 && mario.p.last_vy>0)
if (e2->type==END_LEVEL)
{
finish_level=1;
scoreAdd(400*e2->bonus);
}
}
{// mario box auto hit
gift_t* c=(gift_t*)worldGetCell((mario.p.x+mario.p.w/2),mario.p.y+mario.p.h);
if ((c->type==GIFT || c->type==BRICK) && (c->time_hit_id==0 && mario.p.last_vy>0))
{
//mario.p.vy=0;
if (c->content==0 || c->number)
{
c->time_hit_id=1;
mario.p.last_vy=0;
}
if (c->content==1 && c->number)
{
c->number--;
coinAdd();
}
else if (c->content==2 && c->number)
{
c->number--;
bonusSet(BONUS_CHAMPI,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8);
}
else if (c->content==3 && c->number)
{
c->number--;
bonusSet(BONUS_1UP,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8);
}
else if (c->content==4 && c->number)
{
c->number--;
bonusSet(BONUS_STAR,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8);
}
else if (c->content==0 && mario.size==M_BIG && c->type==BRICK)
{
c->state=1;
mario.p.vy=0;
}
c->hidden=0;
mario.p.last_vy=0;
if (c->number) switch (c->content)
{
case 0: c->time_hit_id=1; break;
case 1: c->number--; coinAdd(); break;
case 2: c->number--; bonusSet(BONUS_CHAMPI,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8); break;
case 3: c->number--; bonusSet(BONUS_1UP,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8); break;
case 4: c->number--; bonusSet(BONUS_STAR,((mario.p.x+mario.p.w/2)/8)*8,mario.p.y+mario.p.h+8); break;
}
else if (c->content==0 && mario.size==M_BIG && c->type==BRICK) c->state=1;
}
}
//}
}

View File

@ -51,7 +51,7 @@ void teleportersActive()
}
/* End of animated section */
resetMarioJump();
marioResetJump();
mario.p.x=t.tx*8+4; mario.p.y=t.ty*8; // Move Mario
mario.p.vx=0; mario.p.vy=0; // Disables every move of mario
cameraAdjust();