293 lines
6.0 KiB
C
Executable File
293 lines
6.0 KiB
C
Executable File
#include "mario.h"
|
|
#include "tile.h"
|
|
#include "world.h"
|
|
#include <gint/display.h>
|
|
#include <keyboard.h>
|
|
#include "box.h"
|
|
#include "score.h"
|
|
#include "bonus.h"
|
|
#include <bullets.h>
|
|
#include <camera.h>
|
|
|
|
static int sgn(int x)
|
|
{
|
|
if (x>0)
|
|
return 1;
|
|
else if (x<0)
|
|
return -1;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
int id_frame=0;
|
|
|
|
pnj mario=
|
|
{
|
|
{26,17,8,8,0,0,0,1},
|
|
M_SMALL,
|
|
M_RIGHT1, M_WALK,
|
|
0
|
|
};
|
|
int mario_x_max=0;
|
|
int mario_has_bullets=0;
|
|
static int mario_time_accel=0;
|
|
int mario_dead=0;
|
|
//static int mario_coins=0;
|
|
int last_vx_sign=1;
|
|
//static int last_bonus=0;
|
|
static int mario_accel=0;
|
|
|
|
int mario_immunity=0;
|
|
int mario_star_mode=0;
|
|
|
|
void mario_bigger()
|
|
{
|
|
mario.p.h=16;
|
|
mario.size=M_BIG;
|
|
mario_has_bullets=0;
|
|
}
|
|
|
|
void mario_smaller()
|
|
{
|
|
mario.p.h=8;
|
|
mario.size=M_SMALL;
|
|
mario_has_bullets=0;
|
|
if (mario_immunity==0)
|
|
mario_immunity=1;
|
|
//mario.p.vy=5;
|
|
}
|
|
|
|
void mario_draw()
|
|
{
|
|
if ( (mario_immunity==0 || (mario_immunity/7)%2==0) && (mario_star_mode/2)%2==0)
|
|
{
|
|
if (mario.size==M_SMALL)
|
|
{
|
|
if (mario.p.vx*sgn(mario.p.vx)>=3)
|
|
draw_tile(mario.p.x-camera_x(mario.p.x), mario.p.y-camera_y(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-camera_x(mario.p.x), mario.p.y-camera_y(mario.p.y), &mario_small, 2*last_vx_sign+(id_frame/8)%2, 0);
|
|
else
|
|
draw_tile(mario.p.x-camera_x(mario.p.x), mario.p.y-camera_y(mario.p.y), &mario_small, 2*last_vx_sign, 0);
|
|
}
|
|
else
|
|
{
|
|
if (mario.p.vx*sgn(mario.p.vx)>=3)
|
|
draw_tile(mario.p.x-camera_x(mario.p.x), mario.p.y-camera_y(mario.p.y), &mario_big, 4*last_vx_sign+(id_frame/4)%2, mario_has_bullets);
|
|
else if (mario.p.vx*sgn(mario.p.vx)>=1)
|
|
draw_tile(mario.p.x-camera_x(mario.p.x), mario.p.y-camera_y(mario.p.y), &mario_big, 4*last_vx_sign+(id_frame/8)%2, mario_has_bullets);
|
|
else
|
|
draw_tile(mario.p.x-camera_x(mario.p.x), mario.p.y-camera_y(mario.p.y), &mario_big, 3*last_vx_sign, mario_has_bullets);
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
if (mario_star_mode)
|
|
{
|
|
mario_star_mode++;
|
|
if (mario_star_mode==200)
|
|
mario_star_mode=0;
|
|
}
|
|
if (mario_immunity)
|
|
{
|
|
mario_immunity++;
|
|
if (mario_immunity==60)
|
|
mario_immunity=0;
|
|
}
|
|
|
|
id_frame++;
|
|
if (mario_has_bullets==1 && mkb_getstate(MK_RUN)==2)
|
|
{
|
|
bullet_throw();
|
|
}
|
|
|
|
if (mkb_getstate(MK_JUMP)==2) //|| keys[MK_JUMP2]==2)
|
|
{
|
|
if (mario.p.vx*sgn(mario.p.vx)>=6)
|
|
box_jump(&mario.p, 9);
|
|
else
|
|
box_jump(&mario.p, 8);
|
|
}
|
|
else if (mario.p.vy>=2 && mkb_getstate(MK_JUMP)==0)
|
|
mario.p.vy--;
|
|
|
|
int vx=sgn(mkb_getstate(MK_RIGHT) - mkb_getstate(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%=2;
|
|
if (mario_time_accel==0)
|
|
{
|
|
if (mkb_getstate(MK_RUN) && (c1==CTG_SOIL || c2==CTG_SOIL))
|
|
{
|
|
if(mario.p.vx*sgn(mario.p.vx)<=8)
|
|
mario.p.vx+=vx;
|
|
}
|
|
else
|
|
{
|
|
if (mario.p.vx*vx<4)
|
|
mario.p.vx+=vx;
|
|
|
|
}
|
|
if (mkb_getstate(MK_RUN)==0)
|
|
{
|
|
if (mario.p.vx*vx>4)
|
|
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<camera_x(mario.p.x))
|
|
{
|
|
mario.p.vx=0;
|
|
mario.p.x=camera_x(mario.p.x);
|
|
}
|
|
//teleporteurs_check();
|
|
box_move(&mario.p); // <-> + gravity
|
|
|
|
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=(coin_t*)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=(coin_t*)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=(coin_t*)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;}
|
|
|
|
end_level_t *e1=(end_level_t*)world_get(mario.p.x, mario.p.y), *e2=(end_level_t*)world_get(mario.p.x+mario.p.w-1, mario.p.y);
|
|
if (e1->type==END_LEVEL)
|
|
{
|
|
finish_level=1;
|
|
score_add(400*e1->bonus);
|
|
}
|
|
if (e2->type==END_LEVEL)
|
|
{
|
|
finish_level=1;
|
|
score_add(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*)world_get((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 (c->content==0 || c->number)
|
|
{
|
|
c->time_hit_id=1;
|
|
mario.p.last_vy=0;
|
|
}
|
|
|
|
if (c->content==1 && c->number)
|
|
{
|
|
c->number--;
|
|
score_add_coin();
|
|
score_add(200);
|
|
}
|
|
else if (c->content==2 && c->number)
|
|
{
|
|
c->number--;
|
|
bonus_set(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--;
|
|
bonus_set(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--;
|
|
bonus_set(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;
|
|
}
|
|
}
|
|
|
|
//}
|
|
|
|
}
|