crystal-tower/src/level.c

173 lines
3.1 KiB
C
Raw Permalink Normal View History

2021-11-10 00:37:45 +01:00
#include "level.h"
2021-11-10 12:10:04 +01:00
#include "camera.h"
2021-11-10 00:37:45 +01:00
#include "conf.h"
2021-11-17 14:51:05 +01:00
#include "levels_bin.h"
2021-11-11 07:09:56 +01:00
#include "particles.h"
#include "player.h"
2021-11-16 23:11:16 +01:00
#include "raygint/display.h"
2021-11-10 06:15:41 +01:00
#include "tile.h"
#include "vec.h"
2021-11-17 15:43:38 +01:00
#include <stdio.h>
2021-11-10 00:37:45 +01:00
#include <stdlib.h>
2021-11-10 08:08:29 +01:00
static struct Level self;
void
2021-11-11 16:59:37 +01:00
level_load(int id)
2021-11-10 00:37:45 +01:00
{
2021-11-12 17:49:16 +01:00
extern int end;
2021-11-17 15:43:38 +01:00
#ifdef GINT
const struct LevelBin *s = levels[id].data;
#endif
2021-11-17 15:14:03 +01:00
#ifdef RAYLIB
2021-11-17 15:43:38 +01:00
const struct LevelBin *s = levels[id].data + 1;
2021-11-17 15:14:03 +01:00
#endif
2021-11-10 00:37:45 +01:00
int i = s->width * s->height;
2021-11-10 08:08:29 +01:00
self.width = s->width;
self.height = s->height;
self.size = i;
self.data = malloc(i);
2021-11-11 18:31:29 +01:00
self.id = id;
2021-11-11 16:03:17 +01:00
while (i-- > 0)
2021-11-10 08:08:29 +01:00
self.data[i] = s->data[i];
2021-11-11 16:59:37 +01:00
player_init(level_find(TILE_PLAYER));
particles_init();
2021-11-17 15:43:38 +01:00
if (levels[self.id + 1].data == NULL)
2021-11-12 17:49:16 +01:00
end = 1;
2021-11-10 00:37:45 +01:00
}
2021-11-11 07:09:56 +01:00
void
level_reload(void)
{
2021-11-11 16:59:37 +01:00
level_load(self.id);
}
void
level_next(void)
{
2021-11-17 15:43:38 +01:00
if (levels[self.id + 1].data != NULL)
2021-11-11 16:59:37 +01:00
level_load(self.id + 1);
else
level_reload();
2021-11-11 07:09:56 +01:00
}
2021-11-10 00:37:45 +01:00
void
2021-11-10 08:08:29 +01:00
level_free(void)
2021-11-10 00:37:45 +01:00
{
2021-11-10 08:08:29 +01:00
free(self.data);
2021-11-10 00:37:45 +01:00
}
2021-11-17 14:51:05 +01:00
#ifdef GINT
2021-11-10 00:37:45 +01:00
void
2021-11-10 08:08:29 +01:00
level_draw(void)
2021-11-10 00:37:45 +01:00
{
extern bopti_image_t bimg_tileset;
const int tileset_width = bimg_tileset.width / TILE_SIZE;
2021-11-10 12:10:04 +01:00
const struct Vec off = camera_offset();
2021-11-10 00:37:45 +01:00
int i;
int x = 0;
int y = 0;
2021-11-10 08:08:29 +01:00
for (i = 0; i < self.size; i++) {
const int tile = self.data[i];
2021-11-12 18:30:48 +01:00
if (tile && tile != TILE_THUNDER) {
2021-11-12 00:52:28 +01:00
const int sx = x * TILE_SIZE + off.x;
const int sy = y * TILE_SIZE + off.y;
const int rx = tile % tileset_width * TILE_SIZE;
const int ry = tile / tileset_width * TILE_SIZE;
2021-11-10 00:37:45 +01:00
2021-11-11 16:59:37 +01:00
dsubimage(sx, sy, &bimg_tileset, rx, ry, TILE_SIZE,
TILE_SIZE, DIMAGE_NONE);
2021-11-12 00:52:28 +01:00
}
2021-11-10 00:37:45 +01:00
2021-11-10 08:08:29 +01:00
if (++x >= self.width) {
2021-11-10 00:37:45 +01:00
x = 0;
y++;
}
}
}
2021-11-17 14:51:05 +01:00
#endif
#ifdef RAYLIB
void
level_draw(void)
{
const struct Vec off = camera_offset();
2021-11-17 15:43:38 +01:00
Color colors[TILE_OOB] = {0};
colors[TILE_SOLID] = C_WHITE;
colors[TILE_SHATTERED] = GRAY;
colors[TILE_SPIKE_L] = SKYBLUE;
colors[TILE_SPIKE_R] = SKYBLUE;
colors[TILE_SPIKE_U] = SKYBLUE;
colors[TILE_SPIKE_D] = SKYBLUE;
2021-11-17 14:51:05 +01:00
int i;
int x = 0;
int y = 0;
for (i = 0; i < self.size; i++) {
const int tile = self.data[i];
if (tile && tile != TILE_THUNDER) {
const int sx1 = x * TILE_SIZE + off.x;
const int sy1 = y * TILE_SIZE + off.y;
const int sx2 = sx1 + TILE_SIZE - 1;
const int sy2 = sy1 + TILE_SIZE - 1;
2021-11-17 15:43:38 +01:00
drect(sx1, sy1, sx2, sy2, colors[tile]);
2021-11-17 14:51:05 +01:00
}
if (++x >= self.width) {
x = 0;
y++;
}
}
}
#endif
2021-11-10 06:15:41 +01:00
struct Vec
2021-11-10 08:08:29 +01:00
level_find(enum Tile seek)
2021-11-10 06:15:41 +01:00
{
int i;
2021-11-10 08:08:29 +01:00
for (i = 0; i < self.size; i++) {
const enum Tile tile = self.data[i];
2021-11-10 06:15:41 +01:00
if (tile == seek) {
2021-11-10 08:08:29 +01:00
return VEC(i % self.width * TILE_SIZE,
2021-11-10 14:05:00 +01:00
i / self.width * TILE_SIZE);
2021-11-10 06:15:41 +01:00
}
}
return VEC(0, 0);
}
2021-11-10 08:08:29 +01:00
enum Tile
level_get(int x, int y)
{
2021-11-11 16:59:37 +01:00
if (y < 0)
return TILE_NEXT;
if (x < 0 || x >= self.width || y >= self.height)
2021-11-10 08:08:29 +01:00
return TILE_OOB;
return self.data[x + y * self.width];
}
enum Tile
level_get_px(int x, int y)
{
return level_get(x / TILE_SIZE, y / TILE_SIZE);
}
void
level_set(int x, int y, enum Tile v)
{
if (x < 0 || y < 0 || x >= self.width || y >= self.height)
return;
2021-11-10 20:44:53 +01:00
self.data[x + y * self.width] = v;
2021-11-10 08:08:29 +01:00
}
void
level_set_px(int x, int y, enum Tile v)
{
level_set(x / TILE_SIZE, y / TILE_SIZE, v);
}
2021-11-11 07:09:56 +01:00
int
level_oob(int x, int y)
{
2021-11-11 16:59:37 +01:00
return level_get_px(x, y) == TILE_OOB;
2021-11-11 07:09:56 +01:00
}