This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement cframe type * add entry to changelog * fix copy paste typo * refactor cframe.new * address pr comments * refactor cframe constructor * rename cframe userdata struct
- Loading branch information
1 parent
053f85f
commit 51edda7
Showing
5 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use rbx_dom_weak::types::{CFrame, Matrix3, Vector3}; | ||
use rlua::{UserData, UserDataMethods, Value as LuaValue}; | ||
|
||
use crate::value::{CFrameValue, Vector3Value}; | ||
|
||
pub struct CFrameUserData; | ||
|
||
impl CFrameUserData { | ||
fn from_position(x: f32, y: f32, z: f32) -> CFrameValue { | ||
CFrameValue::new(CFrame::new( | ||
Vector3::new(x as f32, y as f32, z as f32), | ||
// TODO: replace with `Matrix3::identity()` once | ||
// a version higher than 0.3.0 of rbx_types ships | ||
Matrix3::new( | ||
Vector3::new(1.0, 0.0, 0.0), | ||
Vector3::new(0.0, 1.0, 0.0), | ||
Vector3::new(0.0, 0.0, 1.0), | ||
), | ||
)) | ||
} | ||
} | ||
|
||
fn try_into_f32(value: LuaValue<'_>) -> Option<f32> { | ||
match value { | ||
LuaValue::Number(num) => Some(num as f32), | ||
LuaValue::Integer(int) => Some(int as f32), | ||
_ => None, | ||
} | ||
} | ||
|
||
impl UserData for CFrameUserData { | ||
fn add_methods<'lua, T: UserDataMethods<'lua, Self>>(methods: &mut T) { | ||
methods.add_function( | ||
"new", | ||
|_context, | ||
arguments: ( | ||
Option<LuaValue<'_>>, | ||
Option<LuaValue<'_>>, | ||
Option<LuaValue<'_>>, | ||
)| { | ||
match arguments { | ||
(None, None, None) => return Ok(Self::from_position(0.0, 0.0, 0.0)), | ||
(Some(LuaValue::UserData(user_data)), None, None) => { | ||
let position = &*user_data.borrow::<Vector3Value>()?; | ||
return Ok(CFrameValue::new(CFrame::new( | ||
position.inner(), | ||
// TODO: replace with `rbx_dom_weak::types::Matrix3::identity()` once | ||
// a version higher than 0.3.0 of rbx_types ships | ||
Matrix3::new( | ||
Vector3::new(1.0, 0.0, 0.0), | ||
Vector3::new(0.0, 1.0, 0.0), | ||
Vector3::new(0.0, 0.0, 1.0), | ||
), | ||
))); | ||
} | ||
_ => {} | ||
}; | ||
|
||
let x = arguments.0.and_then(try_into_f32); | ||
let y = arguments.1.and_then(try_into_f32); | ||
let z = arguments.2.and_then(try_into_f32); | ||
|
||
match (x, y, z) { | ||
(Some(x), Some(y), Some(z)) => Ok(Self::from_position(x, y, z)), | ||
_ => Err(rlua::Error::external( | ||
"invalid argument #1 to 'new' (Vector3 expected)", | ||
)), | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
local function assertCFramePosition(vec, x, y, z) | ||
assert(vec.X == x, ("%f ~= %f"):format(vec.X, x)) | ||
assert(vec.Y == y, ("%f ~= %f"):format(vec.Y, y)) | ||
assert(vec.Z == z, ("%f ~= %f"):format(vec.Z, z)) | ||
end | ||
|
||
local function assertVector(vec, x, y, z) | ||
assert(vec.X == x, ("x: %f ~= %f (%s)"):format(vec.X, x, tostring(vec))) | ||
assert(vec.Y == y, ("y: %f ~= %f (%s)"):format(vec.Y, y, tostring(vec))) | ||
assert(vec.Z == z, ("z: %f ~= %f (%s)"):format(vec.Z, z, tostring(vec))) | ||
end | ||
|
||
-- new with combinations of integer and floats | ||
assertCFramePosition(CFrame.new(), 0, 0, 0) | ||
assertCFramePosition(CFrame.new(1, 2, 3), 1, 2, 3) | ||
assertCFramePosition(CFrame.new(1.5, 2, 3), 1.5, 2, 3) | ||
assertCFramePosition(CFrame.new(1, 2.5, 3), 1, 2.5, 3) | ||
assertCFramePosition(CFrame.new(1, 2, 3.5), 1, 2, 3.5) | ||
assertCFramePosition(CFrame.new(1.5, 2.5, 3), 1.5, 2.5, 3) | ||
assertCFramePosition(CFrame.new(1, 2.5, 3.5), 1, 2.5, 3.5) | ||
assertCFramePosition(CFrame.new(1.5, 2.5, 3.5), 1.5, 2.5, 3.5) | ||
|
||
-- new from Vector3 | ||
assertCFramePosition(CFrame.new(Vector3.new(1, 2, 3)), 1, 2, 3) | ||
|
||
-- properties | ||
assertVector(CFrame.new().XVector, 1, 0, 0) | ||
assertVector(CFrame.new().YVector, 0, 1, 0) | ||
assertVector(CFrame.new().ZVector, 0, 0, 1) | ||
|
||
assertVector(CFrame.new().RightVector, 1, 0, 0) | ||
assertVector(CFrame.new().UpVector, 0, 1, 0) | ||
assertVector(CFrame.new().LookVector, -0, -0, -1) | ||
|
||
assert(tostring(CFrame.new(7, 8, 9)) == "7, 8, 9, 1, 0, 0, 0, 1, 0, 0, 0, 1", "got " .. tostring(CFrame.new())) |