mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 04:50:36 +00:00
Implement idiv support for Vector2 and Vector3 (#196)
This commit is contained in:
parent
7e784ba361
commit
395c36fa8b
5 changed files with 67 additions and 0 deletions
|
@ -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<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 })
|
||||
|
|
|
@ -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<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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue