diff --git a/crates/lune-roblox/src/datatypes/types/vector2.rs b/crates/lune-roblox/src/datatypes/types/vector2.rs index e0c352e..343fb70 100644 --- a/crates/lune-roblox/src/datatypes/types/vector2.rs +++ b/crates/lune-roblox/src/datatypes/types/vector2.rs @@ -80,6 +80,7 @@ impl LuaUserData for Vector2 { methods.add_meta_method(LuaMetaMethod::Sub, userdata_impl_sub); methods.add_meta_method(LuaMetaMethod::Mul, userdata_impl_mul_f32); methods.add_meta_method(LuaMetaMethod::Div, userdata_impl_div_f32); + methods.add_meta_method(LuaMetaMethod::IDiv, userdata_impl_idiv_f32); } } @@ -138,6 +139,20 @@ impl ops::Div for Vector2 { } } +impl IDiv for Vector2 { + type Output = Vector2; + fn idiv(self, rhs: Self) -> Self::Output { + Self((self.0 / rhs.0).floor()) + } +} + +impl IDiv for Vector2 { + type Output = Vector2; + fn idiv(self, rhs: f32) -> Self::Output { + Self((self.0 / rhs).floor()) + } +} + impl From for Vector2 { fn from(v: DomVector2) -> Self { Vector2(Vec2 { x: v.x, y: v.y }) diff --git a/crates/lune-roblox/src/datatypes/types/vector3.rs b/crates/lune-roblox/src/datatypes/types/vector3.rs index 32387d3..11bcd4a 100644 --- a/crates/lune-roblox/src/datatypes/types/vector3.rs +++ b/crates/lune-roblox/src/datatypes/types/vector3.rs @@ -141,6 +141,7 @@ impl LuaUserData for Vector3 { methods.add_meta_method(LuaMetaMethod::Sub, userdata_impl_sub); methods.add_meta_method(LuaMetaMethod::Mul, userdata_impl_mul_f32); methods.add_meta_method(LuaMetaMethod::Div, userdata_impl_div_f32); + methods.add_meta_method(LuaMetaMethod::IDiv, userdata_impl_idiv_f32); } } @@ -199,6 +200,20 @@ impl ops::Div for Vector3 { } } +impl IDiv for Vector3 { + type Output = Vector3; + fn idiv(self, rhs: Self) -> Self::Output { + Self((self.0 / rhs.0).floor()) + } +} + +impl IDiv for Vector3 { + type Output = Vector3; + fn idiv(self, rhs: f32) -> Self::Output { + Self((self.0 / rhs).floor()) + } +} + impl From for Vector3 { fn from(v: DomVector3) -> Self { Vector3(Vec3 { diff --git a/crates/lune-roblox/src/shared/userdata.rs b/crates/lune-roblox/src/shared/userdata.rs index 9184625..eaaa129 100644 --- a/crates/lune-roblox/src/shared/userdata.rs +++ b/crates/lune-roblox/src/shared/userdata.rs @@ -149,6 +149,37 @@ where }) } +pub trait IDiv { + type Output; + #[must_use] + fn idiv(self, rhs: Rhs) -> Self::Output; +} + +pub fn userdata_impl_idiv_f32(_: &Lua, datatype: &D, rhs: LuaValue) -> LuaResult +where + D: LuaUserData + IDiv + IDiv + Copy + 'static, +{ + match &rhs { + LuaValue::Number(n) => return Ok(datatype.idiv(*n as f32)), + LuaValue::Integer(i) => return Ok(datatype.idiv(*i as f32)), + LuaValue::UserData(ud) => { + if let Ok(vec) = ud.borrow::() { + return Ok(datatype.idiv(*vec)); + } + } + _ => {} + }; + Err(LuaError::FromLuaConversionError { + from: rhs.type_name(), + to: type_name::(), + message: Some(format!( + "Expected {} or number, got {}", + type_name::(), + rhs.type_name() + )), + }) +} + pub fn userdata_impl_div_i32(_: &Lua, datatype: &D, rhs: LuaValue) -> LuaResult where D: LuaUserData + ops::Div + ops::Div + Copy + 'static, diff --git a/tests/roblox/datatypes/Vector2.luau b/tests/roblox/datatypes/Vector2.luau index cccf373..1ed391e 100644 --- a/tests/roblox/datatypes/Vector2.luau +++ b/tests/roblox/datatypes/Vector2.luau @@ -39,4 +39,7 @@ assert(Vector2.new(2, 4) / Vector2.new(1, 2) == Vector2.new(2, 2)) assert(Vector2.new(2, 4) * 2 == Vector2.new(4, 8)) assert(Vector2.new(2, 4) / 2 == Vector2.new(1, 2)) +assert(Vector2.new(7, 15) // Vector2.new(3, 7) == Vector2.new(2, 2)) +assert(Vector2.new(3, 7) // 2 == Vector2.new(1, 3)) + -- TODO: Vector math diff --git a/tests/roblox/datatypes/Vector3.luau b/tests/roblox/datatypes/Vector3.luau index 64570aa..4a4b745 100644 --- a/tests/roblox/datatypes/Vector3.luau +++ b/tests/roblox/datatypes/Vector3.luau @@ -42,4 +42,7 @@ assert(Vector3.new(2, 4, 8) / Vector3.new(1, 1, 2) == Vector3.new(2, 4, 4)) assert(Vector3.new(2, 4, 8) * 2 == Vector3.new(4, 8, 16)) assert(Vector3.new(2, 4, 8) / 2 == Vector3.new(1, 2, 4)) +assert(Vector3.new(7, 11, 15) // Vector3.new(3, 5, 7) == Vector3.new(2, 2, 2)) +assert(Vector3.new(3, 5, 7) // 2 == Vector3.new(1, 2, 3)) + -- TODO: Vector math