PCBrawl/src/level.c

224 lines
7.2 KiB
C

/* Ne pas lire ce code svp merci */
#include "level.h"
#include "attack.h"
#include "bullet.h"
#include "conf.h"
#include "lzy.h"
#include "particle.h"
#include "player.h"
#include "sandbag.h"
#include <stdlib.h>
#include <string.h>
static int id;
static struct Level level;
static struct Level bg;
static void level_free(void);
static void level_load(void);
static void level_draw_bg(void);
void level_init(int lvl_id, int chara) {
level = (struct Level){
.w = 17,
.h = 10,
};
level.s = level.w * level.h;
id = lvl_id;
level_free();
level_load();
player_init(chara);
sandbag_init();
bullet_table_init();
attack_table_init();
particle_table_init();
}
void level_draw(void) {
level_draw_bg();
for (int m = 0; m < level.h; ++m) {
for (int n = 0; n < level.w; ++n) {
tile_t tile = level_get_tile(n, m);
LZY_DrawTile(tile, n * TILE_S - DRAW_SHIFT, m * TILE_S);
}
}
}
tile_t level_get_px(int x, int y) {
x += DRAW_SHIFT;
const int pos = x / TILE_S + y / TILE_S * level.w;
if (pos > level.s || pos < 0 || x < 0 || x > TILE_S * level.w)
return 0;
const int tile = level.data[pos];
return tile - (tile != 0);
}
tile_t level_get_tile(int x, int y) {
if (x + y * level.w > level.s || x + y * level.w < 0)
return 0;
const int tile = level.data[x + y * level.w];
return tile - (tile != 0);
}
int level_count(tile_t tile) {
int i = 0;
for (int t = 0; t < level.h * level.w - 1; ++t) {
if (level.data[t] == tile) {
++i;
}
}
return i;
}
int level_search_i(tile_t tile, int occ) {
for (int t = 0; t < level.h * level.w - 1; ++t) {
if (level.data[t] == tile) {
if (!(occ - 1)) {
return t;
} else {
--occ;
}
}
}
return 0;
}
struct Vec2 level_search_s(tile_t tile, int occ) {
for (int m = 0; m < level.h; ++m) {
for (int n = 0; n < level.w; ++n) {
if (level.data[n + m * level.w] == tile) {
if (!(occ - 1)) {
return (struct Vec2){n, m};
} else {
--occ;
}
}
}
}
return (struct Vec2){0, 0};
}
struct Vec2 level_get_dim(void) {
return (struct Vec2){level.w, level.h};
}
static void level_free(void) {
level.data = (tile_t *)malloc(170 * sizeof(tile_t));
}
static void level_load(void) {
/* TODO : LEVEL LOADING */
tile_t data[3][170] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 20, 0, 0, 0, 0, 0, 0, 0,
18, 20, 0, 0, 0, 0, 0, 0, 41, 67, 19, 19, 19, 19, 19, 19, 19, 69, 43,
0, 0, 0, 0, 0, 0, 64, 46, 44, 65, 65, 65, 65, 65, 46, 44, 66, 0, 0,
0, 0, 0, 0, 0, 41, 43, 0, 0, 0, 0, 0, 41, 43, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 84, 85, 86, 87, 88, 89, 90, 91, 92, 84, 85, 0, 0, 0,
0, 0, 0, 107, 108, 109, 110, 111, 112, 113, 114, 115, 107, 108, 0, 0, 0,
0, 0, 0, 130, 131, 132, 133, 134, 135, 136, 137, 138, 130, 131, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 183, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 183, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 156, 157, 159, 155, 155,
155, 159, 156, 157, 177, 0, 0, 0, 0, 0, 0, 161, 179, 180, 161, 160,
160, 160, 161, 179, 180, 161, 0, 0, 0, 0, 0, 0, 161, 161, 161, 161,
160, 160, 160, 161, 161, 161, 161, 0, 0, 0}};
memcpy(level.data, data[id], sizeof(data[0]));
if (id == 2) {
bg.data = (tile_t *)malloc(170 * sizeof(tile_t));
tile_t owo[170] = {
240, 262, 262, 0, 288, 242, 241, 242, 242, 242, 240, 242, 288, 0,
262, 262, 241, 0, 242, 242, 0, 288, 242, 242, 242, 242, 242, 242,
242, 288, 0, 242, 242, 0, 0, 262, 262, 0, 0, 241, 263, 0,
0, 0, 264, 240, 0, 0, 262, 262, 0, 0, 0, 0, 0, 263,
0, 242, 242, 242, 242, 242, 0, 264, 0, 0, 0, 0, 286, 287,
0, 0, 240, 0, 241, 242, 242, 242, 240, 0, 242, 0, 0, 286,
287, 0, 0, 0, 0, 0, 0, 0, 262, 262, 262, 0, 0, 240,
0, 288, 0, 0, 263, 0, 242, 0, 239, 239, 285, 262, 262, 262,
285, 239, 239, 0, 241, 0, 264, 242, 0, 0, 264, 0, 0, 0,
0, 0, 0, 0, 0, 0, 263, 0, 0, 242, 242, 286, 287, 242,
0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 286, 287, 242, 242,
263, 264, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 242,
263, 242};
memcpy(bg.data, owo, sizeof(owo));
}
}
static void level_draw_bg(void) {
switch (id) {
case 0:
default:
for (int i = 0; i < 3; ++i) {
LZY_DrawTileEx(115, i * TILE_S * 6, 0, 6, 10);
}
break;
case 1:
LZY_DrawSetColor(3, 2, 30);
LZY_DrawFillRect(0, 112, 396, 112);
for (int i = 0; i < level.w; ++i) {
LZY_DrawTileEx(121, i * TILE_S, 0, 1, 5);
}
LZY_DrawTileEx(122, 162, 94, 3, 2);
LZY_DrawTileEx(168, 66, 216, 2, 1);
LZY_DrawTileEx(168, 282, 216, 2, 1);
LZY_DrawTileEx(170, 66, 120, 2, 1);
LZY_DrawTileEx(170, 282, 120, 2, 1);
LZY_DrawTile(283, -6, 144);
LZY_DrawTile(283, 42, 216);
LZY_DrawTile(283, 42, 120);
LZY_DrawTile(283, 378, 120);
LZY_DrawTileEx(282, 330, 144, 1, 3);
LZY_DrawTileEx(236, 18, 168, 1, 2);
LZY_DrawTileEx(236, 354, 144, 1, 2);
LZY_DrawTileEx(306, 378, 192, 1, 2);
LZY_DrawTileEx(237, -6, 192, 1, 2);
break;
case 2:
LZY_DrawSetColor(255, 255, 255);
for (int m = 0; m < level.h; ++m) {
for (int n = 0; n < level.w; ++n) {
int tile = bg.data[n + m * level.w];
tile = tile - (tile != 0);
if (tile)
LZY_DrawTile(tile, n * TILE_S - 6, m * TILE_S);
else
LZY_DrawFillRect(n * TILE_S - 6, m * TILE_S, 24, 24);
}
}
}
}
void level_delete_tile(tile_t tile) {
for (int m = 0; m < level.h; ++m) {
for (int n = 0; n < level.w; ++n) {
if (level_get_tile(n, m) == tile)
level.data[m + n * level.w] = 0;
}
}
}