crystal-tower/src/level.c

130 lines
2.3 KiB
C
Raw 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-11 07:09:56 +01:00
#include "particles.h"
#include "player.h"
2021-11-10 06:15:41 +01:00
#include "tile.h"
#include "vec.h"
2021-11-10 00:37:45 +01:00
#include <gint/display.h>
#include <stdlib.h>
2021-11-10 08:08:29 +01:00
static struct Level self;
2021-11-11 16:59:37 +01:00
extern struct LevelBin lvl_0;
extern struct LevelBin lvl_2;
static const struct LevelBin *levels[] = {&lvl_0, &lvl_2, NULL};
2021-11-10 08:08:29 +01:00
void
2021-11-11 16:59:37 +01:00
level_load(int id)
2021-11-10 00:37:45 +01:00
{
2021-11-11 16:59:37 +01:00
const struct LevelBin *s = levels[id];
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 16:59:37 +01:00
self.id = 0;
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-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)
{
if (levels[self.id + 1])
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
}
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++) {
2021-11-10 12:10:04 +01:00
const int sx = x * TILE_SIZE + off.x;
const int sy = y * TILE_SIZE + off.y;
2021-11-10 08:08:29 +01:00
const int tile = self.data[i];
2021-11-10 00:37:45 +01:00
const int rx = tile % tileset_width * TILE_SIZE;
const int ry = tile / tileset_width * TILE_SIZE;
2021-11-11 16:59:37 +01:00
if (tile && tile != TILE_OOB)
dsubimage(sx, sy, &bimg_tileset, rx, ry, TILE_SIZE,
TILE_SIZE, DIMAGE_NONE);
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-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
}