mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 10:03:45 +01:00
Split vector.new into 3 constructors
This commit is contained in:
parent
bbfae0cc67
commit
2cefe51d3b
@ -31,6 +31,21 @@ describe("vector", function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("zero()", function()
|
||||||
|
assert.same({x = 0, y = 0, z = 0}, vector.zero())
|
||||||
|
assert.same(vector.new(), vector.zero())
|
||||||
|
assert.equal(vector.new(), vector.zero())
|
||||||
|
assert.is_true(vector.check(vector.zero()))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("copy()", function()
|
||||||
|
local v = vector.new(1, 2, 3)
|
||||||
|
assert.same(v, vector.copy(v))
|
||||||
|
assert.same(vector.new(v), vector.copy(v))
|
||||||
|
assert.equal(vector.new(v), vector.copy(v))
|
||||||
|
assert.is_true(vector.check(vector.copy(v)))
|
||||||
|
end)
|
||||||
|
|
||||||
it("indexes", function()
|
it("indexes", function()
|
||||||
local some_vector = vector.new(24, 42, 13)
|
local some_vector = vector.new(24, 42, 13)
|
||||||
assert.equal(24, some_vector[1])
|
assert.equal(24, some_vector[1])
|
||||||
|
@ -30,16 +30,28 @@ local function fast_new(x, y, z)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function vector.new(a, b, c)
|
function vector.new(a, b, c)
|
||||||
if type(a) == "table" then
|
if a and b and c then
|
||||||
assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
|
|
||||||
return fast_new(a.x, a.y, a.z)
|
|
||||||
elseif a then
|
|
||||||
assert(b and c, "Invalid arguments for vector.new()")
|
|
||||||
return fast_new(a, b, c)
|
return fast_new(a, b, c)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- deprecated, use vector.copy and vector.zero directly
|
||||||
|
if type(a) == "table" then
|
||||||
|
return vector.copy(a)
|
||||||
|
else
|
||||||
|
assert(not a, "Invalid arguments for vector.new()")
|
||||||
|
return vector.zero()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function vector.zero()
|
||||||
return fast_new(0, 0, 0)
|
return fast_new(0, 0, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function vector.copy(v)
|
||||||
|
assert(v.x and v.y and v.z, "Invalid vector passed to vector.copy()")
|
||||||
|
return fast_new(v.x, v.y, v.z)
|
||||||
|
end
|
||||||
|
|
||||||
function vector.from_string(s, init)
|
function vector.from_string(s, init)
|
||||||
local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" ..
|
local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" ..
|
||||||
"%s*([^%s,]+)%s*[,%s]?%s*%)()", init)
|
"%s*([^%s,]+)%s*[,%s]?%s*%)()", init)
|
||||||
|
@ -3271,10 +3271,13 @@ For the following functions, `v`, `v1`, `v2` are vectors,
|
|||||||
vectors are written like this: `(x, y, z)`:
|
vectors are written like this: `(x, y, z)`:
|
||||||
|
|
||||||
* `vector.new([a[, b, c]])`:
|
* `vector.new([a[, b, c]])`:
|
||||||
* Returns a vector.
|
* Returns a new vector `(a, b, c)`.
|
||||||
* A copy of `a` if `a` is a vector.
|
* Deprecated: `vector.new()` does the same as `vector.zero()` and
|
||||||
* `(a, b, c)`, if all of `a`, `b`, `c` are defined numbers.
|
`vector.new(v)` does the same as `vector.copy(v)`
|
||||||
* `(0, 0, 0)`, if no arguments are given.
|
* `vector.zero()`:
|
||||||
|
* Returns a new vector `(0, 0, 0)`.
|
||||||
|
* `vector.copy(v)`:
|
||||||
|
* Returns a copy of the vector `v`.
|
||||||
* `vector.from_string(s[, init])`:
|
* `vector.from_string(s[, init])`:
|
||||||
* Returns `v, np`, where `v` is a vector read from the given string `s` and
|
* Returns `v, np`, where `v` is a vector read from the given string `s` and
|
||||||
`np` is the next position in the string after the vector.
|
`np` is the next position in the string after the vector.
|
||||||
|
Loading…
Reference in New Issue
Block a user