supercasiobros/src/level.c

176 lines
3.4 KiB
C

#include "level.h"
#include "world.h"
#include "mario.h"
#include "score.h"
#include "ennemi.h"
#include "keyboard.h"
#include <plateforme.h>
#include <tuyau.h>
#include <gint/display.h>
#include <gint/std/stdlib.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/std/string.h>
#include <gint/std/stdio.h>
#define PACKED_EMPTY 0
#define PACKED_STONE 1
#define PACKED_COIN 2
#define PACKED_BRICK 3
#define PACKED_BRICK_COIN 4
#define PACKED_BOX_COIN 5
#define PACKED_BOX_CHAMPI 6
#define PACKED_BETON 7
#define PACKED_TUYAU_BOUT 8
#define PACKED_TUYAU_MIDDLE 9
void malloc_error()
{
extern image_t img_ram;
timer_stop(0);
dimage(0,0,&img_ram);
dupdate();
while (1)
mkb_getkey();
}
static uint8_t pack_access(packed_level_t const * const p, int x, int y)
{
if (0<=x && x<w && 0<=y && y<h)
return p->data[x*p->height+y];
else
return PACKED_EMPTY;
}
static void cell_set(cell_t *const array, int w, int h, int x, int y, cell_t const cell)
{
if (0<=x && x<w && 0<=y && y<h)
array[x*w+y] = cell;
}
void unpack_level(packed_level_t const * const p)
{
const unsigned int w = p->width;
const unsigned int h = p->height;
cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h);
if (c==0)
malloc_error();
int sx=0, sy=p->height; // Mario start coords
for (int x=0; x<w; x++)
{
for (int y=0; y<h; y++)
{
int contents = pack_access(p,x,y);
cell_t cell = {0,0};
if (contents==PACKED_EMPTY)
{
cell_t t={0,0};
cell = t;
}
else if (contents==PACKED_STONE)
{
int props=0;
{ // determiner x
int px=1;
if (pack_access(p,x-1,y)!=PACKED_STONE)
px=0;
if (pack_access(p,x+1,y)!=PACKED_STONE)
px=2;
props+=16*px;
}
{ // determiner y
int py=1;
if (pack_access(p,x,y+1)!=PACKED_STONE)
py=0;
props+=py;
}
cell_t t={EARTH,props};
cell = t;
}
else if (contents==PACKED_COIN)
{
cell_t t={COIN,0};
cell = t;
}
else if (contents==PACKED_BRICK)
{
cell_t t={BRICK,0};
cell = t;
}
else if (contents==PACKED_BRICK_COIN)
{
cell_t t={BRICK,0x15};
cell = t;
}
else if (contents==PACKED_BOX_COIN)
{
cell_t t={GIFT,0x11};
cell = t;
}
else if (contents==PACKED_BOX_CHAMPI)
{
cell_t t={GIFT,0x21};
cell = t;
}
else if (contents==PACKED_BETON)
{
cell_t t={BLOC,0};
cell = t;
}
else if (contents==PACKED_TUYAU_BOUT)
{
int x=0, y=0;
int props=0;
{ // haut du tuyau horizontal
if (pack_access(p,x-1,y)==PACKED_TUYAU_BOUT)
{
x=1;
if (pack_access(p,x,y-1)==PACKED_TUYAU_MIDDLE)
y=2;
if (pack_access(p,x,y+1)==PACKED_TUYAU_MIDDLE)
y=4;
}
if (pack_access(p,x+1,y)==PACKED_TUYAU_BOUT)
{
x=0;
if (pack_access(p,x,y-1)==PACKED_TUYAU_MIDDLE)
y=2;
if (pack_access(p,x,y+1)==PACKED_TUYAU_MIDDLE)
y=4;
}
}
{ // bout de tuyau vertical
if (pack_access(p,x,y-1)==PACKED_TUYAU_BOUT)
{
y=0;
if (pack_access(p,x+1,y)==PACKED_TUYAU_MIDDLE)
x=0;
if (pack_access(p,x-1,y)==PACKED_TUYAU_MIDDLE)
x=2;
}
if (pack_access(p,x,y+1)==PACKED_TUYAU_BOUT)
{
y=1;
if (pack_access(p,x+1,y)==PACKED_TUYAU_MIDDLE)
x=0;
if (pack_access(p,x-1,y)==PACKED_TUYAU_MIDDLE)
x=2;
}
}
props=16*x+y;
cell_t t={TUYAU,props};
cell = t;
}
cell_set(c,w,h,x,y,cell);
}
}
}