summaryrefslogtreecommitdiff
path: root/platforms/tortuise-script-compiled/src/entity.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2026-10-26 10:52:04 -0400
committerMica White <botahamec@outlook.com>2026-10-26 10:58:46 -0400
commitec40f6e3acc7874b82709ab81b254c741529fdff (patch)
treee3dfdc315bd0c6f1d2efcf442c2cfb3317d6810f /platforms/tortuise-script-compiled/src/entity.rs
parente3c5839159903658a667f789963f20a31567b25c (diff)
Finish engineHEADmain
Diffstat (limited to 'platforms/tortuise-script-compiled/src/entity.rs')
-rw-r--r--platforms/tortuise-script-compiled/src/entity.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/platforms/tortuise-script-compiled/src/entity.rs b/platforms/tortuise-script-compiled/src/entity.rs
new file mode 100644
index 0000000..507690d
--- /dev/null
+++ b/platforms/tortuise-script-compiled/src/entity.rs
@@ -0,0 +1,73 @@
+use core::ops::{Deref, DerefMut};
+
+use spin::lock_api::{MappedRwLockWriteGuard, RwLock, RwLockWriteGuard};
+
+use crate::ScriptContext;
+
+pub struct Entity<T> {
+ data: RwLock<Option<T>>,
+ default: fn(&ScriptContext) -> T,
+}
+
+pub struct EntityRef<'entity, T> {
+ data: MappedRwLockWriteGuard<'entity, T>,
+}
+
+pub struct EntityMut<'entity, T> {
+ data: MappedRwLockWriteGuard<'entity, T>,
+}
+
+impl<T> Entity<T> {
+ pub const fn new(_name: &'static str, default: fn(&ScriptContext) -> T) -> Self {
+ Self {
+ data: RwLock::new(None),
+ default,
+ }
+ }
+
+ pub const fn from_string(name: String, default: fn(&ScriptContext) -> T) -> Self {
+ core::mem::forget(name);
+ Self {
+ data: RwLock::new(None),
+ default,
+ }
+ }
+
+ pub fn get(&self, context: &ScriptContext) -> EntityRef<'_, T> {
+ puffin::profile_function!();
+ let data = RwLockWriteGuard::map(self.data.write(), |data| {
+ data.get_or_insert_with(|| (self.default)(context))
+ });
+ EntityRef { data }
+ }
+
+ pub fn get_mut(&self, context: &ScriptContext) -> EntityMut<'_, T> {
+ puffin::profile_function!();
+ let data = RwLockWriteGuard::map(self.data.write(), |data| {
+ data.get_or_insert_with(|| (self.default)(context))
+ });
+ EntityMut { data }
+ }
+}
+
+impl<T> Deref for EntityRef<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
+
+impl<T> Deref for EntityMut<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
+
+impl<T> DerefMut for EntityMut<'_, T> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.data
+ }
+}