public void SetNeighbor(BlockData _anotherBlock, GamingLogic2048.EPushingDirection _eDirection) { int iThisToNeighbor = (int)_eDirection; //自己看鄰居的方向 int iNeighborToThis = (int)GamingLogic2048.GetOppositeDirection(_eDirection); //鄰居看自己的方向 this.m_arrNeighbor[iThisToNeighbor] = _anotherBlock; _anotherBlock.m_arrNeighbor[iNeighborToThis] = this; }
private void BuildBoard(int _iLengthX, int _iLengthY) //單純的把棋盤建出來,裡頭都先不要設定數字或障礙 { m_game = new GamingLogic2048(_iLengthX, _iLengthY, 0); int iBoardSize = m_game.GetBoardSize(); for (int i = 0; i < iBoardSize; i++) //為實現資源管理,迴圈需要兩個條件,一個是生成格子時是否生足夠了,另一個則是去找原本的格子是否太多了,需要關閉 { if (i >= m_listBlockView.Count) //超出了現有生成的格子範圍,表示需要額外生成 { Vector3 initPlace = Vector3.zero; initPlace.x += m_vec2BlockInterval.x * (i % _iLengthX); initPlace.y -= m_vec2BlockInterval.y * (i / _iLengthX); GameObject blockTemp = m_objPoolBlockPrefab.GetBlockPrefab(); blockTemp.transform.SetParent(m_transformInitPosition.transform); blockTemp.transform.localPosition = initPlace; BlockView BVTemp = blockTemp.GetComponent <BlockView>(); m_listBlockView.Add(BVTemp); } else if (i < iBoardSize && i < m_listBlockView.Count) //還需要格子,但原本所生成的格子還夠用時,將舊有的格子移動到我們需要的地方 { Vector3 initPlace = Vector3.zero; initPlace.x += m_vec2BlockInterval.x * (i % _iLengthX); initPlace.y -= m_vec2BlockInterval.y * (i / _iLengthX); m_listBlockView[i].transform.localPosition = initPlace; m_listBlockView[i].transform.gameObject.SetActive(true); } } //以下,在每次做完排格子迴圈後,將多出來的格子關閉,在C#當中若要將某個範圍的list刪除的話可以先請另一個暫存的list先幫你抓著,接著再請該list一一將物件轉入物件池 List <BlockView> listToRemove = m_listBlockView.GetRange(iBoardSize, m_listBlockView.Count - iBoardSize); m_listBlockView.RemoveRange(iBoardSize, m_listBlockView.Count - iBoardSize); foreach (BlockView blockView in listToRemove) { m_objPoolBlockPrefab.BackToBlockPool(blockView.gameObject); } this.m_gameObjGameOverView.SetActive(false); }