In order to use them, you must first setup a timer during initialization. For these, I've used
Code: Select all
local startTimer = Spring.GetTimer()
Code: Select all
Spring.DiffTimers()
Code: Select all
DiffTimers()
Code: Select all
--Cycles from 'value1' to 'value2' at designated 'rate'
--There are 3 options that control switching: 0 or nil is a square wave, 1 is a sawtooth wave, 2 is a sine wave
function ColorCycle(r1, g1, b1, a1, r2, g2, b2, a2, rate, option)
local diffTime = DiffTimers(GetTimer(), startTimer)
local factor
if (option == 1) then
factor = math.abs(1.0 - math.mod(diffTime * rate, 2.0))
elseif (option == 2) then
factor = math.abs(math.sin(math.mod(diffTime * rate, 2.0) * math.pi))
else
if (math.mod(math.floor(diffTime * rate), 2.0) == 0) then
return r1, g1, b1, a1
else
return r2, g2, b2, a2
end
end
local r = r1 + ((r2 - r1) * factor)
local g = g1 + ((g2 - g1) * factor)
local b = b1 + ((b2 - b1) * factor)
local a = a1 + ((a2 - a1) * factor)
return r, g, b, a
end
--Fades from 'value1' to 'value2' at designated 'rate'
--Direction is binary: 1 fades in, 0 or nil fades out, default is fade out
function ColorFade(r1, g1, b1, a1, r2, g2, b2, a2, rate, direction)
local diffTime = DiffTimers(GetTimer(), startTimer)
local factor
if (direction == 1) then
factor = math.abs(1.0 - math.mod(diffTime * rate, 1.0))
else
factor = math.abs(math.mod(diffTime * rate, 1.0))
end
local r = r1 + ((r2 - r1) * factor)
local g = g1 + ((g2 - g1) * factor)
local b = b1 + ((b2 - b1) * factor)
local a = a1 + ((a2 - a1) * factor)
return r, g, b, a
end