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

61 lines
1.3 KiB
Rust

use std::sync::atomic::{AtomicU64, Ordering};
use lyra_ecs::Component;
use lyra_resource::{ResHandle, ResourceData};
use crate::lyra_engine;
static SCRIPT_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Clone)]
pub struct Script<T: ResourceData> {
res: ResHandle<T>,
name: String,
id: u64
}
impl<T: ResourceData> Script<T> {
pub fn new(name: &str, script: ResHandle<T>) -> Self {
Self {
res: script,
name: name.to_string(),
id: SCRIPT_ID_COUNTER.fetch_add(1, Ordering::AcqRel)
}
}
pub fn res_handle(&self) -> ResHandle<T> {
self.res.clone()
}
pub fn name(&self) -> &str {
&self.name
}
pub fn id(&self) -> u64 {
self.id
}
}
/// A list of scripts
#[derive(Clone, Default, Component)]
pub struct ScriptList<T: ResourceData>(Vec<Script<T>>);
impl<T: ResourceData> ScriptList<T> {
pub fn new(list: Vec<Script<T>>) -> Self {
Self(list)
}
}
impl<T: ResourceData> std::ops::Deref for ScriptList<T> {
type Target = Vec<Script<T>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ResourceData> std::ops::DerefMut for ScriptList<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}