Implement variadic versions of CFrame methods (#85)

This commit is contained in:
David C 2023-08-14 13:43:01 -07:00 committed by GitHub
parent 0ab32ffffd
commit c86190def1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

@ -2,7 +2,7 @@ use core::fmt;
use std::ops; use std::ops;
use glam::{EulerRot, Mat4, Quat, Vec3}; use glam::{EulerRot, Mat4, Quat, Vec3};
use mlua::prelude::*; use mlua::{prelude::*, Variadic};
use rbx_dom_weak::types::{CFrame as DomCFrame, Matrix3 as DomMatrix3, Vector3 as DomVector3}; use rbx_dom_weak::types::{CFrame as DomCFrame, Matrix3 as DomMatrix3, Vector3 as DomVector3};
use super::{super::*, Vector3}; use super::{super::*, Vector3};
@ -210,29 +210,46 @@ impl LuaUserData for CFrame {
translation, translation,
))) )))
}); });
methods.add_method("ToWorldSpace", |_, this, rhs: LuaUserDataRef<CFrame>| { methods.add_method(
Ok(*this * *rhs) "ToWorldSpace",
}); |_, this, rhs: Variadic<LuaUserDataRef<CFrame>>| {
methods.add_method("ToObjectSpace", |_, this, rhs: LuaUserDataRef<CFrame>| { Ok(Variadic::from_iter(rhs.into_iter().map(|cf| *this * *cf)))
Ok(this.inverse() * *rhs) },
}); );
methods.add_method(
"ToObjectSpace",
|_, this, rhs: Variadic<LuaUserDataRef<CFrame>>| {
let inverse = this.inverse();
Ok(Variadic::from_iter(rhs.into_iter().map(|cf| inverse * *cf)))
},
);
methods.add_method( methods.add_method(
"PointToWorldSpace", "PointToWorldSpace",
|_, this, rhs: LuaUserDataRef<Vector3>| Ok(*this * *rhs), |_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| *this * *v3)))
},
); );
methods.add_method( methods.add_method(
"PointToObjectSpace", "PointToObjectSpace",
|_, this, rhs: LuaUserDataRef<Vector3>| Ok(this.inverse() * *rhs), |_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let inverse = this.inverse();
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| inverse * *v3)))
},
); );
methods.add_method( methods.add_method(
"VectorToWorldSpace", "VectorToWorldSpace",
|_, this, rhs: LuaUserDataRef<Vector3>| Ok((*this - Vector3(this.position())) * *rhs), |_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let result = *this - Vector3(this.position());
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| result * *v3)))
},
); );
methods.add_method( methods.add_method(
"VectorToObjectSpace", "VectorToObjectSpace",
|_, this, rhs: LuaUserDataRef<Vector3>| { |_, this, rhs: Variadic<LuaUserDataRef<Vector3>>| {
let inv = this.inverse(); let inverse = this.inverse();
Ok((inv - Vector3(inv.position())) * *rhs) let result = inverse - Vector3(inverse.position());
Ok(Variadic::from_iter(rhs.into_iter().map(|v3| result * *v3)))
}, },
); );
#[rustfmt::skip] #[rustfmt::skip]

View file

@ -103,6 +103,9 @@ local offset = CFrame.new(0, 0, -5)
assert(offset:ToWorldSpace(offset).Z == offset.Z * 2) assert(offset:ToWorldSpace(offset).Z == offset.Z * 2)
assert(offset:ToObjectSpace(offset).Z == 0) assert(offset:ToObjectSpace(offset).Z == 0)
assert(select("#", offset:ToWorldSpace(offset, offset, offset)) == 3)
assert(select("#", offset:ToObjectSpace(offset, offset, offset)) == 3)
local world = CFrame.fromOrientation(0, math.rad(90), 0) * CFrame.new(0, 0, -5) local world = CFrame.fromOrientation(0, math.rad(90), 0) * CFrame.new(0, 0, -5)
local world2 = CFrame.fromOrientation(0, -math.rad(90), 0) * CFrame.new(0, 0, -5) local world2 = CFrame.fromOrientation(0, -math.rad(90), 0) * CFrame.new(0, 0, -5)
assertEq(CFrame.identity:ToObjectSpace(world), world) assertEq(CFrame.identity:ToObjectSpace(world), world)