begin Instance.__index properties implementation

This commit is contained in:
Quaternions 2024-10-04 19:28:32 -07:00
parent ba9918f2d5
commit 9030646f39
3 changed files with 62 additions and 3 deletions

View File

@ -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<rbx_types::CFrame> 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<rbx_types::CFrame> 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)))
)?
)?;

View File

@ -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::<super::cframe::CFrame>::into(cf).into_lua(lua)),
Some(&rbx_types::Variant::Vector3(v))=>return Ok(Into::<super::vector3::Vector3>::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)
)
})
});

View File

@ -28,6 +28,12 @@ impl Into<rbx_types::Vector3> for Vector3{
}
}
impl From<rbx_types::Vector3> 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()));