Added extension methods for tables and entities, added inrange helper to libs

This commit is contained in:
Ivan 2021-11-04 02:46:33 +05:00
parent b2fd2d88f3
commit 99d967ced6
3 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,7 @@
function getLocalVelocity(entity)
if not entity:isValid() then
return
end
return entity:worldToLocal(entity:getVelocity() + entity:getPos())
end

3
koptilnya/libs/math.txt Normal file
View File

@ -0,0 +1,3 @@
function inrange(value, min, max)
return value >= min and value <= max
end

View File

@ -1,5 +1,4 @@
-- @name koptilnya/libs/table
function table.chunk(tbl, size)
size = size or 1
size = size > 0 and size or 1
@ -64,3 +63,33 @@ function table.contains(tbl, ...)
return result
end
end
function table.map(tbl, action)
local res = {}
for _, field in ipairs(tbl) do
table.insert(res, action(field))
end
return res
end
function table.filter(tbl, predicate)
local res = {}
for _, field in ipairs(tbl) do
if predicate(field) == true then
table.insert(res, field)
end
end
return res
end
function table.find(tbl, predicate)
for _, field in ipairs(tbl) do
if predicate(field) == true then
return field
end
end
end