Ruby support #1
4 changed files with 92 additions and 16 deletions
3
assets/tests/ruby/pass_vec3_from_script.rb
Normal file
3
assets/tests/ruby/pass_vec3_from_script.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
def test_func
|
||||||
|
rust_func(Vec3.new(1.5, 2.5, -3.5))
|
||||||
|
end
|
||||||
7
assets/tests/ruby/pass_vec3_to_script.rb
Normal file
7
assets/tests/ruby/pass_vec3_to_script.rb
Normal file
|
|
@ -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
|
||||||
|
|
@ -166,19 +166,23 @@ impl TryConvert for BevyEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
#[magnus::wrap(class = "BevyVec3")]
|
#[magnus::wrap(class = "Vec3")]
|
||||||
pub struct BevyVec3(pub Vec3);
|
pub struct BevyVec3(pub Vec3);
|
||||||
|
|
||||||
impl BevyVec3 {
|
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
|
self.0.x
|
||||||
}
|
}
|
||||||
|
|
||||||
fn y(&self) -> f32 {
|
pub fn y(&self) -> f32 {
|
||||||
self.0.y
|
self.0.y
|
||||||
}
|
}
|
||||||
|
|
||||||
fn z(&self) -> f32 {
|
pub fn z(&self) -> f32 {
|
||||||
self.0.z
|
self.0.z
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -214,20 +218,13 @@ impl Default for RubyRuntime {
|
||||||
.define_method("index", method!(BevyEntity::index, 0))
|
.define_method("index", method!(BevyEntity::index, 0))
|
||||||
.unwrap();
|
.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("x", method!(BevyVec3::x, 0)).unwrap();
|
||||||
vec3.define_method("y", method!(BevyVec3::y, 0)).unwrap();
|
vec3.define_method("y", method!(BevyVec3::y, 0)).unwrap();
|
||||||
vec3.define_method("z", method!(BevyVec3::z, 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 {
|
Self {
|
||||||
ruby_thread: Some(ruby_thread),
|
ruby_thread: Some(ruby_thread),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ trait AssertStateKeyValue {
|
||||||
|
|
||||||
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
|
||||||
macro_rules! scripting_tests {
|
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::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -474,6 +474,75 @@ macro_rules! scripting_tests {
|
||||||
Some(entity.index())
|
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::<State>();
|
||||||
|
|
||||||
|
app.add_scripting::<$runtime>(|runtime| {
|
||||||
|
runtime.add_function(
|
||||||
|
String::from("rust_func"),
|
||||||
|
|In((v,)): In<($vec_type,)>, mut res: ResMut<State>| {
|
||||||
|
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::<State>().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::<State>();
|
||||||
|
|
||||||
|
app.add_scripting::<$runtime>(|runtime| {
|
||||||
|
runtime.add_function(String::from("mark_success"), |mut res: ResMut<State>| {
|
||||||
|
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::<State>().unwrap().success);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -598,5 +667,5 @@ mod ruby_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity);
|
scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity, BevyVec3);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue