clean code & prepare to add piranha plants

dev
Milang 3 years ago
parent 76fe4d2e3f
commit 6a744b0547
  1. BIN
      SuperCbr.g1a
  2. 21
      include/ennemi.h
  3. 205
      src/ennemi.c
  4. 4
      src/level.c

Binary file not shown.

@ -1,25 +1,30 @@
#ifndef ENNEMI_H
#define ENNEMI_H
#include "box.h"
#define NOMBRE_ENNEMIS 6
#include <box.h>
#define ALIVE 1
#define DEAD 0
#define NOMBRE_ENNEMIS 7
extern const int ennemi_widths[NOMBRE_ENNEMIS];
extern const int ennemi_heights[NOMBRE_ENNEMIS];
#define NONE 0
#define GOOMBA_ID 1
#define KOOPA_V_ID 2
#define CARAPACE_VERTE 3
#define KOOPA_R_ID 4
#define CARAPACE_ROUGE 5
#define PLANTE_ID 6
#define GOOMBA(x,y,dir) {GOOMBA_ID,{x,y,ennemi_widths[GOOMBA_ID],ennemi_heights[GOOMBA_ID],dir,0,0,1},1,0,dir}
#define KOOPA_V(x,y,dir) {KOOPA_V_ID,{x,y,ennemi_widths[KOOPA_V_ID],ennemi_heights[KOOPA_V_ID],dir,0,0,1},1,0,dir}
#define KOOPA_R(x,y,dir) {KOOPA_R_ID,{x,y,ennemi_widths[KOOPA_R_ID],ennemi_heights[KOOPA_R_ID],dir,0,0,1},1,0,dir}
#define GOOMBA(x,y,dir) {GOOMBA_ID,{x,y,ennemi_widths[GOOMBA_ID],ennemi_heights[GOOMBA_ID],dir,0,0,1},ALIVE,0,dir}
#define KOOPA_V(x,y,dir) {KOOPA_V_ID,{x,y,ennemi_widths[KOOPA_V_ID],ennemi_heights[KOOPA_V_ID],dir,0,0,1},ALIVE,0,dir}
#define KOOPA_R(x,y,dir) {KOOPA_R_ID,{x,y,ennemi_widths[KOOPA_R_ID],ennemi_heights[KOOPA_R_ID],dir,0,0,1},ALIVE,0,dir}
#define PLANTE(x,y) {PLANTE_ID,{x,y,ennemi_widths[PLANTE_ID],ennemi_heights[PLANTE_ID],0,0,0,0},ALIVE,0,0}
#define DEAD 0
// Dimensions pr les boites
@ -41,6 +46,6 @@ void ennemiesDisplay();
void ennemiMove(ennemi_t *e);
void EnnemiesInit(ennemi_t * table, int s);
void ennemiesInit(ennemi_t * table, int s);
#endif

@ -8,15 +8,17 @@
#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};
const int ennemi_heights[NOMBRE_ENNEMIS] = {0, 8, 12, 9, 12, 9};
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 * e)
{
if (e->life==0)
if (e->life==0 && e->type!=NONE)
{
if (e->p1==0)
{
@ -27,84 +29,100 @@ void ennemiDisplay(ennemi_t * e)
e->b.vy--;
e->b.y+=e->b.vy;
e->b.x+=e->b.vx;
if (e->p1==30)
e->type=NONE;
if (e->p1>=30) e->type=NONE; // died 1.5 sec before
}
if (e->type==NONE)
return;
if (e->b.x<=cameraX(0)-e->b.w || e->b.x>=cameraX(0)+127)
return;
else
e->discovered=1;
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
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)
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(mario.p.y), &goomba, 1+(time_id/10)%2, 0);
if (e->life==0)
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(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)
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(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)
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(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};
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(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};
tileDraw(e->b.x-cameraX(0), e->b.y-cameraY(mario.p.y), &carapace_rouge, 0, 0);
}
{// 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)
{
if (mario.size==M_SMALL && mario_immunity==0)
{mario_dead=1;finish_level=0;}
else
marioSmaller();
else marioSmaller();
}
void ennemiMove(ennemi_t *e)
{
if (e->b.x<cameraX()+128+30 && e->b.x>cameraX()-30)
e->discovered=1;
e->discovered=1;
//if (e->b.x+e->b.w<=worldGetCell_real_x0())
// e->type=NONE;
if (e->discovered==0)
return;
return;
if (e->life==DEAD)
return;
return;
// e->type=NONE;
if (e->type==NONE)
return;
return;
bool e_hitMario=ennemi_check_collision(e);
if (e_hitMario&&mario_starMode)
{
e->life=DEAD;
e->p1=0;
scoreAdd(200);
scoreAdd(KILL_ENNEMI);
return;
}
@ -134,7 +152,7 @@ void ennemiMove(ennemi_t *e)
{
t->life=DEAD;
t->p1=0;
scoreAdd(200);
scoreAdd(KILL_ENNEMI);
}
}
}
@ -151,7 +169,7 @@ void ennemiMove(ennemi_t *e)
{
e->life=DEAD;
e->p1=0;
scoreAdd(100);
scoreAdd(KILL_ENNEMI);
break;
}
}
@ -177,19 +195,19 @@ void ennemiMove(ennemi_t *e)
}
}
if (e->b.y<0)
e->type=NONE;
e->type=NONE;
if (e_hitMario)
{
if (mario_fatal_hit)
{
e->life=DEAD;
e->p1=0;
scoreAdd(200);
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[GOOMBA_ID]+1;
}
else
hurtMario();
hurtMario();
}
return;
}
@ -211,7 +229,7 @@ void ennemiMove(ennemi_t *e)
}
}
if (e->b.y<0)
e->type=NONE;
e->type=NONE;
if (e_hitMario)
{
if (mario_fatal_hit)
@ -220,12 +238,12 @@ void ennemiMove(ennemi_t *e)
e->b.h=ennemi_heights[CARAPACE_VERTE];
e->p1=2;
e->b.vx=0;
scoreAdd(200);
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
}
else
hurtMario();
hurtMario();
}
return;
}
@ -250,7 +268,7 @@ void ennemiMove(ennemi_t *e)
{ // demi tour automatique
int s=-1;
if (e->b.vx>0)
s=e->b.w;
s=e->b.w;
if (worldGetCellCategory(e->b.x+s, e->b.y-1)==CTG_EMPTY && sgn(e->b.vx)==sgn(s))
{
@ -261,7 +279,7 @@ void ennemiMove(ennemi_t *e)
if (e->b.y<0)
e->type=NONE;
e->type=NONE;
if (e_hitMario)
{
if (mario_fatal_hit)
@ -270,12 +288,12 @@ void ennemiMove(ennemi_t *e)
e->b.h=ennemi_heights[CARAPACE_ROUGE];
e->p1=2;
e->b.vx=0;
scoreAdd(200);
scoreAdd(KILL_ENNEMI);
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_ROUGE]+1;
}
else
hurtMario();
hurtMario();
}
return;
}
@ -290,20 +308,15 @@ void ennemiMove(ennemi_t *e)
e->b.vx=6*e->p1;
}
}
if (e->b.y<0)
e->type=NONE;
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;
{ // transformation carapace->koopa
e->type--;
e->b.h=ennemi_heights[e->type];
e->p1=0;
e->b.vx=0;
e->p1=e->b.vx=0;
}
}
@ -312,9 +325,9 @@ void ennemiMove(ennemi_t *e)
if (e->p1==0 || e->p1>=2)
{
if (mario.p.x>=e->b.x)
e->p1=-1;
e->p1=-1;
if (mario.p.x<=e->b.x)
e->p1=1;
e->p1=1;
e->b.vx=6*e->p1;
if (mario_fatal_hit)
{
@ -322,24 +335,20 @@ void ennemiMove(ennemi_t *e)
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;
{ // 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=0;
e->b.vx=0;
e->p1=e->b.vx=0;
mario.p.vy=4;
mario.p.y=e->b.y+ennemi_heights[CARAPACE_VERTE]+1;
}
else
hurtMario();
else hurtMario();
}
}
@ -349,34 +358,24 @@ void ennemiMove(ennemi_t *e)
}
// 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]);
}
for (int i=0; i<ennemis_global_size; i++) ennemiDisplay(&ennemis_global[i]);
}
void EnnemiesInit(ennemi_t * table, int s)
void ennemiesInit(ennemi_t * table, int s)
{
ennemis_global_size=0;
if (ennemis_global)
{
freeProf(ennemis_global);
ennemis_global=0;
}
if (0==s)
return;
ennemis_global_size=ennemis_global=0;
if (!s) return;
int size=sizeof(ennemi_t)*s;
ennemis_global=mallocProf(size);
if (ennemis_global==0)
mallocError();
if ((ennemis_global=mallocProf(size))==0) mallocError();
ennemis_global_size=s;
memcpy(ennemis_global, table, size);
}

@ -334,7 +334,7 @@ static void unpackLevel(packed_level_t * p)
//freeProf(c);
EnnemiesInit(ennemis, nombre_ennemis);
ennemiesInit(ennemis, nombre_ennemis);
}
void setLevel(int w, int l)
@ -342,7 +342,7 @@ void setLevel(int w, int l)
levelNew();
teleportersSet(0,0);
worldSet(0,0,0,0,0);
EnnemiesInit(0, 0);
ennemiesInit(0, 0);
platformsInit(0, 0);
if (w+1==1 && l+1==1) // 1-2

Loading…
Cancel
Save