use std::fmt::Debug;
use bones_schema::HasSchema;
use crate::input::{InputCollector, PlayerControls};
use super::NetworkInputStatus;
#[allow(missing_docs)]
pub trait NetworkInputConfig<'a> {
type Dense: DenseInput + Debug + Default;
type Control: NetworkPlayerControl<Self::Dense>;
type PlayerControls: PlayerControls<'a, Self::Control> + HasSchema;
type InputCollector: InputCollector<
'a,
<Self::PlayerControls as PlayerControls<'a, Self::Control>>::ControlMapping,
<Self::PlayerControls as PlayerControls<'a, Self::Control>>::ControlSource,
Self::Control,
> + Default;
}
pub trait NetworkPlayerControls<'a, Dense: DenseInput, Control>:
PlayerControls<'a, Control>
{
fn network_update(
&mut self,
player_idx: usize,
dense_input: &Dense,
status: NetworkInputStatus,
);
fn get_dense_control(&self, player_idx: usize) -> Dense;
}
impl<'a, T, Dense, Control> NetworkPlayerControls<'a, Dense, Control> for T
where
Dense: DenseInput,
Control: NetworkPlayerControl<Dense>,
T: PlayerControls<'a, Control>,
{
fn network_update(
&mut self,
player_idx: usize,
dense_input: &Dense,
_status: NetworkInputStatus,
) {
self.get_control_mut(player_idx)
.update_from_dense(dense_input);
}
fn get_dense_control(&self, player_idx: usize) -> Dense {
self.get_control(player_idx).get_dense_input()
}
}
pub trait DenseInput:
bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
{
}
impl<T> DenseInput for T where
T: bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
{
}
pub trait NetworkPlayerControl<Dense: DenseInput>: Send + Sync + Default {
fn get_dense_input(&self) -> Dense;
fn update_from_dense(&mut self, new_control: &Dense);
}
pub trait NetworkInputCollector<'a, Dense, ControlMapping, ControlSource, Control>:
InputCollector<'a, ControlMapping, ControlSource, Control>
where
Dense: DenseInput,
ControlMapping: HasSchema,
Control: NetworkPlayerControl<Dense>,
{
fn get_dense_control(&self, player_idx: usize, control_soure: ControlSource) -> Dense;
}
impl<'a, T, Dense, ControlMapping, ControlSource, Control>
NetworkInputCollector<'a, Dense, ControlMapping, ControlSource, Control> for T
where
Dense: DenseInput,
Control: NetworkPlayerControl<Dense>,
ControlMapping: HasSchema,
T: InputCollector<'a, ControlMapping, ControlSource, Control>,
{
fn get_dense_control(&self, player_idx: usize, control_source: ControlSource) -> Dense {
self.get_control(player_idx, control_source)
.get_dense_input()
}
}