|
//#####START#####
[codes=jass]library COMMON initializer InitCOMMON
globals
gamecache HGGC = null
timer HTMR = null
endglobals
function InitCOMMON takes nothing returns nothing
set HGGC = InitGameCache("GLOBALGAMECACHE")
set HTMR = CreateTimer()
endfunction
function H2I takes handle h returns integer
return h
return 0
endfunction
function I2U takes integer i returns unit
return i
return null
endfunction
function C2I takes code c returns integer
return c
return 0
endfunction
function I2C takes integer i returns code
return i
return null
endfunction
function I2T takes integer i returns trigger
return i
return null
endfunction
function Execute takes code c returns nothing
call TimerStart(HTMR,0,false,c)
endfunction
endlibrary
//added two event: movestart,moveend.
library UNITMOVEEVENT requires COMMON
//! define MOVE_CHECK_TIME 0.04
function MoveEventCore takes nothing returns nothing
local trigger t = GetTriggeringTrigger()
local unit u = I2U(GetStoredInteger(HGGC,"UNITMOVEEVENT",I2S(H2I(t))))
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local code c = I2C(GetStoredInteger(HGGC,"UNITMOVEEVENT_START_MOVING_HANDLER",I2S(H2I(u))))
local code d = I2C(GetStoredInteger(HGGC,"UNITMOVEEVENT_END_MOVING_HANDLER",I2S(H2I(u))))
if (x==GetStoredReal(HGGC,"UNITMOVEEVENT_UNIT_X",I2S(H2I(u)))) and (y==GetStoredReal(HGGC,"UNITMOVEEVENT_UNIT_Y",I2S(H2I(u)))) then
if GetStoredBoolean(HGGC,"UNITMOVEEVENT_UNIT_MOVING",I2S(H2I(u))) and (d!=null) then
call Execute(d)
endif
call StoreBoolean(HGGC,"UNITMOVEEVENT_UNIT_MOVING",I2S(H2I(u)),false)
elseif (x!=GetStoredReal(HGGC,"UNITMOVEEVENT_UNIT_X",I2S(H2I(u)))) or (y!=GetStoredReal(HGGC,"UNITMOVEEVENT_UNIT_Y",I2S(H2I(u)))) then
if (GetStoredBoolean(HGGC,"UNITMOVEEVENT_UNIT_MOVING",I2S(H2I(u)))==false) and (c!=null) then
call Execute(c)
endif
call StoreBoolean(HGGC,"UNITMOVEEVENT_UNIT_MOVING",I2S(H2I(u)),true)
endif
call StoreReal(HGGC,"UNITMOVEEVENT_UNIT_X",I2S(H2I(u)),x)
call StoreReal(HGGC,"UNITMOVEEVENT_UNIT_Y",I2S(H2I(u)),y)
endfunction
function RegMoveEvent takes unit u,boolean b returns nothing
local trigger t = I2T(GetStoredInteger(HGGC,"UNITMOVEEVENT",I2S(H2I(u))))
if (b==false)and (t!=null) then
call DisableTrigger(t)
call DestroyTrigger(t)
call FlushStoredInteger(HGGC,"UNITMOVEEVENT",I2S(H2I(u)))
elseif b and(t==null) then
set t = CreateTrigger()
call StoreInteger(HGGC,"UNITMOVEEVENT",I2S(H2I(u)),H2I(t))
call StoreInteger(HGGC,"UNITMOVEEVENT",I2S(H2I(t)),H2I(u))
call TriggerAddAction(t,function MoveEventCore)
call TriggerRegisterTimerEvent(t,MOVE_CHECK_TIME,true)
endif
endfunction
function IsUnitMoving takes unit u returns boolean
return GetStoredBoolean(HGGC,"UNITMOVEEVENT_UNIT_MOVING",I2S(H2I(u)))
endfunction
function UnitMoveStart takes unit u,code c returns nothing
call StoreInteger(HGGC,"UNITMOVEEVENT_START_MOVING_HANDLER",I2S(H2I(u)),C2I(c))
endfunction
function UnitMoveEnd takes unit u,code c returns nothing
call StoreInteger(HGGC,"UNITMOVEEVENT_END_MOVING_HANDLER",I2S(H2I(u)),C2I(c))
endfunction
endlibrary[/codes]
//#####END#####
RegMoveEvent :注册/取消注册移动事件
UnitMoveStart :注册单位开始移动事件,必须先注册移动事件
UnitMoveEnd :注册单位停止移动事件,必须先注册移动事件
IsUnitMoving :判断单位是否移动中
由于要判断单位是原地转向还是移动中转向比较麻烦,所以我没有把移动转向停留作为移动中来看。
如果被监视的单位转向太急以至于原地小停,将先后触发停止移动事件和开始移动事件。
你可以设置MOVE_CHECK_TIME的值来设置检测周期,值越小则越敏感。
没有使用无限循环,如果没有注册移动事件的单位,则不占资源。
另外,如果你有兴趣你可以添加到UI里面 |
|