supercasiobros/src/bonus.c

180 lines
3.9 KiB
C
Raw Normal View History

#include "bonus.h"
#include "box.h"
#include "mario.h"
#include "world.h"
#include "tile.h"
2019-12-07 14:32:38 +01:00
#include "score.h"
#include "ennemi.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;
}
2019-12-08 16:34:32 +01:00
static bonus_t bonus ={BONUS_NONE, {0,0,8,8,0,0,0,1}, 0};
/* Les balles ont les memes propriétés que les boulets et sont donc gérées ici */
2019-12-08 16:34:32 +01:00
static bonus_t bullets[2] ={{0, {0,0,4,4,0,0,0,1}, 0},{0, {0,0,4,4,0,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;
2019-12-07 14:32:38 +01:00
if (last_vx_sign==0)
bullets[i].b.vx=-3;
else
bullets[i].b.vx=3;
bullets[i].b.vy=0;
bullets[i].p1=last_vx_sign;
return;
}
}
}
2019-12-08 16:34:32 +01:00
void bullet_display()
2019-12-08 16:34:32 +01:00
{
for (int i=0; i<2; i++)
{
if (bullets[i].type==BULLET)
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);
}
}
void bullet_move()
{
for (int i=0; i<2; i++)
{
if (bullets[i].type==BULLET)
{
box_jump(&bullets[i].b,4);
2019-12-08 16:34:32 +01:00
box_move(&bullets[i].b);
if (bullets[i].b.vx==0)
2019-12-07 14:32:38 +01:00
bullets[i].type=0;
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;
2019-12-07 14:32:38 +01:00
for (int a=0; a<ennemis_global_size; a++)
{
ennemi_t* t=&ennemis_global[a];
if (t->discovered && t->type!=NONE)
{
bool x_collide= (bullets[i].b.x<=t->b.x && t->b.x<bullets[i].b.x+bullets[i].b.w) || (bullets[i].b.x<=t->b.x+t->b.w-1 && t->b.x+t->b.w<bullets[i].b.x+bullets[i].b.w);
bool y_collide= (bullets[i].b.y<=t->b.y && t->b.y<bullets[i].b.y+bullets[i].b.h) || (bullets[i].b.y<=t->b.y+t->b.h-1 && t->b.y+t->b.h<bullets[i].b.y+bullets[i].b.h);
if (x_collide&& y_collide)
{
t->type=NONE;
bullets[i].type=0;
score_add(100);
}
}
}
}
2019-12-07 14:32:38 +01:00
}
2019-12-07 14:32:38 +01:00
}
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);
}
2019-12-08 16:34:32 +01:00
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);
}
2019-12-07 14:32:38 +01:00
if (bonus.type==BONUS_1UP)
{
bonus_set(BONUS_NONE,0,0);
score_add(1000);
lifes++;
}
}
}
void bonus_draw()
{
2019-12-08 16:34:32 +01:00
//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);
2019-12-07 14:32:38 +01:00
if (bonus.type==BONUS_1UP)
draw_tile(bonus.b.x-world_get_real_x0(), bonus.b.y-world_get_real_y0(mario.p.y), &life_1up, 0,0);
}