From 3217b1b9fb680fc83423f8356489067f34e03fcc Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Sun, 18 May 2025 15:38:03 +0200 Subject: [PATCH] vec3 --- assets/tests/ruby/pass_vec3_from_script.rb | 3 + assets/tests/ruby/pass_vec3_to_script.rb | 7 +++ src/runtimes/ruby.rs | 25 ++++---- tests/tests.rs | 73 +++++++++++++++++++++- 4 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 assets/tests/ruby/pass_vec3_from_script.rb create mode 100644 assets/tests/ruby/pass_vec3_to_script.rb diff --git a/assets/tests/ruby/pass_vec3_from_script.rb b/assets/tests/ruby/pass_vec3_from_script.rb new file mode 100644 index 0000000..393422e --- /dev/null +++ b/assets/tests/ruby/pass_vec3_from_script.rb @@ -0,0 +1,3 @@ +def test_func + rust_func(Vec3.new(1.5, 2.5, -3.5)) +end diff --git a/assets/tests/ruby/pass_vec3_to_script.rb b/assets/tests/ruby/pass_vec3_to_script.rb new file mode 100644 index 0000000..fae4ac3 --- /dev/null +++ b/assets/tests/ruby/pass_vec3_to_script.rb @@ -0,0 +1,7 @@ +def test_func(vec3) + raise unless vec3.is_a?(Vec3) # TODO: BevyScriptum::Vec3 and add example how to include it globally like Sinatra does + raise unless vec3.x == 1.5 + raise unless vec3.y == 2.5 + raise unless vec3.z == -3.5 + mark_success +end diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index c0bf625..10d7b5f 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -166,19 +166,23 @@ impl TryConvert for BevyEntity { } #[derive(Clone)] -#[magnus::wrap(class = "BevyVec3")] +#[magnus::wrap(class = "Vec3")] pub struct BevyVec3(pub Vec3); impl BevyVec3 { - fn x(&self) -> f32 { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self(Vec3::new(x, y, z)) + } + + pub fn x(&self) -> f32 { self.0.x } - fn y(&self) -> f32 { + pub fn y(&self) -> f32 { self.0.y } - fn z(&self) -> f32 { + pub fn z(&self) -> f32 { self.0.z } } @@ -214,20 +218,13 @@ impl Default for RubyRuntime { .define_method("index", method!(BevyEntity::index, 0)) .unwrap(); - let vec3 = ruby.define_class("BevyVec3", ruby.class_object()).unwrap(); + let vec3 = ruby.define_class("Vec3", ruby.class_object()).unwrap(); + vec3.define_singleton_method("new", function!(BevyVec3::new, 3)) + .unwrap(); vec3.define_method("x", method!(BevyVec3::x, 0)).unwrap(); vec3.define_method("y", method!(BevyVec3::y, 0)).unwrap(); vec3.define_method("z", method!(BevyVec3::z, 0)).unwrap(); })); - // TODO: add test for those types for every runtime - // let vec3_constructor = engine - // .create_function(|_, (x, y, z)| Ok(BevyVec3(Vec3::new(x, y, z)))) - // .expect("Failed to create Vec3 constructor"); - // engine - // .globals() - // .set("Vec3", vec3_constructor) - // .expect("Failed to set Vec3 global"); - // Self { ruby_thread: Some(ruby_thread), } diff --git a/tests/tests.rs b/tests/tests.rs index 83b5332..81894fe 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -65,7 +65,7 @@ trait AssertStateKeyValue { #[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))] macro_rules! scripting_tests { - ($runtime:ty, $script:literal, $extension:literal, $entity_type: ty) => { + ($runtime:ty, $script:literal, $extension:literal, $entity_type: ty, $vec_type: ty) => { use super::*; #[test] @@ -474,6 +474,75 @@ macro_rules! scripting_tests { Some(entity.index()) ); } + + #[test] + fn pass_vec3_from_script() { + let mut app = build_test_app(); + + #[derive(Default, Resource)] + struct State { + success: bool, + } + + app.world_mut().init_resource::(); + + app.add_scripting::<$runtime>(|runtime| { + runtime.add_function( + String::from("rust_func"), + |In((v,)): In<($vec_type,)>, mut res: ResMut| { + assert_eq!(v.0.x, 1.5); + assert_eq!(v.0.y, 2.5); + assert_eq!(v.0.z, -3.5); + res.success = true + }, + ); + }); + + let entity = run_script::<$runtime, _, _>( + &mut app, + format!("tests/{}/pass_vec3_from_script.{}", $script, $extension).to_string(), + call_script_on_update_from_rust::<$runtime>, + ); + + assert!(app.world().get_resource::().unwrap().success); + } + + #[test] + fn pass_vec3_to_script() { + let mut app = build_test_app(); + + #[derive(Default, Resource)] + struct State { + success: bool, + } + + app.world_mut().init_resource::(); + + app.add_scripting::<$runtime>(|runtime| { + runtime.add_function(String::from("mark_success"), |mut res: ResMut| { + res.success = true + }); + }); + + run_script::<$runtime, _, _>( + &mut app, + format!("tests/{}/pass_vec3_to_script.{}", $script, $extension).to_string(), + |mut scripted_entities: Query<(Entity, &mut <$runtime as Runtime>::ScriptData)>, + scripting_runtime: ResMut<$runtime>| { + let (entity, mut script_data) = scripted_entities.single_mut().unwrap(); + scripting_runtime + .call_fn( + "test_func", + &mut script_data, + entity, + (<$vec_type>::new(1.5, 2.5, -3.5),), + ) + .unwrap(); + }, + ); + + assert!(app.world().get_resource::().unwrap().success); + } }; } @@ -598,5 +667,5 @@ mod ruby_tests { } } - scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity); + scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity, BevyVec3); }