Improve tilemap usage

This commit is contained in:
Juhani Krekelä 2022-01-15 15:30:27 +00:00
parent e19444181a
commit d0eb8dab8e
1 changed files with 35 additions and 18 deletions

View File

@ -41,7 +41,7 @@ static char tilemap[] = " "
" g"
" g "
" g "
" gdg "
"@ gdg "
"ggggdddggg";
static uint32_t main_window = 0;
@ -74,14 +74,7 @@ static void on_keyboard(void *context, uint32_t window, uint32_t codepoint) {
case -KBKEY_RIGHT: right_held = false; break;
case KBKEY_SPACE: jump_held = true; break;
case -KBKEY_SPACE: jump_held = false; break;
case KBKEY_Q: jump_maxhold += 0.1; break;
case KBKEY_A: jump_maxhold -= 0.1; break;
case KBKEY_W: jump_maxheight += 0.1; break;
case KBKEY_S: jump_maxheight -= 0.1; break;
case KBKEY_E: jump_duration += 0.1; break;
case KBKEY_D: jump_duration -= 0.1; break;
}
//printf("hold %i height %i duration %i\n", (int)(jump_maxhold * 10), (int)(jump_maxheight * 10), (int)(jump_duration * 10));
}
static void on_quit(void *context, uint32_t window) {
@ -110,22 +103,44 @@ static char sample_tilemap(size_t x, size_t y) {
return tilemap[ty * TILES + tx];
}
static bool solid_tile(size_t x, size_t y) {
char tile = sample_tilemap(x, y);
return tile != ' ' && tile != '!' && tile != '@';
}
static bool jumping = false;
static bool on_ground = false;
static double jump_start_y;
static double in_jump;
static double player_x = 0, player_y = 80;
static double player_x, player_y;
static double player_dx, player_dy;
static void initialize(void) {
jumping = false;
on_ground = false;
player_dx = 0;
player_x = 0;
player_y = 0;
for (size_t ty = 0; ty < TILES; ty++) {
for (size_t tx = 0; tx < TILES; tx++) {
if (tilemap[ty * TILES + tx] == '@') {
player_x = tx * TILE_SIDE;
player_y = ty * TILE_SIDE;
}
}
}
}
static void update(struct timespec now, struct timespec dt_timespec) {
(void) now;
double dt = timespec2double(dt_timespec);
player_x += player_dx * dt;
//player_y += player_dy * dt;
if (left_held) player_dx = -30;
else if (right_held) player_dx = 30;
else player_dx = 0;
(void) jump_maxhold;
if (on_ground && jump_held && !jumping) {
jumping = true;
in_jump = 0;
@ -194,15 +209,15 @@ static void update(struct timespec now, struct timespec dt_timespec) {
size_t right_cx = px + TILE_SIDE;
size_t leftright_top_cy = py;
size_t leftright_bottom_cy = py + TILE_SIDE - 3; //XXX: proper handling of snapping
if ((sample_tilemap(left_cx, leftright_top_cy) != ' ' ||
sample_tilemap(left_cx, leftright_bottom_cy) != ' ') &&
if ((solid_tile(left_cx, leftright_top_cy) ||
solid_tile(left_cx, leftright_bottom_cy)) &&
player_dx < 0) {
size_t snapped_px = (px + TILE_SIDE - 1) / TILE_SIDE * TILE_SIDE;
player_x = snapped_px;
player_dx = 0;
}
if ((sample_tilemap(right_cx, leftright_top_cy) != ' ' ||
sample_tilemap(right_cx, leftright_bottom_cy) != ' ') &&
if ((solid_tile(right_cx, leftright_top_cy) ||
solid_tile(right_cx, leftright_bottom_cy)) &&
player_dx > 0) {
size_t snapped_px = px / TILE_SIDE * TILE_SIDE;
player_x = snapped_px;
@ -217,8 +232,8 @@ static void update(struct timespec now, struct timespec dt_timespec) {
size_t bottom_cy = py + TILE_SIDE;
on_ground = false;
if (sample_tilemap(topbottom_left_cx, bottom_cy) != ' ' ||
sample_tilemap(topbottom_right_cx, bottom_cy) != ' ') {
if (solid_tile(topbottom_left_cx, bottom_cy) ||
solid_tile(topbottom_right_cx, bottom_cy)) {
on_ground = true;
if (player_dy > 0) {
jumping = false;
@ -226,8 +241,8 @@ static void update(struct timespec now, struct timespec dt_timespec) {
player_y = snapped_py;
}
}
if ((sample_tilemap(topbottom_left_cx, top_cy) != ' ' ||
sample_tilemap(topbottom_right_cx, top_cy) != ' ') &&
if ((solid_tile(topbottom_left_cx, top_cy) ||
solid_tile(topbottom_right_cx, top_cy)) &&
player_dy < 0) {
size_t snapped_py = (py + TILE_SIDE - 1) / TILE_SIDE * TILE_SIDE;
player_y = snapped_py;
@ -302,6 +317,8 @@ int main(int argc, char *argv[]) {
struct timespec last_frame;
clock_gettime(CLOCK_MONOTONIC, &last_frame);
initialize();
int fps = 0;
while (game_running) {
struct timespec now;