-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
611 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- Royal Utility | ||
|
||
---@author Royal Modding | ||
---@version 2.1.0.0 | ||
---@date 26/02/2021 | ||
|
||
---@alias Array table<integer, any> Table with numeric indexes only, always ordered and sequential | ||
|
||
--- Array utilities class built with performances in mind (with 'array' we mean tables with numeric indexes only, always ordered and sequential) | ||
---@class ArrayUtility | ||
ArrayUtility = ArrayUtility or {} | ||
|
||
--- Remove matching elements from an array | ||
---@param array Array | ||
---@param removeFunc fun(array: Array, index: number, moveAt: number): boolean | "function(array, index, moveAt) local element = array[index] return true end" | ||
---@return number removedCount count of removed elements | ||
function ArrayUtility.remove(array, removeFunc) | ||
local removedCount = 0 | ||
local moveAt, length = 1, #array | ||
for index = 1, length do | ||
if removeFunc(array, index, moveAt) then | ||
array[index] = nil | ||
removedCount = removedCount + 1 | ||
else | ||
-- move kept element's value to moveAt's position, if it's not already there | ||
if (index ~= moveAt) then | ||
array[moveAt] = array[index] | ||
array[index] = nil | ||
end | ||
-- increment position of where we'll place the next kept value | ||
moveAt = moveAt + 1 | ||
end | ||
end | ||
return removedCount | ||
end | ||
|
||
--- Remove element at the given index from an array | ||
---@param array Array | ||
---@param index number | ||
---@return Array | ||
function ArrayUtility.removeAt(array, index) | ||
ArrayUtility.remove( | ||
array, | ||
function(_, i) | ||
return index == i | ||
end | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- Royal Utility | ||
|
||
---@author Royal Modding | ||
---@version 2.1.0.0 | ||
---@date 08/03/17 | ||
|
||
---@class DelayedCallBack | ||
DelayedCallBack = {} | ||
|
||
---@param callback function | ||
---@param callbackObject any | ||
---@return DelayedCallBack | ||
function DelayedCallBack:new(callback, callbackObject) | ||
if DelayedCallBack_mt == nil then | ||
DelayedCallBack_mt = Class(DelayedCallBack) | ||
end | ||
|
||
---@type DelayedCallBack | ||
local dcb = setmetatable({}, DelayedCallBack_mt) | ||
dcb.callBack = callback | ||
dcb.callbackObject = callbackObject | ||
dcb.callbackCalled = true | ||
dcb.delay = 0 | ||
dcb.timer = 0 | ||
dcb.skipOneFrame = false | ||
return dcb | ||
end | ||
|
||
---@param dt number | ||
function DelayedCallBack:update(dt) | ||
if not self.callbackCalled then | ||
if not self.skipOneFrame then | ||
self.timer = self.timer + dt | ||
end | ||
if self.timer >= self.delay then | ||
self:callCallBack() | ||
end | ||
if self.skipOneFrame then | ||
self.timer = self.timer + dt | ||
end | ||
end | ||
end | ||
|
||
---@param delay number | ||
function DelayedCallBack:call(delay, ...) | ||
self.callbackCalled = false | ||
self.callbackParams = {...} | ||
if delay == nil or delay == 0 then | ||
self:callCallBack() | ||
else | ||
self.delay = delay | ||
self.timer = 0 | ||
end | ||
end | ||
|
||
function DelayedCallBack:callCallBack() | ||
if self.callbackObject ~= nil then | ||
self.callBack(self.callbackObject, unpack(self.callbackParams)) | ||
else | ||
self.callBack(unpack(self.callbackParams)) | ||
end | ||
self.callbackCalled = true | ||
end |
Oops, something went wrong.