bevy_symbios_shape 0.7.0

Bevy integration for Symbios Shape.
//! Bevy Component surface for upstream `symbios-shape` occlusion labels.
//!
//! A grammar stamps an occlusion class on a branch with `Label("chimneys")`;
//! the interpreter carries it to every terminal derived beneath, where the
//! `If*` conditionals use it to filter their spatial tests. This module
//! mirrors that class into the ECS as [`TerminalLabel`], so gameplay,
//! picking, and debug-draw systems can select the same groups the grammar
//! reasoned about.
//!
//! ```ignore
//! // Grammar:  Roof --> Label("roof") I("Tile")
//! fn highlight_roofs(q: Query<(Entity, &TerminalLabel)>) {
//!     for (e, label) in &q {
//!         if label.is("roof") { /* … */ }
//!     }
//! }
//! ```

use bevy::prelude::Component;

/// The occlusion class a terminal was stamped with by `Label("…")`.
///
/// Inserted by [`SpawnShapeExt::spawn_shape`] onto every spawned terminal
/// whose upstream [`Terminal::label`] is `Some`. Terminals from unlabelled
/// branches carry no component at all, so `Query<&TerminalLabel>` naturally
/// iterates only the classified ones.
///
/// [`SpawnShapeExt::spawn_shape`]: crate::spawner::SpawnShapeExt::spawn_shape
/// [`Terminal::label`]: symbios_shape::Terminal
#[derive(Component, Debug, Clone, PartialEq, Eq, Hash)]
pub struct TerminalLabel(pub String);

impl TerminalLabel {
    /// The label text.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// True when this terminal belongs to the named class.
    pub fn is(&self, class: &str) -> bool {
        self.0 == class
    }
}

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

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

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

    #[test]
    fn class_matching_and_conversion() {
        let l = TerminalLabel::from("roof");
        assert!(l.is("roof"));
        assert!(!l.is("walls"));
        assert_eq!(l.as_str(), "roof");
        assert_eq!(TerminalLabel::from("roof".to_string()), l);
    }
}