Skip to content

Commit

Permalink
Merge pull request #67 from johanhelsing/bevy-time
Browse files Browse the repository at this point in the history
Use Bevy abstractions instead of using `instant` directly
  • Loading branch information
gschup authored Oct 15, 2023
2 parents a959d7b + db86c96 commit 88e7bf5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ wasm-bindgen = ["instant/wasm-bindgen", "ggrs/wasm-bindgen"]
[dependencies]
bevy = { version = "0.11", default-features = false}
bytemuck = { version = "1.7", features=["derive"]}
instant = "0.1"
instant = { version = "0.1", optional = true }
log = "0.4"
ggrs = { version= "0.9.4", features=["sync-send"]}
#ggrs = { git = "https://github.com/gschup/ggrs", features=["sync-send"]}
Expand Down
12 changes: 6 additions & 6 deletions src/ggrs_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ use crate::{
FixedTimestepData, GgrsSchedule, LocalInputs, LocalPlayers, PlayerInputs, ReadInputs,
RollbackFrameCount, RollbackTypeRegistry, Session,
};
use bevy::prelude::*;
use bevy::{prelude::*, utils::Duration};
use ggrs::{
Config, GGRSError, GGRSRequest, GameStateCell, InputStatus, P2PSession, SessionState,
SpectatorSession, SyncTestSession,
};
use instant::{Duration, Instant};

pub(crate) fn run<T: Config>(world: &mut World) {
let mut time_data = world
.remove_resource::<FixedTimestepData>()
.expect("failed to extract GGRS FixedTimeStepData");

// get delta time from last run() call and accumulate it
let delta = Instant::now().duration_since(time_data.last_update);
let delta = world
.get_resource::<Time>()
.expect("Time resource not found, did you remove it?")
.delta();

let mut fps_delta = 1. / time_data.fps as f64;
if time_data.run_slow {
fps_delta *= 1.1;
}
time_data.accumulator = time_data.accumulator.saturating_add(delta);
time_data.last_update = Instant::now();

// no matter what, poll remotes and send responses
if let Some(mut session) = world.get_resource_mut::<Session<T>>() {
Expand Down Expand Up @@ -57,7 +58,6 @@ pub(crate) fn run<T: Config>(world: &mut World) {
Some(Session::Spectator(s)) => run_spectator(world, s),
_ => {
// No session has been started yet, reset time data and snapshots
time_data.last_update = Instant::now();
time_data.accumulator = Duration::ZERO;
time_data.run_slow = false;
world.insert_resource(LocalPlayers::default());
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use bevy::{
ecs::schedule::{LogLevel, ScheduleBuildSettings, ScheduleLabel},
prelude::*,
reflect::{FromType, GetTypeRegistration, TypeRegistry, TypeRegistryInternal},
utils::HashMap,
utils::{Duration, HashMap},
};
use ggrs::{Config, InputStatus, P2PSession, PlayerHandle, SpectatorSession, SyncTestSession};
use instant::{Duration, Instant};
use parking_lot::RwLock;
use std::{marker::PhantomData, sync::Arc};
use world_snapshot::RollbackSnapshots;
Expand Down Expand Up @@ -50,8 +49,6 @@ pub struct PlayerInputs<T: Config>(Vec<(T::Input, InputStatus)>);
struct FixedTimestepData {
/// fixed FPS our logic is running with
pub fps: usize,
/// internal time control variables
last_update: Instant,
/// accumulated time. once enough time has been accumulated, an update is executed
accumulator: Duration,
/// boolean to see if we should run slow to let remote clients catch up
Expand All @@ -62,7 +59,6 @@ impl Default for FixedTimestepData {
fn default() -> Self {
Self {
fps: DEFAULT_FPS,
last_update: Instant::now(),
accumulator: Duration::ZERO,
run_slow: false,
}
Expand Down
7 changes: 4 additions & 3 deletions tests/entity_mapping.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bevy::{prelude::*, utils::HashMap};

use bevy::{
prelude::*,
utils::{Duration, HashMap},
};
use bevy_ggrs::*;
use ggrs::*;
use instant::Duration;

pub struct GgrsConfig;
impl Config for GgrsConfig {
Expand Down

0 comments on commit 88e7bf5

Please sign in to comment.