WIP time trial: spawn target in front + implement canceling race
This commit is contained in:
parent
afeba6b63d
commit
d856c8487a
65
src/game.rs
65
src/game.rs
|
@ -722,16 +722,23 @@ fn handle_race(
|
||||||
mut settings: ResMut<Settings>,
|
mut settings: ResMut<Settings>,
|
||||||
mut race: ResMut<RaceState>,
|
mut race: ResMut<RaceState>,
|
||||||
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
||||||
q_player: Query<(&Position, &LinearVelocity), With<PlayerCamera>>,
|
q_player: Query<(&Position, &LinearVelocity, &Transform), With<PlayerCamera>>,
|
||||||
mut q_racetarget: Query<
|
mut q_racetarget: Query<
|
||||||
(Entity, &mut Position, &mut LinearVelocity, &mut Visibility),
|
(Entity, &mut Position, &mut LinearVelocity, &mut Visibility),
|
||||||
(With<RaceTarget>, Without<PlayerCamera>),
|
(With<RaceTarget>, Without<PlayerCamera>),
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
|
let mut deinitialize = false;
|
||||||
if !settings.race_active {
|
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
|
val
|
||||||
} else {
|
} else {
|
||||||
error!("No player found in handle_race!");
|
error!("No player found in handle_race!");
|
||||||
|
@ -745,31 +752,39 @@ fn handle_race(
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut spawn_target = false;
|
if !deinitialize {
|
||||||
|
let mut spawn_target = false;
|
||||||
|
|
||||||
if !race.initialized {
|
if !race.initialized {
|
||||||
log.warning("Time Trial Race START!".to_string());
|
log.warning("Time Trial Race START!".to_string());
|
||||||
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Honk));
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Honk));
|
||||||
race.score = 0;
|
race.score = 0;
|
||||||
race.initialized = true;
|
race.initialized = true;
|
||||||
spawn_target = 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 {
|
if deinitialize {
|
||||||
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));
|
|
||||||
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::PowerDown));
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::PowerDown));
|
||||||
race.initialized = false;
|
race.initialized = false;
|
||||||
settings.race_active = false;
|
settings.race_active = false;
|
||||||
|
|
|
@ -787,7 +787,7 @@ pub fn handle_input(
|
||||||
ew_updatemenu.send(UpdateMenuEvent);
|
ew_updatemenu.send(UpdateMenuEvent);
|
||||||
}
|
}
|
||||||
MenuAction::Race => {
|
MenuAction::Race => {
|
||||||
settings.race_active = true;
|
settings.race_active ^= true;
|
||||||
ew_game.send(GameEvent::SetMenu(Turn::Off));
|
ew_game.send(GameEvent::SetMenu(Turn::Off));
|
||||||
ew_updatemenu.send(UpdateMenuEvent);
|
ew_updatemenu.send(UpdateMenuEvent);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue