supercasiobros/src/world.c

269 lines
5.0 KiB
C
Raw Normal View History

2019-11-16 11:44:09 +01:00
#include "world.h"
#include "tile.h"
#include "mario.h"
2019-12-04 19:27:27 +01:00
#include "ennemi.h"
2019-11-20 15:33:34 +01:00
#include <gint/display.h>
#include "bonus.h"
2020-01-06 20:56:10 +01:00
#include <liblog.h>
#include "constants.h"
#include "ennemi.h"
#include <plateforme.h>
2020-01-24 12:08:40 +01:00
#include <tuyau.h>
2020-01-29 14:34:47 +01:00
#include <bullets.h>
#include <keyboard.h>
#include <base.h>
#include <camera.h>
2020-02-02 16:24:40 +01:00
#include <level.h>
#include <gint/std/string.h>
#include <gint/std/stdlib.h>
2020-01-06 20:56:10 +01:00
map_t * map_current=0;
int world_get_width()
{
return map_current->w*8;
}
2019-11-16 11:44:09 +01:00
cell_t death={0,0};
2019-12-04 19:27:27 +01:00
cell_t* world_get(int x, int y)
2019-11-16 11:44:09 +01:00
{
2019-12-04 19:27:27 +01:00
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];
2019-12-04 19:27:27 +01:00
else
{
return &death;
}
2019-11-16 11:44:09 +01:00
}
void display_cell(int cx, int cy, int sx, int sy, int plan)
2019-11-16 11:44:09 +01:00
{
const cell_t * cell=world_get(cx,cy);
2019-12-07 19:26:55 +01:00
if (cell==0)
return;
2019-12-07 19:26:55 +01:00
if (plan==1)
2019-12-04 19:27:27 +01:00
{
if (cell->type==TUYAU)
{
2019-12-07 19:26:55 +01:00
tuyau_t* i=(tuyau_t*)cell;
draw_tile(sx, sy, &tuyau, i->x, i->y);
}
2019-12-04 19:27:27 +01:00
if (cell->type==ARBRE)
{
arbre_t* i=(arbre_t*)cell;
draw_tile(sx, sy, &arbre, i->x, i->y);
}
if (cell->type==EARTH)
2019-12-04 19:27:27 +01:00
{
2019-12-07 19:26:55 +01:00
earth_t* i=(earth_t*)cell;
draw_tile(sx, sy, &earth, i->x, i->y);
2019-12-04 19:27:27 +01:00
}
2019-12-08 16:34:32 +01:00
if (cell->type==BLOC)
{
draw_tile(sx, sy, &bloc, 0, 0);
}
2019-12-04 19:27:27 +01:00
if (cell->type==BLOC)
2019-12-04 19:27:27 +01:00
{
2019-12-07 19:26:55 +01:00
draw_tile(sx, sy, &bloc, 0, 0);
2019-12-04 19:27:27 +01:00
}
if (cell->type==BRICK)
{
2019-12-07 19:26:55 +01:00
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;
2019-12-07 14:32:38 +01:00
if (i->state==1 && i->time_hit_id==0)
i->type=0;
}
if (i->hidden==0)
{
2019-12-07 14:32:38 +01:00
if (i->time_hit_id || i->content==0 || i->number>0)
draw_tile(sx, sy, &brick, 0, i->state);
else
draw_tile(sx, sy, &gift, 1, 0);
if (i->content==1 && i->time_hit_id && i->time_hit_id<=4)
draw_tile(sx, sy+8, &coin, 0, 0);
}
}
if (cell->type==GIFT)
{
gift_t* i=(gift_t*)cell;
2019-12-07 14:32:38 +01:00
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)
draw_tile(sx, sy, &gift, 0, 0);
else
draw_tile(sx, sy, &gift, 1, 0);
if (i->content==1 && i->time_hit_id && i->time_hit_id<=4)
draw_tile(sx, sy+8, &coin, 0, 0);
}
}
}
else
2019-12-04 19:27:27 +01:00
{
if (cell->type==COIN)
{
2020-01-29 14:34:47 +01:00
coin_t const * i=(coin_t*)cell;
2019-12-07 19:26:55 +01:00
draw_tile(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)
draw_tile(sx, sy, &buisson, i->x, i->y);
if (i->type==NUAGE)
draw_tile(sx, sy, &nuage, i->x, i->y);
if (i->type==COLLINE)
draw_tile(sx, sy, &colline, i->x, i->y);
if (i->type==CASTLE)
draw_tile(sx, sy, &castle, i->x, i->y);
if (i->type==END_LEVEL)
draw_tile(sx-8, sy, &end_level, 0, i->y);
}
2019-12-04 19:27:27 +01:00
}
2019-11-16 11:44:09 +01:00
}
2019-11-20 15:33:34 +01:00
int world_get_ctg(int x, int y)
2019-11-16 12:02:30 +01:00
{
cell_t *c=world_get(x,y);
2019-12-07 19:26:55 +01:00
if (c==0)
2019-12-04 19:27:27 +01:00
{
return CTG_DEATH;
}
if (plateforme_check_collide(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;
}
2019-12-07 19:26:55 +01:00
switch (c->type)
2019-12-04 19:27:27 +01:00
{
case TUYAU:
case GIFT:
case BRICK:
case EARTH:
2019-12-08 16:34:32 +01:00
case BLOC:
case ARBRE:
2019-12-04 19:27:27 +01:00
return CTG_SOIL;
default:
return CTG_EMPTY;
}
2019-11-20 15:33:34 +01:00
}
2020-01-29 14:34:47 +01:00
void world_draw()
2019-11-20 15:33:34 +01:00
{
camera_move(0);
//camera_adjust();
2020-01-29 14:34:47 +01:00
int mx0=camera_x();
2020-01-29 18:24:33 +01:00
int my0=camera_y();
2019-12-04 19:27:27 +01:00
int sx0=mx0%8;
int sy0=my0%8;
2020-02-16 18:47:20 +01:00
//int mx=mx0;
int mx, my;
for (int i=0, mx=mx0; i<=17; i++, mx+=8)
{
2020-02-16 18:47:20 +01:00
//int my=my0;
for (int j=0, my=my0; j<=9; j++, my+=8)
{
display_cell(mx, my, 8*i-sx0, 8*j-sy0,0); //ap
2020-02-16 18:47:20 +01:00
//my+=8;
}
2020-02-16 18:47:20 +01:00
//mx+=8;
}
bonus_draw();
2019-12-07 14:32:38 +01:00
display_ennemi_table();
bullet_display();
display_plateformes();
2019-12-04 19:27:27 +01:00
mario_draw();
2019-12-04 19:27:27 +01:00
//int mx, my;
mx=mx0;
2019-12-04 19:27:27 +01:00
for (int i=0; i<=17; i++)
{
int my=my0;
2020-02-16 18:47:20 +01:00
for (int j=0; j<=9; j++)
2019-12-04 19:27:27 +01:00
{
display_cell(mx, my, 8*i-sx0, 8*j-sy0,1); //pp
2019-12-04 19:27:27 +01:00
my+=8;
}
mx+=8;
}
//teleporteurs_display();
2020-01-06 20:56:10 +01:00
//dvline(teleporteurs[0].x-world_get_real_x0(),C_BLACK);
/*
if (mkb_getstate(MK_JUMP)) drect(1,60,6,63,C_BLACK);
if (mkb_getstate(MK_RUN)) drect(8,60,13,63,C_BLACK);
*/
// draw immunity remaining time
if (mario_immunity) drect(0,4+mario_immunity,2,64, C_BLACK);
2019-12-08 16:34:32 +01:00
}
void world_move()
{
2020-01-29 14:34:47 +01:00
mkb_update();
2020-01-06 20:56:10 +01:00
for (int i=0; i<ennemis_global_size; i++)
{
move_ennemi(&ennemis_global[i]);
}
2019-12-08 16:34:32 +01:00
bonus_move();
bullet_move();
move_plateformes();
2019-12-08 16:34:32 +01:00
mario_move();
2020-01-24 12:08:40 +01:00
teleport_active();
}
2020-02-01 14:39:08 +01:00
2020-02-02 16:05:56 +01:00
void world_set(int w, int h, int x, int y, cell_t const * a)
2020-02-01 14:39:08 +01:00
{
// Resets mario's vx
mario.p.vx=0; mario.p.vy=0;
// Free the previous map
if (map_current)
2020-02-03 11:12:54 +01:00
{
free_prof(map_current);
map_current=0;
}
2020-02-01 14:39:08 +01:00
// If the new map size is null => invalid map, return
if (0==w*h)
return;
2020-02-01 14:39:08 +01:00
// Calculates the new struct size
int size= 4*sizeof(int) + sizeof(cell_t)*w*h;
2020-02-16 18:47:20 +01:00
// Copy map into ram
map_current=(map_t*)malloc_prof(size); if (map_current==0) malloc_error();
2020-02-01 14:39:08 +01:00
// 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);
}