欢迎光临散文网 会员登陆 & 注册

jass基础-UI案例推箱子

2023-08-06 00:57 作者:庞各庄大棚  | 我要投稿

#include "../../jass/BlizzardAPI.j"

library demo initializer test


    globals

        string array boxArr

        integer parentUI = 0 // 父UI

        integer array childBoxArr // 子UI数组


        CardData heroCard  // 人物数据

        CardData array boxCard  // 箱子数据

        integer boxCount = 0  // 箱子数量(动态获取)

        integer targetCount = 0 // 已完成的数量


        integer array numArr // 关卡静态地图数组


        boolean isShow = false // 面板是否显示

        boolean hasPass = false // 是否已经通关


        // 关卡

        string level1 = "000000000001111100011000110010434010010220010010124010011000110001111100000000000"

        string level2 = "111111000100001110104140010100342010110010110011002210001100110000111100000000000"

    endglobals


    struct CardData

        integer indexX

        integer indexY

        integer cardType

        integer cardUI


        public static method create takes integer parentUI, integer cardType, integer indexX, integer indexY returns CardData

            local CardData this = CardData.allocate()

            set this.indexX = indexX

            set this.indexY = indexY

            set this.cardType = cardType

            set this.cardUI = DzCreateFrameByTagName("BACKDROP", "", parentUI, "", 0)

            call DzFrameSetSize(this.cardUI, 0.04, 0.04)

            call DzFrameSetPoint(this.cardUI, 6, parentUI, 6, 0.04 * indexX, 0.04 * indexY)

            call DzFrameSetTexture(this.cardUI, boxArr[this.cardType], 0)

            return this

        endmethod

    endstruct


    function setNumArr takes string str returns nothing

        local integer i = 0

        local string singleChar

        loop

            exitwhen i >= 81

            set singleChar = SubString(str, i, i + 1)

            set numArr[i] = S2I(singleChar)

            set i = i + 1

        endloop

    endfunction


    function canMove takes integer value, integer preNum, integer preNum2, integer preI, integer preI2 returns boolean

        if value < 0 or value > 8 then

            call BJDebugMsg("到边界了")

            return false

        endif

        if preNum == 1 then

            call BJDebugMsg("遇到了墙")

            return false

        endif

        if preI >= 0 and preI2 == - 1 then

            call BJDebugMsg("隔位是障碍")

            return false

        endif

        return true

    endfunction


    function isBox takes integer preIndex returns integer

        local integer i = 0

        local integer x = ModuloInteger(preIndex, 9)

        local integer y = preIndex / 9

        loop

            exitwhen i >= boxCount

            if boxCard[i].indexX == x and boxCard[i].indexY == y then

                return i

            endif

            set i = i + 1

        endloop

        return - 1

    endfunction


    function canBoxMove takes integer preIndex2 returns integer

        if ModuloInteger(preIndex2, 9) < 0 or ModuloInteger(preIndex2, 9) > 8then

            return - 1

        endif

        if isBox(preIndex2) >= 0 then

            return - 1

        endif

        if numArr[preIndex2] == 0 then

            return 0 // 可以正常走

        endif

        if numArr[preIndex2] == 2 then

            return 1 // 前面是目标点

        endif

        return - 1

    endfunction


    function setBoxCard takes integer preI, integer dir returns nothing

        if dir == 1 then

            set boxCard[preI].indexX = boxCard[preI].indexX - 1

        elseif dir == 2 then

            set boxCard[preI].indexX = boxCard[preI].indexX + 1

        elseif dir == 3 then

            set boxCard[preI].indexY = boxCard[preI].indexY + 1

        elseif dir == 4 then

            set boxCard[preI].indexY = boxCard[preI].indexY - 1

        endif

    endfunction


    function aaa takes integer preIndex, integer preI, integer preI2, integer dir returns nothing

        if preI == - 1 then

            if numArr[preIndex] == 0 or numArr[preIndex] == 2 then

                call DzFrameSetPoint(heroCard.cardUI, 6, parentUI, 6, 0.04 * heroCard.indexX, 0.04 * heroCard.indexY)

                return

            endif

        else

            if preI2 == - 1 then

                return

            endif

            call DzFrameSetPoint(heroCard.cardUI, 6, parentUI, 6, 0.04 * heroCard.indexX, 0.04 * heroCard.indexY)

            call setBoxCard(preI, dir)

            call DzFrameSetPoint(boxCard[preI].cardUI, 6, parentUI, 6, 0.04 * boxCard[preI].indexX, 0.04 * boxCard[preI].indexY)

            if preI2 == 1 then

                call DzFrameSetTexture(boxCard[preI].cardUI, boxArr[5], 0)

                if numArr[preIndex] == 0 then

                    set targetCount = targetCount + 1

                endif

            else

                call DzFrameSetTexture(boxCard[preI].cardUI, boxArr[4], 0)

                if numArr[preIndex] == 2 then

                    set targetCount = targetCount - 1

                endif

            endif

        endif

    endfunction


    function isPass takes nothing returns nothing

        call BJDebugMsg("targetCount:" + I2S(targetCount))

        if targetCount == boxCount then

            call BJDebugMsg("恭喜过关")

            set hasPass = true

        endif

    endfunction


    function move takes integer dir returns nothing

        local integer heroIndex

        local integer preIndex = 0

        local integer preIndex2 = 0

        local integer preI

        local integer preI2


        if parentUI == 0 or not isShow then

            call BJDebugMsg("游戏没有打开,操作无效")

            return

        endif


        if hasPass then

            call BJDebugMsg("已经通关,不能再操作")

            return

        endif


        set heroIndex = heroCard.indexX + 9 * heroCard.indexY

       

        if dir == 1 then  // 左

            set preIndex = heroIndex - 1

            set preIndex2 = heroIndex - 2

            set preI = isBox(preIndex)

            set preI2 = canBoxMove(preIndex2)

            if not canMove(heroCard.indexX - 1, numArr[preIndex], numArr[preIndex2], preI, preI2) then

                return

            endif

            set heroCard.indexX = heroCard.indexX - 1

           

        elseif dir == 2 then // 右

            set preIndex = heroIndex + 1

            set preIndex2 = heroIndex + 2

            set preI = isBox(preIndex)

            set preI2 = canBoxMove(preIndex2)

            if not canMove(heroCard.indexX + 1, numArr[preIndex], numArr[preIndex2], preI, preI2)then

                return

            endif

            set heroCard.indexX = heroCard.indexX + 1

        elseif dir == 3 then // 上

            set preIndex = heroIndex + 9

            set preIndex2 = heroIndex + 18

            set preI = isBox(preIndex)

            set preI2 = canBoxMove(preIndex2)

            if not canMove(heroCard.indexY + 1, numArr[preIndex], numArr[preIndex2], preI, preI2) then

                return

            endif

            set heroCard.indexY = heroCard.indexY + 1

        elseif dir == 4 then // 下

            set preIndex = heroIndex - 9

            set preIndex2 = heroIndex - 18

            set preI = isBox(preIndex)

            set preI2 = canBoxMove(preIndex2)

            if not canMove(heroCard.indexY - 1, numArr[preIndex], numArr[preIndex2], preI, preI2) then

                return

            endif

            set heroCard.indexY = heroCard.indexY - 1

        endif

        call aaa(preIndex, preI, preI2, dir)

        call isPass()

    endfunction


    // 向左走

    function clickJ takes nothing returns nothing

        call move(1)

    endfunction


    // 向右走

    function clickL takes nothing returns nothing

        call move(2)

    endfunction


    // 向上走

    function clickI takes nothing returns nothing

        call move(3)

    endfunction


    // 向下走

    function clickK takes nothing returns nothing

        call move(4)

    endfunction


    function initLevel takes nothing returns nothing

        local integer i = 0

        local integer j = 0

        local integer index = 0

        local integer temp = 0


        set parentUI = DzCreateFrameByTagName("FRAME", "", DzGetGameUI(), "", 0)

        call DzFrameSetSize(parentUI, 0.4, 0.3)

        call DzFrameSetAbsolutePoint(parentUI, 6, 0.2, 0.2)

        call setNumArr(level1)

        set isShow = true

        loop

            exitwhen i >= 9

            loop

                exitwhen j >= 9

                set temp = j + 9 * i

                set childBoxArr[temp] = DzCreateFrameByTagName("BACKDROP", "", parentUI, "", 0)

                call DzFrameSetSize(childBoxArr[temp], 0.04, 0.04)

                call DzFrameSetPoint(childBoxArr[temp], 6, parentUI, 6, 0.04 * j, 0.04 * i)

                set index = numArr[temp]

                if index == 3 or index == 4 then

                    call DzFrameSetTexture(childBoxArr[temp], boxArr[0], 0)

                elseif index == 5 then

                    call DzFrameSetTexture(childBoxArr[temp], boxArr[2], 0)

                    set targetCount = targetCount + 1

                else

                    call DzFrameSetTexture(childBoxArr[temp], boxArr[index], 0)

                endif

                set j = j + 1

            endloop

            set j = 0

            set i = i + 1

        endloop

        set i = 0

        set j = 0

        loop

            exitwhen i >= 9

            loop

                exitwhen j >= 9

                set temp = j + 9 * i

                set index = numArr[temp]

                if index == 3 then

                    set heroCard = CardData.create(parentUI, index, j, i)

                    set numArr[temp] = 0

                elseif index == 4 or index == 5 then

                    set boxCard[boxCount] = CardData.create(parentUI, index, j, i)

                    set boxCount = boxCount + 1

                    set numArr[temp] = 0

                endif

                set j = j + 1

            endloop

            set j = 0

            set i = i + 1

        endloop

    endfunction


    function initBlock takes nothing returns nothing

        set boxArr[0] = "" // 空地面 ReplaceableTextures\\CommandButtons\\BTNTranquility.blp

        set boxArr[1] = "ReplaceableTextures\\CommandButtons\\BTNPowerGenerator.blp" // 墙

        set boxArr[2] = "ReplaceableTextures\\CommandButtons\\BTNSentryWard.blp" // 目标点

        set boxArr[3] = "ReplaceableTextures\\CommandButtons\\BTNHeroPaladin.blp" // 人

        set boxArr[4] = "ReplaceableTextures\\CommandButtons\\BTNMagicVault.blp" // 箱子

        set boxArr[5] = "ReplaceableTextures\\CommandButtons\\BTNFarm.blp" // 推到目标点的箱子

    endfunction


    function doSomething3 takes nothing returns nothing

        if parentUI == 0 then

            return

        endif

        set isShow = not isShow

        call DzFrameShow(parentUI, isShow)

    endfunction


    function triggerInit takes nothing returns nothing

        local trigger t = CreateTrigger()

        local trigger t2 = CreateTrigger()

        local trigger tLeft = CreateTrigger()

        local trigger tDown = CreateTrigger()

        local trigger tUp = CreateTrigger()

        local trigger tRight = CreateTrigger()

        call TriggerRegisterPlayerChatEvent(t, Player(0), "1", true)

        call TriggerAddAction(t, function initLevel)

        call TriggerRegisterPlayerChatEvent(t2, Player(0), "2", true) // 输入2  打开/关闭面板

        call TriggerAddAction(t2, function doSomething3)


        call DzTriggerRegisterKeyEventTrg(tLeft, 1, 'J')

        call TriggerAddAction(tLeft, function clickJ)


        call DzTriggerRegisterKeyEventTrg(tDown, 1, 'K')

        call TriggerAddAction(tDown, function clickK)


        call DzTriggerRegisterKeyEventTrg(tUp, 1, 'I')

        call TriggerAddAction(tUp, function clickI)


        call DzTriggerRegisterKeyEventTrg(tRight, 1, 'L')

        call TriggerAddAction(tRight, function clickL)

        set t = null

    endfunction


    function gameInit takes nothing returns nothing

        local unit u = CreateUnit(Player(0), 'hfoo', - 100, - 100, 0)

        call FogEnable(false)

        call FogMaskEnable(false)

    endfunction


    function test takes nothing returns nothing

        call gameInit()

        call triggerInit()

        call initBlock()

    endfunction

endlibrary

jass基础-UI案例推箱子的评论 (共 条)

分享到微博请遵守国家法律