mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 17:19:31 +00:00
ubiquitousse.util
This commit is contained in:
parent
6283522003
commit
523c5d36c0
2 changed files with 54 additions and 1 deletions
3
init.lua
3
init.lua
|
|
@ -85,7 +85,8 @@ ubiquitousse = {
|
||||||
input = false,
|
input = false,
|
||||||
scene = false,
|
scene = false,
|
||||||
event = false,
|
event = false,
|
||||||
asset = false
|
asset = false,
|
||||||
|
util = false
|
||||||
},
|
},
|
||||||
|
|
||||||
--- Backend name.
|
--- Backend name.
|
||||||
|
|
|
||||||
52
util.lua
Normal file
52
util.lua
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
-- ubiquitousse.util
|
||||||
|
|
||||||
|
--- Various functions useful for game developement.
|
||||||
|
-- No dependicy on either ubiquitousse or a ubiquitousse backend.
|
||||||
|
local util
|
||||||
|
util = {
|
||||||
|
--- AABB collision check.
|
||||||
|
-- @tparam number x1 first rectangle top-left x coordinate
|
||||||
|
-- @tparam number y1 first rectangle top-left y coordinate
|
||||||
|
-- @tparam number w1 first rectangle width
|
||||||
|
-- @tparam number h1 first rectangle height
|
||||||
|
-- @tparam number x2 second rectangle top-left x coordinate
|
||||||
|
-- @tparam number y2 second rectangle top-left y coordinate
|
||||||
|
-- @tparam number w2 second rectangle width
|
||||||
|
-- @tparam number h2 second rectangle height
|
||||||
|
-- @treturn true if the objects collide, false otherwise
|
||||||
|
aabb = function(x1, y1, w1, h1, x2, y2, w2, h2)
|
||||||
|
if w1 < 0 then x1 = x1 + w1; w1 = -w1 end
|
||||||
|
if h1 < 0 then y1 = y1 + h1; h1 = -h1 end
|
||||||
|
if w2 < 0 then x2 = x2 + w2; w2 = -w2 end
|
||||||
|
if h2 < 0 then y2 = y2 + h2; h2 = -h2 end
|
||||||
|
return x1 + w1 >= x2 and x1 <= x2 + w2 and
|
||||||
|
y1 + h1 >= y2 and y1 <= y2 + h2
|
||||||
|
end,
|
||||||
|
|
||||||
|
--- Remove the first occurence of an element in a table.
|
||||||
|
-- @tparam table t the table
|
||||||
|
-- @param x the element to remove
|
||||||
|
-- @return x
|
||||||
|
remove = function(t, x)
|
||||||
|
for i, v in ipairs(t) do
|
||||||
|
if v == x then
|
||||||
|
table.remove(t, i)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return x
|
||||||
|
end,
|
||||||
|
|
||||||
|
--- Returns a new table where the keys and values have been inverted.
|
||||||
|
-- @tparam table t the table
|
||||||
|
-- @treturn table the inverted table
|
||||||
|
invert = function(t)
|
||||||
|
local r = {}
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
r[v] = k
|
||||||
|
end
|
||||||
|
return r
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
return util
|
||||||
Loading…
Add table
Add a link
Reference in a new issue