fp: Add camera shake

This commit is contained in:
Joshalosh 2023-10-18 10:28:15 +11:00
parent 1de986b6ac
commit 4158674031
2 changed files with 26 additions and 1 deletions

View File

@ -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;
}

View File

@ -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