struct s_fadeunit
unit u = null
integer alpha
integer alphatotal
real fadetime
integer index
static s_fadeunit array stack
static integer n = 0
static timer refresh = CreateTimer()
static constant real refreshPeriod = 0.025
method onDestroy takes nothing returns nothing
set thistype.n = thistype.n-1
set thistype.stack[this.index] = thistype.stack[thistype.n]
set thistype.stack[this.index].index = this.index
//* removes the struct from the stack;
endmethod
static method onRefresh takes nothing returns nothing
local integer i
local s_fadeunit fu
set i = thistype.n-1
loop
exitwhen i < 0
set fu = thistype.stack[i]
if fu != 0 then
set fu.alpha = fu.alpha - R2I(fu.alphatotal/fu.fadetime)
if fu.alpha <= 0 then
call fu.destroy()
endif
endif
set i = i-1
endloop
endmethod
static method create takes unit u, integer alpha, real fadetime returns s_fadeunit
local s_fadeunit fu = s_fadeunit.allocate()
set fu.u = u
set fu.alpha = alpha
call SetUnitVertexColor(u, 255, 255, 255, alpha) // i used 255 as the value for Red/Green/Blue for simplicity
set fu.alphatotal = alpha
set fu.fadetime = fadetime
//* the purpose of this is so that only one timer needs to be used to update the entire stack
set fu.index = thistype.n
set thistype.stack[thistype.n] = fu
set thistype.n = thistype.n+1
return fu
endmethod
static method onInit takes nothing returns nothing
call TimerStart(thistype.refresh, thistype.refreshPeriod, true, function thistype.onRefresh)
endmethod
endstruct