make boosters work

This commit is contained in:
Quaternions 2024-09-18 20:16:14 -07:00
parent 2dddda685a
commit a394778a8e

View File

@ -29,6 +29,14 @@ fn dom<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua:
f(&mut *lua.app_data_mut::<WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?) f(&mut *lua.app_data_mut::<WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?)
} }
fn coerce_float(value:&mlua::Value)->Option<f32>{
match value{
&mlua::Value::Integer(i)=>Some(i as f32),
&mlua::Value::Number(f)=>Some(f as f32),
_=>None,
}
}
impl mlua::UserData for Instance{ impl mlua::UserData for Instance{
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("Parent",|lua,this|{ fields.add_field_method_get("Parent",|lua,this|{
@ -46,6 +54,24 @@ impl mlua::UserData for Instance{
} }
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("GetChildren",|lua,this,_:()|
dom(lua,|dom|{
let instance=this.get(dom)?;
let children:Vec<_>=instance
.children()
.iter()
.copied()
.map(Instance::new)
.collect();
Ok(children)
})
);
methods.add_method("IsA",|lua,this,classname:mlua::String|
dom(lua,|dom|{
let instance=this.get(dom)?;
Ok(crate::context::class_is_a(instance.class.as_str(),classname.to_str()?))
})
);
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)| methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)|
dom(lua,|dom|{ dom(lua,|dom|{
let instance=this.get_mut(dom)?; let instance=this.get_mut(dom)?;
@ -57,11 +83,11 @@ impl mlua::UserData for Instance{
let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?;
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into())); instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into()));
}, },
rbx_types::Variant::Float64(_)=>{ rbx_types::Variant::Float32(_)=>{
let typed_value:f64=value.as_f64().ok_or(mlua::Error::runtime("Expected f64"))?; let typed_value:f32=coerce_float(&value).ok_or(mlua::Error::runtime("Expected f32"))?;
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float64(typed_value)); instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value));
}, },
_=>unimplemented!(), other=>println!("Unimplemented property type: {other:?}"),
} }
Ok(()) Ok(())
}) })