supercasiobros/src/ennemi.c

388 lines
7.4 KiB
C
Raw Normal View History

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"
#include <base.h>
2020-01-29 14:34:47 +01:00
#include <camera.h>
#include <level.h>
#include <gint/std/string.h>
2019-12-03 19:53:30 +01:00
2019-12-04 19:27:27 +01:00
#include <stdbool.h>
2019-12-03 19:53:30 +01:00
const int ennemi_widths [NOMBRE_ENNEMIS] = {0, 8, 8, 8, 8 , 8};
const int ennemi_heights[NOMBRE_ENNEMIS] = {0, 8, 12, 9, 12, 9};
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==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;
2020-01-29 14:34:47 +01:00
if (e->b.x<=camera_x(0)-e->b.w || e->b.x>=camera_x(0)+127)
return;
else
e->discovered=1;
2019-12-16 19:23:36 +01:00
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)
2020-01-29 14:34:47 +01:00
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)
2020-01-29 14:34:47 +01:00
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)
2020-01-29 14:34:47 +01:00
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)
2020-01-29 14:34:47 +01:00
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};
2020-01-29 14:34:47 +01:00
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};
2020-01-29 14:34:47 +01:00
draw_tile(e->b.x-camera_x(0), e->b.y-camera_y(mario.p.y), &carapace_rouge, 0, 0);
}
2019-12-04 19:27:27 +01:00
2019-12-03 19:53:30 +01:00
}
2019-12-04 19:27:27 +01:00
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);
2019-12-04 19:27:27 +01:00
}
void hurt_mario()
{ if (mario.size==M_SMALL && mario_immunity==0)
{mario_dead=1;finish_level=0;}
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)
{
2020-01-29 14:34:47 +01:00
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)
{
2019-12-07 14:32:38 +01:00
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);
}
2019-12-08 16:34:32 +01:00
}
}
}
}
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);
2019-12-04 19:27:27 +01:00
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();
}
2020-01-06 20:56:10 +01:00
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();
}
2020-01-06 20:56:10 +01:00
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;
}
2019-12-07 19:26:55 +01:00
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;
}
}
2019-12-07 19:26:55 +01:00
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();
}
2020-01-12 12:45:42 +01:00
}
2020-01-12 12:45:42 +01:00
box_move(&e->b);
2020-01-06 20:56:10 +01:00
return;
}
2019-12-07 19:26:55 +01:00
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++)
{
display_ennemi(&ennemis_global[i]);
}
2019-12-16 19:23:36 +01:00
}
2020-02-01 14:36:34 +01:00
void init_ennemi(ennemi_t * table, int s)
{
2020-02-03 11:12:54 +01:00
ennemis_global_size=0;
2020-02-01 14:36:34 +01:00
if (ennemis_global)
{
2020-02-03 11:12:54 +01:00
free_prof(ennemis_global);
2020-02-01 14:36:34 +01:00
ennemis_global=0;
}
2020-02-03 11:12:54 +01:00
if (0==s)
2020-02-01 14:36:34 +01:00
return;
2020-02-03 11:12:54 +01:00
int size=sizeof(ennemi_t)*s;
ennemis_global=malloc_prof(size);
2020-02-01 14:36:34 +01:00
if (ennemis_global==0)
malloc_error();
2020-02-03 11:12:54 +01:00
ennemis_global_size=s;
2020-02-01 14:36:34 +01:00
memcpy(ennemis_global, table, size);
}