forked from StrafesNET/strafe-project
52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
|
#[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))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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)));
|
||
|
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>{
|
||
|
match value {
|
||
|
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
|
||
|
_ => unreachable!(),
|
||
|
}
|
||
|
}
|
||
|
}
|