From d856c8487a4961a9833be5fd165bd782a18783b4 Mon Sep 17 00:00:00 2001 From: yuni Date: Mon, 18 Nov 2024 04:02:12 +0100 Subject: [PATCH] WIP time trial: spawn target in front + implement canceling race --- src/game.rs | 65 ++++++++++++++++++++++++++++++++--------------------- src/menu.rs | 2 +- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/game.rs b/src/game.rs index 86cefa3..612e0ba 100644 --- a/src/game.rs +++ b/src/game.rs @@ -722,16 +722,23 @@ fn handle_race( mut settings: ResMut, mut race: ResMut, mut ew_sfx: EventWriter, - q_player: Query<(&Position, &LinearVelocity), With>, + q_player: Query<(&Position, &LinearVelocity, &Transform), With>, mut q_racetarget: Query< (Entity, &mut Position, &mut LinearVelocity, &mut Visibility), (With, Without), >, ) { + let mut deinitialize = false; if !settings.race_active { - return; + if race.initialized { + log.warning(format!("Stopped race! Final score: {}", race.score)); + deinitialize = true; + } else { + return; + } } - let (player_pos, player_v) = if let Ok(val) = q_player.get_single() { + + let (player_pos, player_v, player_trans) = if let Ok(val) = q_player.get_single() { val } else { error!("No player found in handle_race!"); @@ -745,31 +752,39 @@ fn handle_race( return; }; - let mut spawn_target = false; + if !deinitialize { + let mut spawn_target = false; - if !race.initialized { - log.warning("Time Trial Race START!".to_string()); - ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Honk)); - race.score = 0; - race.initialized = true; - spawn_target = true; + if !race.initialized { + log.warning("Time Trial Race START!".to_string()); + ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Honk)); + race.score = 0; + race.initialized = true; + spawn_target = true; + } + + if player_pos.distance(target_pos.0) < RACE_TARGET_RADIUS { + race.score += 1; + spawn_target = true; + } + + if spawn_target { + race.timeout = time.elapsed_seconds_f64() + 5.0; + let mut delta = DVec3::new(0.0, 0.0, 100.0); + delta = (player_trans.rotation * delta.as_vec3()).as_dvec3(); + *target_pos = Position(player_pos.0 + delta); + *target_v = *player_v; + *target_vis = Visibility::Inherited; + commands.entity(target_entity).insert(RigidBody::Kinematic); + } + + if race.timeout <= time.elapsed_seconds_f64() { + log.warning(format!("GAME OVER! Final score: {}", race.score)); + deinitialize = true; + } } - if player_pos.distance(target_pos.0) < RACE_TARGET_RADIUS { - race.score += 1; - spawn_target = true; - } - - if spawn_target { - race.timeout = time.elapsed_seconds_f64() + 5.0; - *target_pos = Position(player_pos.0 + DVec3::new(100.0, 0.0, 0.0)); - *target_v = *player_v; - *target_vis = Visibility::Inherited; - commands.entity(target_entity).insert(RigidBody::Kinematic); - } - - if race.timeout <= time.elapsed_seconds_f64() { - log.warning(format!("GAME OVER! Final score: {}", race.score)); + if deinitialize { ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::PowerDown)); race.initialized = false; settings.race_active = false; diff --git a/src/menu.rs b/src/menu.rs index 3e8af28..9e55fdd 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -787,7 +787,7 @@ pub fn handle_input( ew_updatemenu.send(UpdateMenuEvent); } MenuAction::Race => { - settings.race_active = true; + settings.race_active ^= true; ew_game.send(GameEvent::SetMenu(Turn::Off)); ew_updatemenu.send(UpdateMenuEvent); }