2024-09-17 17:17:12 -07: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-10-05 12:35:38 -07:00
|
|
|
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
2024-10-03 16:26:23 -07:00
|
|
|
let vector3_table=lua.create_table()?;
|
|
|
|
|
|
|
|
//Vector3.new
|
|
|
|
vector3_table.raw_set("new",
|
2024-10-05 11:52:18 -07:00
|
|
|
lua.create_function(|_,(x,y,z):(f32,f32,f32)|
|
|
|
|
Ok(Vector3::new(x,y,z))
|
2024-10-03 16:26:23 -07:00
|
|
|
)?
|
|
|
|
)?;
|
|
|
|
|
|
|
|
globals.set("Vector3",vector3_table)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-18 18:47:55 -07: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-10-04 19:28:32 -07:00
|
|
|
impl From<rbx_types::Vector3> for Vector3{
|
|
|
|
fn from(value:rbx_types::Vector3)->Vector3{
|
|
|
|
Vector3::new(value.x,value.y,value.z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:17:12 -07:00
|
|
|
impl mlua::UserData for Vector3{
|
2024-10-05 12:35:38 -07:00
|
|
|
fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
|
2024-10-05 12:19:55 -07:00
|
|
|
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;
|
2024-09-17 17:17:12 -07:00
|
|
|
Ok(())
|
|
|
|
});
|
2024-10-05 12:19:55 -07:00
|
|
|
fields.add_field_method_get("y",|_,this|Ok(this.0.y));
|
|
|
|
fields.add_field_method_set("y",|_,this,val|{
|
|
|
|
this.0.y=val;
|
2024-09-17 17:17:12 -07:00
|
|
|
Ok(())
|
|
|
|
});
|
2024-10-05 12:19:55 -07:00
|
|
|
fields.add_field_method_get("z",|_,this|Ok(this.0.z));
|
|
|
|
fields.add_field_method_set("z",|_,this,val|{
|
|
|
|
this.0.z=val;
|
2024-09-17 17:17:12 -07:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-05 12:35:38 -07:00
|
|
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
2024-09-17 17:17:12 -07:00
|
|
|
//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 12:31:41 -07: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-17 17:17:12 -07:00
|
|
|
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self|
|
|
|
|
Ok(format!("Vector3.new({},{},{})",
|
|
|
|
this.0.x,
|
|
|
|
this.0.y,
|
|
|
|
this.0.z,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:53:17 -07:00
|
|
|
type_from_lua_userdata!(Vector3);
|