supercasiobros/src/world.c

209 lines
5.0 KiB
C
Executable File

#include <world.h>
#include <base.h>
#include <tile.h>
#include <level.h>
#include <keyboard.h>
#include <camera.h>
#include <mario.h>
#include <ennemi.h>
#include <bonus.h>
#include <ennemi.h>
#include <plateforme.h>
#include <tuyau.h>
#include <bullets.h>
#include <liblog.h>
#include <gint/display.h>
#include <gint/std/string.h>
#include <gint/std/stdlib.h>
map_t * map_current=0;
int worldGetWidth()
{
return map_current->w*8;
}
cell_t death={0,0};
cell_t* worldGetCell(int x, int y)
{
x/=8;
y/=8;
if (0<=x && x<map_current->w && 0<=y && y<map_current->h)
return &map_current->data[x*map_current->h+y];
else
{
return &death;
}
}
void cellDraw(int cx, int cy, int sx, int sy, int plan)
{
const cell_t * cell=worldGetCell(cx,cy);
if (cell==0)
return;
if (plan==1) switch (cell->type)
{
case TUYAU:
tileDraw(sx, sy, &tuyau, ((tuyau_t*)cell)->x, ((tuyau_t*)cell)->y);
break;
case ARBRE:
tileDraw(sx, sy, &arbre, ((arbre_t*)cell)->x, ((arbre_t*)cell)->y);
break;
case EARTH:
tileDraw(sx, sy, &earth, ((earth_t*)cell)->x, ((earth_t*)cell)->y);
break;
case BLOC:
tileDraw(sx, sy, &bloc, 0, 0);
break;
case BRICK:
if (((brick_t*)cell)->time_hit_id) // calculate collision animation
{
((brick_t*)cell)->time_hit_id++; // Next frame for the animation
sy+=2+(((brick_t*)cell)->time_hit_id-4)/4; // For the brick coordinate
if (((brick_t*)cell)->time_hit_id==8) ((brick_t*)cell)->time_hit_id=0; // End of animation
if (((brick_t*)cell)->state==1 && ((brick_t*)cell)->time_hit_id==0) ((brick_t*)cell)->type=0; // Delete brick
}
if (!((brick_t*)cell)->hidden)
{
if (((brick_t*)cell)->time_hit_id || !((brick_t*)cell)->content || ((brick_t*)cell)->number) tileDraw(sx, sy, &brick, 0, ((brick_t*)cell)->state);
else tileDraw(sx, sy, &gift, 1, 0);
if (((brick_t*)cell)->content==1 && ((brick_t*)cell)->time_hit_id && ((brick_t*)cell)->time_hit_id<=4) tileDraw(sx, sy+8, &coin, 0, 0); // Draw a coin
}
break;
case GIFT:
if (((gift_t*)cell)->time_hit_id)
{
((gift_t*)cell)->time_hit_id++;
sy+=2+(((gift_t*)cell)->time_hit_id-4)/4;
if (((gift_t*)cell)->time_hit_id==8) ((gift_t*)cell)->time_hit_id=0;
}
if (((gift_t*)cell)->hidden==0)
{
if (((gift_t*)cell)->time_hit_id || ((gift_t*)cell)->number) tileDraw(sx, sy, &gift, 0, 0);
else tileDraw(sx, sy, &gift, 1, 0);
if (((gift_t*)cell)->content==1 && ((gift_t*)cell)->time_hit_id && ((gift_t*)cell)->time_hit_id<=4) tileDraw(sx, sy+8, &coin, 0, 0);
}
break;
}
else switch (cell->type)
{
case COIN:
tileDraw(sx, sy, &coin, ((coin_t*)cell)->taken, 0);
break;
case BUISSON:
tileDraw(sx, sy, &buisson, ((deco_t*)cell)->x, ((deco_t*)cell)->y);
break;
case NUAGE:
tileDraw(sx, sy, &nuage, ((deco_t*)cell)->x, ((deco_t*)cell)->y);
break;
case COLLINE:
tileDraw(sx, sy, &colline, ((deco_t*)cell)->x, ((deco_t*)cell)->y);
break;
case CASTLE:
tileDraw(sx, sy, &castle, ((deco_t*)cell)->x, ((deco_t*)cell)->y);
break;
case END_LEVEL:
tileDraw(sx-8, sy, &end_level, ((deco_t*)cell)->x, ((deco_t*)cell)->y);
break;
}
}
int worldGetCellCategory(int x, int y)
{
cell_t *c=worldGetCell(x,y); // get cell props
if (c==0) return CTG_DEATH; // out of the map
if (platformsCollisionTest(x,y)) return CTG_SOIL; // tested point is a platform
if (c->type==ARBRE && (((arbre_t*)c)->x==1&&((arbre_t*)c)->y==1)) return CTG_EMPTY; // tree trunk
switch (c->type)
{
case TUYAU: case GIFT: case BRICK: case EARTH: case BLOC: case ARBRE:
return CTG_SOIL;
default:
return CTG_EMPTY;
}
}
void worldDraw()
{
cameraMove();
const int mx0=cameraX(), my0=cameraY();
const int sx0=mx0%8, sy0=my0%8;
//background tiles
for (int i=0, mx=mx0; i<=17; i++, mx+=8) for (int j=0, my=my0; j<=9; j++, my+=8) cellDraw(mx, my, 8*i-sx0, 8*j-sy0,0);
// animated items
bonusDraw();
ennemiesDisplay();
bulletsDraw();
platformsDraw();
marioDraw();
//foreground tiles
for (int i=0, mx=mx0; i<=17; i++, mx+=8) for (int j=0, my=my0; j<=9; j++, my+=8) cellDraw(mx, my, 8*i-sx0, 8*j-sy0,1);
// draw immunity remaining time [It's a progress bar on the left side of the screen]
if (mario_immunity) drect(0,64-MARIO_IMMUNITY_TIME+mario_immunity,2,64, C_BLACK);
}
void worldMove()
{
MKB_update();
for (int i=0; i<ennemis_global_size; i++) ennemiMove(&ennemis_global[i]);
bonusMove();
bulletsMove();
platformsMove();
marioMove();
teleportersActive();
}
void worldSet(int w, int h, int x, int y, cell_t * a)
{
// Resets mario's vx
mario.p.vx=mario.p.vy=0;
// Free the previous map
if (map_current)
{
if (map_current->data) freeProf(map_current->data);
freeProf(map_current);
map_current=0;
}
// If the new map size is null => invalid map, return
if (0==w*h) return;
// Copy map into ram
map_current=(map_t*)mallocProf(sizeof(map_t)); if (!map_current) mallocError();
// Copy the map to ram
map_current->w = w;
map_current->h = h;
mario.p.x = map_current->start_x = x;
mario.p.y = map_current->start_y = y;
map_current->data=a;
//memcpy(map_current->data, a, sizeof(cell_t)*w*h);
}