144 lines
3.0 KiB
C
144 lines
3.0 KiB
C
|
#include "bonus.h"
|
||
|
#include "box.h"
|
||
|
#include "mario.h"
|
||
|
#include "world.h"
|
||
|
#include "tile.h"
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define BONUS_FLEUR 4 // interne, généré depuis champi
|
||
|
#define BULLET 1
|
||
|
typedef struct
|
||
|
{
|
||
|
int type;
|
||
|
box_t b;
|
||
|
int p1;
|
||
|
} bonus_t;
|
||
|
|
||
|
static int sgn(int x)
|
||
|
{
|
||
|
if (x>0)
|
||
|
return 1;
|
||
|
else if (x<0)
|
||
|
return -1;
|
||
|
else
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
static bonus_t bonus ={BONUS_NONE, {0,0,8,8,0,0,1}, 0};
|
||
|
/* Les balles ont les memes propriétés que les boulets et sont donc gérées ici */
|
||
|
static bonus_t bullets[2] ={{0, {0,0,4,4,0,0,1}, 0},{0, {0,0,4,4,0,0,1}, 0}};
|
||
|
|
||
|
void lance_bullet()
|
||
|
{
|
||
|
for (int i=0; i<2; i++)
|
||
|
{
|
||
|
if (bullets[i].type==0)
|
||
|
{
|
||
|
bullets[i].type=BULLET;
|
||
|
bullets[i].b.x=mario.p.x;
|
||
|
bullets[i].b.y=mario.p.y+8;
|
||
|
bullets[i].b.vx=4*last_vx_sign-2;
|
||
|
bullets[i].b.vy=0;
|
||
|
bullets[i].p1=last_vx_sign;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
void bullet_display()
|
||
|
{
|
||
|
for (int i=0; i<2; i++)
|
||
|
{
|
||
|
if (bullets[i].type==BULLET)
|
||
|
{
|
||
|
box_jump(&bullets[i].b,4);
|
||
|
box_move(&bullets[i].b);
|
||
|
draw_tile(bullets[i].b.x-world_get_real_x0(), bullets[i].b.y-world_get_real_y0(), &bullet, 1+sgn(bullets[i].b.vy)/2, 0);
|
||
|
if (bullets[i].b.vx==0)
|
||
|
{
|
||
|
bullets[i].p1*=-1;
|
||
|
bullets[i].b.vx=4*bullets[i].p1;
|
||
|
}
|
||
|
if (bullets[i].b.y<0)
|
||
|
bullets[i].type=0;
|
||
|
if (bullets[i].b.x<=world_get_real_x0()-bullets[i].b.w || bullets[i].b.x>=world_get_real_x0()+127)
|
||
|
bullets[i].type=0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void bonus_set(int type, int x, int y)
|
||
|
{
|
||
|
bonus.type=type;
|
||
|
if (mario.size==M_BIG && type==BONUS_CHAMPI)
|
||
|
bonus.type=BONUS_FLEUR;
|
||
|
bonus.b.x=x;
|
||
|
bonus.b.y=y;
|
||
|
bonus.b.vx=0;
|
||
|
bonus.b.vy=0;
|
||
|
bonus.p1=0;
|
||
|
}
|
||
|
|
||
|
void bonus_move() //+collision
|
||
|
{
|
||
|
if (bonus.b.x<=world_get_real_x0()-bonus.b.w || bonus.b.x>=world_get_real_x0()+127)
|
||
|
bonus_set(BONUS_NONE,0,0);
|
||
|
if (bonus.type==BONUS_NONE)
|
||
|
return;
|
||
|
|
||
|
|
||
|
if (bonus.type==BONUS_STAR)
|
||
|
{
|
||
|
box_jump(&bonus.b,5);
|
||
|
}
|
||
|
box_move(&bonus.b);
|
||
|
if (bonus.type==BONUS_CHAMPI || bonus.type==BONUS_1UP || bonus.type==BONUS_STAR)
|
||
|
{
|
||
|
|
||
|
if (bonus.b.vx==0)
|
||
|
{
|
||
|
if (bonus.p1==0)
|
||
|
{
|
||
|
bonus.b.vx=1;
|
||
|
bonus.p1=1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
bonus.p1*=-1;
|
||
|
bonus.b.vx=bonus.p1;
|
||
|
}
|
||
|
}
|
||
|
if (bonus.b.y<0)
|
||
|
bonus_set(BONUS_NONE,0,0);
|
||
|
}
|
||
|
|
||
|
bool x_collide= (mario.p.x<=bonus.b.x && bonus.b.x<mario.p.x+mario.p.w) || (mario.p.x<=bonus.b.x+bonus.b.w-1 && bonus.b.x+bonus.b.w<mario.p.x+mario.p.w);
|
||
|
bool y_collide= (mario.p.y<=bonus.b.y && bonus.b.y<mario.p.y+mario.p.h) || (mario.p.y<=bonus.b.y+bonus.b.h-1 && bonus.b.y+bonus.b.h<mario.p.y+mario.p.h);
|
||
|
if (x_collide&&y_collide)
|
||
|
{
|
||
|
if (bonus.type==BONUS_CHAMPI)
|
||
|
{
|
||
|
bonus_set(BONUS_NONE,0,0);
|
||
|
score_add(1000);
|
||
|
mario_bigger();
|
||
|
}
|
||
|
if (bonus.type==BONUS_FLEUR)
|
||
|
{
|
||
|
bonus_set(BONUS_NONE,0,0);
|
||
|
mario_has_bullets=1;
|
||
|
score_add(1000);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void bonus_draw()
|
||
|
{
|
||
|
bonus_move();
|
||
|
if (bonus.type==BONUS_NONE)
|
||
|
return;
|
||
|
if (bonus.type==BONUS_CHAMPI)
|
||
|
draw_tile(bonus.b.x-world_get_real_x0(), bonus.b.y-world_get_real_y0(mario.p.y), &champi, 0,0);
|
||
|
if (bonus.type==BONUS_FLEUR)
|
||
|
draw_tile(bonus.b.x-world_get_real_x0(), bonus.b.y-world_get_real_y0(mario.p.y), &fleur, 0,0);
|
||
|
}
|