244 lines
4.7 KiB
C
Executable File
244 lines
4.7 KiB
C
Executable File
#include "world.h"
|
|
#include "tile.h"
|
|
#include "mario.h"
|
|
#include "ennemi.h"
|
|
#include <gint/display.h>
|
|
#include "bonus.h"
|
|
#include <liblog.h>
|
|
#include "constants.h"
|
|
#include "ennemi.h"
|
|
#include <plateforme.h>
|
|
#include <tuyau.h>
|
|
#include <bullets.h>
|
|
#include <keyboard.h>
|
|
#include <base.h>
|
|
#include <camera.h>
|
|
#include <level.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)
|
|
{
|
|
if (cell->type==TUYAU)
|
|
{
|
|
tuyau_t* i=(tuyau_t*)cell;
|
|
tileDraw(sx, sy, &tuyau, i->x, i->y);
|
|
}
|
|
|
|
if (cell->type==ARBRE)
|
|
{
|
|
arbre_t* i=(arbre_t*)cell;
|
|
tileDraw(sx, sy, &arbre, i->x, i->y);
|
|
}
|
|
|
|
|
|
if (cell->type==EARTH)
|
|
{
|
|
earth_t* i=(earth_t*)cell;
|
|
tileDraw(sx, sy, &earth, i->x, i->y);
|
|
}
|
|
if (cell->type==BLOC)
|
|
{
|
|
tileDraw(sx, sy, &bloc, 0, 0);
|
|
}
|
|
|
|
if (cell->type==BLOC)
|
|
{
|
|
tileDraw(sx, sy, &bloc, 0, 0);
|
|
}
|
|
|
|
if (cell->type==BRICK)
|
|
{
|
|
brick_t* i=(brick_t*)cell;
|
|
if (i->time_hit_id)
|
|
{
|
|
i->time_hit_id++;
|
|
sy+=2+(i->time_hit_id-4)/4;
|
|
if (i->time_hit_id==8)
|
|
i->time_hit_id=0;
|
|
if (i->state==1 && i->time_hit_id==0)
|
|
i->type=0;
|
|
}
|
|
if (i->hidden==0)
|
|
{
|
|
if (i->time_hit_id || i->content==0 || i->number>0)
|
|
tileDraw(sx, sy, &brick, 0, i->state);
|
|
else
|
|
tileDraw(sx, sy, &gift, 1, 0);
|
|
if (i->content==1 && i->time_hit_id && i->time_hit_id<=4)
|
|
tileDraw(sx, sy+8, &coin, 0, 0);
|
|
}
|
|
}
|
|
|
|
if (cell->type==GIFT)
|
|
{
|
|
gift_t* i=(gift_t*)cell;
|
|
if (i->time_hit_id)
|
|
{
|
|
i->time_hit_id++;
|
|
sy+=2+(i->time_hit_id-4)/4;
|
|
if (i->time_hit_id==8)
|
|
i->time_hit_id=0;
|
|
}
|
|
if (i->hidden==0)
|
|
{
|
|
if (i->time_hit_id || i->number)
|
|
tileDraw(sx, sy, &gift, 0, 0);
|
|
else
|
|
tileDraw(sx, sy, &gift, 1, 0);
|
|
if (i->content==1 && i->time_hit_id && i->time_hit_id<=4)
|
|
tileDraw(sx, sy+8, &coin, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (cell->type==COIN)
|
|
{
|
|
coin_t const * i=(coin_t*)cell;
|
|
tileDraw(sx, sy, &coin, i->taken, 0);
|
|
}
|
|
|
|
if (cell->type==BUISSON || cell->type==NUAGE || cell->type==COLLINE || cell->type==CASTLE || cell->type==END_LEVEL)
|
|
{
|
|
deco_t* i=(deco_t*)cell;
|
|
if (i->type==BUISSON)
|
|
tileDraw(sx, sy, &buisson, i->x, i->y);
|
|
if (i->type==NUAGE)
|
|
tileDraw(sx, sy, &nuage, i->x, i->y);
|
|
if (i->type==COLLINE)
|
|
tileDraw(sx, sy, &colline, i->x, i->y);
|
|
if (i->type==CASTLE)
|
|
tileDraw(sx, sy, &castle, i->x, i->y);
|
|
if (i->type==END_LEVEL)
|
|
tileDraw(sx-8, sy, &end_level, 0, i->y);
|
|
}
|
|
}
|
|
}
|
|
|
|
int worldGetCellCategory(int x, int y)
|
|
{
|
|
cell_t *c=worldGetCell(x,y);
|
|
if (c==0)
|
|
{
|
|
return CTG_DEATH;
|
|
}
|
|
if (platformsCollisionTest(x,y))
|
|
return CTG_SOIL;
|
|
if (c->type==ARBRE)
|
|
{
|
|
arbre_t* a=(arbre_t*)c;
|
|
if (a->x==1&&a->y==1)
|
|
return CTG_EMPTY;
|
|
}
|
|
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(0);
|
|
//cameraAdjust();
|
|
int mx0=cameraX();
|
|
int my0=cameraY();
|
|
int sx0=mx0%8;
|
|
int 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);
|
|
|
|
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
|
|
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 const * a)
|
|
{
|
|
// Resets mario's vx
|
|
mario.p.vx=0; mario.p.vy=0;
|
|
|
|
// Free the previous map
|
|
if (map_current)
|
|
{
|
|
freeProf(map_current);
|
|
map_current=0;
|
|
}
|
|
|
|
// If the new map size is null => invalid map, return
|
|
if (0==w*h)
|
|
return;
|
|
|
|
// Calculates the new struct size
|
|
int size= 4*sizeof(int) + sizeof(cell_t)*w*h;
|
|
|
|
// Copy map into ram
|
|
map_current=(map_t*)mallocProf(size); if (map_current==0) 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;
|
|
memcpy(map_current->data, a, sizeof(cell_t)*w*h);
|
|
}
|