Added extension methods for tables and entities, added inrange helper to libs
This commit is contained in:
parent
b2fd2d88f3
commit
99d967ced6
7
koptilnya/libs/entity.txt
Normal file
7
koptilnya/libs/entity.txt
Normal 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
3
koptilnya/libs/math.txt
Normal file
@ -0,0 +1,3 @@
|
||||
function inrange(value, min, max)
|
||||
return value >= min and value <= max
|
||||
end
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user