- Joined
- Feb 20, 2013
- Messages
- 136
A bit sloppy at the end, hopefully it's alright.
Edit: Fixed some leaks
Edit: Fixed some leaks
JASS:
scope UnitSpawn initializer Init
globals
private unit array barracks
private integer array unitsData
endglobals
private function isPlayerForce takes nothing returns boolean
return GetUnitTypeId(GetFilterUnit()) == 'hfoo' or GetUnitTypeId(GetFilterUnit()) == 'hrif'
endfunction
private function ExplodeAllUnits takes nothing returns nothing
local unit u
local group g = CreateGroup()
call GroupEnumUnitsInRect(g, GetPlayableMapRect(), Filter(function isPlayerForce))
loop
set u = FirstOfGroup(g)
exitwhen u == null
call SetUnitExploded(u, true)
call KillUnit(u)
call GroupRemoveUnit(g, u)
endloop
call DestroyGroup(g)
set g = null
set u = null
endfunction
private function InitiateTraining takes nothing returns nothing
local integer i = 0
loop
exitwhen i == 4
if (barracks[i] != null) then
call IssueTrainOrderByIdBJ(barracks[i], unitsData[GetRandomInt(0,1)])
endif
set i = i + 1
endloop
endfunction
private function VictoryCondition takes nothing returns boolean
set unitsData[2] = unitsData[2] - 1
if (unitsData[2] == 0) then
call ExplodeAllUnits()
set barracks[0] = null
set barracks[1] = null
set barracks[2] = null
set barracks[3] = null
call DisplayTextToForce(GetPlayersAll(), "You won!")
endif
return TRUE
endfunction
private function Init takes nothing returns nothing
local integer i = 0
local timer t = CreateTimer()
local real x
local real y
local trigger unitDiesTrigger = CreateTrigger()
local player playerHostile = Player(PLAYER_NEUTRAL_AGGRESSIVE)
local unit u
call TriggerRegisterPlayerUnitEvent(unitDiesTrigger, playerHostile, EVENT_PLAYER_UNIT_DEATH, null)
call TriggerAddCondition(unitDiesTrigger, function VictoryCondition)
set unitsData[0] = 'hfoo'
set unitsData[1] = 'hrif'
// Spawning neutral hostile units
loop
exitwhen i == 20
// There are no units on some lvls so they don't spawn sometimes
loop
set u = CreateUnit(playerHostile, ChooseRandomCreep(GetRandomInt(1, 10)), 0, 0, 0)
exitwhen u != null
endloop
set unitsData[2] = unitsData[2] + 1
set i = i + 1
endloop
// Creating player barracks
set i = 0
loop
exitwhen i == 4
set x = 3000 * Cos(bj_PI * (i * 0.5 - 0.25))
set y = 3000 * Sin(bj_PI * (i * 0.5 - 0.25))
set barracks[i] = CreateUnit(Player(i), 'hbar', x, y, bj_UNIT_FACING)
call IssuePointOrder(barracks[i], "smart", 0, 0)
set i = i + 1
endloop
call TimerStart(t, 2.5, true, function InitiateTraining)
set t = null
set unitDiesTrigger = null
set playerHostile = null
set u = null
endfunction
endscope
Attachments
Last edited: