Skip to content

Commit

Permalink
Implement idiv support for Vector2 and Vector3 (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjcscat authored May 15, 2024
1 parent 7e784ba commit 395c36f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/lune-roblox/src/datatypes/types/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -138,6 +139,20 @@ impl ops::Div<f32> for Vector2 {
}
}

impl IDiv for Vector2 {
type Output = Vector2;
fn idiv(self, rhs: Self) -> Self::Output {
Self((self.0 / rhs.0).floor())
}
}

impl IDiv<f32> for Vector2 {
type Output = Vector2;
fn idiv(self, rhs: f32) -> Self::Output {
Self((self.0 / rhs).floor())
}
}

impl From<DomVector2> for Vector2 {
fn from(v: DomVector2) -> Self {
Vector2(Vec2 { x: v.x, y: v.y })
Expand Down
15 changes: 15 additions & 0 deletions crates/lune-roblox/src/datatypes/types/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -199,6 +200,20 @@ impl ops::Div<f32> for Vector3 {
}
}

impl IDiv for Vector3 {
type Output = Vector3;
fn idiv(self, rhs: Self) -> Self::Output {
Self((self.0 / rhs.0).floor())
}
}

impl IDiv<f32> for Vector3 {
type Output = Vector3;
fn idiv(self, rhs: f32) -> Self::Output {
Self((self.0 / rhs).floor())
}
}

impl From<DomVector3> for Vector3 {
fn from(v: DomVector3) -> Self {
Vector3(Vec3 {
Expand Down
31 changes: 31 additions & 0 deletions crates/lune-roblox/src/shared/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ where
})
}

pub trait IDiv<Rhs = Self> {
type Output;
#[must_use]
fn idiv(self, rhs: Rhs) -> Self::Output;
}

pub fn userdata_impl_idiv_f32<D>(_: &Lua, datatype: &D, rhs: LuaValue) -> LuaResult<D>
where
D: LuaUserData + IDiv<D, Output = D> + IDiv<f32, Output = D> + 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::<D>() {
return Ok(datatype.idiv(*vec));
}
}
_ => {}
};
Err(LuaError::FromLuaConversionError {
from: rhs.type_name(),
to: type_name::<D>(),
message: Some(format!(
"Expected {} or number, got {}",
type_name::<D>(),
rhs.type_name()
)),
})
}

pub fn userdata_impl_div_i32<D>(_: &Lua, datatype: &D, rhs: LuaValue) -> LuaResult<D>
where
D: LuaUserData + ops::Div<D, Output = D> + ops::Div<i32, Output = D> + Copy + 'static,
Expand Down
3 changes: 3 additions & 0 deletions tests/roblox/datatypes/Vector2.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions tests/roblox/datatypes/Vector3.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 395c36f

Please sign in to comment.