public override IEnumerator Update(bool playAnimation, Action onComplete = null)
    {
        m_isGenerating = true;

        m_cellList = new List <BaseCell> ();

        if (playAnimation)
        {
            yield return(null);
        }

        m_cellList.Add(new BaseCell(0, 0, m_mazeGenerator));

        BaseCell  cellCache      = null;
        Direction directionCache = Direction.None;

        while (m_cellList.Count != 0)
        {
            cellCache      = m_cellList[m_cellList.Count - 1];
            directionCache = cellCache.GetRandomDirection();

            if (directionCache != Direction.None)
            {
                if (playAnimation)
                {
                    yield return(null);
                }

                m_cellList.Add(new BaseCell(cellCache, directionCache, m_mazeGenerator));
            }
            else
            {
                m_cellList.RemoveAt(m_cellList.Count - 1);

                if (m_cellList.Count >= 2)
                {
                    BaseCell firstCell = m_cellList[0];
                    m_cellList.RemoveAt(0);
                    m_cellList.Add(firstCell);
                }
            }
        }

        m_isGenerating = false;

        if (null != onComplete)
        {
            onComplete();
        }
    }
예제 #2
0
    public override IEnumerator Update(bool playAnimation, Action onComplete = null)
    {
        m_isGenerating = true;

        m_cellList = new List <BaseCell> ();

        if (playAnimation)
        {
            yield return(null);
        }

        m_cellList.Add(new BaseCell(0, 0, m_mazeGenerator));

        BaseCell  cellCache      = null;
        Direction directionCache = Direction.None;
        int       randomCache    = 0;

        while (m_cellList.Count != 0)
        {
            randomCache    = UnityEngine.Random.Range(0, m_cellList.Count);
            cellCache      = m_cellList[randomCache];
            directionCache = cellCache.GetRandomDirection();

            if (directionCache != Direction.None)
            {
                if (playAnimation)
                {
                    yield return(null);
                }

                m_cellList.Add(new BaseCell(cellCache, directionCache, m_mazeGenerator));
            }
            else
            {
                m_cellList.RemoveAt(randomCache);
            }
        }

        m_isGenerating = false;

        if (null != onComplete)
        {
            onComplete();
        }
    }
    public override IEnumerator Update(bool playAnimation, Action onComplete = null)
    {
        m_isGenerating = true;

        m_cellStack = new Stack <BaseCell> ();

        if (playAnimation)
        {
            yield return(null);
        }

        m_cellStack.Push(new BaseCell(0, 0, m_mazeGenerator));

        BaseCell  cellCache      = null;
        Direction directionCache = Direction.None;

        while (m_cellStack.Count != 0)
        {
            cellCache      = m_cellStack.Peek();
            directionCache = cellCache.GetRandomDirection();

            if (directionCache != Direction.None)
            {
                if (playAnimation)
                {
                    yield return(null);
                }

                m_cellStack.Push(new BaseCell(cellCache, directionCache, m_mazeGenerator));
            }
            else
            {
                m_cellStack.Pop();
            }
        }

        m_isGenerating = false;

        if (null != onComplete)
        {
            onComplete();
        }
    }
    //創建資料主程式
    public override void Update()
    {
        //將模式設為創建中
        m_isGenerating = true;

        m_cellStack = new Stack <BaseCell>();

        //將新創建物件加入至堆疊 並呼叫最初始的 BaseCell
        m_cellStack.Push(new BaseCell(0, 0, m_mazeGenerator));

        BaseCell cellCache      = null;
        int      directionCache = 0;

        //執行到迷宮創建完成
        while (m_cellStack.Count != 0)
        {
            //取得堆疊最上面的 BaseCell
            cellCache = m_cellStack.Peek();

            //取得 cellCache 的隨機牆壁方向
            directionCache = cellCache.GetRandomDirection();

            //判斷 cellCache 的牆壁所有方向是不是不空的
            if (directionCache != 0)
            {
                m_cellStack.Push(new BaseCell(cellCache, directionCache, m_mazeGenerator));
            }
            else
            {
                m_cellStack.Pop();
            }
        }

        //迷宮創建結束
        m_isGenerating = false;

        //Debug
        //m_mazeGenerator.maze.TestMaze();
    }