use bevy::{prelude::*, reflect::Reflect};
use bevy::platform::collections::HashSet;
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
pub struct Guards {
pub guards: HashSet<String>,
}
impl Guards {
pub fn new() -> Self {
Self {
guards: HashSet::new(),
}
}
pub fn init(guards: impl IntoIterator<Item = impl Guard>) -> Self {
Self {
guards: guards.into_iter().map(|guard| guard.name()).collect(),
}
}
pub fn has_guard(&self, guard: impl Guard) -> bool {
self.guards.contains(&guard.name())
}
pub fn add_guard(&mut self, guard: impl Guard) {
self.guards.insert(guard.name());
}
pub fn remove_guard(&mut self, guard: impl Guard) {
self.guards.remove(&guard.name());
}
pub fn check(&self) -> bool {
self.guards.is_empty()
}
}
pub trait Guard {
fn name(&self) -> String;
}
impl Guard for String {
fn name(&self) -> String {
self.clone()
}
}
impl Guard for &str {
fn name(&self) -> String {
self.to_string()
}
}