line 206
This commit is contained in:
parent
ef6ae30394
commit
1ac594f195
147
src/main.rs
147
src/main.rs
@ -25,77 +25,86 @@ fn main()->Result<(),anyhow::Error>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Vector3(glam::Vec3);
|
#[derive(Clone,Copy)]
|
||||||
|
struct Vector3(glam::Vec3A);
|
||||||
|
|
||||||
impl Vector3{
|
impl Vector3{
|
||||||
fn new(x:f32,y:f32,z:f32)->Self{
|
fn new(x:f32,y:f32,z:f32)->Self{
|
||||||
Self(glam::vec3(x,y,z))
|
Self(glam::vec3a(x,y,z))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl mlua::UserData for Vector3{
|
impl mlua::UserData for Vector3{
|
||||||
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
|
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("magnitude",|_,this| Ok(this.0.length()));
|
||||||
fields.add_field_method_get("x", |_, this| Ok(this.0.x));
|
fields.add_field_method_get("x",|_,this| Ok(this.0.x));
|
||||||
fields.add_field_method_set("x", |_, this, val| {
|
fields.add_field_method_set("x",|_,this,val| {
|
||||||
this.0.x = val;
|
this.0.x = val;
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
fields.add_field_method_get("y", |_, this| Ok(this.0.y));
|
fields.add_field_method_get("y",|_,this| Ok(this.0.y));
|
||||||
fields.add_field_method_set("y", |_, this, val| {
|
fields.add_field_method_set("y",|_,this,val| {
|
||||||
this.0.y = val;
|
this.0.y = val;
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
fields.add_field_method_get("z", |_, this| Ok(this.0.z));
|
fields.add_field_method_get("z",|_,this| Ok(this.0.z));
|
||||||
fields.add_field_method_set("z", |_, this, val| {
|
fields.add_field_method_set("z",|_,this,val| {
|
||||||
this.0.z = val;
|
this.0.z = val;
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua,M: mlua::UserDataMethods<'lua,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_method(mlua::MetaMethod::Add, |_, this, val:&Self| Ok(Self(this.0+val.0)));
|
methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'lua> mlua::FromLua<'lua> for &Vector3{
|
impl<'lua> mlua::FromLua<'lua> for Vector3{
|
||||||
fn from_lua(value:mlua::prelude::LuaValue<'lua>,lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||||||
value.as_userdata()
|
match value {
|
||||||
.ok_or(mlua::prelude::LuaError::FromLuaConversionError{from:"LuaValue",to:"UserData",message:None})?
|
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
|
||||||
.user_value()
|
_ => unreachable!(),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CFrame{
|
|
||||||
translation:glam::Vec3,
|
|
||||||
rotation:glam::Mat3,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CFrame{
|
|
||||||
fn new(x:f32,y:f32,z:f32)->Self{
|
|
||||||
Self{
|
|
||||||
translation:glam::vec3(x,y,z),
|
|
||||||
rotation:glam::Mat3::IDENTITY,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone,Copy)]
|
||||||
|
struct CFrame(glam::Affine3A);
|
||||||
|
|
||||||
|
impl CFrame{
|
||||||
|
fn new(x:f32,y:f32,z:f32)->Self{
|
||||||
|
Self(glam::Affine3A::from_translation(glam::vec3(x,y,z)))
|
||||||
|
}
|
||||||
|
fn angles(x:f32,y:f32,z:f32)->Self{
|
||||||
|
Self(glam::Affine3A::from_mat3(glam::Mat3::from_euler(glam::EulerRot::YXZ,y,x,z)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl mlua::UserData for CFrame{
|
impl mlua::UserData for CFrame{
|
||||||
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
|
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||||
fields.add_field_method_get("p", |ctx, this| Ok(Vector3(this.translation)));
|
//CFrame.p
|
||||||
|
fields.add_field_method_get("p",|_,this|Ok(Vector3(this.0.translation)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,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_method(mlua::MetaMethod::Mul, |_, this, val:&Vector3| Ok(Vector3(this.rotation*val.0+this.translation)));
|
//methods.add_meta_method(mlua::MetaMethod::Mul,|_,this,val:&Vector3|Ok(Vector3(this.0.matrix3*val.0+this.0.translation)));
|
||||||
|
methods.add_meta_function(mlua::MetaMethod::Mul,|_,(this,val):(Self,Self)|Ok(Self(this.0*val.0)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'lua> mlua::FromLua<'lua> for CFrame{
|
||||||
|
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!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
|
fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
|
||||||
|
|
||||||
//read the file
|
//read the file
|
||||||
let script={
|
let script={
|
||||||
let mut file=std::fs::File::open(script_path)?;
|
let mut file=std::fs::File::open(script_path)?;
|
||||||
@ -106,35 +115,55 @@ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
|
|||||||
|
|
||||||
let lua=mlua::Lua::new();
|
let lua=mlua::Lua::new();
|
||||||
|
|
||||||
|
//global environment
|
||||||
|
let globals=lua.globals();
|
||||||
|
|
||||||
//Vector3
|
//Vector3
|
||||||
let vector3=lua.create_table()?;
|
{
|
||||||
|
let vector3_table=lua.create_table()?;
|
||||||
let new_vec=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{
|
|
||||||
let v=ctx.create_userdata(Vector3::new(x,y,z));
|
//Vector3.new
|
||||||
Ok(v)
|
vector3_table.raw_set("new",
|
||||||
})?;
|
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
|
||||||
|
Ok(ctx.create_userdata(Vector3::new(x,y,z)))
|
||||||
vector3.raw_set("new",new_vec)?;
|
)?
|
||||||
|
)?;
|
||||||
lua.globals().set("Vector3",vector3)?;
|
|
||||||
|
globals.set("Vector3",vector3_table)?;
|
||||||
|
}
|
||||||
|
|
||||||
//CFrame
|
//CFrame
|
||||||
let cframe=lua.create_table()?;
|
{
|
||||||
|
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)))
|
||||||
|
)?
|
||||||
|
)?;
|
||||||
|
|
||||||
|
//CFrame.Angles
|
||||||
|
cframe_table.raw_set("Angles",
|
||||||
|
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
|
||||||
|
Ok(ctx.create_userdata(CFrame::angles(x,y,z)))
|
||||||
|
)?
|
||||||
|
)?;
|
||||||
|
|
||||||
|
globals.set("CFrame",cframe_table)?;
|
||||||
|
}
|
||||||
|
|
||||||
let new_cf=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{
|
//script
|
||||||
let v=ctx.create_userdata(CFrame::new(x,y,z));
|
{
|
||||||
Ok(v)
|
let script_table=lua.create_table()?;
|
||||||
})?;
|
//script.Name
|
||||||
|
script_table.raw_set("Name","Placeholder")?;
|
||||||
cframe.raw_set("new",new_cf)?;
|
globals.set("script",script_table)?;
|
||||||
|
}
|
||||||
lua.globals().set("CFrame",cframe)?;
|
|
||||||
|
|
||||||
match lua.load(script).exec(){
|
match lua.load(script).exec(){
|
||||||
Ok(())=>(),
|
Ok(())=>(),
|
||||||
Err(e)=>{
|
Err(e)=>println!("Lua Error: {e}"),
|
||||||
println!("Error: {e}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user