2019-12-03 19:53:30 +01:00
|
|
|
#include "ennemi.h"
|
|
|
|
#include "mario.h"
|
|
|
|
#include "box.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "world.h"
|
|
|
|
#include "score.h"
|
|
|
|
|
2019-12-04 19:27:27 +01:00
|
|
|
#include <stdbool.h>
|
2019-12-03 19:53:30 +01:00
|
|
|
|
2019-12-04 19:27:27 +01:00
|
|
|
void display_ennemi(ennemi_t * e)
|
2019-12-03 19:53:30 +01:00
|
|
|
{
|
|
|
|
if (e->life==DEAD)
|
|
|
|
return;
|
2019-12-05 19:24:16 +01:00
|
|
|
if (e->b.x<=world_get_real_x0(0)-e->b.w || e->b.x>=world_get_real_x0(0)+127)
|
2019-12-03 19:53:30 +01:00
|
|
|
return;
|
2019-12-04 19:27:27 +01:00
|
|
|
else
|
|
|
|
e->discovered=1;
|
|
|
|
|
2019-12-03 19:53:30 +01:00
|
|
|
if (e->type==GOOMBA)
|
|
|
|
{
|
|
|
|
extern image_t img_goomba;
|
|
|
|
tileset_t goomba={&img_goomba, 8, 8, 1};
|
|
|
|
if (e->life==1)
|
|
|
|
draw_tile(e->b.x-world_get_real_x0(0), e->b.y-world_get_real_y0(mario.p.y), &goomba, 1+(time_id/10)%2, 0);
|
|
|
|
if (e->life==0)
|
|
|
|
draw_tile(e->b.x-world_get_real_x0(0), e->b.y-world_get_real_y0(mario.p.y), &goomba, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
|
|
|
|
bool ennemi_check_collision(ennemi_t *e)
|
|
|
|
{
|
2019-12-05 19:24:16 +01:00
|
|
|
if (mario_immunity)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool x_collide= (mario.p.x<=e->b.x && e->b.x<mario.p.x+mario.p.w) || (mario.p.x<=e->b.x+e->b.w-1 && e->b.x+e->b.w<mario.p.x+mario.p.w);
|
|
|
|
bool y_collide= (mario.p.y<=e->b.y && e->b.y<mario.p.y+mario.p.h) || (mario.p.y<=e->b.y+e->b.h-1 && e->b.y+e->b.h<mario.p.y+mario.p.h);
|
|
|
|
return (x_collide&&y_collide);
|
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:24:16 +01:00
|
|
|
void hurt_mario()
|
|
|
|
{
|
|
|
|
if (mario.size=M_SMALL)
|
|
|
|
mario_dead=1;
|
|
|
|
else
|
|
|
|
mario_smaller();
|
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
|
2019-12-03 19:53:30 +01:00
|
|
|
void move_ennemi(ennemi_t *e)
|
|
|
|
{
|
|
|
|
|
2019-12-05 19:24:16 +01:00
|
|
|
if (e->b.x-e->b.w>world_get_real_x0() && e->b.x<world_get_real_x0()+127)
|
2019-12-04 19:27:27 +01:00
|
|
|
e->discovered=1;
|
2019-12-05 19:24:16 +01:00
|
|
|
if (e->b.x+e->b.w<=world_get_real_x0())
|
2019-12-04 19:27:27 +01:00
|
|
|
e->type=NONE;
|
|
|
|
if (e->discovered==0)
|
2019-12-03 19:53:30 +01:00
|
|
|
return;
|
|
|
|
if (e->life==DEAD)
|
2019-12-04 19:27:27 +01:00
|
|
|
e->type=NONE;
|
2019-12-03 19:53:30 +01:00
|
|
|
if (e->type==NONE)
|
|
|
|
return;
|
2019-12-04 19:27:27 +01:00
|
|
|
|
2019-12-05 19:24:16 +01:00
|
|
|
for (int i=0; i<ennemis_global_size; i++)
|
|
|
|
{
|
|
|
|
ennemi_t* t=&ennemis_global[i];
|
|
|
|
if (t!=e && t->discovered && t->type!=NONE)
|
|
|
|
{
|
|
|
|
bool x_collide= (t->b.x<=e->b.x+e->b.vx && e->b.x+e->b.vx<t->b.x+t->b.w) || (t->b.x<=e->b.x+e->b.w-1+e->b.vx && e->b.x+e->b.w+e->b.vx<t->b.x+t->b.w);
|
|
|
|
bool y_collide= (t->b.y<=e->b.y+e->b.vy && e->b.y+e->b.vy<t->b.y+t->b.h) || (t->b.y<=e->b.y+e->b.h-1+e->b.vy && e->b.y+e->b.h+e->b.vy<t->b.y+t->b.h);
|
|
|
|
if (x_collide&&y_collide)
|
|
|
|
{
|
|
|
|
e->b.vx=0;
|
|
|
|
e->b.vy=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:53:30 +01:00
|
|
|
if (e->type==GOOMBA)
|
|
|
|
{
|
2019-12-04 19:27:27 +01:00
|
|
|
if (e->life==1)
|
|
|
|
{
|
|
|
|
int old_vx=e->b.vx;
|
|
|
|
if (time_id%2)
|
|
|
|
e->b.vx=0;
|
2019-12-03 19:53:30 +01:00
|
|
|
|
2019-12-04 19:27:27 +01:00
|
|
|
box_move(&e->b);
|
2019-12-03 19:53:30 +01:00
|
|
|
|
2019-12-04 19:27:27 +01:00
|
|
|
if (time_id%2)
|
|
|
|
e->b.vx=old_vx;
|
|
|
|
|
|
|
|
if (e->b.vx==0)
|
2019-12-03 19:53:30 +01:00
|
|
|
{
|
2019-12-04 19:27:27 +01:00
|
|
|
if (e->p1==0)
|
|
|
|
{
|
|
|
|
e->b.vx=-1;
|
|
|
|
e->p1=-1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
e->p1*=-1;
|
|
|
|
e->b.vx=e->p1;
|
|
|
|
}
|
2019-12-03 19:53:30 +01:00
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
if (e->b.y<0)
|
|
|
|
e->type=NONE;
|
|
|
|
if (ennemi_check_collision(e))
|
2019-12-03 19:53:30 +01:00
|
|
|
{
|
2019-12-04 19:27:27 +01:00
|
|
|
if (mario.p.vy<0)
|
|
|
|
{
|
|
|
|
e->life=0;
|
|
|
|
e->p1=time_id;
|
|
|
|
score_add(100);
|
|
|
|
mario.p.vy=4;
|
|
|
|
}
|
|
|
|
else
|
2019-12-05 19:24:16 +01:00
|
|
|
hurt_mario();
|
2019-12-03 19:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (e->p1>=time_id+10 || e->p1<time_id)
|
|
|
|
e->life=DEAD;
|
|
|
|
}
|
2019-12-03 19:53:30 +01:00
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
display_ennemi(e);
|
2019-12-03 19:53:30 +01:00
|
|
|
}
|
2019-12-04 19:27:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-03 19:53:30 +01:00
|
|
|
|
|
|
|
ennemi_t * ennemis_global=0;
|
|
|
|
int ennemis_global_size=0;
|
|
|
|
void display_ennemi_table()
|
|
|
|
{
|
|
|
|
for (int i=0; i<ennemis_global_size; i++)
|
|
|
|
{
|
|
|
|
move_ennemi(&ennemis_global[i]);
|
|
|
|
}
|
|
|
|
}
|