mirror of https://git.sr.ht/~kikoodx/crystal-tower
code mvp done lezgo
This commit is contained in:
parent
31536578fa
commit
44a944c00c
|
@ -24,7 +24,8 @@ set(SOURCES
|
|||
)
|
||||
|
||||
set(LEVELS
|
||||
lvl/test.kble
|
||||
lvl/0.kble
|
||||
lvl/2.kble
|
||||
)
|
||||
|
||||
set(ASSETS
|
||||
|
|
|
@ -15,11 +15,12 @@ struct Level {
|
|||
int height;
|
||||
int size;
|
||||
char *data;
|
||||
struct LevelBin *source;
|
||||
int id;
|
||||
};
|
||||
|
||||
void level_load(struct LevelBin *);
|
||||
void level_load(int id);
|
||||
void level_reload(void);
|
||||
void level_next(void);
|
||||
void level_free(void);
|
||||
void level_draw(void);
|
||||
struct Vec level_find(enum Tile);
|
||||
|
|
|
@ -5,5 +5,6 @@ enum Tile {
|
|||
TILE_SOLID,
|
||||
TILE_PLAYER,
|
||||
TILE_SHATTERED,
|
||||
TILE_OOB = TILE_VOID
|
||||
TILE_OOB,
|
||||
TILE_NEXT,
|
||||
};
|
||||
|
|
|
@ -3,3 +3,4 @@
|
|||
int sign(int);
|
||||
float signf(float);
|
||||
int abs(int);
|
||||
float absf(float);
|
||||
|
|
Binary file not shown.
BIN
res/tileset.png
BIN
res/tileset.png
Binary file not shown.
Before Width: | Height: | Size: 9.8 KiB After Width: | Height: | Size: 11 KiB |
35
src/level.c
35
src/level.c
|
@ -9,26 +9,39 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
static struct Level self;
|
||||
extern struct LevelBin lvl_0;
|
||||
extern struct LevelBin lvl_2;
|
||||
static const struct LevelBin *levels[] = {&lvl_0, &lvl_2, NULL};
|
||||
|
||||
void
|
||||
level_load(struct LevelBin *s)
|
||||
level_load(int id)
|
||||
{
|
||||
const struct LevelBin *s = levels[id];
|
||||
int i = s->width * s->height;
|
||||
self.width = s->width;
|
||||
self.height = s->height;
|
||||
self.size = i;
|
||||
self.data = malloc(i);
|
||||
self.source = s;
|
||||
self.id = 0;
|
||||
while (i-- > 0)
|
||||
self.data[i] = s->data[i];
|
||||
player_init(level_find(TILE_PLAYER));
|
||||
particles_init();
|
||||
}
|
||||
|
||||
void
|
||||
level_reload(void)
|
||||
{
|
||||
level_load(self.source);
|
||||
player_init(level_find(TILE_PLAYER));
|
||||
particles_init();
|
||||
level_load(self.id);
|
||||
}
|
||||
|
||||
void
|
||||
level_next(void)
|
||||
{
|
||||
if (levels[self.id + 1])
|
||||
level_load(self.id + 1);
|
||||
else
|
||||
level_reload();
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -54,8 +67,9 @@ level_draw(void)
|
|||
const int rx = tile % tileset_width * TILE_SIZE;
|
||||
const int ry = tile / tileset_width * TILE_SIZE;
|
||||
|
||||
dsubimage(sx, sy, &bimg_tileset, rx, ry, TILE_SIZE, TILE_SIZE,
|
||||
DIMAGE_NONE);
|
||||
if (tile && tile != TILE_OOB)
|
||||
dsubimage(sx, sy, &bimg_tileset, rx, ry, TILE_SIZE,
|
||||
TILE_SIZE, DIMAGE_NONE);
|
||||
|
||||
if (++x >= self.width) {
|
||||
x = 0;
|
||||
|
@ -81,7 +95,9 @@ level_find(enum Tile seek)
|
|||
enum Tile
|
||||
level_get(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= self.width || y >= self.height)
|
||||
if (y < 0)
|
||||
return TILE_NEXT;
|
||||
if (x < 0 || x >= self.width || y >= self.height)
|
||||
return TILE_OOB;
|
||||
return self.data[x + y * self.width];
|
||||
}
|
||||
|
@ -109,6 +125,5 @@ level_set_px(int x, int y, enum Tile v)
|
|||
int
|
||||
level_oob(int x, int y)
|
||||
{
|
||||
return x < 0 || x >= self.width * TILE_SIZE || y < 0 ||
|
||||
y >= self.height * TILE_SIZE;
|
||||
return level_get_px(x, y) == TILE_OOB;
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
#include <gint/display.h>
|
||||
#include <gint/timer.h>
|
||||
|
||||
extern struct LevelBin lvl_test;
|
||||
|
||||
static void update(void);
|
||||
static void draw(void);
|
||||
static int callback(volatile int *);
|
||||
|
@ -26,7 +24,7 @@ main(void)
|
|||
GINT_CALL(callback, &has_ticked));
|
||||
timer_start(timer);
|
||||
input_init();
|
||||
level_load(&lvl_test);
|
||||
level_load(0);
|
||||
camera_init(player_pos());
|
||||
|
||||
while (!input_down(K_EXIT)) {
|
||||
|
|
36
src/player.c
36
src/player.c
|
@ -14,6 +14,7 @@ static int jump(int on_ground);
|
|||
static void walljump(void);
|
||||
static void death(void);
|
||||
static int oob(int x, int y);
|
||||
static int collide_tile(int x, int y, enum Tile);
|
||||
static int collide_solid(int x, int y);
|
||||
|
||||
void
|
||||
|
@ -75,6 +76,13 @@ player_update(void)
|
|||
/* death */
|
||||
if (oob(self.pos.x, self.pos.y)) {
|
||||
death();
|
||||
return;
|
||||
}
|
||||
|
||||
/* next level */
|
||||
if (collide_tile(self.pos.x, self.pos.y, TILE_NEXT)) {
|
||||
level_next();
|
||||
return;
|
||||
}
|
||||
|
||||
player_move(player_update_rem());
|
||||
|
@ -154,6 +162,7 @@ player_update_rem(void)
|
|||
void
|
||||
player_move(struct Vec spd)
|
||||
{
|
||||
int vertical_shatter = 0;
|
||||
float sign_x = signf(self.spd.x);
|
||||
const float sign_y = signf(self.spd.y);
|
||||
if (!sign_x && !sign_y)
|
||||
|
@ -170,23 +179,38 @@ player_move(struct Vec spd)
|
|||
|
||||
self.pos.y += spd.y;
|
||||
if (collide_solid(self.pos.x, self.pos.y)) {
|
||||
vertical_shatter = (absf(self.spd.y) > MAX_WALK_SPEED)
|
||||
? (sign(self.spd.y))
|
||||
: (0);
|
||||
self.spd.y = 0.0f;
|
||||
self.rem.y = 0.0f;
|
||||
}
|
||||
while (collide_solid(self.pos.x, self.pos.y)) {
|
||||
self.pos.y -= sign_y;
|
||||
}
|
||||
if (vertical_shatter == -1) {
|
||||
struct Vec shatter_pos = VEC(self.pos.x, self.pos.y);
|
||||
shatter_pos.y +=
|
||||
(vertical_shatter == 1) ? (PLAYER_HEIGHT) : (-1);
|
||||
shatter(shatter_pos.x, shatter_pos.y);
|
||||
shatter_pos.x += PLAYER_WIDTH - 1;
|
||||
shatter(shatter_pos.x, shatter_pos.y);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
collide_tile(int x, int y, enum Tile t)
|
||||
{
|
||||
const int x2 = x + PLAYER_WIDTH - 1;
|
||||
const int y2 = y + PLAYER_HEIGHT - 1;
|
||||
return level_get_px(x, y) == t || level_get_px(x, y2) == t ||
|
||||
level_get_px(x2, y) == t || level_get_px(x2, y2) == t;
|
||||
}
|
||||
|
||||
static int
|
||||
collide_solid(int x, int y)
|
||||
{
|
||||
const int x2 = x + PLAYER_WIDTH - 1;
|
||||
const int y2 = y + PLAYER_HEIGHT - 1;
|
||||
return level_get_px(x, y) == TILE_SOLID ||
|
||||
level_get_px(x, y2) == TILE_SOLID ||
|
||||
level_get_px(x2, y) == TILE_SOLID ||
|
||||
level_get_px(x2, y2) == TILE_SOLID;
|
||||
return collide_tile(x, y, TILE_SOLID);
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
|
@ -9,7 +9,7 @@ sign(int n)
|
|||
float
|
||||
signf(float n)
|
||||
{
|
||||
return (n > 0.0) - (n < 0.0);
|
||||
return (n > 0.0f) - (n < 0.0f);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -17,3 +17,9 @@ abs(int n)
|
|||
{
|
||||
return (n > 0) ? (n) : (-n);
|
||||
}
|
||||
|
||||
float
|
||||
absf(float n)
|
||||
{
|
||||
return (n > 0.0f) ? (n) : (-n);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue