I created this because many people asked how to make a projectile explode when it got into range of enemy units. Well, I created this to show how easy it is.
Uses CSSafety and ABC.
Requires NewGen and little-medium JASS experience.
Code:
Screenshot:
Not needed, it's just a fireball being fired of.
Well, enjoy.
Uses CSSafety and ABC.
Requires NewGen and little-medium JASS experience.
Code:
JASS:
library PST
globals
// Below: Needed for the code, don't edit.
private unit GLOBALUNIT
private real Range
private attacktype Atype
private damagetype Dtype
private real dmg
private string sfx
endglobals
private struct Data
timer t
timer ti
unit dummy
unit caster
real facing
real createx
real createy
location currentloc
player owner
trigger dietrig
private method onDestroy takes nothing returns nothing
call ReleaseTimer(.t)
call ReleaseTimer(.ti)
call ClearTimerStructA(.t)
call ClearTimerStructA(.ti)
call RemoveLocation(.currentloc)
set .currentloc = null
set .dummy = null
set .owner = null
set .createy = 0.00
set .createx = 0.00
set .facing = 0.00
set .t = null
set .ti = null
call DestroyTimer(.t)
call DestroyTimer(.ti)
endmethod
endstruct
private function Move takes nothing returns nothing
local Data d = GetTimerStructA(GetExpiredTimer())
local real x
local real y
set x = GetLocationX(d.currentloc) + 5.00 * Cos(d.facing * bj_DEGTORAD)
set y = GetLocationY(d.currentloc) + 5.00 * Sin(d.facing * bj_DEGTORAD)
call SetUnitPosition(d.dummy, x, y)
set d.currentloc = GetUnitLoc(d.dummy)
set x = 0.00
set y = 0.00
endfunction
private function Group takes nothing returns nothing
local unit u = GetEnumUnit()
local real x = GetUnitX(u)
local real y = GetUnitY(u)
call DestroyEffect(AddSpecialEffect(sfx, x, y))
call UnitDamageTarget(GLOBALUNIT, u, dmg, false, true, Atype, Dtype, WEAPON_TYPE_WHOKNOWS)
endfunction
private function InRangeConds takes nothing returns boolean
return ( IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(GLOBALUNIT)) == true ) and ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) == false ) and ( GetWidgetLife(GetFilterUnit())>.405 )
endfunction
private function InRange takes nothing returns boolean
local Data d = GetTimerStructA(GetExpiredTimer())
local real x = GetLocationX(d.currentloc)
local real y = GetLocationY(d.currentloc)
local group g = CreateGroup()
call GroupEnumUnitsInRange(g, x, y, Range, Filter(function InRangeConds))
call ForGroup(g, function Group)
if CountUnitsInGroup(g) >= 1 then
call ClearTriggerStructA(d.dietrig)
call DestroyTrigger(d.dietrig)
set d.dietrig = null
call KillUnit(d.dummy)
call d.destroy()
endif
return false
endfunction
private function GroupConds takes nothing returns boolean
return ( IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(GLOBALUNIT)) == true ) and ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) == false ) and ( GetWidgetLife(GetFilterUnit())<.405 )
endfunction
private function DummyDie takes nothing returns boolean
local Data d = GetTriggerStructA(GetTriggeringTrigger())
local real x = GetUnitX(d.dummy)
local real y = GetUnitY(d.dummy)
local unit u
local group g = CreateGroup()
call GroupEnumUnitsInRange(g, x, y, 256.0, Filter(function GroupConds))
call DestroyEffect(AddSpecialEffect(sfx, x, y))
call KillUnit(d.dummy)
loop
set u = FirstOfGroup(g)
exitwhen CountUnitsInGroup(g) <= 0
call UnitDamageTarget(d.caster, u, dmg, false, true, Atype, Dtype, WEAPON_TYPE_WHOKNOWS)
call GroupRemoveUnit(g, u)
endloop
call d.destroy()
call DestroyGroup(g)
set g = null
set u = null
return false
endfunction
public function Create takes integer DummyID, string FX, real Damage, real r, real LivingTime, attacktype attype, damagetype datype returns nothing
local Data d = Data.create()
set d.caster = GetTriggerUnit()
set d.owner = GetOwningPlayer(d.caster)
set d.facing = GetUnitFacing(d.caster)
set d.createx = GetUnitX(d.caster) + 35.00 * Cos(d.facing * bj_DEGTORAD)
set d.createy = GetUnitY(d.caster) + 35.00 * Sin(d.facing * bj_DEGTORAD)
set d.dummy = CreateUnit(d.owner, DummyID, d.createx, d.createy, d.facing)
set d.currentloc = GetUnitLoc(d.dummy)
set sfx = FX
set GLOBALUNIT = d.caster
set Range = r
set Atype = attype
set Dtype = datype
set dmg = Damage
call UnitApplyTimedLife(d.dummy, 'BTLF', LivingTime)
set d.t = NewTimer()
call SetTimerStructA(d.t, d)
call TimerStart(d.t, 0.01, true, function Move)
set d.ti = NewTimer()
call SetTimerStructA(d.ti, d)
call TimerStart(d.ti, 0.01, true, function InRange)
set d.dietrig = CreateTrigger()
call TriggerRegisterUnitEvent(d.dietrig, d.dummy, EVENT_UNIT_DEATH)
call TriggerAddCondition(d.dietrig, Filter(function DummyDie))
call SetTriggerStructA(d.dietrig, d)
endfunction
endlibrary
Screenshot:
Not needed, it's just a fireball being fired of.
Well, enjoy.