394 lines
7.4 KiB
C
394 lines
7.4 KiB
C
#include "ennemi.h"
|
|
#include "mario.h"
|
|
#include "box.h"
|
|
#include "tile.h"
|
|
#include "world.h"
|
|
#include "score.h"
|
|
#include <camera.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
static int sgn(int x)
|
|
{
|
|
if (x==0)
|
|
return 0;
|
|
else if (x>0)
|
|
return 1;
|
|
return -1;
|
|
}
|
|
|
|
const int ennemi_widths [NOMBRE_ENNEMIS] = {0, 8, 8, 8, 8 , 8};
|
|
const int ennemi_heights[NOMBRE_ENNEMIS] = {0, 8, 12, 9, 12, 9};
|
|
|
|
void display_ennemi(ennemi_t * e)
|
|
{
|
|
if (e->life==0)
|
|
{
|
|
if (e->p1==0)
|
|
{
|
|
e->b.vx*=-1;
|
|
e->b.vy=5;
|
|
}
|
|
e->p1++;
|
|
e->b.vy--;
|
|
e->b.y+=e->b.vy;
|
|
e->b.x+=e->b.vx;
|
|
if (e->p1==30)
|
|
e->type=NONE;
|
|
}
|
|
if (e->type==NONE)
|
|
return;
|
|
if (e->b.x<=camera_x(0)-e->b.w || e->b.x>=camera_x(0)+127)
|
|
return;
|
|
else
|
|
e->discovered=1;
|
|
|
|
if (e->type==GOOMBA_ID)
|
|
{
|
|
extern image_t img_goomba;
|
|
tileset_t goomba={&img_goomba, ennemi_widths[GOOMBA_ID], ennemi_heights[GOOMBA_ID], 1};
|
|
if (e->life==1)
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &goomba, 1+(time_id/10)%2, 0);
|
|
if (e->life==0)
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &goomba, 0, 0);
|
|
}
|
|
if (e->type==KOOPA_V_ID)
|
|
{
|
|
extern image_t img_koopa_verte;
|
|
tileset_t koopa_verte={&img_koopa_verte, ennemi_widths[KOOPA_V_ID], ennemi_heights[KOOPA_V_ID], 1};
|
|
if (e->life==1)
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &koopa_verte, (1+e->p1)+(time_id/8)%2, 0);
|
|
}
|
|
if (e->type==KOOPA_R_ID)
|
|
{
|
|
extern image_t img_koopa_rouge;
|
|
tileset_t koopa_rouge={&img_koopa_rouge, ennemi_widths[KOOPA_R_ID], ennemi_heights[KOOPA_R_ID], 1};
|
|
if (e->life==1)
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &koopa_rouge, (1+e->p1)+(time_id/8)%2, 0);
|
|
}
|
|
if (e->type==CARAPACE_VERTE)
|
|
{
|
|
extern image_t img_carapace_verte;
|
|
tileset_t carapace_verte={&img_carapace_verte, ennemi_widths[CARAPACE_VERTE], ennemi_heights[CARAPACE_VERTE], 1};
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &carapace_verte, 0, 0);
|
|
}
|
|
if (e->type==CARAPACE_ROUGE)
|
|
{
|
|
extern image_t img_carapace_rouge;
|
|
tileset_t carapace_rouge={&img_carapace_rouge, ennemi_widths[CARAPACE_ROUGE], ennemi_heights[CARAPACE_ROUGE], 1};
|
|
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &carapace_rouge, 0, 0);
|
|
}
|
|
|
|
}
|
|
|
|
bool ennemi_check_collision(ennemi_t *e)
|
|
{ bool x_collide= (mario.p.x<=e->b.x && e->b.x<=mario.p.x+mario.p.w-1) || (mario.p.x<=e->b.x+e->b.w-1 && e->b.x+e->b.w-1<=mario.p.x+mario.p.w-1);
|
|
bool y_collide= (mario.p.y<=e->b.y && e->b.y<=mario.p.y+mario.p.h-1) || (mario.p.y<=e->b.y+e->b.h-1 && e->b.y+e->b.h-1<=mario.p.y+mario.p.h-1);
|
|
return (x_collide&&y_collide);
|
|
}
|
|
|
|
void hurt_mario()
|
|
{ if (mario.size==M_SMALL && mario_immunity==0)
|
|
{mario_dead=1;finish_level=0;}
|
|
else
|
|
mario_smaller();
|
|
}
|
|
|
|
void move_ennemi(ennemi_t *e)
|
|
{
|
|
|
|
if (e->b.x<camera_x()+128+30 && e->b.x>camera_x()-30)
|
|
e->discovered=1;
|
|
//if (e->b.x+e->b.w<=world_get_real_x0())
|
|
// e->type=NONE;
|
|
if (e->discovered==0)
|
|
return;
|
|
if (e->life==DEAD)
|
|
return;
|
|
// e->type=NONE;
|
|
if (e->type==NONE)
|
|
return;
|
|
|
|
bool e_hit_mario=ennemi_check_collision(e);
|
|
if (e_hit_mario&&mario_star_mode)
|
|
{
|
|
e->life=DEAD;
|
|
e->p1=0;
|
|
score_add(200);
|
|
return;
|
|
}
|
|
|
|
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 && t->life!=DEAD)
|
|
{
|
|
if (e->type!=CARAPACE_VERTE && e->type!=CARAPACE_ROUGE)
|
|
{
|
|
e->b.vx=0;
|
|
e->b.vy=0;
|
|
}
|
|
else
|
|
{
|
|
if (t->type==CARAPACE_VERTE || t->type==CARAPACE_ROUGE)
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=6*e->p1;
|
|
}
|
|
else
|
|
{
|
|
t->life=DEAD;
|
|
t->p1=0;
|
|
score_add(200);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
int previous_case=-5;
|
|
for (int j=0; j<e->b.h; j++)
|
|
{
|
|
if ((e->b.x+j)/8!=previous_case)
|
|
{
|
|
previous_case=(e->b.x+j)/8;
|
|
gift_t * c=(gift_t*)world_get(e->b.x+j ,e->b.y-1);
|
|
if ((c->type==GIFT || c->type==BRICK) && (c->time_hit_id || c->state) && e->life!=DEAD)
|
|
{
|
|
e->life=DEAD;
|
|
e->p1=0;
|
|
score_add(100);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
const bool mario_fatal_hit = (mario.p.last_vy<=-2 || mario.p.vy<=-2);
|
|
|
|
if (e->type==GOOMBA_ID)
|
|
{
|
|
box_move(&e->b);
|
|
|
|
if (e->b.vx==0)
|
|
{
|
|
if (e->p1==0)
|
|
{
|
|
e->b.vx=-1;
|
|
e->p1=-1;
|
|
}
|
|
else
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=e->p1;
|
|
}
|
|
}
|
|
if (e->b.y<0)
|
|
e->type=NONE;
|
|
if (e_hit_mario)
|
|
{
|
|
if (mario_fatal_hit)
|
|
{
|
|
e->life=DEAD;
|
|
e->p1=0;
|
|
score_add(200);
|
|
mario.p.vy=4;
|
|
mario.p.y=e->b.y+ennemi_heights[GOOMBA_ID]+1;
|
|
}
|
|
else
|
|
hurt_mario();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (e->type==KOOPA_V_ID)
|
|
{
|
|
box_move(&e->b);
|
|
if (e->b.vx==0)
|
|
{
|
|
if (e->p1==0)
|
|
{
|
|
e->b.vx=-1;
|
|
e->p1=-1;
|
|
}
|
|
else
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=e->p1;
|
|
}
|
|
}
|
|
if (e->b.y<0)
|
|
e->type=NONE;
|
|
if (e_hit_mario)
|
|
{
|
|
if (mario_fatal_hit)
|
|
{
|
|
e->type=CARAPACE_VERTE;
|
|
e->b.h=ennemi_heights[CARAPACE_VERTE];
|
|
e->p1=2;
|
|
e->b.vx=0;
|
|
score_add(200);
|
|
mario.p.vy=4;
|
|
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
|
|
}
|
|
else
|
|
hurt_mario();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (e->type==KOOPA_R_ID)
|
|
{
|
|
box_move(&e->b);
|
|
if (e->b.vx==0)
|
|
{
|
|
if (e->p1==0)
|
|
{
|
|
e->b.vx=-1;
|
|
e->p1=-1;
|
|
}
|
|
else
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=e->p1;
|
|
}
|
|
}
|
|
else
|
|
{ // demi tour automatique
|
|
int s=-1;
|
|
if (e->b.vx>0)
|
|
s=e->b.w;
|
|
|
|
if (world_get_ctg(e->b.x+s, e->b.y-1)==CTG_EMPTY && sgn(e->b.vx)==sgn(s))
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=e->p1;
|
|
}
|
|
}
|
|
|
|
|
|
if (e->b.y<0)
|
|
e->type=NONE;
|
|
if (e_hit_mario)
|
|
{
|
|
if (mario_fatal_hit)
|
|
{
|
|
e->type=CARAPACE_ROUGE;
|
|
e->b.h=ennemi_heights[CARAPACE_ROUGE];
|
|
e->p1=2;
|
|
e->b.vx=0;
|
|
score_add(200);
|
|
mario.p.vy=4;
|
|
mario.p.y=e->b.y+ennemi_heights[CARAPACE_ROUGE]+1;
|
|
}
|
|
else
|
|
hurt_mario();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (e->type==CARAPACE_VERTE || e->type==CARAPACE_ROUGE)
|
|
{
|
|
if (e->b.vx==0)
|
|
{
|
|
if (e->p1<2)
|
|
{
|
|
e->p1*=-1;
|
|
e->b.vx=6*e->p1;
|
|
}
|
|
}
|
|
if (e->b.y<0)
|
|
e->type=NONE;
|
|
if (e->p1>=2)
|
|
{
|
|
e->p1++;
|
|
if (e->p1==80)
|
|
{
|
|
if (e->type==CARAPACE_VERTE)
|
|
e->type=KOOPA_V_ID;
|
|
else
|
|
e->type=KOOPA_R_ID;
|
|
e->b.h=ennemi_heights[e->type];
|
|
e->p1=0;
|
|
e->b.vx=0;
|
|
}
|
|
}
|
|
|
|
if (e_hit_mario)
|
|
{
|
|
if (e->p1==0 || e->p1>=2)
|
|
{
|
|
if (mario.p.x>=e->b.x)
|
|
e->p1=-1;
|
|
if (mario.p.x<=e->b.x)
|
|
e->p1=1;
|
|
e->b.vx=6*e->p1;
|
|
if (mario_fatal_hit)
|
|
{
|
|
mario.p.vy=4;
|
|
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE];
|
|
}
|
|
else
|
|
{
|
|
if (mario.p.x>=e->b.x)
|
|
mario.p.x=e->b.x+e->b.w;
|
|
if (mario.p.x<=e->b.x)
|
|
mario.p.x=e->b.x-mario.p.w;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (mario_fatal_hit)
|
|
{
|
|
e->p1=0;
|
|
e->b.vx=0;
|
|
mario.p.vy=4;
|
|
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
|
|
}
|
|
else
|
|
hurt_mario();
|
|
}
|
|
|
|
}
|
|
box_move(&e->b);
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ennemi_t * ennemis_global=0;
|
|
int ennemis_global_size=0;
|
|
void display_ennemi_table()
|
|
{
|
|
for (int i=0; i<ennemis_global_size; i++)
|
|
{
|
|
display_ennemi(&ennemis_global[i]);
|
|
}
|
|
}
|
|
|
|
|
|
void init_ennemi(ennemi_t * table, int s)
|
|
{
|
|
ennemis_global_size=s;
|
|
if (ennemis_global)
|
|
{
|
|
free(ennemis_global);
|
|
ennemis_global=0;
|
|
}
|
|
if (0==ennemis_global_size)
|
|
return;
|
|
int size=sizeof(ennemi_t)*ennemis_global_size;
|
|
ennemis_global=malloc(size);
|
|
if (ennemis_global==0)
|
|
malloc_error();
|
|
|
|
memcpy(ennemis_global, table, size);
|
|
} |