lyra-engine/lyra-scripting/src/lib.rs

81 lines
1.9 KiB
Rust

#[cfg(feature = "lua")]
pub mod lua;
pub mod world;
pub use world::*;
pub mod wrap;
pub use wrap::*;
pub mod host;
pub use host::*;
pub mod script;
pub use script::*;
use lyra_game::game::Game;
#[allow(unused_imports)]
pub(crate) mod lyra_engine {
pub use lyra_ecs as ecs;
}
use lyra_reflect::{ReflectedComponent, Reflect};
#[derive(Clone)]
pub enum ReflectBranch {
Component(ReflectedComponent),
}
impl ReflectBranch {
/// Gets self as a [`ReflectedComponent`].
///
/// # Panics
/// If `self` is not a variant of [`ReflectBranch::Component`].
pub fn as_component_unchecked(&self) -> &ReflectedComponent {
match self {
ReflectBranch::Component(c) => c,
_ => panic!("`self` is not an instance of `ReflectBranch::Component`")
}
}
pub fn is_component(&self) -> bool {
matches!(self, ReflectBranch::Component(_))
}
}
pub struct ScriptBorrow {
reflect_branch: ReflectBranch,
data: Option<Box<dyn Reflect>>,
}
impl Clone for ScriptBorrow {
fn clone(&self) -> Self {
let data = self.data.as_ref().map(|b| b.clone_inner());
Self {
reflect_branch: self.reflect_branch.clone(),
data,
}
}
}
/// An extension trait that adds some helpful methods that makes it easier to do scripting things
pub trait GameScriptExt {
fn add_script_api_provider<T, P>(&mut self, provider: P)
where
T: ScriptHost,
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static;
}
impl GameScriptExt for Game {
fn add_script_api_provider<T, P>(&mut self, provider: P)
where
T: ScriptHost,
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static
{
let world = self.world();
let mut providers = world.get_resource_mut::<ScriptApiProviders<T>>();
providers.add_provider(provider);
}
}