diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d55760..8ade427 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ set(SOURCES src/main.c src/util.c src/level/load.c + src/level/update.c src/level/draw.c src/level/get_tile.c src/level/is_water.c diff --git a/assets/graphics/water.png b/assets/graphics/water.png index ed474a5..0ab2f8b 100644 Binary files a/assets/graphics/water.png and b/assets/graphics/water.png differ diff --git a/include/conf.h b/include/conf.h index d84c8c4..4f471eb 100644 --- a/include/conf.h +++ b/include/conf.h @@ -19,6 +19,7 @@ #define JUMP_SPD -4.0 #define AIR_JMP_SPD -3.0 #define SWIM_SPD -1.0 +#define SWIM_OUT_SPD -1.6 #define BOUNCE_SPD -5.0 #define JUMP_BUFFER 10 #define JUMP_GRACE 6 @@ -26,4 +27,6 @@ #define BURST_BOOST 0.5 #define V_TRANS_SPD (1.0 / 20.0) #define H_TRANS_SPD (1.0 / 20.0) +#define WATER_FLOW 16 +#define WATER_FRAMES 4 /* #define RECORDING */ diff --git a/include/level.h b/include/level.h index e8eaf7f..caf168f 100644 --- a/include/level.h +++ b/include/level.h @@ -22,6 +22,7 @@ struct Level { Tile data[LEVEL_WIDTH * LEVEL_HEIGHT]; struct VisualTile visual_data[LEVEL_WIDTH * LEVEL_HEIGHT]; int water_level; + int water_timer; int width; int height; int gold; @@ -34,6 +35,7 @@ extern struct Level level; /* need to set global before call: level_id */ void level_load_bfile(void); void level_load_binary(void); +void level_update(void); void level_draw(void); Tile level_get_tile(int x, int y); int level_is_water(int x, int y); diff --git a/include/player.h b/include/player.h index 1a8a1d6..0d1f9ff 100644 --- a/include/player.h +++ b/include/player.h @@ -26,6 +26,7 @@ struct Player { int jump_buffer; int jump_grace; int jumps_left; + int was_in_water; enum AirState air_state; /* animations */ struct Particle anim; diff --git a/src/level/draw.c b/src/level/draw.c index 6dd65cb..5829516 100644 --- a/src/level/draw.c +++ b/src/level/draw.c @@ -41,6 +41,9 @@ water_draw(void) while (x-- > 0) { const Tile tile = level_get_tile(x, y); if (tile != TILE_SOLID) - dimage(x * TILE_WIDTH, y * TILE_HEIGHT, &bimg_water); + dsubimage( + x * TILE_WIDTH, y * TILE_HEIGHT, &bimg_water, + (int)(level.water_timer / WATER_FLOW) * TILE_WIDTH, + 0, TILE_WIDTH, bimg_water.height, DIMAGE_NOCLIP); } } diff --git a/src/level/load.c b/src/level/load.c index 5f22090..17b3f82 100644 --- a/src/level/load.c +++ b/src/level/load.c @@ -118,6 +118,7 @@ level_load_post(void) /* disable water by default */ level.water_level = -1; + level.water_timer = 0; y = level.height; while (y-- > 0) { diff --git a/src/level/update.c b/src/level/update.c new file mode 100644 index 0000000..2f728f0 --- /dev/null +++ b/src/level/update.c @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ + +#include "conf.h" +#include "level.h" + +void +level_update(void) +{ + level.water_timer += 1; + if (level.water_timer == WATER_FRAMES * WATER_FLOW) + level.water_timer = 0; +} diff --git a/src/main.c b/src/main.c index 4b5dfb1..5b7488c 100644 --- a/src/main.c +++ b/src/main.c @@ -157,6 +157,7 @@ main(void) particles_update(); player_return_code = player_update(&player, input); + level_update(); switch (player_return_code) { case -1: game_state = GamePause; diff --git a/src/player/init.c b/src/player/init.c index 6f75680..fe5268a 100644 --- a/src/player/init.c +++ b/src/player/init.c @@ -26,6 +26,7 @@ player_init(void) .jump_buffer = 0, .jump_grace = 0, .jumps_left = 0, + .was_in_water = 0, .air_state = AirNeutral, .trail_state = 0, .blink_timer = BLINK_DELAY, diff --git a/src/player/update.c b/src/player/update.c index bb13ad7..914b673 100644 --- a/src/player/update.c +++ b/src/player/update.c @@ -106,9 +106,12 @@ player_update(struct Player *restrict player, struct Input input) } /* swim */ - if (k_jump && in_water) { + if (k_jump && (player->was_in_water || in_water)) { jumped = 1; - player->spd_y = SWIM_SPD; + if (in_water) + player->spd_y = SWIM_SPD; + else + player->spd_y = SWIM_OUT_SPD; player->air_state = AirRising; } @@ -207,6 +210,8 @@ player_update(struct Player *restrict player, struct Input input) if (dir_x) player->anim.flip_h = dir_x == -1; + player->was_in_water = in_water; + return 0; } diff --git a/src/trail/update.c b/src/trail/update.c index 929afa0..f96b0e2 100644 --- a/src/trail/update.c +++ b/src/trail/update.c @@ -15,7 +15,8 @@ void trail_update(struct Player player) { int i = TRAIL_LIFE; - color_t color = player.trail_state ? ZX_WHITE : ZX_GRAY; + color_t color = ZX_WHITE; + /* color = player.trail_state ? ZX_WHITE : ZX_GRAY; */ if (player_collide_water(player)) color = ZX_CYAN;