bones_framework/networking/
input.rs1use std::fmt::Debug;
5
6use bones_schema::HasSchema;
7
8use crate::input::{InputCollector, PlayerControls};
9
10use super::NetworkInputStatus;
11
12#[allow(missing_docs)]
17pub trait NetworkInputConfig<'a> {
18 type Dense: DenseInput + Debug + Default;
19 type Control: NetworkPlayerControl<Self::Dense>;
20
21 type PlayerControls: PlayerControls<'a, Self::Control> + HasSchema;
23
24 type InputCollector: InputCollector<
26 'a,
27 <Self::PlayerControls as PlayerControls<'a, Self::Control>>::ControlMapping,
28 <Self::PlayerControls as PlayerControls<'a, Self::Control>>::ControlSource,
29 Self::Control,
30 > + Default;
31}
32
33pub trait NetworkPlayerControls<'a, Dense: DenseInput, Control>:
35 PlayerControls<'a, Control>
36{
37 fn network_update(
41 &mut self,
42 player_idx: usize,
43 dense_input: &Dense,
44 status: NetworkInputStatus,
45 );
46
47 fn get_dense_control(&self, player_idx: usize) -> Dense;
49}
50
51impl<'a, T, Dense, Control> NetworkPlayerControls<'a, Dense, Control> for T
52where
53 Dense: DenseInput,
54 Control: NetworkPlayerControl<Dense>,
55 T: PlayerControls<'a, Control>,
56{
57 fn network_update(
59 &mut self,
60 player_idx: usize,
61 dense_input: &Dense,
62 _status: NetworkInputStatus,
63 ) {
64 self.get_control_mut(player_idx)
65 .update_from_dense(dense_input);
66 }
67
68 fn get_dense_control(&self, player_idx: usize) -> Dense {
69 self.get_control(player_idx).get_dense_input()
70 }
71}
72
73pub trait DenseInput:
75 bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
76{
77}
78
79impl<T> DenseInput for T where
81 T: bytemuck::Pod + bytemuck::Zeroable + Copy + Clone + PartialEq + Eq + Send + Sync
82{
83}
84
85pub trait NetworkPlayerControl<Dense: DenseInput>: Send + Sync + Default {
87 fn get_dense_input(&self) -> Dense;
89
90 fn update_from_dense(&mut self, new_control: &Dense);
92}
93
94pub trait NetworkInputCollector<'a, Dense, ControlMapping, ControlSource, Control>:
99 InputCollector<'a, ControlMapping, ControlSource, Control>
100where
101 Dense: DenseInput,
102 ControlMapping: HasSchema,
103 Control: NetworkPlayerControl<Dense>,
104{
105 fn get_dense_control(&self, player_idx: usize, control_soure: ControlSource) -> Dense;
107}
108
109impl<'a, T, Dense, ControlMapping, ControlSource, Control>
112 NetworkInputCollector<'a, Dense, ControlMapping, ControlSource, Control> for T
113where
114 Dense: DenseInput,
115 Control: NetworkPlayerControl<Dense>,
116 ControlMapping: HasSchema,
117 T: InputCollector<'a, ControlMapping, ControlSource, Control>,
118{
119 fn get_dense_control(&self, player_idx: usize, control_source: ControlSource) -> Dense {
120 self.get_control(player_idx, control_source)
121 .get_dense_input()
122 }
123}