bevy_archie 0.1.7

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
//! Input condition modifiers for contextual action triggering.
//!
//! This module provides conditions that can be attached to input bindings
//! to control when actions should trigger (e.g., only when running, only in menus).
//!
//! # Example
//!
//! ```rust,no_run
//! use bevy::prelude::*;
//! use bevy_archie::conditions::{InputCondition, ConditionContext, StateCondition};
//!
//! #[derive(States, Default, Clone, Eq, PartialEq, Debug, Hash)]
//! enum GameState { #[default] Playing, Paused, Menu }
//!
//! // Create a condition that only triggers in Playing state
//! let condition = InputCondition::in_state(GameState::Playing);
//! ```

use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use std::any::TypeId;

/// A condition that determines whether an input binding should trigger.
#[derive(Debug, Clone, Default)]
pub enum InputCondition {
    /// Always allow the action.
    #[default]
    Always,

    /// Never allow the action.
    Never,

    /// Only allow when a specific state is active.
    InState(StateCondition),

    /// Only allow when NOT in a specific state.
    NotInState(StateCondition),

    /// Only allow when a resource exists.
    ResourceExists(ResourceCondition),

    /// Only allow when a resource doesn't exist.
    ResourceAbsent(ResourceCondition),

    /// Custom condition evaluated by a registered system.
    Custom(CustomConditionId),

    /// Combine multiple conditions with AND logic.
    All(Vec<InputCondition>),

    /// Combine multiple conditions with OR logic.
    Any(Vec<InputCondition>),

    /// Negate a condition.
    Not(Box<InputCondition>),
}

impl InputCondition {
    /// Create a condition that's always true.
    #[must_use]
    pub fn always() -> Self {
        Self::Always
    }

    /// Create a condition that's never true.
    #[must_use]
    pub fn never() -> Self {
        Self::Never
    }

    /// Create a condition that requires a specific state.
    #[must_use]
    pub fn in_state<S: States>(state: S) -> Self {
        Self::InState(StateCondition::new::<S>(state))
    }

    /// Create a condition that requires NOT being in a specific state.
    #[must_use]
    pub fn not_in_state<S: States>(state: S) -> Self {
        Self::NotInState(StateCondition::new::<S>(state))
    }

    /// Create a condition that requires a resource to exist.
    #[must_use]
    pub fn resource_exists<R: Resource>() -> Self {
        Self::ResourceExists(ResourceCondition::new::<R>())
    }

    /// Create a condition that requires a resource to NOT exist.
    #[must_use]
    pub fn resource_absent<R: Resource>() -> Self {
        Self::ResourceAbsent(ResourceCondition::new::<R>())
    }

    /// Create a custom condition with an ID.
    #[must_use]
    pub fn custom(id: impl Into<String>) -> Self {
        Self::Custom(CustomConditionId(id.into()))
    }

    /// Combine with another condition using AND logic.
    #[must_use]
    pub fn and(self, other: Self) -> Self {
        match (self, other) {
            (Self::Always, other) | (other, Self::Always) => other,
            (Self::Never, _) | (_, Self::Never) => Self::Never,
            (Self::All(mut a), Self::All(b)) => {
                a.extend(b);
                Self::All(a)
            }
            (Self::All(mut a), other) => {
                a.push(other);
                Self::All(a)
            }
            (other, Self::All(mut a)) => {
                a.insert(0, other);
                Self::All(a)
            }
            (a, b) => Self::All(vec![a, b]),
        }
    }

    /// Combine with another condition using OR logic.
    #[must_use]
    pub fn or(self, other: Self) -> Self {
        match (self, other) {
            (Self::Always, _) | (_, Self::Always) => Self::Always,
            (Self::Never, other) | (other, Self::Never) => other,
            (Self::Any(mut a), Self::Any(b)) => {
                a.extend(b);
                Self::Any(a)
            }
            (Self::Any(mut a), other) => {
                a.push(other);
                Self::Any(a)
            }
            (other, Self::Any(mut a)) => {
                a.insert(0, other);
                Self::Any(a)
            }
            (a, b) => Self::Any(vec![a, b]),
        }
    }

    /// Negate this condition.
    #[must_use]
    pub fn negated(self) -> Self {
        match self {
            Self::Always => Self::Never,
            Self::Never => Self::Always,
            Self::Not(inner) => *inner,
            other => Self::Not(Box::new(other)),
        }
    }
}

impl std::ops::Not for InputCondition {
    type Output = Self;

    fn not(self) -> Self::Output {
        self.negated()
    }
}

/// A condition based on Bevy state.
#[derive(Debug, Clone)]
pub struct StateCondition {
    /// The `TypeId` of the state type
    #[expect(dead_code, reason = "stored for future state comparison functionality")]
    state_type_id: TypeId,
    /// Function to check if the current state matches
    #[expect(dead_code, reason = "stored for future state comparison functionality")]
    check_fn: fn(&World) -> bool,
    /// Name for debugging
    state_name: &'static str,
}

impl StateCondition {
    /// Create a new state condition.
    pub fn new<S: States>(_expected: S) -> Self {
        Self {
            state_type_id: TypeId::of::<S>(),
            check_fn: {
                // We need to capture the expected state value
                // This is a simplified version - in production you'd use a different approach
                |_world| {
                    // This is a placeholder - actual implementation would use world access
                    true
                }
            },
            state_name: std::any::type_name::<S>(),
        }
    }

    /// Get the state type name for debugging.
    #[must_use]
    pub fn state_name(&self) -> &'static str {
        self.state_name
    }
}

/// A condition based on resource existence.
#[derive(Debug, Clone)]
pub struct ResourceCondition {
    /// The `TypeId` of the resource
    resource_type_id: TypeId,
    /// Name for debugging
    resource_name: &'static str,
}

impl ResourceCondition {
    /// Create a new resource condition.
    #[must_use]
    pub fn new<R: Resource>() -> Self {
        Self {
            resource_type_id: TypeId::of::<R>(),
            resource_name: std::any::type_name::<R>(),
        }
    }

    /// Check if the resource exists in the world.
    ///
    /// # Panics
    ///
    /// Panics if the resource type ID is not registered with the world.
    #[must_use]
    pub fn check(&self, world: &World) -> bool {
        world.contains_resource_by_id(
            world
                .components()
                .get_resource_id(self.resource_type_id)
                .unwrap(),
        )
    }

    /// Get the resource type name for debugging.
    #[must_use]
    pub fn resource_name(&self) -> &'static str {
        self.resource_name
    }
}

/// A custom condition identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CustomConditionId(pub String);

impl From<&str> for CustomConditionId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<String> for CustomConditionId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

/// Context for evaluating conditions.
#[derive(Debug)]
pub struct ConditionContext<'w> {
    /// Reference to the world for state/resource checks
    world: &'w World,
    /// Custom condition results (pre-computed by registered systems)
    custom_results: &'w CustomConditionResults,
}

impl<'w> ConditionContext<'w> {
    /// Create a new condition context.
    #[must_use]
    pub fn new(world: &'w World, custom_results: &'w CustomConditionResults) -> Self {
        Self {
            world,
            custom_results,
        }
    }

    /// Evaluate a condition.
    #[must_use]
    pub fn evaluate(&self, condition: &InputCondition) -> bool {
        match condition {
            InputCondition::Always => true,
            InputCondition::Never => false,
            InputCondition::InState(_state_cond) => {
                // In a real implementation, we'd check the actual state
                // This is simplified for the example
                true
            }
            InputCondition::NotInState(state_cond) => {
                !self.evaluate(&InputCondition::InState(state_cond.clone()))
            }
            InputCondition::ResourceExists(res_cond) => res_cond.check(self.world),
            InputCondition::ResourceAbsent(res_cond) => !res_cond.check(self.world),
            InputCondition::Custom(id) => self.custom_results.get(id).unwrap_or(false),
            InputCondition::All(conditions) => conditions.iter().all(|c| self.evaluate(c)),
            InputCondition::Any(conditions) => conditions.iter().any(|c| self.evaluate(c)),
            InputCondition::Not(inner) => !self.evaluate(inner),
        }
    }
}

/// Storage for custom condition results.
#[derive(Resource, Debug, Default)]
pub struct CustomConditionResults {
    results: std::collections::HashMap<CustomConditionId, bool>,
}

impl CustomConditionResults {
    /// Set the result for a custom condition.
    pub fn set(&mut self, id: impl Into<CustomConditionId>, value: bool) {
        self.results.insert(id.into(), value);
    }

    /// Get the result for a custom condition.
    #[must_use]
    pub fn get(&self, id: &CustomConditionId) -> Option<bool> {
        self.results.get(id).copied()
    }

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

/// A binding with an attached condition.
#[derive(Debug, Clone)]
pub struct ConditionalBinding<B> {
    /// The actual binding
    pub binding: B,
    /// The condition that must be true for this binding to be active
    pub condition: InputCondition,
}

impl<B> ConditionalBinding<B> {
    /// Create a new conditional binding.
    #[must_use]
    pub fn new(binding: B, condition: InputCondition) -> Self {
        Self { binding, condition }
    }

    /// Create an always-active binding.
    #[must_use]
    pub fn always(binding: B) -> Self {
        Self::new(binding, InputCondition::Always)
    }

    /// Add a condition to this binding.
    #[must_use]
    pub fn when(mut self, condition: InputCondition) -> Self {
        self.condition = self.condition.and(condition);
        self
    }
}

/// Plugin to add condition evaluation support.
pub struct ConditionsPlugin;

impl Plugin for ConditionsPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<CustomConditionResults>();
    }
}

/// Trait for types that can have conditions attached.
pub trait Conditionable: Sized {
    /// Add a condition that must be true for this to be active.
    fn when(self, condition: InputCondition) -> ConditionalBinding<Self>;

    /// Only active when in the specified state.
    fn when_in_state<S: States>(self, state: S) -> ConditionalBinding<Self> {
        self.when(InputCondition::in_state(state))
    }

    /// Only active when NOT in the specified state.
    fn when_not_in_state<S: States>(self, state: S) -> ConditionalBinding<Self> {
        self.when(InputCondition::not_in_state(state))
    }
}

impl<T> Conditionable for T {
    fn when(self, condition: InputCondition) -> ConditionalBinding<Self> {
        ConditionalBinding::new(self, condition)
    }
}

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

    #[test]
    fn test_condition_always() {
        let cond = InputCondition::always();
        assert!(matches!(cond, InputCondition::Always));
    }

    #[test]
    fn test_condition_never() {
        let cond = InputCondition::never();
        assert!(matches!(cond, InputCondition::Never));
    }

    #[test]
    fn test_condition_and() {
        // Always AND Always = Always
        let cond = InputCondition::always().and(InputCondition::always());
        assert!(matches!(cond, InputCondition::Always));

        // Always AND Never = Never
        let cond = InputCondition::always().and(InputCondition::never());
        assert!(matches!(cond, InputCondition::Never));

        // Never AND Always = Never
        let cond = InputCondition::never().and(InputCondition::always());
        assert!(matches!(cond, InputCondition::Never));
    }

    #[test]
    fn test_condition_or() {
        // Always OR Never = Always
        let cond = InputCondition::always().or(InputCondition::never());
        assert!(matches!(cond, InputCondition::Always));

        // Never OR Always = Always
        let cond = InputCondition::never().or(InputCondition::always());
        assert!(matches!(cond, InputCondition::Always));

        // Never OR Never = Never
        let cond = InputCondition::never().or(InputCondition::never());
        assert!(matches!(cond, InputCondition::Never));
    }

    #[test]
    fn test_condition_not() {
        use std::ops::Not;

        // NOT Always = Never
        let cond = InputCondition::always().not();
        assert!(matches!(cond, InputCondition::Never));

        // NOT Never = Always
        let cond = InputCondition::never().not();
        assert!(matches!(cond, InputCondition::Always));

        // NOT NOT X = X
        let cond = InputCondition::custom("test").not().not();
        assert!(matches!(cond, InputCondition::Custom(_)));
    }

    #[test]
    fn test_custom_condition_results() {
        let mut results = CustomConditionResults::default();

        results.set("can_jump", true);
        results.set("is_grounded", false);

        assert_eq!(
            results.get(&CustomConditionId("can_jump".into())),
            Some(true)
        );
        assert_eq!(
            results.get(&CustomConditionId("is_grounded".into())),
            Some(false)
        );
        assert_eq!(results.get(&CustomConditionId("unknown".into())), None);
    }

    #[test]
    fn test_conditional_binding() {
        let binding = "action_binding".when(InputCondition::always());
        assert!(matches!(binding.condition, InputCondition::Always));

        let binding = "action_binding"
            .when(InputCondition::custom("can_jump"))
            .when(InputCondition::custom("is_grounded"));

        assert!(matches!(binding.condition, InputCondition::All(_)));
    }
}