2024-09-18 00:17:12 +00:00
|
|
|
#[derive(Clone,Copy)]
|
|
|
|
pub struct Vector3(pub(crate)glam::Vec3A);
|
|
|
|
|
|
|
|
impl Vector3{
|
|
|
|
pub const fn new(x:f32,y:f32,z:f32)->Self{
|
|
|
|
Self(glam::vec3a(x,y,z))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 01:47:55 +00:00
|
|
|
impl Into<rbx_types::Vector3> for Vector3{
|
|
|
|
fn into(self)->rbx_types::Vector3{
|
|
|
|
rbx_types::Vector3::new(self.0.x,self.0.y,self.0.z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 00:17:12 +00:00
|
|
|
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()));
|
|
|
|
fields.add_field_method_get("x",|_,this| Ok(this.0.x));
|
|
|
|
fields.add_field_method_set("x",|_,this,val| {
|
|
|
|
this.0.x = val;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
fields.add_field_method_get("y",|_,this| Ok(this.0.y));
|
|
|
|
fields.add_field_method_set("y",|_,this,val| {
|
|
|
|
this.0.y = val;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
fields.add_field_method_get("z",|_,this| Ok(this.0.z));
|
|
|
|
fields.add_field_method_set("z",|_,this,val| {
|
|
|
|
this.0.z = val;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_methods<'lua,M: mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
|
|
|
//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)));
|
2024-09-28 19:31:41 +00:00
|
|
|
methods.add_meta_function(mlua::MetaMethod::Div,|_,(this,val):(Self,mlua::Value)|{
|
|
|
|
match val{
|
|
|
|
mlua::Value::Integer(n)=>Ok(Self(this.0/(n as f32))),
|
|
|
|
mlua::Value::Number(n)=>Ok(Self(this.0/(n as f32))),
|
|
|
|
mlua::Value::UserData(ud)=>{
|
|
|
|
let rhs:Vector3=ud.take()?;
|
|
|
|
Ok(Self(this.0/rhs.0))
|
|
|
|
},
|
|
|
|
other=>Err(mlua::Error::runtime(format!("Attempt to divide Vector3 by {other:?}"))),
|
|
|
|
}
|
|
|
|
});
|
2024-09-18 00:17:12 +00:00
|
|
|
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self|
|
|
|
|
Ok(format!("Vector3.new({},{},{})",
|
|
|
|
this.0.x,
|
|
|
|
this.0.y,
|
|
|
|
this.0.z,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua> mlua::FromLua<'lua> for Vector3{
|
|
|
|
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
2024-09-19 01:48:07 +00:00
|
|
|
match value{
|
|
|
|
mlua::Value::UserData(ud)=>ud.take(),
|
|
|
|
other=>Err(mlua::Error::runtime(format!("Expected Vector3 got {:?}",other))),
|
2024-09-18 00:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|