[Unity XLua]熱更新XLua入門(二)-俄羅斯方塊實例篇

前言

在xLua沒出來之前,開源的lua框架基本都是以界面用Lua開發爲主,核心戰鬥用C#開發,但xLua出來之後主推C#開發,Lua用作HotFix,這裏我展示的第一個例子就是基於界面的經典2D小遊戲——俄羅斯方塊,界面邏輯是用C#寫,啓動加載邏輯是用lua,後面我會繼續第二個同樣的Demo,但是以純Lua爲主,這個案例明天更新。

效果圖

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
由於我不會美術,所以這裏使用的開源的遊戲資源,感謝此作者。

遊戲啓動

C#啓動Lua邏輯

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;

[LuaCallCSharp]
public class LuaRunningBehaviour : MonoBehaviour
{
    public TextAsset luaScript;

    internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only!
    internal static float lastGCTime = 0;
    internal const float GCInterval = 1;//1 second 

    private Action luaStart;
    private Action luaUpdate;
    private Action luaOnDestroy;

    private LuaTable scriptEnv;

    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();
        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);

        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);

        Action luaAwake = scriptEnv.Get<Action>("awake");
        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }

    // Use this for initialization
    void Start()
    {
        if (luaStart != null)
        {
            luaStart();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (luaUpdate != null)
        {
            luaUpdate();
        }
        if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
        {
            luaEnv.Tick();
            LuaBehaviour.lastGCTime = Time.time;
        }
    }

    void OnDestroy()
    {
        if (luaOnDestroy != null)
        {
            luaOnDestroy();
        }
        luaOnDestroy = null;
        luaUpdate = null;
        luaStart = null;
        scriptEnv.Dispose();
    }
}

Lua加載遊戲界面預設邏輯


local util = require 'xlua.util'

local function initGamePanel()
    local obj = CS.UnityEngine.Resources.Load("TetrisPanelOne")
    local gamePanel = CS.UnityEngine.Object.Instantiate(obj.gameObject).transform
    gamePanel.name = "GamePanel"
    gamePanel.gameObject.name = "GamePanel"
    gamePanel:SetParent(self.transform)
    gamePanel.localPosition = CS.UnityEngine.Vector3.zero
    gamePanel.localScale = CS.UnityEngine.Vector3.one
end

function start()
    print("lua start, 遊戲開始...")
    initGamePanel()
end

function ondestroy()
    print("lua destroy")
end

遊戲主要界面對應的C#邏輯

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class MainGameCSScript : MonoBehaviour
{
    const int cRow = 16;
    const int cCol = 10;
    List<int>[] aTable = new List<int>[cRow];
    int lineCount = 0;
    public Texture[] BoxTexture = new Texture[4];
    private string[] boxColors = new string[] { "eluosi_lan", "eluosi_zi", "eluosi_lv", "eluosi_hong" };

    public int LineCount
    {
        get { return lineCount; }
        set
        {
            lineCount = value;
            this.onLineCountChange();
        }
    }
    int score = 0;

    public int Score
    {
        get { return score; }
        set
        {
            score = value;
            this.onScoreChange();
        }
    }
    int nextCellID = 0;
    int curCellID = 0;

    float curSpeed = 1;
    float fCurSpeed = 1;
    float fDeltaTime = 0;
    bool bPause = false;

    bool bStart = false;
    bool bGameOver = false;

    public bool BGameOver
    {
        get { return bGameOver; }
        set
        {
            bGameOver = value;
            this.onGameOverChange(bGameOver);
        }
    }

    System.Random rdm = new System.Random();

    const int boxTop = 16;
    const int boxLeft = 290;
    /// <summary>
    /// 彩色磚塊的寬度
    /// </summary>
    const int boxSize = 38;

    int[][] nextCell = null;
    int[][] curCell = null;
    int curCellCol = 0;
    int curCellRow = 0;

    public GameObject BtnUp, BtnDown, BtnLeft, BtnRight, BtnIsPause;
    public UILabel TimeLabel, ScoreLabel, LineCountLabel;

    private Action onScoreChange = null;
    private Action onLineCountChange = null;
    private Action<bool> onGameOverChange = null;
    public GameObject EndPanel, BtnRestart;
    public UISprite[] BoxSprites = null;

    void Start()
    {
        BoxSprites = new UISprite[4];
        for (int i = 0; i < 4; i++)
        {
            BoxTexture[i] = Resources.Load<Texture>(boxColors[i]);
        }
        this.onScoreChange = this.ScoreChange;
        this.onLineCountChange = this.LineCountChange;
        this.onGameOverChange = this.GameOverChange;
        TimeLabel = transform.Find("LeftNode/CutDownBG/CountLabel").GetComponent<UILabel>();
        LineCountLabel = transform.Find("ScoreNode/LineCount").GetComponent<UILabel>();
        ScoreLabel = transform.Find("ScoreNode/Score").GetComponent<UILabel>();
        BtnLeft = transform.Find("InputNode/Left").gameObject;
        BtnRight = transform.Find("InputNode/Right").gameObject;
        BtnUp = transform.Find("InputNode/Up").gameObject;
        BtnDown = transform.Find("InputNode/Down").gameObject;
        BtnIsPause = transform.Find("LeftNode/RestartBtn").gameObject;
        EndPanel = transform.Find("EndPanel").gameObject;
        BtnRestart = transform.Find("EndPanel/ReplayBtn").gameObject;

        for (int i = 1; i <= 4; i++)
        {
            var obj = transform.Find(string.Format("BlcokTop/Blocks/block{0}", i)).gameObject;
            var sprite = obj.GetComponent<UISprite>();
            BoxSprites[i - 1] = sprite;
        }

        UIEventListener.Get(BtnRestart).onClick = go =>
        {
            EndPanel.gameObject.SetActive(false);
            Debug.Log("重新開始");
            DoStart();
        };

        UIEventListener.Get(BtnIsPause).onClick = go =>
        {
            Debug.Log("開始/暫停");
            bPause = !bPause;
        };

        UIEventListener.Get(BtnUp).onClick = go =>
        {
            Debug.Log("上翻");
            DoTransform();
        };

        UIEventListener.Get(BtnDown).onPress = (go, state) =>
        {
            Debug.Log("下按");
            //state  true:按下   false:鬆開
            if (state)
                DoSpeedUp();
            else
                DoSpeedRestore();
        };

        UIEventListener.Get(BtnLeft).onClick = go =>
        {
            Debug.Log("左移");
            DoLeft();
        };

        UIEventListener.Get(BtnRight).onClick = go =>
        {
            Debug.Log("右移");
            DoRight();
        };

        this.DoStart();
    }

    void ScoreChange()
    {
        ScoreLabel.text = "獲得積分:" + this.Score.ToString();
    }

    void LineCountChange()
    {
        LineCountLabel.text = "消滅行數:" + this.LineCount.ToString();
    }

    void GameOverChange(bool state)
    {
        if (state)
            EndPanel.SetActive(true);
    }

    /// <summary>
    /// 初始化10*16的格子
    /// </summary>
    void DoInit()
    {
        this.LineCount = 0;
        this.Score = 0;
        curSpeed = 1;

        for (int i = 0; i < aTable.Length; i++)
        {
            aTable[i] = new List<int>();
            for (int j = 0; j < cCol; j++)
                aTable[i].Add(0);
        }
    }

    void OnGUI()
    {
        if (!bStart)
            return;

        if (BGameOver)
        {
            return;
        }

        if (bPause)
        {
            return;
        }

        for (int i = 0; i < curCell.Length; i++)
        {
            for (int j = 0; j < curCell[i].Length; j++)
            {
                if (curCell[i][j] == 0)
                {
                    continue;
                }
                if (i + curCellRow < 0)
                {
                    continue;
                }
                GUI.DrawTexture(new Rect(boxLeft + boxSize * j + curCellCol * boxSize, boxTop + boxSize * i + curCellRow * boxSize, boxSize, boxSize), BoxTexture[0]);
            }
        }


        //背景圖
        for (int i = 0; i < aTable.Length; i++)
        {
            for (int j = 0; j < cCol; j++)
            {
                if (aTable[i][j] == 0)
                {
                    continue;
                }
                GUI.DrawTexture(new Rect(boxLeft + boxSize * j, boxTop + boxSize * i, boxSize, boxSize), BoxTexture[aTable[i][j] - 1]);
            }
        }
    }

    string GetContent(int cellContent)
    {
        switch (cellContent)
        {
            case 0:
                return "";
            default:
                return cellContent.ToString();
        }
    }

    void DoStart()
    {
        DoInit();
        DoNextCell();

        bStart = true;
        BGameOver = false;
    }

    void DrawNextBox()
    {
        //繪製下一個圖
        int boxIndex = 0;
        for (int i = 0; i < nextCell.Length; i++)
        {
            for (int j = 0; j < nextCell[i].Length; j++)
            {
                if (nextCell[i][j] == 0)
                {
                    continue;
                }
                BoxSprites[boxIndex++].transform.localPosition = new Vector3(j * 35, i * (-35), 0);
            }
        }
    }

    /// <summary>
    /// 隨機生成下面一個模型
    /// </summary>
    void DoNextCell()
    {
        if (nextCell == null)
        {
            nextCellID = rdm.Next(aCells.Length);
            nextCell = aCells[nextCellID];
        }
        curCellCol = cCol / 2 - 2;
        curCellRow = -4;
        curCell = nextCell;
        curCellID = nextCellID;
        nextCellID = rdm.Next(aCells.Length);
        nextCell = aCells[nextCellID];

        DrawNextBox();

        DetectIsFail();
    }

    /// <summary>
    /// 放置當前的模型
    /// </summary>
    private void DoSetCurCellDown()
    {
        for (int i = 0; i < curCell.Length; i++)
        {
            for (int j = 0; j < curCell[i].Length; j++)
            {
                if (curCell[i][j] == 0)
                {
                    continue;
                }
                if (curCellRow + i < 0)
                {
                    BGameOver = true;
                    return;
                }
                aTable[curCellRow + i][curCellCol + j] = curCell[i][j];
            }
        }
        //速度重置
        DoSpeedRestore();
        //消除一行
        DoLine();
        //生成下一個模型
        DoNextCell();
    }

    void Update()
    {
        if (!bStart)
        {
            return;
        }

        if (BGameOver)
        {
            return;
        }

        if (bPause)
        {
            return;
        }

        //自動下滑
        fDeltaTime += Time.deltaTime;
        if (fDeltaTime > fCurSpeed)
        {
            fDeltaTime -= fCurSpeed;
            if (CanMoveTo(curCellCol, curCellRow + 1))
            {
                curCellRow++;
            }
            else
            {
                DoSetCurCellDown();
                return;
            }
        }

        if (BGameOver)
        {
            return;
        }

        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))  //左移
        {
            DoLeft();
        }
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) //右移
        {
            DoRight();
        }
        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))  //上翻
        {
            DoTransform();
        }
        if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))  //按下快速下移
        {
            DoSpeedUp();
        }
        if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))  //按鍵取消 取消快速下移
        {
            DoSpeedRestore();
        }
    }

    /// <summary>
    /// 檢測遊戲是否結束
    /// </summary>
    private void DetectIsFail()
    {
        if (CanMoveTo(curCellCol, curCellRow))
        {
            return;
        }
        BGameOver = true;
    }

    /// <summary>
    /// 判斷模型移動
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="cell"></param>
    /// <returns></returns>
    bool CanMoveTo(int x, int y, int[][] cell = null) // 3, -4
    {
        if (cell == null)
        {
            cell = curCell;
        }
        for (int i = 0; i < cell.Length; i++)
        {
            for (int j = 0; j < cell[i].Length; j++)
            {
                if (cell[i][j] == 0)
                {
                    continue;
                }
                if (x + j < 0 || x + j >= cCol)
                {
                    return false;
                }
                if (y + i >= cRow) //到頂了
                {
                    return false;
                }
                if (y + i < 0)
                {
                    continue;
                }
                if (aTable[i + y][j + x] != 0)
                {
                    return false;
                }
            }
        }
        return true;
    }

    private void DoSpeedRestore()
    {
        fCurSpeed = curSpeed;
    }

    //下移加速
    private void DoSpeedUp()
    {
        fCurSpeed = curSpeed / 8;
        fDeltaTime = fCurSpeed;
    }

    //右移
    private void DoRight()
    {
        if (CanMoveTo(curCellCol + 1, curCellRow))
        {
            curCellCol++;
        }
    }

    //左移
    private void DoLeft()
    {
        if (CanMoveTo(curCellCol - 1, curCellRow))
        {
            curCellCol--;
        }
    }

    //上翻
    private void DoTransform()
    {
        int transformedCellID = curCellID / 4 * 4 + (curCellID % 4 + 1) % 4;
        if (CanMoveTo(curCellCol, curCellRow, aCells[transformedCellID]))
        {
            curCellID = transformedCellID;
            curCell = aCells[transformedCellID];
        }
    }

    /// <summary>
    /// 消除一行
    /// </summary>
    private void DoLine()
    {
        List<int> fulledLines = new List<int>();
        for (int i = curCellRow; i < curCellRow + 4; i++)
        {
            if (i >= cRow)
            {
                continue;
            }
            if (i < 0)
            {
                BGameOver = true;
                return;
            }
            for (int j = 0; j < cCol; j++)
            {
                if (aTable[i][j] == 0)
                {
                    break;
                }
                if (j == cCol - 1)
                {
                    fulledLines.Add(i);
                }
            }
        }

        if (fulledLines.Count == 0)
        {
            return;
        }

        for (int i = 0; i < fulledLines.Count; i++)
        {
            for (int j = 0; j < cCol; j++)
            {
                aTable[fulledLines[i]][j] = 0;
            }
        }

        int ilastEmptyRow = fulledLines[fulledLines.Count - 1];
        int ilastNotEmptyRow = ilastEmptyRow - 1;
        while (true)
        {
            if (ilastNotEmptyRow < 0)
            {
                break;
            }

            if (!IsRowEmpty(ilastNotEmptyRow))
            {
                for (int j = 0; j < cCol; j++)
                {
                    aTable[ilastEmptyRow][j] = aTable[ilastNotEmptyRow][j];
                    aTable[ilastNotEmptyRow][j] = 0;
                }
                ilastEmptyRow--;
            }
            ilastNotEmptyRow--;
        }

        for (int i = 0; i < fulledLines.Count; i++)
        {
            for (int j = 0; j < cCol; j++)
            {
                aTable[i][j] = 0;
            }
        }
        //消除的行數
        LineCount += fulledLines.Count;
        //分數加成
        Score += fulledLines.Count * fulledLines.Count;

        curSpeed *= 0.99f;
    }

    bool IsRowEmpty(int irow)
    {
        for (int j = 0; j < cCol; j++)
        {
            if (aTable[irow][j] != 0)
            {
                return false;
            }
        }
        return true;
    }

    /// <summary>
    /// 各種形態的磚塊  28個
    /// </summary>
    private int[][][] aCells = new int[][][]
        {
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,1},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,1,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,1},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,1,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,0,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,1,1},
                        new int[]{0,0,0,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,1,1},
                        new int[]{0,0,0,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{1,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,0,0},
                        new int[]{1,1,1,0},
                        new int[]{0,1,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{1,1,0,0},
                        new int[]{0,1,0,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,0},
                        new int[]{1,1,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,1,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,1,0},
                        new int[]{1,0,0,0},
                        new int[]{0,0,0,0},
                },

                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,1,1,0},
                        new int[]{0,1,0,0},
                        new int[]{0,1,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,1,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,0,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{0,0,1,0},
                        new int[]{0,0,1,0},
                        new int[]{0,1,1,0},
                },
                new int[][]
                {
                        new int[]{0,0,0,0},
                        new int[]{1,0,0,0},
                        new int[]{1,1,1,0},
                        new int[]{0,0,0,0},
                },
        };
}

歡迎加羣交流

1.unity遊戲開發羣
QQ羣
unity3d unity 遊戲開發

2.專門探討XLua的程序羣:437645698


下載地址

https://git.oschina.net/dingxiaowei/Aladdin_XLua.git
Github同步:https://github.com/dingxiaowei/Aladdin_XLua
關注後續更新請點start或者fork,感謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章