roblox_emulator: fixup Vector3

This commit is contained in:
Quaternions 2025-04-22 19:15:04 -07:00
parent 7649d30b55
commit 78593200eb
Signed by: Quaternions
GPG Key ID: D0DF5964F79AC131

@ -49,23 +49,20 @@ impl mlua::UserData for Vector3{
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){ fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
//methods.add_method("area",|_,this,()| Ok(this.length * this.width)); //methods.add_method("area",|_,this,()| Ok(this.length * this.width));
methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0))); methods.add_meta_function(mlua::MetaMethod::Add,|_,(Vector3(this),Vector3(val)):(Self,Self)|Ok(Self(this+val)));
methods.add_meta_function(mlua::MetaMethod::Div,|_,(this,val):(Self,mlua::Value)|{ methods.add_meta_function(mlua::MetaMethod::Div,|_,(Vector3(this),val):(Self,mlua::Value)|{
match val{ match val{
mlua::Value::Integer(n)=>Ok(Self(this.0/(n as f32))), mlua::Value::Integer(n)=>Ok(Self(this/(n as f32))),
mlua::Value::Number(n)=>Ok(Self(this.0/(n as f32))), mlua::Value::Number(n)=>Ok(Self(this/(n as f32))),
mlua::Value::UserData(ud)=>{ mlua::Value::UserData(ud)=>ud.borrow_scoped(|Vector3(rhs):&Vector3|Self(this/rhs)),
let rhs:Vector3=ud.take()?;
Ok(Self(this.0/rhs.0))
},
other=>Err(mlua::Error::runtime(format!("Attempt to divide Vector3 by {other:?}"))), other=>Err(mlua::Error::runtime(format!("Attempt to divide Vector3 by {other:?}"))),
} }
}); });
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self| methods.add_meta_function(mlua::MetaMethod::ToString,|_,Vector3(this):Self|
Ok(format!("Vector3.new({},{},{})", Ok(format!("Vector3.new({},{},{})",
this.0.x, this.x,
this.0.y, this.y,
this.0.z, this.z,
)) ))
); );
} }