From 77fb1f5c586bd8a4c411943519925775617e641c Mon Sep 17 00:00:00 2001 From: christianlincoln Date: Thu, 29 Jan 2026 16:47:52 +0000 Subject: [PATCH] . --- .idea/.gitignore | 8 ++++ .idea/bevy2.iml | 11 ++++++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ Cargo.lock | 1 + Cargo.toml | 1 + {src => archive}/duck.rs | 79 ++++++++++++++++++++++++++++------------ src/duck/instance.rs | 49 +++++++++++++++++++++++++ src/duck/mod.rs | 28 ++++++++++++++ src/duck/part.rs | 31 ++++++++++++++++ src/duck/script.rs | 65 +++++++++++++++++++++++++++++++++ 11 files changed, 263 insertions(+), 24 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/bevy2.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml rename {src => archive}/duck.rs (55%) create mode 100644 src/duck/instance.rs create mode 100644 src/duck/mod.rs create mode 100644 src/duck/part.rs create mode 100644 src/duck/script.rs diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/bevy2.iml b/.idea/bevy2.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/bevy2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..cc1c825 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 487606b..d32cb92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -310,6 +310,7 @@ dependencies = [ "include_dir", "mlua", "nameof", + "thiserror 2.0.18", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f281755..611415c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ bevy = { version = "0.16", features = ["file_watcher"] } include_dir = "0.7.4" mlua = { version = "0.11.5", features = ["luau","luau-jit","luau-vector4","serde","send"] } nameof = "1.3.0" +thiserror = "2.0.18" [profile.dev.package."*"] opt-level = 3 diff --git a/src/duck.rs b/archive/duck.rs similarity index 55% rename from src/duck.rs rename to archive/duck.rs index b9d5db2..ad27c6d 100644 --- a/src/duck.rs +++ b/archive/duck.rs @@ -1,3 +1,4 @@ +use bevy::ecs::resource; use mlua::UserData; use mlua::Lua; use bevy::prelude::*; @@ -11,16 +12,23 @@ use include_dir::{include_dir, Dir}; static DATA_DIR: Dir<'_> = include_dir!("data"); -struct BevyContext<'a> { - commands: Commands<'a, 'a> +struct Context<'a> { + commands: Commands<'a, 'a>, + resource: Option<&'a Resource> } -impl<'a> BevyContext<'a> { - fn from_raw(commands: Commands<'a,'a>) -> Self { - BevyContext { commands: commands } + +#[derive(Resource)] +struct Resource { + default_script_context: Entity +} +impl Resource { + fn new_from_world(world: &mut World) -> Self { + let mut context = Context { commands: world.commands(), resource: Option::None }; + let (entity_commands,_) = ScriptContext::new_entity( &mut context ); + Resource { + default_script_context: entity_commands.id() + } } - /*fn get_instance(&self, entity: Entity) -> &T { - self.commands.entity(entity). - }*/ } // Id for this object to the server @@ -31,7 +39,7 @@ trait Class { // Class name for filtering fn class_name() -> String { name_of_type!(Self).into() } // Create a default object - fn new_entity<'a>(context: &BevyContext) -> (EntityCommands<'a>, Self); + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>, Self) where Self: Sized; // fn clone(&self) -> Self; fn add_fields>(_fields: &mut F) {} fn add_methods>(_methods: &mut F) {} @@ -53,7 +61,7 @@ impl UserData for Instance { } } impl Instance { - fn new(context: &BevyContext) -> Self { + fn new_entity(context: &'_ mut Context) -> Self { let (entity,sub) = S::new_entity(context); Instance { name: S::class_name(), @@ -68,9 +76,11 @@ struct Constructor; impl UserData for Constructor { fn add_methods>(methods: &mut M) { - methods.add_method("part", |lua, this, ()| { - let part = IPart::new() - + methods.add_method("part",|lua, this: &Constructor, ()| { + Ok(IPart::new_entity(lua.app_data_mut::().unwrap().deref_mut())) + }); + methods.add_method("script",|lua, this: &Constructor, ()| { + Ok(IScript::new_entity(lua.app_data_mut::().unwrap().deref_mut())) }); } } @@ -80,7 +90,7 @@ struct ScriptContext { lua: Lua } impl Class for ScriptContext { - fn new_entity(context: &BevyContext) -> (EntityCommands<'_>,Self) { + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>,Self) { ( context.commands.spawn_empty(), ScriptContext::new() ) } } @@ -98,21 +108,30 @@ struct Script { context: Entity } impl Class for Script { - fn new_entity(context: &BevyContext) -> (Entity, Self) { - ( context.) + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>, Self) { + ( + context.commands.spawn_empty(), + Script { + context: context.resource.as_ref().unwrap().default_script_context + } + ) } } // Attachment struct Node; -impl Class for Node {} +impl Class for Node { + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>, Self) { + ( context.commands.spawn_empty(), Node {} ) + } +} struct Part { } impl Class for Part { - fn new_entity(context: &BevyContext) -> Self { - commands.spawn() + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>, Self) { + ( context.commands.spawn_empty(), Part {} ) } } impl UserData for Part {} @@ -129,9 +148,9 @@ impl UserData for Located { } } impl Class for Located { - fn new_entity(context: &BevyContext) -> Self { + fn new_entity<'a>(context: &'a mut Context) -> (EntityCommands<'a>,Self) { let (entity,sub) = S::new_entity(context); - Located { class: S::new_entity(context), } + ( entity, Located { class: sub } ) } } @@ -152,15 +171,27 @@ enum AnyInstance { Part(IPart) } -fn script_executor() { +fn script_executor(mut commands: Commands, resource: Res) { + +} +fn script_test(commands: Commands, resource: Res) { + let mut context = Context {commands: commands, resource: Some(resource.as_ref())}; + IScript::new_entity(&mut context); +} + +fn setup(world: &mut World) { + let res = Resource::new_from_world(world); + world.insert_resource(res); } pub struct DuckPlugin; impl Plugin for DuckPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update,(script_executor)); - app.insert_resource(ScriptContext::new_entity()) + app.add_systems(Startup,setup); + app.add_systems(Update,script_executor); + app.add_systems(Startup,script_test); + app.insert_resource(ScriptContext::new()); } } \ No newline at end of file diff --git a/src/duck/instance.rs b/src/duck/instance.rs new file mode 100644 index 0000000..ffe39b3 --- /dev/null +++ b/src/duck/instance.rs @@ -0,0 +1,49 @@ +use bevy::prelude::*; +use mlua::prelude::*; + +use crate::duck::part::*; +use crate::duck::script::*; + +pub trait Class { + fn new(entity: EntityWorldMut) -> AnyInstance; + fn add_fields>(_fields: &mut F) {} + fn add_methods>(_methods: &mut F) {} +} + +pub struct Instance(pub S); + +impl LuaUserData for Instance { + fn add_fields>(fields: &mut F) { + S::add_fields(fields) + } + fn add_methods>(methods: &mut M) { + S::add_methods(methods) + } +} + +impl Instance { + fn new_entity(world: &mut World) -> Entity { + let mut entity = world.spawn_empty(); + entity.entry().or_insert(Name::new("Instance")); + entity.insert(AnyInstance {sub: S::new(entity)}); + return entity.id(); + } +} + +#[derive(Component)] +pub enum AnyInstance { + Script(Instance