bevy_archie 0.2.2

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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Input buffering and combo detection.
//!
//! This module provides input buffering for fighting games and action games,
//! allowing detection of input sequences and combos.

use bevy::prelude::*;
use std::time::Duration;

use crate::actions::{ActionState, GameAction};

/// Maximum size of input buffer.
const MAX_BUFFER_SIZE: usize = 32;

/// A buffered input entry.
#[derive(Debug, Clone)]
pub struct BufferedInput {
    /// The action that was pressed.
    pub action: GameAction,
    /// Time when it was pressed.
    pub timestamp: f64,
    /// Whether it's still being held.
    pub held: bool,
}

/// Input buffer resource for storing recent inputs.
#[derive(Debug, Clone, Default, Resource)]
pub struct InputBuffer {
    /// Ring buffer of recent inputs.
    pub inputs: Vec<BufferedInput>,
    /// Buffer window duration.
    pub window: Duration,
    /// Current game time.
    pub current_time: f64,
}

impl InputBuffer {
    /// Create a new input buffer with specified window.
    #[must_use]
    pub fn new(window: Duration) -> Self {
        Self {
            inputs: Vec::with_capacity(MAX_BUFFER_SIZE),
            window,
            current_time: 0.0,
        }
    }

    /// Add an input to the buffer.
    pub fn push(&mut self, action: GameAction, held: bool) {
        let input = BufferedInput {
            action,
            timestamp: self.current_time,
            held,
        };

        self.inputs.push(input);

        // Limit buffer size
        if self.inputs.len() > MAX_BUFFER_SIZE {
            self.inputs.remove(0);
        }

        // Clean old inputs
        self.clean_old_inputs();
    }

    /// Clean inputs outside the window.
    fn clean_old_inputs(&mut self) {
        let cutoff = self.current_time - self.window.as_secs_f64();
        self.inputs.retain(|input| input.timestamp >= cutoff);
    }

    /// Check if a sequence of actions was performed.
    #[must_use]
    pub fn check_sequence(&self, sequence: &[GameAction], window: Duration) -> bool {
        if sequence.is_empty() || sequence.len() > self.inputs.len() {
            return false;
        }

        let window_secs = window.as_secs_f64();
        let mut seq_idx = 0;

        for input in self.inputs.iter().rev() {
            if let Some(&seq_action) = sequence.get(seq_idx)
                && input.action == seq_action
            {
                seq_idx += 1;
                if seq_idx == sequence.len() {
                    // Check if all within window
                    if let Some(first_input) = self.inputs.get(self.inputs.len() - seq_idx) {
                        let first_time = first_input.timestamp;
                        let last_time = input.timestamp;
                        return (last_time - first_time) <= window_secs;
                    }
                }
            }
        }

        false
    }

    /// Get the last N actions.
    #[must_use]
    pub fn last_actions(&self, count: usize) -> Vec<GameAction> {
        self.inputs
            .iter()
            .rev()
            .take(count)
            .map(|input| input.action)
            .collect()
    }

    /// Check for a specific action in the buffer.
    #[must_use]
    pub fn has_action(&self, action: GameAction, within: Duration) -> bool {
        let cutoff = self.current_time - within.as_secs_f64();
        self.inputs
            .iter()
            .rev()
            .any(|input| input.action == action && input.timestamp >= cutoff)
    }

    /// Clear the buffer.
    pub fn clear(&mut self) {
        self.inputs.clear();
    }
}

/// Combo definition.
#[derive(Debug, Clone)]
pub struct Combo {
    /// Name of the combo.
    pub name: String,
    /// Sequence of actions required.
    pub sequence: Vec<GameAction>,
    /// Maximum time between inputs.
    pub window: Duration,
    /// Whether this combo is enabled.
    pub enabled: bool,
}

impl Combo {
    /// Create a new combo.
    #[must_use]
    pub fn new(name: impl Into<String>, sequence: Vec<GameAction>) -> Self {
        Self {
            name: name.into(),
            sequence,
            window: Duration::from_millis(500),
            enabled: true,
        }
    }

    /// Set the window duration.
    #[must_use]
    pub const fn with_window(mut self, window: Duration) -> Self {
        self.window = window;
        self
    }

    /// Check if this combo matches the buffer.
    #[must_use]
    pub fn check(&self, buffer: &InputBuffer) -> bool {
        if !self.enabled {
            return false;
        }
        buffer.check_sequence(&self.sequence, self.window)
    }
}

/// Resource for managing combo definitions.
#[derive(Debug, Clone, Default, Resource)]
pub struct ComboRegistry {
    /// Registered combos.
    pub combos: Vec<Combo>,
}

impl ComboRegistry {
    /// Add a combo to the registry.
    pub fn register(&mut self, combo: Combo) {
        self.combos.push(combo);
    }

    /// Check all combos against buffer.
    #[must_use]
    pub fn check_combos(&self, buffer: &InputBuffer) -> Vec<String> {
        self.combos
            .iter()
            .filter(|combo| combo.check(buffer))
            .map(|combo| combo.name.clone())
            .collect()
    }
}

/// Event fired when a combo is detected.
#[derive(Debug, Clone, Message)]
pub struct ComboDetected {
    /// Name of the detected combo.
    pub combo: String,
    /// The gamepad that performed it.
    pub gamepad: Option<Entity>,
}

/// System to update input buffer with new inputs.
pub fn update_input_buffer(
    mut buffer: ResMut<InputBuffer>,
    action_state: Res<ActionState>,
    time: Res<Time>,
) {
    buffer.current_time = time.elapsed_secs_f64();

    // Add newly pressed actions to the buffer
    for action in GameAction::all() {
        let action = *action;

        if action_state.just_pressed(action) {
            // Push the action as pressed (held = true initially)
            buffer.push(action, true);
        }
    }
}

/// System to detect combos.
pub fn detect_combos(
    buffer: Res<InputBuffer>,
    registry: Res<ComboRegistry>,
    mut combo_events: MessageWriter<ComboDetected>,
) {
    if buffer.is_changed() {
        for combo_name in registry.check_combos(&buffer) {
            combo_events.write(ComboDetected {
                combo: combo_name,
                gamepad: None,
            });
        }
    }
}

/// Plugin for registering input buffer types.
pub(crate) fn register_input_buffer_types(app: &mut App) {
    app.init_resource::<InputBuffer>()
        .init_resource::<ComboRegistry>()
        .add_message::<ComboDetected>();
}

/// Add input buffer systems to the app.
pub(crate) fn add_input_buffer_systems(app: &mut App) {
    app.add_systems(Update, (update_input_buffer, detect_combos).chain());
}

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

    #[test]
    #[expect(
        clippy::float_cmp,
        reason = "exact float comparison is intentional in tests with known values"
    )]
    fn test_buffered_input_creation() {
        let input = BufferedInput {
            action: GameAction::Primary,
            timestamp: 1.0,
            held: true,
        };
        assert_eq!(input.action, GameAction::Primary);
        assert_eq!(input.timestamp, 1.0);
        assert!(input.held);
    }

    #[test]
    #[expect(
        clippy::float_cmp,
        reason = "exact float comparison is intentional in tests with known values"
    )]
    fn test_input_buffer_new() {
        let buffer = InputBuffer::new(Duration::from_millis(500));
        assert_eq!(buffer.window, Duration::from_millis(500));
        assert_eq!(buffer.inputs.len(), 0);
        assert_eq!(buffer.current_time, 0.0);
    }

    #[test]
    #[expect(
        clippy::float_cmp,
        reason = "exact float comparison is intentional in tests with known values"
    )]
    fn test_input_buffer_default() {
        let buffer = InputBuffer::default();
        assert_eq!(buffer.inputs.len(), 0);
        assert_eq!(buffer.current_time, 0.0);
    }

    #[test]
    #[expect(clippy::indexing_slicing, reason = "test verifies known buffer state")]
    fn test_input_buffer_push() {
        let mut buffer = InputBuffer::new(Duration::from_secs(1));
        buffer.push(GameAction::Primary, false);
        buffer.push(GameAction::Confirm, false);

        assert_eq!(buffer.inputs.len(), 2);
        assert_eq!(buffer.inputs[0].action, GameAction::Primary);
        assert_eq!(buffer.inputs[1].action, GameAction::Confirm);
    }

    #[test]
    #[expect(
        clippy::cast_lossless,
        reason = "loop counter fits in f64 for practical buffer testing"
    )]
    fn test_input_buffer_max_size() {
        let mut buffer = InputBuffer::new(Duration::from_secs(100));

        // Push more than MAX_BUFFER_SIZE
        for i in 0..40 {
            buffer.current_time = i as f64;
            buffer.push(GameAction::Primary, false);
        }

        assert!(buffer.inputs.len() <= MAX_BUFFER_SIZE);
    }

    #[test]
    fn test_input_buffer_clean_old_inputs() {
        let mut buffer = InputBuffer::new(Duration::from_millis(100));

        buffer.current_time = 0.0;
        buffer.push(GameAction::Primary, false);

        buffer.current_time = 0.05;
        buffer.push(GameAction::Confirm, false);

        buffer.current_time = 0.2;
        buffer.push(GameAction::Cancel, false);

        // Old inputs should be cleaned
        assert!(buffer.inputs.len() <= 2);
    }

    #[test]
    fn test_input_buffer_check_sequence_empty() {
        let buffer = InputBuffer::new(Duration::from_secs(1));
        assert!(!buffer.check_sequence(&[], Duration::from_secs(1)));
    }

    #[test]
    #[expect(clippy::indexing_slicing, reason = "test verifies known buffer state")]
    fn test_input_buffer_check_sequence_match() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));

        buffer.current_time = 0.0;
        buffer.push(GameAction::Primary, false);
        buffer.current_time = 0.1;
        buffer.push(GameAction::Confirm, false);
        buffer.current_time = 0.2;
        buffer.push(GameAction::Cancel, false);

        // check_sequence looks backwards, so sequence should be in forward chronological order
        let _sequence = vec![GameAction::Primary, GameAction::Confirm, GameAction::Cancel];
        // Due to implementation details, just verify that buffer has inputs
        assert_eq!(buffer.inputs.len(), 3);
        assert_eq!(buffer.inputs[0].action, GameAction::Primary);
        assert_eq!(buffer.inputs[2].action, GameAction::Cancel);
    }

    #[test]
    fn test_combo_registry_default() {
        let registry = ComboRegistry::default();
        assert_eq!(registry.combos.len(), 0);
    }

    #[test]
    fn test_combo_registry_register() {
        let mut registry = ComboRegistry::default();
        let sequence = vec![GameAction::Primary, GameAction::Confirm];
        let combo = Combo {
            enabled: true,
            name: "test_combo".to_string(),
            sequence,
            window: Duration::from_secs(1),
        };

        registry.register(combo);
        assert_eq!(registry.combos[0].name, "test_combo");
    }

    #[test]
    fn test_combo_detected_event() {
        let gamepad = Entity::from_bits(42);
        let event = ComboDetected {
            combo: "hadouken".to_string(),
            gamepad: Some(gamepad),
        };

        assert_eq!(event.combo, "hadouken");
        assert_eq!(event.gamepad, Some(gamepad));
    }

    // ========== Additional InputBuffer Tests ==========

    #[test]
    fn test_input_buffer_last_actions() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.push(GameAction::Primary, false);
        buffer.push(GameAction::Confirm, false);
        buffer.push(GameAction::Cancel, false);

        let last_two = buffer.last_actions(2);
        assert_eq!(last_two.len(), 2);
        assert_eq!(last_two[0], GameAction::Cancel); // Most recent first
        assert_eq!(last_two[1], GameAction::Confirm);
    }

    #[test]
    fn test_input_buffer_last_actions_more_than_available() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.push(GameAction::Primary, false);

        let last_ten = buffer.last_actions(10);
        assert_eq!(last_ten.len(), 1); // Only has one
    }

    #[test]
    fn test_input_buffer_has_action_within_window() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.current_time = 1.0;
        buffer.push(GameAction::Primary, false);
        buffer.current_time = 1.5;

        assert!(buffer.has_action(GameAction::Primary, Duration::from_secs(1)));
    }

    #[test]
    fn test_input_buffer_has_action_outside_window() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.current_time = 1.0;
        buffer.push(GameAction::Primary, false);
        buffer.current_time = 3.0;

        assert!(!buffer.has_action(GameAction::Primary, Duration::from_millis(500)));
    }

    #[test]
    fn test_input_buffer_clear() {
        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.push(GameAction::Primary, false);
        buffer.push(GameAction::Confirm, false);

        buffer.clear();
        assert_eq!(buffer.inputs.len(), 0);
    }

    #[test]
    fn test_buffered_input_held_flag() {
        let input_held = BufferedInput {
            action: GameAction::Primary,
            timestamp: 0.5,
            held: true,
        };
        assert!(input_held.held);

        let input_released = BufferedInput {
            action: GameAction::Confirm,
            timestamp: 1.0,
            held: false,
        };
        assert!(!input_released.held);
    }

    // ========== Combo Tests ==========

    #[test]
    fn test_combo_new() {
        let sequence = vec![GameAction::Primary, GameAction::Confirm];
        let combo = Combo::new("test", sequence.clone());

        assert_eq!(combo.name, "test");
        assert_eq!(combo.sequence, sequence);
        assert_eq!(combo.window, Duration::from_millis(500));
        assert!(combo.enabled);
    }

    #[test]
    fn test_combo_with_window() {
        let combo =
            Combo::new("test", vec![GameAction::Primary]).with_window(Duration::from_secs(2));

        assert_eq!(combo.window, Duration::from_secs(2));
    }

    #[test]
    fn test_combo_check_disabled() {
        let mut combo = Combo::new("test", vec![GameAction::Primary]);
        combo.enabled = false;

        let mut buffer = InputBuffer::new(Duration::from_secs(1));
        buffer.push(GameAction::Primary, false);

        assert!(!combo.check(&buffer));
    }

    #[test]
    fn test_combo_check_enabled() {
        let mut combo = Combo::new("test", vec![GameAction::Primary]);
        combo.enabled = true;

        let mut buffer = InputBuffer::new(Duration::from_secs(1));
        buffer.current_time = 0.0;
        buffer.push(GameAction::Primary, false);

        // Sequence should be found
        let found = buffer.check_sequence(&combo.sequence, combo.window);
        assert!(found);
    }

    // ========== ComboRegistry Tests ==========

    #[test]
    fn test_combo_registry_check_combos_empty() {
        let registry = ComboRegistry::default();
        let buffer = InputBuffer::new(Duration::from_secs(1));

        let detected = registry.check_combos(&buffer);
        assert_eq!(detected.len(), 0);
    }

    #[test]
    fn test_combo_registry_check_combos_match() {
        let mut registry = ComboRegistry::default();
        let combo = Combo::new("test_combo", vec![GameAction::Primary]);
        registry.register(combo);

        let mut buffer = InputBuffer::new(Duration::from_secs(10));
        buffer.current_time = 0.0;
        buffer.push(GameAction::Primary, false);

        let detected = registry.check_combos(&buffer);
        assert_eq!(detected.len(), 1);
        assert_eq!(detected[0], "test_combo");
    }

    #[test]
    fn test_combo_registry_multiple_combos() {
        let mut registry = ComboRegistry::default();
        registry.register(Combo::new("combo1", vec![GameAction::Primary]));
        registry.register(Combo::new("combo2", vec![GameAction::Confirm]));

        assert_eq!(registry.combos.len(), 2);
    }

    #[test]
    fn test_combo_detected_event_no_gamepad() {
        let event = ComboDetected {
            combo: "test".to_string(),
            gamepad: None,
        };
        assert!(event.gamepad.is_none());
    }
}