Skip to content

Commit

Permalink
1.0.0.0 modhub release
Browse files Browse the repository at this point in the history
  • Loading branch information
TyKonKet committed Mar 31, 2021
1 parent 3604eb5 commit b421afc
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/rmod/Main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Mod

---@author Royal Modding
---@version 1.4.0.0
---@version 1.5.0.0
---@date 03/12/2020

--- Initialize RoyalMod
Expand Down
152 changes: 132 additions & 20 deletions src/rmod/RoyalMod.lua

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/utility/Array.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 26/02/2021

---@alias Array table<number, any> tables with numeric indexes only, always ordered and sequential
---@alias Array table tables 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
Expand All @@ -13,12 +13,14 @@ 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 Array
---@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
Expand All @@ -29,7 +31,7 @@ function ArrayUtility.remove(array, removeFunc)
moveAt = moveAt + 1
end
end
return array
return removedCount
end

--- Remove element at the given index from an array
Expand Down
2 changes: 1 addition & 1 deletion src/utility/Debug.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 05/01/2021

---@class DebugUtility
Expand Down
63 changes: 63 additions & 0 deletions src/utility/DelayedCallBack.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--- Royal Utility

---@author Royal Modding
---@version 2.0.3.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
2 changes: 1 addition & 1 deletion src/utility/Entity.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 05/01/2021

---@class EntityUtility
Expand Down
153 changes: 153 additions & 0 deletions src/utility/FadeEffect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
--- Royal Utility

---@author Royal Modding
---@version 2.0.3.0
---@date 17/02/2017

---@class FadeEffect
FadeEffect = {}

FadeEffect.STATES = {}
FadeEffect.STATES.FADEIN = 1
FadeEffect.STATES.STAY = 2
FadeEffect.STATES.FADEOUT = 3
FadeEffect.STATES.IDLE = 4

FadeEffect.ALIGNS = {}
FadeEffect.ALIGNS.LEFT = 0
FadeEffect.ALIGNS.TOP = 0
FadeEffect.ALIGNS.CENTER = 1
FadeEffect.ALIGNS.RIGHT = 2
FadeEffect.ALIGNS.BOTTOM = 2

---@class FadeEffectSettings
FadeEffect.defaultSettings = {
color = {
r = 1,
g = 1,
b = 1
},
position = {
x = 0.5,
y = 0.5
},
align = {
x = FadeEffect.ALIGNS.CENTER,
y = FadeEffect.ALIGNS.CENTER
},
bold = true,
size = 0.025,
text = "Fade Effect",
shadow = false,
shadowPosition = {
x = 0,
y = 0
},
initialAlpha = 0,
statesTime = {1, 1, 1},
statesAlpha = {1, 1, 0},
loop = false
}

---@param settings FadeEffectSettings
---@return FadeEffect
function FadeEffect:new(settings)
if FadeEffect_mt == nil then
FadeEffect_mt = Class(FadeEffect)
end

---@type FadeEffect
local fe = setmetatable({}, FadeEffect_mt)

---@type FadeEffectSettings
fe.settings = {}

for k, v in pairs(self.defaultSettings) do
fe.settings[k] = v
end

for k, v in pairs(settings) do
fe.settings[k] = v
end

fe:play(fe.settings.text)
fe.state = FadeEffect.STATES.IDLE

return fe
end

function FadeEffect:alignText()
self.settings.position.alignedX = self.settings.position.x
self.settings.position.alignedY = self.settings.position.y
if self.settings.align.x == FadeEffect.ALIGNS.CENTER then
self.settings.position.alignedX = self.settings.position.x - (getTextWidth(self.settings.size, self.settings.text) / 2)
end
if self.settings.align.x == FadeEffect.ALIGNS.RIGHT then
self.settings.position.alignedX = self.settings.position.x - getTextWidth(self.settings.size, self.settings.text)
end
if self.settings.align.y == FadeEffect.ALIGNS.CENTER then
self.settings.position.alignedY = self.settings.position.y - (getTextHeight(self.settings.size, self.settings.text) / 2)
end
if self.settings.align.y == FadeEffect.ALIGNS.TOP then
self.settings.position.alignedY = self.settings.position.y - getTextHeight(self.settings.size, self.settings.text)
end
end

---@param text string
function FadeEffect:setText(text)
self.settings.text = text
self:alignText()
end

---@param text string
function FadeEffect:play(text)
if text ~= nil then
self.settings.text = text
self:alignText()
end
self.alpha = self.settings.initialAlpha
self.initialAlpha = self.settings.initialAlpha
self.state = FadeEffect.STATES.FADEIN
self.tmpStateTime = 0
end

function FadeEffect:stop()
self.state = FadeEffect.STATES.IDLE
end

function FadeEffect:draw()
if self.state ~= FadeEffect.STATES.IDLE then
setTextBold(self.settings.bold)
if self.settings.shadow then
setTextColor(0, 0, 0, self.alpha)
renderText(self.settings.position.alignedX + self.settings.shadowPosition.x, self.settings.position.alignedY - self.settings.shadowPosition.y, self.settings.size, self.settings.text)
end
setTextColor(self.settings.color.r, self.settings.color.g, self.settings.color.b, self.alpha)
renderText(self.settings.position.alignedX, self.settings.position.alignedY, self.settings.size, self.settings.text)
setTextBold(false)
setTextColor(1, 1, 1, 1)
end
end

---@param dt number
function FadeEffect:update(dt)
if self.state ~= FadeEffect.STATES.IDLE then
local stateTime = self.settings.statesTime[self.state] * 1000
if (self.tmpStateTime + dt) >= stateTime then
self.tmpStateTime = (self.tmpStateTime + dt - stateTime)
self.alpha = self.settings.statesAlpha[self.state]
self.initialAlpha = self.alpha
self.state = self.state + 1
if self.settings.loop and self.state == FadeEffect.STATES.IDLE then
self.state = 1
end
else
self.tmpStateTime = self.tmpStateTime + dt
end
if self.initialAlpha == self.settings.statesAlpha[self.state] then
self.alpha = self.settings.statesAlpha[self.state]
else
self.alpha = math.abs(self.initialAlpha - (1 / stateTime) * self.tmpStateTime)
end
end
end
2 changes: 1 addition & 1 deletion src/utility/Gameplay.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 05/01/2021

---@class GameplayUtility
Expand Down
2 changes: 1 addition & 1 deletion src/utility/Interpolator.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 11/01/2021

--- Interpolators utilities class
Expand Down
4 changes: 3 additions & 1 deletion src/utility/Main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 21/11/2020

--- Initialize RoyalUtility
Expand All @@ -15,6 +15,8 @@ function InitRoyalUtility(utilityDirectory)
source(Utils.getFilename("Table.lua", utilityDirectory))
source(Utils.getFilename("Interpolator.lua", utilityDirectory))
source(Utils.getFilename("Array.lua", utilityDirectory))
source(Utils.getFilename("FadeEffect.lua", utilityDirectory))
source(Utils.getFilename("DelayedCallBack.lua", utilityDirectory))
g_logManager:devInfo("Royal Utility loaded successfully by " .. g_currentModName)
return true
end
2 changes: 1 addition & 1 deletion src/utility/String.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 05/01/2021

--- String utilities class
Expand Down
14 changes: 13 additions & 1 deletion src/utility/Table.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 05/01/2021

--- Table utilities class
Expand Down Expand Up @@ -50,6 +50,18 @@ function TableUtility.contains(t, value)
return false
end

--- Map a table to a new table
---@param t table source table
---@param func function | "function(e) return { f1 = e.f1, f2 = e.f2 } end" mapping function
---@return table mapped mapped table
function TableUtility.map(t, func)
local mapped = {}
for k, v in pairs(t) do
mapped[k] = func(v)
end
return mapped
end

--- Get if a matching element exists
---@param t table
---@param func function | "function(e) return true end"
Expand Down
24 changes: 23 additions & 1 deletion src/utility/Utility.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Utility

---@author Royal Modding
---@version 1.9.0.0
---@version 2.0.3.0
---@date 09/11/2020

--- Utilities class
Expand All @@ -20,6 +20,28 @@ function Utility.clamp(minValue, value, maxValue)
return math.max(minValue, math.min(maxValue, value))
end

--- Get random number sign (1 or -1)
---@return number
function Utility.randomSign()
if math.random(2) > 1 then
return -1
else
return 1
end
end

--- Normalize value by given maximum and minimum
---@param minValue number
---@param value number
---@param maxValue number
---@return number
function Utility.normalize(minValue, value, maxValue)
minValue = minValue or 0
maxValue = maxValue or 1
value = value or 0.5
return (value - minValue) / (maxValue - minValue)
end

---@param target table
---@param name string
---@param newFunc function
Expand Down

0 comments on commit b421afc

Please sign in to comment.