Compare commits

...

6 Commits

Author SHA1 Message Date
f4dc80713f Instance.Destroy 2024-10-04 19:32:30 -07:00
9030646f39 begin Instance.__index properties implementation 2024-10-04 19:28:32 -07:00
ba9918f2d5 None maps to Nil ezpz 2024-10-04 19:09:07 -07:00
acbb1c8478 workspace.Terrain 2024-10-04 19:09:07 -07:00
96c8b1035a game.workspace 2024-10-04 19:09:07 -07:00
67223efa26 find_first_child_of_class 2024-10-04 19:09:07 -07:00
3 changed files with 128 additions and 24 deletions

View File

@ -4,7 +4,22 @@ use super::vector3::Vector3;
pub struct CFrame(pub(crate)glam::Affine3A);
impl CFrame{
pub fn new(x:f32,y:f32,z:f32)->Self{
pub fn new(
x:f32,y:f32,z:f32,
xx:f32,yx:f32,zx:f32,
xy:f32,yy:f32,zy:f32,
xz:f32,yz:f32,zz:f32,
)->Self{
Self(glam::Affine3A::from_mat3_translation(
glam::mat3(
glam::vec3(xx,yx,zx),
glam::vec3(xy,yy,zy),
glam::vec3(xz,yz,zz)
),
glam::vec3(x,y,z)
))
}
pub fn point(x:f32,y:f32,z:f32)->Self{
Self(glam::Affine3A::from_translation(glam::vec3(x,y,z)))
}
pub fn angles(x:f32,y:f32,z:f32)->Self{
@ -12,13 +27,45 @@ impl CFrame{
}
}
fn vec3_to_glam(v:glam::Vec3A)->rbx_types::Vector3{
rbx_types::Vector3::new(v.x,v.y,v.z)
}
fn vec3_from_glam(v:rbx_types::Vector3)->glam::Vec3A{
glam::vec3a(v.x,v.y,v.z)
}
impl Into<rbx_types::CFrame> for CFrame{
fn into(self)->rbx_types::CFrame{
rbx_types::CFrame::new(
vec3_to_glam(self.0.translation),
rbx_types::Matrix3::new(
vec3_to_glam(self.0.matrix3.x_axis),
vec3_to_glam(self.0.matrix3.y_axis),
vec3_to_glam(self.0.matrix3.z_axis),
)
)
}
}
impl From<rbx_types::CFrame> for CFrame{
fn from(value:rbx_types::CFrame)->Self{
CFrame(glam::Affine3A{
matrix3:glam::mat3a(
vec3_from_glam(value.orientation.x),
vec3_from_glam(value.orientation.y),
vec3_from_glam(value.orientation.z),
),
translation:vec3_from_glam(value.position)
})
}
}
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
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)))
Ok(ctx.create_userdata(CFrame::point(x,y,z)))
)?
)?;

View File

@ -29,6 +29,10 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
full_name
}
pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class)
}
//workaround until I have an enum of classes
struct Dereferent(Ref);
impl mlua::UserData for Dereferent{}
@ -167,20 +171,37 @@ impl Instance{
};
dom(lua,|dom|{
let instance=this.get(dom)?;
let child=match search_descendants{
true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str),
false=>instance.children().iter().filter_map(|&r|
dom.get_by_ref(r)
).find(|inst|inst.name==name_str),
};
match child{
Some(instance)=>Instance::new(instance.referent()).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
}
Ok(
match search_descendants{
true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str),
false=>instance.children().iter().filter_map(|&r|
dom.get_by_ref(r)
).find(|inst|inst.name==name_str),
}
.map(|instance|
Instance::new(instance.referent())
)
)
})
};
methods.add_method("FindFirstChild",ffc);
methods.add_method("WaitForChild",ffc);
methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,mlua::Value)|{
let class_str=class.to_str()?;
let search_descendants=match search_descendants{
mlua::Value::Nil=>false,
mlua::Value::Boolean(b)=>b,
_=>Err(mlua::Error::runtime("Invalid argument #3 bool expected"))?,
};
if search_descendants==true{
return Err(mlua::Error::runtime("FFC of class searching descendants not supported get rekt"));
}
dom(lua,|dom|{
Ok(find_first_child_of_class(dom,this.get(dom)?,class_str)
.map(|inst|(Instance::new(inst.referent())))
)
})
});
methods.add_method("GetDescendants",|lua,this,_:()|
dom(lua,|dom|{
let children:Vec<_>=dom
@ -198,22 +219,33 @@ impl Instance{
Ok(crate::context::class_is_a(instance.class.as_str(),classname.to_str()?))
})
);
methods.add_method("Destroy",|lua,this,()|
dom(lua,|dom|{
dom.destroy(this.referent());
Ok(())
})
);
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(mlua::AnyUserData,mlua::String)|{
let index_str=index.to_str()?;
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
dom(lua,|dom|{
let instance=dereferent.get(dom)?;
//find a child with a matching name
let maybe_child=instance.children()
.iter()
.find(|&&r|
dom.get_by_ref(r)
.is_some_and(|instance|instance.name==index_str)
);
match maybe_child{
Some(&referent)=>Instance::new(referent).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
match instance.properties.get(index_str){
Some(&rbx_types::Variant::CFrame(cf))=>return Ok(Into::<super::cframe::CFrame>::into(cf).into_lua(lua)),
Some(&rbx_types::Variant::Vector3(v))=>return Ok(Into::<super::vector3::Vector3>::into(v).into_lua(lua)),
//None=>get_default_value
_=>(),
}
//find a child with a matching name
Ok(
instance.children()
.iter()
.find(|&&r|
dom.get_by_ref(r)
.is_some_and(|instance|instance.name==index_str)
)
.map(|&referent|Instance::new(referent)).into_lua(lua)
)
})
});
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(mlua::AnyUserData,mlua::String,mlua::Value)|{
@ -278,6 +310,13 @@ class!(DataModel);
class_composition!(DataModel,(Instance,DataModel));
impl DataModel{
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
fields.add_field_method_get("workspace",|lua,this|{
dom(lua,|dom|{
Ok(find_first_child_of_class(dom,this.get(dom)?,"Workspace")
.map(|inst|Workspace::new(inst.referent()))
)
})
});
fields.add_field_method_get("PlaceId",|lua,this|{
Ok(mlua::Value::Integer(0))
});
@ -306,8 +345,20 @@ impl DataModel{
}
class!(Workspace);
class_composition!(Workspace,(Instance));
class_composition!(Workspace,(Instance,Workspace));
impl Workspace{
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
fields.add_field_method_get("Terrain",|lua,this|{
dom(lua,|dom|{
Ok(find_first_child_of_class(dom,this.get(dom)?,"Terrain")
.map(|inst|Terrain::new(inst.referent()))
)
})
});
}
fn composition_add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(_methods:&mut M){
}
}
class!(Lighting);
class_composition!(Lighting,(Instance));

View File

@ -28,6 +28,12 @@ impl Into<rbx_types::Vector3> for Vector3{
}
}
impl From<rbx_types::Vector3> for Vector3{
fn from(value:rbx_types::Vector3)->Vector3{
Vector3::new(value.x,value.y,value.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()));