From 9030646f39f997684a1cc90892dc6a5d06b7f677 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 4 Oct 2024 19:28:32 -0700 Subject: [PATCH] begin Instance.__index properties implementation --- src/runner/cframe.rs | 51 ++++++++++++++++++++++++++++++++++++++++-- src/runner/instance.rs | 8 ++++++- src/runner/vector3.rs | 6 +++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/runner/cframe.rs b/src/runner/cframe.rs index 8942578..8dfcbd9 100644 --- a/src/runner/cframe.rs +++ b/src/runner/cframe.rs @@ -4,7 +4,22 @@ use super::vector3::Vector3; pub struct CFrame(pub(crate)glam::Affine3A); impl CFrame{ - pub fn new(x:f32,y:f32,z:f32)->Self{ + pub fn new( + x:f32,y:f32,z:f32, + xx:f32,yx:f32,zx:f32, + xy:f32,yy:f32,zy:f32, + xz:f32,yz:f32,zz:f32, + )->Self{ + Self(glam::Affine3A::from_mat3_translation( + glam::mat3( + glam::vec3(xx,yx,zx), + glam::vec3(xy,yy,zy), + glam::vec3(xz,yz,zz) + ), + glam::vec3(x,y,z) + )) + } + pub fn point(x:f32,y:f32,z:f32)->Self{ Self(glam::Affine3A::from_translation(glam::vec3(x,y,z))) } pub fn angles(x:f32,y:f32,z:f32)->Self{ @@ -12,13 +27,45 @@ impl CFrame{ } } +fn vec3_to_glam(v:glam::Vec3A)->rbx_types::Vector3{ + rbx_types::Vector3::new(v.x,v.y,v.z) +} +fn vec3_from_glam(v:rbx_types::Vector3)->glam::Vec3A{ + glam::vec3a(v.x,v.y,v.z) +} + +impl Into for CFrame{ + fn into(self)->rbx_types::CFrame{ + rbx_types::CFrame::new( + vec3_to_glam(self.0.translation), + rbx_types::Matrix3::new( + vec3_to_glam(self.0.matrix3.x_axis), + vec3_to_glam(self.0.matrix3.y_axis), + vec3_to_glam(self.0.matrix3.z_axis), + ) + ) + } +} +impl From for CFrame{ + fn from(value:rbx_types::CFrame)->Self{ + CFrame(glam::Affine3A{ + matrix3:glam::mat3a( + vec3_from_glam(value.orientation.x), + vec3_from_glam(value.orientation.y), + vec3_from_glam(value.orientation.z), + ), + translation:vec3_from_glam(value.position) + }) + } +} + pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{ let cframe_table=lua.create_table()?; //CFrame.new cframe_table.raw_set("new", lua.create_function(|ctx,(x,y,z):(f32,f32,f32)| - Ok(ctx.create_userdata(CFrame::new(x,y,z))) + Ok(ctx.create_userdata(CFrame::point(x,y,z))) )? )?; diff --git a/src/runner/instance.rs b/src/runner/instance.rs index ed695ad..49714e0 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -224,6 +224,12 @@ impl Instance{ let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?; dom(lua,|dom|{ let instance=dereferent.get(dom)?; + match instance.properties.get(index_str){ + Some(&rbx_types::Variant::CFrame(cf))=>return Ok(Into::::into(cf).into_lua(lua)), + Some(&rbx_types::Variant::Vector3(v))=>return Ok(Into::::into(v).into_lua(lua)), + //None=>get_default_value + _=>(), + } //find a child with a matching name Ok( instance.children() @@ -232,7 +238,7 @@ impl Instance{ dom.get_by_ref(r) .is_some_and(|instance|instance.name==index_str) ) - .map(|&referent|Instance::new(referent)) + .map(|&referent|Instance::new(referent)).into_lua(lua) ) }) }); diff --git a/src/runner/vector3.rs b/src/runner/vector3.rs index bff57a6..e6c09cd 100644 --- a/src/runner/vector3.rs +++ b/src/runner/vector3.rs @@ -28,6 +28,12 @@ impl Into for Vector3{ } } +impl From for Vector3{ + fn from(value:rbx_types::Vector3)->Vector3{ + Vector3::new(value.x,value.y,value.z) + } +} + impl mlua::UserData for Vector3{ fn add_fields<'lua,F: mlua::UserDataFields<'lua,Self>>(fields: &mut F) { fields.add_field_method_get("magnitude",|_,this| Ok(this.0.length()));