傭兵護主

本篇為多執行緒教學的範例,若讀者對多執行緒不熟,建議先閱讀多執行緒教學

function DistanceBetweenPointsXY takes real x1, real y1, real x2, real y2 returns real
    return SquareRoot((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
endfunction
function UnitGuardUnit_child takes nothing returns nothing
    local unit slave = bj_meleeNearestMine
    local unit master = bj_groupRandomCurrentPick
    local real angle
    local real dist
    local real x
    local real y
    loop
        exitwhen IsUnitDeadBJ(slave)
        //距離主人太遠時,強制移向主人;夠近時,移動攻擊
        if ( DistanceBetweenPointsXY(GetUnitX(slave), GetUnitY(slave), GetUnitX(master), 下行接續
            GetUnitY(master) ) > 1000.0 ) then
            call IssuePointOrder(slave, "move", GetUnitX(master), GetUnitY(master))
        else
            set angle = GetRandomDirectionDeg()
            set dist = GetRandomReal(100.00, 500.00)
            set x = GetUnitX(master) + dist * Cos(angle * bj_DEGTORAD)
            set y = GetUnitY(master) + dist * Sin(angle * bj_DEGTORAD)
            call IssuePointOrder(slave, "attack", x, y)
        endif
        call TriggerSleepAction( 1.0+GetRandomReal(0.0,1.5))
    endloop
endfunction

//主要程式,命令部隊(slave)在主人(master)身邊巡邏,直到自己死掉為止。
function UnitGuardUnit takes unit slave, unit master returns nothing
    local unit Bj_meleeNearestMine = bj_meleeNearestMine
    local unit Bj_groupRandomCurrentPick = bj_groupRandomCurrentPick
    set bj_meleeNearestMine = slave
    set bj_groupRandomCurrentPick = master
    call ExecuteFunc("UnitGuardUnit_child")
    set bj_meleeNearestMine = Bj_meleeNearestMine
    set bj_groupRandomCurrentPick = Bj_groupRandomCurrentPick
endfunction

類似前面的範例,一樣以新增執行緒的方式來處理。每呼叫一次這個函數,就會自動產生一組程式碼來控制部隊的巡邏,一直到該結束時才停止。

綜合教學/jass入門教學/e3.傭兵護主.txt · 上一次變更: 2007年11月11日 3:31 pm 來自 wasabi
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0