This commit is contained in:
1 parent
d1a56f77cc
commit
0e7aed40f1
21 files changed
+1253
-19
No files matched your search
@@ -1,9 +1,12 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use avian3d::prelude::{Physics, PhysicsTime};
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::{CursorGrabMode, CursorOptions, PrimaryWindow};
|
||||
use bevy_egui::PrimaryEguiContext;
|
||||
use game::player::PlayerCamera;
|
||||
use game::{GameInputEnabled, GameInputFocused, SimEnabled};
|
||||
use protocol::PlayerInputIntent;
|
||||
use protocol::{PlayerInputIntent, SIM_TICK_RATE_HZ};
|
||||
use sim::{Crouching, Grounded, JumpState, Player, PlayerVelocity};
|
||||
|
||||
use crate::camera::EditorCamera;
|
||||
@@ -195,6 +198,7 @@ pub(crate) fn sync_mode_resources_and_cameras(
|
||||
mut step: ResMut<PlayStepOnce>,
|
||||
mut input_enabled: ResMut<GameInputEnabled>,
|
||||
mut sim_enabled: ResMut<SimEnabled>,
|
||||
mut physics_time: ResMut<Time<Physics>>,
|
||||
mut editor_cameras: Query<&mut Camera, With<EditorCamera>>,
|
||||
mut player_cameras: Query<&mut Camera, (With<PlayerCamera>, Without<EditorCamera>)>,
|
||||
mut egui_cameras: Query<
|
||||
@@ -212,9 +216,11 @@ pub(crate) fn sync_mode_resources_and_cameras(
|
||||
let ejected = playing && *possession == PlayPossession::Ejected;
|
||||
let possessed = playing && *possession == PlayPossession::Possessed;
|
||||
|
||||
sim_enabled.0 = playing && (!paused.0 || step.0);
|
||||
let stepping = step.0;
|
||||
sim_enabled.0 = playing && (!paused.0 || stepping);
|
||||
input_enabled.0 = possessed && !paused.0;
|
||||
if step.0 {
|
||||
sync_editor_physics_clock(&mut physics_time, playing, paused.0, stepping);
|
||||
if stepping {
|
||||
step.0 = false;
|
||||
}
|
||||
|
||||
@@ -240,6 +246,22 @@ pub(crate) fn sync_mode_resources_and_cameras(
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_editor_physics_clock(
|
||||
physics_time: &mut Time<Physics>,
|
||||
playing: bool,
|
||||
paused: bool,
|
||||
stepping: bool,
|
||||
) {
|
||||
if playing && !paused {
|
||||
physics_time.unpause();
|
||||
return;
|
||||
}
|
||||
physics_time.pause();
|
||||
if playing && stepping {
|
||||
physics_time.advance_by(Duration::from_secs_f64(1.0 / SIM_TICK_RATE_HZ as f64));
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_play_input_focus(
|
||||
mode: Res<State<EditorMode>>,
|
||||
possession: Res<PlayPossession>,
|
||||
@@ -414,3 +436,29 @@ pub fn toggle_play_possession_world(world: &mut World) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn editor_physics_clock_tracks_edit_play_pause_and_single_step() {
|
||||
let mut time = Time::<Physics>::default();
|
||||
|
||||
sync_editor_physics_clock(&mut time, false, false, false);
|
||||
assert!(time.is_paused());
|
||||
|
||||
sync_editor_physics_clock(&mut time, true, false, false);
|
||||
assert!(!time.is_paused());
|
||||
|
||||
sync_editor_physics_clock(&mut time, true, true, false);
|
||||
assert!(time.is_paused());
|
||||
|
||||
sync_editor_physics_clock(&mut time, true, true, true);
|
||||
assert!(time.is_paused());
|
||||
assert_eq!(
|
||||
time.delta(),
|
||||
Duration::from_secs_f64(1.0 / SIM_TICK_RATE_HZ as f64)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user