From 415867403150b41fd3c7d43e1a64974c3cf470cd Mon Sep 17 00:00:00 2001 From: Joshalosh Date: Wed, 18 Oct 2023 10:28:15 +1100 Subject: [PATCH] fp: Add camera shake --- feely_pona.cpp | 25 ++++++++++++++++++++++++- feely_pona_game.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/feely_pona.cpp b/feely_pona.cpp index 0aa3f89..ebc7d0e 100644 --- a/feely_pona.cpp +++ b/feely_pona.cpp @@ -1996,6 +1996,24 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input if (entity->flags & FP_GameEntityFlag_CameraTracking) { FP_GameCamera *camera = &game->play.camera; + + // NOTE: calculate camera position based on camera shake + Dqn_V2 camera_position = camera->world_pos; + if (camera->shake_duration > 0) { + Dqn_f32 offset_x = (Dqn_PCG32_NextF32(&game->play.rng) * 1000 - 500) * camera->shake_intensity; + Dqn_f32 offset_y = (Dqn_PCG32_NextF32(&game->play.rng) * 1000 - 500) * camera->shake_intensity; + + camera_position.x += offset_x; + camera_position.y += offset_y; + + camera->shake_duration -= input->delta_s; + } + + Dqn_f64 camera_smoothing = 5.0f; + + camera->world_pos.x += (camera_position.x - camera->world_pos.x) * (camera_smoothing * input->delta_s); + camera->world_pos.y += (camera_position.y - camera->world_pos.y) * (camera_smoothing * input->delta_s); + camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, entity->handle) * camera->scale; } @@ -2247,12 +2265,17 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input defender->hp = defender->hp >= attacker->base_attack ? defender->hp - attacker->base_attack : 0; defender->hit_on_frame = game->play.update_counter; + if (game->play.player == defender->handle) { + game->play.camera.shake_intensity = 0.1f; + game->play.camera.shake_duration = 0.25f; + } + if (defender->hp <= 0) { if (!defender->is_dying) { FP_GameEntity *coin_receiver = FP_Game_GetEntity(game, attacker->projectile_owner); if (FP_Game_IsNilEntity(coin_receiver)) coin_receiver = attacker; - coin_receiver->coins += 1; + coin_receiver->coins += 2; } defender->is_dying = true; } diff --git a/feely_pona_game.h b/feely_pona_game.h index 736ee2f..d2c20e0 100644 --- a/feely_pona_game.h +++ b/feely_pona_game.h @@ -261,6 +261,8 @@ struct FP_GameCamera Dqn_V2 world_pos_target; Dqn_f32 rotate_rads; Dqn_V2 scale; + Dqn_f32 shake_intensity; + Dqn_f64 shake_duration; }; enum FP_GameAudio