- Joined
- Oct 23, 2011
- Messages
- 182
A vJass system to handle natural movements of units.
Criticism is welcome and appreciated.
Credits to Jesus4Lyf for original ideas.
Demo
Criticism is welcome and appreciated.
Credits to Jesus4Lyf for original ideas.
JASS:
library Movement requires CTL, Event, UnitIndexer
/*
Movement v0.2.0
Combined vJass system managing movements of units.
Requirements :
UnitIndexer by Nestharus
Event by Nesetharus
CTL by Nestharus
API :
Movement[unit]
Movement[unit].speed *returns real*
Retrieves the movement speed of the unit.
Movement[unit].speed = real
Changes the movement speed of a unit.
Setting it to <= 0 will remove the unit from list and disallow it from firing moving/stop events.
Setting it to 0 ~ 522 allows the unit to fire move/stop events.
Setting it above 522 allows the units movement to be handled differently, allowing it to move at speed over 522
*WARNING : Setting the speed above 1000~ish causes bugs.
Movement[unit].x/y *returns real*
Retrieves the x/y value of unit.
Movement[unit].x/y = real
Changes units coordinates.
This is necessary because changing units x/y while the speed is above 522 will cause problems.
Movement[unit].moving *returns boolean*
Checks if unit is moving or not.
Movement.onStart.register(boolexpr code)
Registers a code to be fired when a unit starts moving.
Movement.onStop.register(boolexpr code)
Registers a code to be fired when a unit stops moving.
GetMovingUnit() *returns unit*
Retrieves the unit firing event
Credits :
Bribe for IsUnitMoving
PurgeandFire111 for MoveSpeedX
Jesus4Lyf for original ideas
*/
private function FilterFunc takes unit b returns boolean
return not IsUnitPaused(b) and GetUnitAbilityLevel(b, 'Bstn') == 0
endfunction
globals
private constant real MARGIN = .15625
private boolean array j
private boolean array f
private boolean array m
private integer array next
private integer array prev
private real array t
private real array s
private real array g
private real array h
private real d
private real dx
private real dy
private unit u
endglobals
function GetMovingUnit takes nothing returns unit
return u
endfunction
private module M
private static method onInit takes nothing returns nothing
set onStart = CreateEvent()
set onStop = CreateEvent()
endmethod
endmodule
struct Movement extends array
implement M
readonly static Event onStart
readonly static Event onStop
private method index takes nothing returns nothing
if (GetUnitMoveSpeed(GetUnitById(this)) != 0) then
call .add()
endif
set g[this] = GetUnitX(GetUnitById(this))
set h[this] = GetUnitY(GetUnitById(this))
endmethod
private method deindex takes nothing returns nothing
set f[this] = false
set m[this] = false
call .remove()
endmethod
private method add takes nothing returns nothing
if not j[this] then
set j[this] = true
set next[this] = 0
set prev[this] = prev[0]
set next[prev[0]] = this
set prev[0] = this
endif
endmethod
private method remove takes nothing returns nothing
if j[this] then
set j[this] = false
set next[prev[this]] = next[this]
set prev[next[this]] = prev[this]
endif
endmethod
method operator x takes nothing returns real
return g[this]
endmethod
method operator x= takes real nx returns nothing
set g[this] = nx
call SetUnitX(GetUnitById(this), nx)
endmethod
method operator y takes nothing returns real
return h[this]
endmethod
method operator y= takes real ny returns nothing
set h[this] = ny
call SetUnitY(GetUnitById(this), ny)
endmethod
method operator moving takes nothing returns boolean
return m[this]
endmethod
method operator speed takes nothing returns real
if f[this] then
return (s[this] / .03125) + 522
endif
return GetUnitMoveSpeed(GetUnitById(this))
endmethod
method operator speed= takes real ns returns nothing
call SetUnitMoveSpeed(GetUnitById(this), ns)
if f[this] then
if (ns > 522) then
set s[this] = (ns - 522) * .03125
else
set f[this] = false
endif
elseif (ns > 522) then
set f[this] = true
set s[this] = (ns - 522) * .03125
elseif (ns <= 0) then
if m[this] then
set m[this] = false
set u = GetUnitById(this)
call onStop.fire()
endif
call .remove()
else
call .add()
endif
endmethod
implement CT32
local thistype this = next[0]
local integer oid
local real nx
local real ny
loop
exitwhen (0 == this)
set u = GetUnitById(this)
set oid = GetUnitCurrentOrder(u)
set nx = GetUnitX(u)
set ny = GetUnitY(u)
if FilterFunc(u) and (oid == 851971 or oid == 851983 or oid == 851990) and (nx != g[this] or ny != h[this]) then
if f[this] then
set dx = nx - g[this]
set dy = ny - h[this]
set d = s[this] / SquareRoot(dx * dx + dy * dy)
set nx = nx + dx * d
set ny = ny + dy * d
call SetUnitX(u, nx)
call SetUnitY(u, ny)
endif
set t[this] = MARGIN
if (not m[this] and GetUnitTypeId(u) != 0) then
set m[this] = true
call onStart.fire()
endif
else
set t[this] = t[this] - .03125
if (t[this] <= 0) then
if m[this] then
set m[this] = false
call onStop.fire()
endif
endif
endif
set g[this] = nx
set h[this] = ny
set this = next[this]
endloop
implement CT32End
implement UnitIndexStruct
endstruct
endlibrary
Demo
JASS:
library Demo uses Movement
private module M
private static method onInit takes nothing returns nothing
local trigger trg = CreateTrigger()
call TriggerRegisterPlayerChatEvent(trg, Player(0), "-", false)
call TriggerAddCondition(trg, Filter(function thistype.onChat))
call Movement.onStart.register(Filter(function thistype.onStart))
call Movement.onStop.register(Filter(function thistype.onStop))
set trg = null
endmethod
endmodule
private struct Demo extends array
implement M
static unit u
private static method onStart takes nothing returns boolean
call BJDebugMsg(GetUnitName(GetMovingUnit()) + " started moving.")
return false
endmethod
private static method onStop takes nothing returns boolean
call BJDebugMsg(GetUnitName(GetMovingUnit()) + " stopped moving.")
return false
endmethod
private static method onChat takes nothing returns boolean
set u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
set Movement[u].speed = S2R(SubString(GetEventPlayerChatString(), 1, 4))
return false
endmethod
endstruct
endlibrary
Last edited: