- Joined
- Jan 21, 2006
- Messages
- 2,552
This is an extremely lite system for creating the effect of a knock-back on a specific unit. The knock-back uses
The aim of this library is for users who seek out a very very basic knock-back. The user must specify a distance a unit will be knocked back, and the time it will take.
Its as easy to use as:
Requirements
Knock Back Lite
GetUnitX/GetUnitY
so a single unit can be applied multiple knock-backs and not have it act weird. It uses a single timer to update a stack of data making it very efficient. The amount of function calls is kept to a bear minimum, which should yield quick results.The aim of this library is for users who seek out a very very basic knock-back. The user must specify a distance a unit will be knocked back, and the time it will take.
Its as easy to use as:
JASS:
call knockback.create( unitVar, angleInRadians, distance, time )
Requirements
- JassHelper (to compile)
Knock Back Lite
JASS:
library Knockback
globals
//*********************
//* Configuration
//* ¯¯¯¯¯¯¯¯¯¯¯¯¯
//*
private constant real LOOP_REF = 0.03
//*
//*
//*****************************************************************************
endglobals
struct knockback
readonly unit subject = null
readonly real velocity
readonly real rotation
private real rotationCos
private real rotationSin
readonly real friction
private static real checkItemX = GetRectMaxX(bj_mapInitialPlayableArea)
private static real checkItemY = GetRectMaxY(bj_mapInitialPlayableArea)
private static item checkItem
private static timer priv_stackLoop = CreateTimer()
private static constant real priv_stackLoopRef = LOOP_REF
private real priv_time
private boolean priv_stopped = false
private integer priv_index
private static thistype array priv_stack
private static integer priv_stackN = 0
method stop takes nothing returns nothing
// Instead of directly destroying an instance from knockback, the user can tell the system
// that he/she wants the knockback to be stopped. If the knockback is to be stopped, it will
// be on the next iteration.
set .priv_stopped = true
endmethod
private method onDestroy takes nothing returns nothing
set thistype.priv_stackN = thistype.priv_stackN-1
set thistype.priv_stack[.priv_index] = thistype.priv_stack[thistype.priv_stackN]
set thistype.priv_stack[.priv_index].priv_index = .priv_index
if (.priv_stackN == 0) then
call PauseTimer(thistype.priv_stackLoop)
endif
endmethod
private static method onRef takes nothing returns nothing
local integer i = .priv_stackN-1
local thistype kb
local real x
local real y
local real iX
local real iY
loop
exitwhen(i < 0)
set kb = .priv_stack[i]
if (kb != 0) then
set x = GetWidgetX(kb.subject) + kb.velocity * kb.rotationCos
set y = GetWidgetY(kb.subject) + kb.velocity * kb.rotationSin
//use an item to check the pathability of the new coordinate and
//move the unit if the point is available.
call SetItemPosition(checkItem, x, y)
set iX = GetWidgetX(checkItem)
set iY = GetWidgetY(checkItem)
if ((iX-x)*(iX-x) + (iY-y)*(iY-y) <= 32) then
call SetUnitX(kb.subject, x)
call SetUnitY(kb.subject, y)
endif
set kb.velocity = kb.velocity + kb.friction
set kb.priv_time = kb.priv_time - .priv_stackLoopRef
if (kb.priv_time <= 0.00) or kb.priv_stopped then
call kb.destroy()
endif
endif
set i = i - 1
endloop
call SetItemPosition(checkItem, checkItemX, checkItemY)
call SetItemVisible(checkItem, false)
endmethod
static method create takes unit u, real angle, real distance, real time returns thistype
local thistype kb = .allocate()
set kb.subject = u
set kb.velocity = 2*distance/time
set kb.rotation = angle
set kb.rotationCos = Cos(angle)
set kb.rotationSin = Sin(angle)
set kb.friction = -kb.velocity/time
set kb.priv_index = .priv_stackN
set kb.priv_time = time
set kb.velocity = kb.velocity * .priv_stackLoopRef
set kb.friction = kb.friction * .priv_stackLoopRef * .priv_stackLoopRef
if (.priv_stackN == 0) then
call TimerStart(.priv_stackLoop, .priv_stackLoopRef, true, function thistype.onRef)
endif
set .priv_stack[.priv_stackN] = kb
set .priv_stackN = .priv_stackN + 1
return kb
endmethod
static method onInit takes nothing returns nothing
//the item will be have maintained invisibility as to not interfere with
//in-game actions, such as a unit picking the item up.
set checkItem=CreateItem('afac', checkItemX, checkItemY)
call SetItemVisible(checkItem, false)
endmethod
endstruct
endlibrary
Attachments
Last edited: