use crate::{clocksync::ClockSyncer, network_resource::NetworkResource, world::World, Config};
mod syncing_clock;
pub use syncing_clock::SyncingClock;
mod syncing_initial_state;
pub use syncing_initial_state::SyncingInitialState;
mod ready;
pub use ready::Ready;
use super::ActiveClient;
#[derive(Debug)]
pub(crate) enum StageOwned<WorldType: World> {
SyncingClock(ClockSyncer),
SyncingInitialState(ActiveClient<WorldType>),
Ready(ActiveClient<WorldType>),
}
impl<WorldType: World> StageOwned<WorldType> {
pub fn update<NetworkResourceType: NetworkResource<WorldType>>(
&mut self,
delta_seconds: f64,
seconds_since_startup: f64,
config: &Config,
net: &mut NetworkResourceType,
) {
let should_transition = match self {
StageOwned::SyncingClock(clocksyncer) => {
clocksyncer.update(delta_seconds, seconds_since_startup, net);
clocksyncer.is_ready()
}
StageOwned::SyncingInitialState(client) => {
client.update(delta_seconds, seconds_since_startup, net);
client.is_ready()
}
StageOwned::Ready(client) => {
client.update(delta_seconds, seconds_since_startup, net);
false
}
};
if should_transition {
let config = config.clone();
take_mut::take(self, |stage| match stage {
StageOwned::SyncingClock(clocksyncer) => StageOwned::SyncingInitialState(
ActiveClient::new(seconds_since_startup, config, clocksyncer),
),
StageOwned::SyncingInitialState(client) => StageOwned::Ready(client),
StageOwned::Ready(_) => unreachable!(),
});
}
}
}
impl<'a, WorldType: World> From<&'a StageOwned<WorldType>> for Stage<'a, WorldType> {
fn from(stage: &'a StageOwned<WorldType>) -> Stage<'a, WorldType> {
match stage {
StageOwned::SyncingClock(clocksyncer) => Stage::SyncingClock(clocksyncer.into()),
StageOwned::SyncingInitialState(active_client) => {
Stage::SyncingInitialState(active_client.into())
}
StageOwned::Ready(active_client) => Stage::Ready(active_client.into()),
}
}
}
impl<'a, WorldType: World> From<&'a mut StageOwned<WorldType>> for StageMut<'a, WorldType> {
fn from(stage: &'a mut StageOwned<WorldType>) -> StageMut<'a, WorldType> {
match stage {
StageOwned::SyncingClock(clocksyncer) => StageMut::SyncingClock(clocksyncer.into()),
StageOwned::SyncingInitialState(active_client) => {
StageMut::SyncingInitialState(active_client.into())
}
StageOwned::Ready(active_client) => StageMut::Ready(active_client.into()),
}
}
}
#[derive(Debug)]
pub enum Stage<'a, WorldType: World> {
SyncingClock(SyncingClock<&'a ClockSyncer>),
SyncingInitialState(SyncingInitialState<WorldType, &'a ActiveClient<WorldType>>),
Ready(Ready<WorldType, &'a ActiveClient<WorldType>>),
}
#[derive(Debug)]
pub enum StageMut<'a, WorldType: World> {
SyncingClock(SyncingClock<&'a mut ClockSyncer>),
SyncingInitialState(SyncingInitialState<WorldType, &'a mut ActiveClient<WorldType>>),
Ready(Ready<WorldType, &'a mut ActiveClient<WorldType>>),
}