Le nouveau Super Mario Bros pour la gamme des fx9860g https://www.planet-casio.com/Fr/forums/topic15994-1-[addin]-SuperCasioBros-v0.1.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
supercasiobros/src/ennemi.c

381 lines
6.9 KiB

#include "ennemi.h"
#include "mario.h"
#include "box.h"
#include "tile.h"
#include "world.h"
#include "score.h"
#include <base.h>
#include <camera.h>
#include <level.h>
#include <gint/std/string.h>
#include <constants.h>
#include <stdbool.h>
const int ennemi_widths [NOMBRE_ENNEMIS] = {0, 8, 8, 8, 8 , 8, 8};
//const int ennemi_heights[NOMBRE_ENNEMIS] = {0, 8, 12, 9, 12, 9, 8};
const int ennemi_heights[NOMBRE_ENNEMIS] = {0, 8, 8, 8, 8, 8, 8};
void ennemiDisplay(ennemi_t const * e)
{
if (e->type==NONE) return;
if (e->b.x<=cameraX(0)-e->b.w || e->b.x>=cameraX(0)+127) return; // do not draw if out of the screen
else e->discovered=1; // for security, tag as discover all drawed ennemies
{// draw
int tx=0, ty=0, dsx=0, dsy=0;
tileset_t t={0,ennemi_widths[e->type], ennemi_heights[e->type],1};
switch (e->type)
{
case GOOMBA_ID:
{
extern image_t img_goomba;
t.sheet=&img_goomba;
tx = e->life*(1+(time_id/10)%2);
}
break;
case KOOPA_V_ID:
{
extern image_t img_koopa_verte;
t.sheet=&img_koopa_verte;
t.height=ennemi_heights[KOOPA_V_ID]+ennemi_heights[KOOPA_V_ID]/2;
tx = (1+e->p1)+(time_id/8)%2;
}
break;
case KOOPA_R_ID:
{
extern image_t img_koopa_rouge;
t.sheet=&img_koopa_rouge;
t.height=ennemi_heights[KOOPA_R_ID]+ennemi_heights[KOOPA_R_ID]/2;
tx = (1+e->p1)+(time_id/8)%2;
}
break;
case CARAPACE_VERTE:
{
extern image_t img_carapace_verte;
t.sheet=&img_carapace_verte;
t.height=ennemi_heights[CARAPACE_VERTE]+ennemi_heights[CARAPACE_VERTE]/8;
}
break;
case CARAPACE_ROUGE:
{
extern image_t img_carapace_rouge;
t.sheet=&img_carapace_rouge;
t.height=ennemi_heights[CARAPACE_ROUGE]+ennemi_heights[CARAPACE_ROUGE]/8;
}
break;
case PLANTE_ID:
{
extern image_t img_plante;
t.sheet=&img_plante;
// TODO tx ty tsx,tsy
}
}
tileDraw(e->b.x-cameraX(0)+dsx, e->b.y-cameraY(mario.p.y)+dsy, &t, tx, ty);
}
}
bool ennemi_check_collision(ennemi_t *e) { return (boxContact(&e->b, &mario.p)); }
void hurtMario()
{
if (mario.size==M_SMALL && mario_immunity==0)
{mario_dead=1;finish_level=0;}
else marioSmaller();
}
void ennemiMove(ennemi_t *e)
{
if (e->life==0 && e->type!=NONE)
{
if (e->p1==0)
{
e->b.vx=-2*sgn(e->b.vx);
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; // died 1.5 sec before
}
if (e->b.x<cameraX()+128+30 && e->b.x>cameraX()-30)
e->discovered=1;
//if (e->b.x+e->b.w<=worldGetCell_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_hitMario=ennemi_check_collision(e);
if (e_hitMario&&mario_starMode)
{
e->life=DEAD;
e->p1=0;
scoreAdd(KILL_ENNEMI);
return;
}
for (int i=0; i<ennemis_global_size; i++)
{
ennemi_t* t=&ennemis_global[i];
if (t->type!=NONE)
{
e->b.x+=sgn(e->b.vx)*(abs(e->b.vx)+(time_id%2))/2;
const int contact=boxContact(&e->b, &t->b);
e->b.x-=sgn(e->b.vx)*(abs(e->b.vx)+(time_id%2))/2;
if (contact && 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;
scoreAdd(KILL_ENNEMI);
}
}
}
}
}
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*)worldGetCell(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;
scoreAdd(KILL_ENNEMI);
break;
}
}
}
const bool mario_fatal_hit = (mario.p.last_vy<=-2 || mario.p.vy<=-2);
if (e->type==GOOMBA_ID)
{
boxMove(&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_hitMario)
{
if (mario_fatal_hit)
{
e->life=DEAD;
e->p1=0;
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[GOOMBA_ID]+1;
}
else
hurtMario();
}
return;
}
if (e->type==KOOPA_V_ID)
{
boxMove(&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_hitMario)
{
if (mario_fatal_hit)
{
e->type=CARAPACE_VERTE;
e->b.h=ennemi_heights[CARAPACE_VERTE];
e->p1=2;
e->b.vx=0;
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
}
else
hurtMario();
}
return;
}
if (e->type==KOOPA_R_ID)
{
boxMove(&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 (worldGetCellCategory(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_hitMario)
{
if (mario_fatal_hit)
{
e->type=CARAPACE_ROUGE;
e->b.h=ennemi_heights[CARAPACE_ROUGE];
e->p1=2;
e->b.vx=0;
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_ROUGE]+1;
}
else
hurtMario();
}
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)
{ // transformation carapace->koopa
e->type--;
e->b.h=ennemi_heights[e->type];
e->p1=e->b.vx=0;
}
}
if (e_hitMario)
{
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
{ // mario bounce
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=e->b.vx=0;
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
}
else hurtMario();
}
}
boxMove(&e->b);
return;
}
}
// Global variables for ennemies
ennemi_t * ennemis_global=0;
int ennemis_global_size=0;
void ennemiesDisplay()
{
for (int i=0; i<ennemis_global_size; i++) ennemiDisplay(&ennemis_global[i]);
}
void ennemiesInit(ennemi_t * table, int s)
{
if (ennemis_global)
freeProf(ennemis_global);
ennemis_global_size=ennemis_global=0;
if (!s) return;
int size=sizeof(ennemi_t)*s;
if ((ennemis_global=mallocProf(size))==0) mallocError();
ennemis_global_size=s;
memcpy(ennemis_global, table, size);
}