Gold and exit lock

This commit is contained in:
KikooDX 2021-04-04 17:33:17 +02:00
parent ae1fbb12b5
commit 6bd8858e78
10 changed files with 67 additions and 3 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 901 B

After

Width:  |  Height:  |  Size: 698 B

View File

@ -17,4 +17,4 @@
#define AIR_JMP_SPD -3.0
#define JUMP_BUFFER 10
#define JUMP_GRACE 10
#define AIR_JUMPS 2
#define AIR_JUMPS 3

View File

@ -13,6 +13,8 @@ struct Level {
Tile data[LEVEL_WIDTH * LEVEL_HEIGHT];
int width;
int height;
int gold;
int exit_locked;
};
/* need to set global before call: level_id */

View File

@ -37,3 +37,4 @@ void player_collide(Tile collisions[COLLIDE_POINTS], int x, int y);
int player_collide_tile(Tile collisions[COLLIDE_POINTS], int x, int y,
Tile tile, int update);
int player_collide_solid(int x, int y);
int player_collide_sub(int x, int y, Tile sub, Tile rep);

View File

@ -8,5 +8,7 @@ enum {
TILE_SOLID,
TILE_START,
TILE_EXIT,
TILE_SWITCH,
TILE_GOLD,
TILE_LETAL,
};

View File

@ -2,4 +2,5 @@
# https://sr.ht/~kikoodx/sle
sle -tile-width 16 -tile-height 16 -level-width 25 -level-height 14 \
-editor-width 396 -editor-height 224 -editor-off-x -2 -editor-off-y 0 \
-editor-bg-color '#000000' -picker-bg-color '#000000' $@
-editor-bg-color '#000000' -picker-bg-color '#000000' \
-tileset assets/graphics/tileset.png $@

View File

@ -2,6 +2,7 @@
/* Copyright (C) 2021 KikooDX */
#include "level.h"
#include "tiles.h"
#include <gint/bfile.h>
#include <gint/std/stdlib.h>
#include <stdint.h>
@ -69,10 +70,23 @@ void level_load(void)
"file size doesn't make sense");
/* read file content */
level.gold = 0;
level.exit_locked = 0;
for (i = 0; i < level_size; i += 1) {
tile = read_merge_bytes(file, tile_size);
assert(tile != (Tile)-1, "can't read tile");
level.data[i] = tile;
/* special tiles */
switch (tile) {
case TILE_GOLD:
level.gold += 1;
break;
case TILE_SWITCH:
level.exit_locked = 1;
break;
default:
break;
}
}
/* close file */

View File

@ -9,6 +9,7 @@
extern struct Level level;
static Tile collide_single(int x, int y);
static int collide_sub_single(int x, int y, Tile sub, Tile rep);
void player_collide(Tile collisions[COLLIDE_POINTS], int x, int y)
{
@ -32,6 +33,17 @@ int player_collide_tile(Tile collisions[COLLIDE_POINTS], int x, int y,
return 0;
}
/* collide and replace tiles, return the number of tiles affecter */
int player_collide_sub(int x, int y, Tile sub, Tile rep)
{
const int rx = x + PLAYER_WIDTH - 1;
const int dy = y + PLAYER_HEIGHT - 1;
return collide_sub_single(x, y, sub, rep) +
collide_sub_single(rx, y, sub, rep) +
collide_sub_single(x, dy, sub, rep) +
collide_sub_single(rx, dy, sub, rep);
}
int player_collide_solid(int x, int y)
{
Tile collisions[COLLIDE_POINTS];
@ -47,3 +59,17 @@ static Tile collide_single(int x, int y)
return TILE_OOB;
return level.data[tx + ty * level.width];
}
/* try collide and replace tile at pixel position, return 1 if tile
* replaced */
static int collide_sub_single(int x, int y, Tile sub, Tile rep)
{
if (collide_single(x, y) == sub) {
const int tile_index =
(int)(x / TILE_WIDTH) +
(int)(y / TILE_WIDTH) * level.width;
level.data[tile_index] = rep;
return 1;
}
return 0;
}

View File

@ -2,9 +2,12 @@
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "level.h"
#include "player.h"
#include <gint/display.h>
extern struct Level level;
extern bopti_image_t bimg_player;
extern bopti_image_t bimg_burst;
@ -14,4 +17,5 @@ void player_draw(struct Player player)
if (player.air_state == AirRising &&
player.jumps_left < AIR_JUMPS)
dimage(player.x, player.y + PLAYER_HEIGHT, &bimg_burst);
dprint(2, 2, C_WHITE, "%d", level.gold);
}

View File

@ -2,12 +2,15 @@
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "level.h"
#include "player.h"
#include "tiles.h"
#include <gint/keyboard.h>
static void player_move(struct Player *player, int x, int y);
extern struct Level level;
/* return 1 if exit reached, -1 on death/reset and 0 otherwise */
int player_update(struct Player *player)
{
@ -101,8 +104,19 @@ int player_update(struct Player *player)
player_move(player, 0, spd_y);
player_move(player, spd_x, 0);
/* get gold ($$$) */
level.gold -= player_collide_sub(player->x, player->y,
TILE_GOLD, TILE_VOID);
/* unlock exit */
if (level.exit_locked &&
player_collide_sub(player->x, player->y, TILE_SWITCH,
TILE_VOID))
level.exit_locked = 0;
/* check for death and exit */
if (player_collide_tile(collisions, player->x, player->y,
if (!level.exit_locked &&
player_collide_tile(collisions, player->x, player->y,
TILE_EXIT, 1))
return 1;
if (player_collide_tile(collisions, player->x, player->y,