supercasiobros/src/mario.c

223 lines
4.4 KiB
C
Executable File

#include "mario.h"
#include "tile.h"
#include "world.h"
#include <gint/display.h>
#include <gint/keyboard.h>
#include "keyboard.h"
#include "box.h"
#include "score.h"
static int sgn(int x)
{
if (x>0)
return 1;
else if (x<0)
return -1;
else
return 0;
}
extern image_t img_mariosmall;
int id_frame=0;
tileset_t mario_small={&img_mariosmall, 8,8, 1};
pnj mario=
{
{26,17,8,8,0,0,1},
M_SMALL,
M_RIGHT1, M_WALK,
0
};
int mario_x_max=0;
int mario_time_accel=0;
int mario_dead=0;
int mario_coins=0;
int last_vx_sign=1;
int last_bonus=0;
int mario_accel=0;
void mario_draw()
{
if (mario.size==M_SMALL)
{
if (mario.p.vx*sgn(mario.p.vx)>=3)
draw_tile(mario.p.x-world_get_real_x0(mario.p.x), mario.p.y-world_get_real_y0(mario.p.y), &mario_small, 2*last_vx_sign+(id_frame/4)%2, 0);
else if (mario.p.vx*sgn(mario.p.vx)>=1)
draw_tile(mario.p.x-world_get_real_x0(mario.p.x), mario.p.y-world_get_real_y0(mario.p.y), &mario_small, 2*last_vx_sign+(id_frame/8)%2, 0);
else
draw_tile(mario.p.x-world_get_real_x0(mario.p.x), mario.p.y-world_get_real_y0(mario.p.y), &mario_small, 2*last_vx_sign, 0);
}
}
int test_sol_le_plus_proche(int x, int y)
{
int distance=0;
int t=0;
while (t==0)
{
int type_sol = world_get_ctg(x, y-1);
if (type_sol==CTG_SOIL)
t=1;
if (type_sol==CTG_DEATH)
return distance+1;
type_sol = world_get_ctg(x+7, y-1);
if (type_sol==CTG_SOIL)
t=1;
if (type_sol==CTG_DEATH)
return distance+1;
y--;
distance++;
}
return distance;
}
int global_quit=0;
void mario_move()
{
id_frame++;
update_keyboard();
int jump = keys[MK_JUMP1] || keys[MK_JUMP2];
if (keys[MK_JUMP1]==2 || keys[MK_JUMP2]==2)
{
if (mario.p.vx*sgn(mario.p.vx)>=3)
box_jump(&mario.p, 8);
else
box_jump(&mario.p, 7);
}
else if (mario.p.vy>=6 && jump==0)
mario.p.vy=5;
int vx=sgn(keys[MK_RIGHT] - keys[MK_LEFT]);
int c1=world_get_ctg(mario.p.x, mario.p.y-1/*+mario.p.h*/);
int c2=world_get_ctg(mario.p.x+mario.p.w-1, mario.p.y-1/*+mario.p.h*/);
if (vx)
{
if (vx==sgn(mario.p.vx) || vx==0)
mario_time_accel++;
else if (vx==-sgn(mario.p.vx))
mario_time_accel=0;
mario_time_accel%=4;
if (mario_time_accel==0)
{
if (keys[MK_RUN] && (c1==CTG_SOIL || c2==CTG_SOIL))
{
if(mario.p.vx*sgn(mario.p.vx)<=4)
mario.p.vx+=vx;
}
else
{
if (mario.p.vx*vx<2)
mario.p.vx+=vx;
}
}
}
else
{
if (mario.p.vx)
{
if (mario_accel)
{
mario_time_accel++;
mario_time_accel%=4;
if (mario_time_accel==0)
{
mario.p.vx-=sgn(mario.p.vx);
mario_accel=-sgn(mario.p.vx);
}
}
else
{
mario.p.vx-=sgn(mario.p.vx);
mario_accel=-sgn(mario.p.vx);
}
}
else
mario_time_accel=0;
}
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<world_get_real_x0(mario.p.x))
mario.p.vx=0;
box_move(&mario.p); // <-> + gravity
if (mario.p.x>mario_x_max)
mario_x_max=mario.p.x;
if (mario.p.y<0)
mario_dead=1;
if (mario.p.vx==0)
mario_time_accel=0;
// coins
coin_t* c;
c=(coin_t*)world_get(mario.p.x, mario.p.y);
if (c->type==COIN && c->taken==0)
{score_add_coin();c->taken=1;}
c=world_get(mario.p.x+mario.p.w-1, mario.p.y);
if (c->type==COIN && c->taken==0)
{score_add_coin();c->taken=1;}
c=world_get(mario.p.x, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0)
{score_add_coin();c->taken=1;}
c=world_get(mario.p.x+mario.p.w-1, mario.p.y+mario.p.h-1);
if (c->type==COIN && c->taken==0)
{score_add_coin();c->taken=1;}
// box
if ((mario.p.x+mario.p.w/2)/8==(mario.p.x+mario.p.w/2-1)/8)
{
gift_t* c=(gift_t*)world_get((mario.p.x+mario.p.w/2),mario.p.y+mario.p.h);
if (c->type==GIFT)
{
if (c->time_hit_id==0 && c->content==1 && c->number!=0) // piece
{
c->number--;
c->time_hit_id=1;
score_add_coin();
}
}
}
if ((mario.p.x+mario.p.w/2)/8==(mario.p.x+mario.p.w/2-1)/8)
{
world_t* t=(gift_t*)world_get((mario.p.x+mario.p.w/2),mario.p.y+mario.p.h);
if (t->type==GIFT)
{
gift_t *c=(gift_t*)t;
if (c->time_hit_id==0 && c->content==1 && c->number!=0) // piece
{
c->number--;
c->time_hit_id=1;
score_add_coin();
}
}
if (t->type==BRICK)
{
brick_t *c=(brick_t*)t;
if ((c->content==0 || c->number) && mario.size==M_SMALL) // piece
{
if (c->number)
{
c->number--;
score_add_coin();
}
c->time_hit_id=1;
}
}
}
}