From 010f7325d1cb1bf01a08e58b67cfd631befc821a Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 16 Mar 2023 18:44:54 +0100 Subject: [PATCH] Implement missing Vector3 constructors --- .../lib-roblox/src/datatypes/types/vector3.rs | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/lib-roblox/src/datatypes/types/vector3.rs b/packages/lib-roblox/src/datatypes/types/vector3.rs index 8f7a7a5..a6ab2fe 100644 --- a/packages/lib-roblox/src/datatypes/types/vector3.rs +++ b/packages/lib-roblox/src/datatypes/types/vector3.rs @@ -5,14 +5,14 @@ use glam::Vec3; use mlua::prelude::*; use rbx_dom_weak::types::Vector3 as RbxVector3; -use super::super::*; +use super::{super::*, EnumItem}; /** An implementation of the [Vector3](https://create.roblox.com/docs/reference/engine/datatypes/Vector3) Roblox datatype, backed by [`glam::Vec3`]. - This implements all documented properties & methods of the Vector3 - class as of March 2023, as well as the `new(x, y, z)` constructor. + This implements all documented properties, methods & + constructors of the Vector3 class as of March 2023. Note that this does not use native Luau vectors to simplify implementation and instead allow us to implement all abovementioned APIs accurately. @@ -29,6 +29,55 @@ impl Vector3 { datatype_table.set("zero", Vector3(Vec3::ZERO))?; datatype_table.set("one", Vector3(Vec3::ONE))?; // Constructors + datatype_table.set( + "fromAxis", + lua.create_function(|_, normal_id: EnumItem| { + if normal_id.parent.desc.name == "Axis" { + Ok(match normal_id.name.as_str() { + "X" => Vector3(Vec3::X), + "Y" => Vector3(Vec3::Y), + "Z" => Vector3(Vec3::Z), + name => { + return Err(LuaError::RuntimeError(format!( + "Axis '{}' is not known", + name + ))) + } + }) + } else { + Err(LuaError::RuntimeError(format!( + "EnumItem must be a Axis, got {}", + normal_id.parent.desc.name + ))) + } + })?, + )?; + datatype_table.set( + "fromNormalId", + lua.create_function(|_, normal_id: EnumItem| { + if normal_id.parent.desc.name == "NormalId" { + Ok(match normal_id.name.as_str() { + "Left" => Vector3(Vec3::X), + "Top" => Vector3(Vec3::Y), + "Front" => Vector3(-Vec3::Z), + "Right" => Vector3(-Vec3::X), + "Bottom" => Vector3(-Vec3::Y), + "Back" => Vector3(Vec3::Z), + name => { + return Err(LuaError::RuntimeError(format!( + "NormalId '{}' is not known", + name + ))) + } + }) + } else { + Err(LuaError::RuntimeError(format!( + "EnumItem must be a NormalId, got {}", + normal_id.parent.desc.name + ))) + } + })?, + )?; datatype_table.set( "new", lua.create_function(|_, (x, y, z): (Option, Option, Option)| { @@ -39,7 +88,6 @@ impl Vector3 { })) })?, ) - // FUTURE: Implement FromNormalId and FromAxis constructors? } }