library MakeUnitHover
private keyword Data
globals
private constant integer ENABLE_FLY='Amrf'
private constant real INTERVAL=0.03
private constant real DIVIDE=100
private constant real RATE=5000
private constant real TIME = 2
private constant real HEIGHT = 100
private Data array D
private integer N=0
private timer T=CreateTimer()
private location l=Location(0,0)
endglobals
//! WARNING WARNING WARNING WARNING WARNING WARNING WARNING
//! Real Snippet Code!
//! Don't touch if you don't know what you are doing!
//! WARNING WARNING WARNING WARNING WARNING WARNING WARNING
private struct Data
unit swayer
real z
real max
real min
real height
real durcount
real counter=0
real duration=0
boolean hasReachedMax
boolean isTimed
endstruct
private function Parabola takes real x,real d,real h returns real
return 4*h*x*(d-x)/(d*d)
endfunction
private function GetUnitZ takes unit whichUnit returns real
call MoveLocation(l,GetUnitX(whichUnit),GetUnitY(whichUnit))
return GetLocationZ(l)+GetUnitFlyHeight(whichUnit)
endfunction
function StopUnitHover takes unit whichUnit returns nothing
local Data d
local integer i=N
loop
exitwhen i<=0
set d=D[i]
if d.swayer==whichUnit then
if d.swayer==null then
call BJDebugMsg("|cffff0000MakeUnitSway|r: invalid unit.")
return
endif
set D[i]=D[N]
set N=N-1
if N==0 then
call PauseTimer(T)
endif
call SetUnitFlyHeight(whichUnit,d.height,RATE)
call BJDebugMsg("rofl")
call d.destroy()
endif
set i=i-1
endloop
endfunction
private function Callback takes nothing returns nothing
local Data d
local integer i=N
local real parab
local real dif
local real totalTime
local real swayHeight
loop
exitwhen i==0
set d=D[i]
set dif=RAbsBJ(d.max-d.min)/HEIGHT
set swayHeight=RAbsBJ(d.max-d.min)
set totalTime=TIME*dif
set d.counter=d.counter+INTERVAL
if d.isTimed then
set d.durcount=d.durcount+INTERVAL
endif
if d.durcount>=d.duration and d.isTimed then
call StopUnitHover(d.swayer)
call BJDebugMsg("lol")
endif
if d.counter>=totalTime then
set d.counter=0
endif
set parab=Parabola(d.counter,totalTime,swayHeight)+d.min
call SetUnitFlyHeight(d.swayer,parab,0)
set i=i-1
endloop
endfunction
function MakeUnitHover takes unit whichUnit, real max, real min returns nothing
local Data d=Data.create()
set d.swayer=whichUnit
set d.height=GetUnitFlyHeight(whichUnit)
call UnitAddAbility(whichUnit,ENABLE_FLY)
call SetUnitFlyHeight(whichUnit,min,RATE)
set d.z=min-1
call UnitRemoveAbility(whichUnit,ENABLE_FLY)
set d.max=max
set d.min=min
set d.hasReachedMax=false
set N=N+1
set D[N]=d
set d.isTimed=false
set d.durcount=0
set d.duration=0
if N==1 then
call TimerStart(T,INTERVAL,true,function Callback)
endif
endfunction
function MakeUnitHoverTimed takes unit whichUnit, real max, real min, real duration returns nothing
local Data d=Data.create()
set d.swayer=whichUnit
set d.height=GetUnitFlyHeight(whichUnit)
call UnitAddAbility(whichUnit,ENABLE_FLY)
call SetUnitFlyHeight(whichUnit,min,RATE)
set d.z=min-1
call UnitRemoveAbility(whichUnit,ENABLE_FLY)
set d.max=max
set d.min=min
set d.hasReachedMax=false
set N=N+1
set D[N]=d
set d.isTimed=true
set d.duration=duration
set d.durcount=0
if N==1 then
call TimerStart(T,INTERVAL,true,function Callback)
endif
endfunction
endlibrary