- Joined
- Jun 28, 2008
- Messages
- 776
This is a little code snippet I created for a map I am creating. I could not find a working one anywhere.
Description :
This little piece of code checks if a unit is moving.
Requires :
Use
to add a unit to the check list and use
to check if that unit is moving.
Description :
This little piece of code checks if a unit is moving.
Requires :
- JNGP
JASS:
library IsUnitMoving initializer init
globals
private group CHECKUNITS = CreateGroup()
private trigger MAINTRIGGER = CreateTrigger()
private integer counter = 0
MoveData array ARRMove
endglobals
struct MoveData
unit witchunit = null
real cX = 0
real cY = 0
real nX = 0
real nY = 0
boolean moving = false
endstruct
private function GetIndex takes unit u returns integer
local integer i = 0
loop
set i = i + 1
if ARRMove[i].witchunit == u then
return i
endif
exitwhen i == counter
endloop
return 0
endfunction
function IsUnitMoving takes unit u returns boolean
local integer i = GetIndex(u)
return ARRMove[i].moving
endfunction
private function EnumUnits takes nothing returns nothing
local integer i = GetIndex(GetEnumUnit())
set ARRMove[i].cX = GetUnitX(GetEnumUnit())
set ARRMove[i].cY = GetUnitY(GetEnumUnit())
if (ARRMove[i].cX == ARRMove[i].nX) and (ARRMove[i].cY == ARRMove[i].nY) then
set ARRMove[i].moving = false
else
set ARRMove[i].moving = true
endif
set ARRMove[i].nX = GetUnitX(GetEnumUnit())
set ARRMove[i].nY = GetUnitY(GetEnumUnit())
endfunction
private function onLoop takes nothing returns nothing
call ForGroup(CHECKUNITS, function EnumUnits)
endfunction
function AddUnit takes unit u returns nothing
set counter = counter + 1
set ARRMove[counter] = MoveData.create()
set ARRMove[counter].witchunit = u
set ARRMove[counter].cX = GetUnitX(u)
set ARRMove[counter].cY = GetUnitY(u)
call GroupAddUnit(CHECKUNITS, u)
endfunction
private function init takes nothing returns nothing
call TriggerRegisterTimerEvent(MAINTRIGGER, 0.05, true)
call TriggerAddAction(MAINTRIGGER, function onLoop)
endfunction
endlibrary
Use
JASS:
AddUnit(WichUnit)
JASS:
IsUnitMoving( unit WitchUnit)