use crate::ECSResult;
use super::model::Model;
pub type NestedBridge = Box<dyn FnMut(&mut Model, &Model) -> ECSResult<()> + Send>;
pub struct NestedModel {
name: String,
model: Box<Model>,
bridge: Option<NestedBridge>,
}
impl NestedModel {
pub fn new(name: impl Into<String>, model: Model) -> Self {
Self {
name: name.into(),
model: Box::new(model),
bridge: None,
}
}
pub fn with_bridge(
mut self,
bridge: impl FnMut(&mut Model, &Model) -> ECSResult<()> + Send + 'static,
) -> Self {
self.bridge = Some(Box::new(bridge));
self
}
#[inline]
pub fn name(&self) -> &str {
&self.name
}
#[inline]
pub fn model(&self) -> &Model {
&self.model
}
#[inline]
pub fn model_mut(&mut self) -> &mut Model {
&mut self.model
}
pub(crate) fn tick_and_bridge(&mut self, parent: &mut Model) -> ECSResult<()> {
self.model.tick()?;
if let Some(bridge) = &mut self.bridge {
bridge(parent, &self.model)?;
}
Ok(())
}
}