From d0eb8dab8e695a281d2488e8d9f97ef0e8c1a8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 15 Jan 2022 15:30:27 +0000 Subject: [PATCH] Improve tilemap usage --- platformer.c | 53 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/platformer.c b/platformer.c index f5d5d16..cc04753 100644 --- a/platformer.c +++ b/platformer.c @@ -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;