bevy_archie 0.2.1

A comprehensive game controller support module for Bevy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Network synchronization for action states.
//!
//! This module provides types for efficiently synchronizing action state
//! changes across a network, useful for multiplayer games.
//!
//! # Example
//!
//! ```rust,no_run
//! use bevy_archie::networking::{ActionDiff, ActionDiffBuffer};
//!
//! // Create a diff buffer for tracking changes
//! let mut buffer = ActionDiffBuffer::<u32>::new();
//!
//! // Record a button press
//! buffer.record_press(1); // Action ID 1 was pressed
//!
//! // Get diffs to send over network
//! let diffs = buffer.drain_diffs();
//! ```

use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// A single action state change for network transmission.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ActionDiff<A> {
    /// An action was pressed/started.
    Pressed {
        /// The action that was pressed
        action: A,
        /// Timestamp (frame number or time)
        timestamp: u64,
    },
    /// An action was released/ended.
    Released {
        /// The action that was released
        action: A,
        /// Timestamp (frame number or time)
        timestamp: u64,
    },
    /// An action's axis value changed.
    AxisChanged {
        /// The action with changed axis
        action: A,
        /// The new axis value
        value: f32,
        /// Timestamp
        timestamp: u64,
    },
    /// An action's dual axis value changed.
    DualAxisChanged {
        /// The action with changed axis
        action: A,
        /// The new X axis value
        x: f32,
        /// The new Y axis value
        y: f32,
        /// Timestamp
        timestamp: u64,
    },
}

impl<A: Clone> ActionDiff<A> {
    /// Get the action this diff applies to.
    #[must_use]
    pub fn action(&self) -> A {
        match self {
            Self::Pressed { action, .. }
            | Self::Released { action, .. }
            | Self::AxisChanged { action, .. }
            | Self::DualAxisChanged { action, .. } => action.clone(),
        }
    }

    /// Get the timestamp of this diff.
    #[must_use]
    pub fn timestamp(&self) -> u64 {
        match self {
            Self::Pressed { timestamp, .. }
            | Self::Released { timestamp, .. }
            | Self::AxisChanged { timestamp, .. }
            | Self::DualAxisChanged { timestamp, .. } => *timestamp,
        }
    }
}

/// A buffer for collecting action diffs to send over the network.
#[derive(Debug, Clone, Default)]
pub struct ActionDiffBuffer<A> {
    /// Queued diffs waiting to be sent
    diffs: VecDeque<ActionDiff<A>>,
    /// Current timestamp (usually frame number)
    current_timestamp: u64,
    /// Maximum diffs to buffer before dropping old ones
    max_buffer_size: usize,
}

impl<A: Clone + PartialEq> ActionDiffBuffer<A> {
    /// Create a new diff buffer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            diffs: VecDeque::new(),
            current_timestamp: 0,
            max_buffer_size: 256,
        }
    }

    /// Create a buffer with a custom max size.
    #[must_use]
    pub fn with_max_size(max_size: usize) -> Self {
        Self {
            max_buffer_size: max_size,
            ..Self::new()
        }
    }

    /// Set the current timestamp (call each frame).
    pub fn set_timestamp(&mut self, timestamp: u64) {
        self.current_timestamp = timestamp;
    }

    /// Increment the timestamp.
    pub fn tick(&mut self) {
        self.current_timestamp += 1;
    }

    /// Record a button press.
    pub fn record_press(&mut self, action: A) {
        self.push_diff(ActionDiff::Pressed {
            action,
            timestamp: self.current_timestamp,
        });
    }

    /// Record a button release.
    pub fn record_release(&mut self, action: A) {
        self.push_diff(ActionDiff::Released {
            action,
            timestamp: self.current_timestamp,
        });
    }

    /// Record an axis value change.
    pub fn record_axis(&mut self, action: A, value: f32) {
        self.push_diff(ActionDiff::AxisChanged {
            action,
            value,
            timestamp: self.current_timestamp,
        });
    }

    /// Record a dual axis value change.
    pub fn record_dual_axis(&mut self, action: A, x: f32, y: f32) {
        self.push_diff(ActionDiff::DualAxisChanged {
            action,
            x,
            y,
            timestamp: self.current_timestamp,
        });
    }

    /// Push a diff to the buffer.
    fn push_diff(&mut self, diff: ActionDiff<A>) {
        // Enforce max buffer size
        if self.diffs.len() >= self.max_buffer_size {
            self.diffs.pop_front();
        }
        self.diffs.push_back(diff);
    }

    /// Get all pending diffs without removing them.
    #[must_use]
    pub fn peek_diffs(&self) -> &VecDeque<ActionDiff<A>> {
        &self.diffs
    }

    /// Drain all pending diffs.
    pub fn drain_diffs(&mut self) -> Vec<ActionDiff<A>> {
        self.diffs.drain(..).collect()
    }

    /// Get the number of pending diffs.
    #[must_use]
    pub fn len(&self) -> usize {
        self.diffs.len()
    }

    /// Check if there are no pending diffs.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.diffs.is_empty()
    }

    /// Clear all pending diffs.
    pub fn clear(&mut self) {
        self.diffs.clear();
    }
}

/// Component for entities that need networked input.
#[derive(Component, Debug, Default)]
pub struct NetworkedInput<A> {
    /// Buffer for outgoing diffs
    pub outgoing: ActionDiffBuffer<A>,
    /// Buffer for incoming diffs (to be applied)
    pub incoming: VecDeque<ActionDiff<A>>,
}

impl<A: Clone + PartialEq> NetworkedInput<A> {
    /// Create a new networked input component.
    #[must_use]
    pub fn new() -> Self {
        Self {
            outgoing: ActionDiffBuffer::new(),
            incoming: VecDeque::new(),
        }
    }

    /// Queue incoming diffs to be applied.
    pub fn receive_diffs(&mut self, diffs: impl IntoIterator<Item = ActionDiff<A>>) {
        self.incoming.extend(diffs);
    }

    /// Get the next incoming diff to apply.
    pub fn next_incoming(&mut self) -> Option<ActionDiff<A>> {
        self.incoming.pop_front()
    }

    /// Check if there are incoming diffs.
    #[must_use]
    pub fn has_incoming(&self) -> bool {
        !self.incoming.is_empty()
    }
}

/// Serialize diffs for network transmission.
///
/// Returns bytes that can be sent over the network.
///
/// # Errors
///
/// Returns an error if serialization fails.
pub fn serialize_diffs<A: Serialize>(
    diffs: &[ActionDiff<A>],
) -> Result<Vec<u8>, serde_json::Error> {
    serde_json::to_vec(diffs)
}

/// Deserialize diffs received from network.
///
/// # Errors
///
/// Returns an error if deserialization fails.
pub fn deserialize_diffs<A: for<'de> Deserialize<'de>>(
    bytes: &[u8],
) -> Result<Vec<ActionDiff<A>>, serde_json::Error> {
    serde_json::from_slice(bytes)
}

/// A snapshot of action states for full state sync.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionStateSnapshot<A> {
    /// Currently pressed actions
    pub pressed: Vec<A>,
    /// Current axis values
    pub axes: Vec<(A, f32)>,
    /// Current dual axis values
    pub dual_axes: Vec<(A, f32, f32)>,
    /// Snapshot timestamp
    pub timestamp: u64,
}

impl<A: Clone + PartialEq> ActionStateSnapshot<A> {
    /// Create an empty snapshot.
    #[must_use]
    pub fn new(timestamp: u64) -> Self {
        Self {
            pressed: Vec::new(),
            axes: Vec::new(),
            dual_axes: Vec::new(),
            timestamp,
        }
    }

    /// Add a pressed action to the snapshot.
    pub fn add_pressed(&mut self, action: A) {
        if !self.pressed.contains(&action) {
            self.pressed.push(action);
        }
    }

    /// Add an axis value to the snapshot.
    pub fn add_axis(&mut self, action: A, value: f32) {
        self.axes.push((action, value));
    }

    /// Add a dual axis value to the snapshot.
    pub fn add_dual_axis(&mut self, action: A, x: f32, y: f32) {
        self.dual_axes.push((action, x, y));
    }
}

/// Configuration for network input synchronization.
#[derive(Resource, Debug, Clone)]
pub struct NetworkInputConfig {
    /// How often to send full snapshots (in frames)
    pub snapshot_interval: u32,
    /// Whether to use delta compression
    pub delta_compression: bool,
    /// Maximum age of diffs to accept (for lag compensation)
    pub max_diff_age: u64,
    /// Whether to interpolate between received states
    pub interpolation: bool,
}

impl Default for NetworkInputConfig {
    fn default() -> Self {
        Self {
            snapshot_interval: 60, // Once per second at 60fps
            delta_compression: true,
            max_diff_age: 30, // Half second of lag tolerance
            interpolation: true,
        }
    }
}

/// Plugin for network input synchronization.
pub struct NetworkInputPlugin<A: Clone + PartialEq + Send + Sync + 'static> {
    _marker: std::marker::PhantomData<A>,
}

impl<A: Clone + PartialEq + Send + Sync + 'static> Default for NetworkInputPlugin<A> {
    fn default() -> Self {
        Self {
            _marker: std::marker::PhantomData,
        }
    }
}

impl<A: Clone + PartialEq + Send + Sync + 'static> Plugin for NetworkInputPlugin<A> {
    fn build(&self, app: &mut App) {
        app.init_resource::<NetworkInputConfig>();
    }
}

/// Helper trait for creating diffs from state changes.
pub trait ActionDiffExt<A: Clone + PartialEq> {
    /// Compare two states and generate diffs.
    fn diff_states(old_pressed: &[A], new_pressed: &[A], timestamp: u64) -> Vec<ActionDiff<A>>;
}

impl<A: Clone + PartialEq> ActionDiffExt<A> for ActionDiff<A> {
    fn diff_states(old_pressed: &[A], new_pressed: &[A], timestamp: u64) -> Vec<ActionDiff<A>> {
        let mut diffs = Vec::new();

        // Find newly pressed actions
        for action in new_pressed {
            if !old_pressed.contains(action) {
                diffs.push(ActionDiff::Pressed {
                    action: action.clone(),
                    timestamp,
                });
            }
        }

        // Find newly released actions
        for action in old_pressed {
            if !new_pressed.contains(action) {
                diffs.push(ActionDiff::Released {
                    action: action.clone(),
                    timestamp,
                });
            }
        }

        diffs
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_action_diff_buffer() {
        let mut buffer = ActionDiffBuffer::<u32>::new();

        buffer.tick();
        buffer.record_press(1);
        buffer.tick();
        buffer.record_release(1);

        let diffs = buffer.drain_diffs();
        assert_eq!(diffs.len(), 2);

        assert!(matches!(
            diffs[0],
            ActionDiff::Pressed {
                action: 1,
                timestamp: 1
            }
        ));
        assert!(matches!(
            diffs[1],
            ActionDiff::Released {
                action: 1,
                timestamp: 2
            }
        ));
    }

    #[test]
    fn test_diff_states() {
        let old = vec![1, 2, 3];
        let new = vec![2, 3, 4];

        let diffs = ActionDiff::diff_states(&old, &new, 100);

        // Should have: Released(1), Pressed(4)
        assert_eq!(diffs.len(), 2);

        let has_release = diffs
            .iter()
            .any(|d| matches!(d, ActionDiff::Released { action: 1, .. }));
        let has_press = diffs
            .iter()
            .any(|d| matches!(d, ActionDiff::Pressed { action: 4, .. }));

        assert!(has_release);
        assert!(has_press);
    }

    #[test]
    fn test_serialization() {
        let diffs = vec![
            ActionDiff::Pressed::<u32> {
                action: 1,
                timestamp: 100,
            },
            ActionDiff::Released::<u32> {
                action: 2,
                timestamp: 101,
            },
        ];

        let bytes = serialize_diffs(&diffs).unwrap();
        let restored: Vec<ActionDiff<u32>> = deserialize_diffs(&bytes).unwrap();

        assert_eq!(diffs, restored);
    }

    #[test]
    fn test_buffer_max_size() {
        let mut buffer = ActionDiffBuffer::<u32>::with_max_size(3);

        buffer.record_press(1);
        buffer.record_press(2);
        buffer.record_press(3);
        buffer.record_press(4); // Should drop oldest

        assert_eq!(buffer.len(), 3);
        let diffs = buffer.drain_diffs();
        assert!(diffs.iter().any(|d| d.action() == 4));
        assert!(!diffs.iter().any(|d| d.action() == 1)); // 1 was dropped
    }

    #[test]
    fn test_action_snapshot() {
        let mut snapshot = ActionStateSnapshot::<u32>::new(100);

        snapshot.add_pressed(1);
        snapshot.add_pressed(2);
        snapshot.add_axis(3, 0.5);
        snapshot.add_dual_axis(4, 0.3, -0.7);

        assert_eq!(snapshot.pressed.len(), 2);
        assert_eq!(snapshot.axes.len(), 1);
        assert_eq!(snapshot.dual_axes.len(), 1);
    }
}