jass基础-农场加钱漂字

#include "../../jass/BlizzardAPI.j"
library demo initializer test
// 漂浮文字案例 - 农场加钱漂字
globals
integer nm = 'h003'
integer nc1 = 'h001'
integer nc2 = 'h002'
integer nc3 = 'h000'
integer maxCount = 5
hashtable ht
timer tm = null
unit array farmArr
endglobals
function doSomething takes nothing returns nothing
endfunction
function addMoney takes nothing returns nothing
local integer i = 0
local texttag tt
loop
exitwhen i >= maxCount
if farmArr[i] != null then
call SetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD) + LoadInteger(ht, StringHash("农场"), GetUnitTypeId(farmArr[i])))
set tt = CreateTextTagUnitBJ("+" + I2S(LoadInteger(ht, StringHash("农场"), GetUnitTypeId(farmArr[i]))), farmArr[i], 0, 14, 0, 100, 0, 0)
call SetTextTagVelocityBJ(tt, 50, 90)
call SetTextTagLifespan(tt, 2)
call SetTextTagFadepoint(tt, 1)
call SetTextTagPermanent(tt, false)
endif
set i = i + 1
endloop
set tt = null
endfunction
function getFreeIndex takes nothing returns integer
local integer i = 0
loop
exitwhen i >= maxCount
if farmArr[i] == null then
return i
endif
set i = i + 1
endloop
return - 1
endfunction
function buildAction takes nothing returns nothing
local integer index = getFreeIndex()
if tm == null then
set tm = CreateTimer()
call TimerStart(tm, 1, true, function addMoney)
endif
set farmArr[index] = GetTriggerUnit()
endfunction
function deathAction takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer i = 0
loop
exitwhen i >= maxCount
if farmArr[i] == u then
set farmArr[i] = null
return
endif
set i = i + 1
endloop
set u = null
endfunction
function triggerInit takes nothing returns nothing
local trigger t = CreateTrigger()
local trigger t2 = CreateTrigger()
local trigger t3 = CreateTrigger()
call TriggerRegisterPlayerChatEvent(t, Player(0), "1", true)
call TriggerAddAction(t, function doSomething)
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_CONSTRUCT_FINISH)
call TriggerAddAction(t2, function buildAction)
call TriggerRegisterAnyUnitEventBJ(t3, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddAction(t3, function deathAction)
set t = null
set t2 = null
set t3 = null
endfunction
function gameInit takes nothing returns nothing
call FogEnable(false)
call FogMaskEnable(false)
set ht = InitHashtable()
call SaveInteger(ht, StringHash("农场"), nc1, 5)
call SaveInteger(ht, StringHash("农场"), nc2, 10)
call SaveInteger(ht, StringHash("农场"), nc3, 15)
call CreateUnit(Player(0), nm, 0, 0, 0)
call SetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD, 10000)
call SetPlayerState(Player(0), PLAYER_STATE_RESOURCE_LUMBER, 10000)
call SetPlayerTechMaxAllowed(Player(0), nc1, maxCount)
endfunction
function test takes nothing returns nothing
call gameInit()
call triggerInit()
endfunction
endlibrary