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