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

61 lines
1.2 KiB
Rust
Raw Normal View History

use std::sync::atomic::{AtomicU64, Ordering};
2024-01-05 01:52:47 +00:00
use lyra_ecs::Component;
use lyra_resource::ResHandle;
use crate::lyra_engine;
static SCRIPT_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
2024-01-05 01:52:47 +00:00
#[derive(Clone)]
pub struct Script<T> {
res: ResHandle<T>,
name: String,
id: u64
}
impl<T> 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)
2024-01-05 01:52:47 +00:00
}
}
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: 'static>(Vec<Script<T>>);
impl<T> ScriptList<T> {
pub fn new(list: Vec<Script<T>>) -> Self {
Self(list)
}
}
impl<T> std::ops::Deref for ScriptList<T> {
type Target = Vec<Script<T>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> std::ops::DerefMut for ScriptList<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}