From cde3c3360cc50abb7a8e820793b1b2775fc2358d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 14:44:40 +0100 Subject: [PATCH] Vector3: add snap_to --- .tests/Vector3/add.test.lua | 9 --------- .tests/Vector3/snap_to.test.lua | 26 ++++++++------------------ worldeditadditions/utils/vector3.lua | 2 +- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/.tests/Vector3/add.test.lua b/.tests/Vector3/add.test.lua index de2bae4..cba5a2a 100644 --- a/.tests/Vector3/add.test.lua +++ b/.tests/Vector3/add.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.add", function() it("should add 2 positive vectors", function() local a = Vector3.new(3, 4, 5) diff --git a/.tests/Vector3/snap_to.test.lua b/.tests/Vector3/snap_to.test.lua index 27028c4..389de35 100644 --- a/.tests/Vector3/snap_to.test.lua +++ b/.tests/Vector3/snap_to.test.lua @@ -1,35 +1,25 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.snap_to", function() it("should snap_to a positive vector", function() local a = Vector3.new(3.1, 4.75, 5.9) - print("DEBUG "..tostring(a:snap_to(10))) assert.are.same( - a:snap_to(10), - Vector3.new(0, 0, 10) + Vector3.new(0, 0, 10), + a:snap_to(10) ) end) it("should snap_to a negative vector", function() local a = Vector3.new(-2.5, -4.2, -5.3) assert.are.same( - a:snap_to(6), - Vector3.new(0, -6, -6) + Vector3.new(0, -6, -6), + a:snap_to(6) ) end) it("should work with integers", function() local a = Vector3.new(3, 4, 5) assert.are.same( - a:snap_to(3), - Vector3.new(3, 3, 6) + Vector3.new(3, 3, 6), + a:snap_to(3) ) end) it("should return a new Vector3 instance", function() @@ -37,8 +27,8 @@ describe("Vector3.snap_to", function() local result = a:snap_to(3) assert.are.same( - result, - Vector3.new(3, 6, 6) + Vector3.new(3, 6, 6), + result ) assert.are_not.equal(result, a) end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 239e59b..8d08af2 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -123,7 +123,7 @@ end -- @param number grid_size The size of the squares on the imaginary grid to which to snap. -- @returns Vector3 A new Vector3 instance snapped to an imaginary grid of the specified size. function Vector3.snap_to(a, grid_size) - return a:round(a / grid_size) * grid_size + return (a / grid_size):round() * grid_size end --- Returns the area of this vector.