Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (_needRestart)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                InitialMap();
                _currentShape = null;
                _needRestart  = false;
            }
            else
            {
                return;
            }
        }

        if (_currentShape == null)
        {
            _currentDownList.Clear();
            _currentShape       = _nextOnesList[0];
            _currentDownColor   = _currentShape.ShapeColor;
            _currentDownList    = _currentShape.GetMapDownSquareListByOrigin(_currentShape.StartOffSet);
            _currentShapeOrigin = _currentShape.StartOffSet;

            //产生一个新的图形来补充到接下来的图形列表当中
            _nextOnesList.RemoveAt(0);
            BaseShape bs = ShapeManager.Instance.GetNextShape();
            bs.ResetStartShapeAndColor();
            _nextOnesList.Add(bs);

            //刷新接下来图形的显示界面
            //将所有的接下来形状显示区域刷新为黑色
            for (int i = 0; i < _nextOnesSquareList.Count; i++)
            {
                _nextOnesSquareList[i].GetComponent <Renderer>().material.color = Color.black;
            }
            //将每一个接下来图形显示出来
            for (int index = 0; index < 4; index++)
            {
                List <Vector2> shapeList = _nextOnesList[index].GetMapDownSquareListByOrigin(Vector2.zero);
                for (int i = 0; i < shapeList.Count; i++)
                {
                    GetNextSquareByIndex(index, (int)shapeList[i].x, (int)shapeList[i].y).GetComponent <Renderer>().material.color = _nextOnesList[index].ShapeColor;
                }
            }


            for (int i = 0; i < _currentDownList.Count; i++)
            {
                if ((int)_currentDownList[i].x >= 0)
                {
                    if (CheckValidByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y))
                    {
                        GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).SetActive(true);
                        GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).GetComponent <Renderer>().material.color = _currentDownColor;
                    }
                    else
                    {
                        Debug.Log("Game Over");
                        UIManager.Instance.GameMessageText.text = "Game Over";
                        _needRestart = true;
                        return;
                    }
                }
            }

            _fallDownTimeAccumulation = 0f;
        }
        else
        {
            _fallDownTimeAccumulation += Time.deltaTime;
            if (_fallDownTimeAccumulation > _fallDownTimeLimit)
            {
                _fallDownTimeAccumulation -= _fallDownTimeLimit;
                //检查碰撞
                if (!CheckDownMove())
                {
                    //将已落下的方块稳定下来,重新生成另外一个方块
                    for (int i = 0; i < _currentDownList.Count; i++)
                    {
                        if ((int)_currentDownList [i].x >= 0)
                        {
                            _stableMatrix [(int)_currentDownList [i].x, (int)_currentDownList [i].y] = true;
                        }
                    }

                    //重置参数
                    _fallDownTimeAccumulation = 0f;
                    _currentShape             = null;

                    //消除填满的格子
                    //检查已经填满的行
                    _fullRowIndexList.Clear();
                    for (int i = MAP_ROW_COUNT - 1; i >= 0; i--)
                    {
                        bool isFull = true;
                        for (int j = 0; j < MAP_COL_COUNT; j++)
                        {
                            if (!_stableMatrix[i, j])
                            {
                                isFull = false;
                                break;
                            }
                        }
                        if (isFull)
                        {
                            //消除满行
                            for (int j = 0; j < MAP_COL_COUNT; j++)
                            {
                                _stableMatrix [i, j] = false;
//                                Sequence sequence = DOTween.Sequence();
//                                sequence.AppendCallback(()=>{
//                                    GetSquareByIndex (i, j).transform.DOScale(0, 1.3f);
//                                });
//                                sequence.AppendInterval(1.3f);
//                                sequence.AppendCallback(()=>{
//                                    GetSquareByIndex (i, j).transform.localScale = Vector3.one;
//                                });
//                                sequence.Play();
                                GetSquareByIndex(i, j).SetActive(false);
                            }
                            _fullRowIndexList.Add(i);
                        }
                    }

                    //如果有满行,执行清除动作
                    if (_fullRowIndexList.Count > 0)
                    {
                        //找到当前棋盘的山顶
                        int summit;
                        for (summit = 0; summit < MAP_ROW_COUNT; summit++)
                        {
                            bool isSummit = false;
                            for (int j = 0; j < MAP_COL_COUNT; j++)
                            {
                                if (_stableMatrix [summit, j])
                                {
                                    isSummit = true;
                                    break;
                                }
                            }
                            if (isSummit)
                            {
                                break;
                            }
                        }

                        int fullRowIndex = 0;
                        for (int row = MAP_ROW_COUNT - 1; row >= summit; row--)
                        {
                            while (fullRowIndex < _fullRowIndexList.Count && row == _fullRowIndexList[fullRowIndex])
                            {
                                row--;
                                fullRowIndex++;
                            }
                            if (fullRowIndex > 0)
                            {
                                for (int col = 0; col < MAP_COL_COUNT; col++)
                                {
                                    _stableMatrix[row + fullRowIndex, col] = _stableMatrix[row, col];
                                    GetSquareByIndex(row + fullRowIndex, col).SetActive(_stableMatrix [row + fullRowIndex, col]);
                                    if (_stableMatrix[row, col])
                                    {
                                        GetSquareByIndex(row + fullRowIndex, col).GetComponent <Renderer>().material.color = GetSquareByIndex(row, col).GetComponent <Renderer>().material.color;
                                    }
                                    //将当前行清空
                                    _stableMatrix[row, col] = false;
                                    GetSquareByIndex(row, col).SetActive(false);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < _currentDownList.Count; i++)
                    {
                        //隐藏之前的位置
                        if ((int)_currentDownList[i].x >= 0)
                        {
                            GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).SetActive(false);
                        }
                        //下落一格子
                        _currentDownList [i] = new Vector2(_currentDownList[i].x + 1, _currentDownList[i].y);
                    }
                    _currentShapeOrigin += new Vector2(1, 0);

                    //重新显示下降的方块
                    for (int i = 0; i < _currentDownList.Count; i++)
                    {
                        if ((int)_currentDownList[i].x >= 0)
                        {
                            GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).SetActive(true);
                            GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).GetComponent <Renderer>().material.color = _currentDownColor;
                        }
                    }
                }
            }
        }

        //玩家输入
        //向左移动
        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
        {
            MoveLeft();
            _leftTimeAccumulation = 0f;
        }
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            _leftTimeAccumulation += Time.deltaTime;
            if (_leftTimeAccumulation > _horizonTimeLimit)
            {
                _leftTimeAccumulation -= _horizonTimeLimit;
                MoveLeft();
            }
        }

        //向右移动
        if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
        {
            MoveRight();
            _rightTimeAccumulation = 0f;
        }
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            _rightTimeAccumulation += Time.deltaTime;
            if (_rightTimeAccumulation > _horizonTimeLimit)
            {
                _rightTimeAccumulation -= _horizonTimeLimit;
                MoveRight();
            }
        }

        //加速下降
        if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            //找到最后的位置,直接落下
            int downCount = 0;
            while (CheckDownMove(downCount))
            {
                downCount++;
            }
            downCount--;

            if (downCount > 0)
            {
                for (int i = 0; i < _currentDownList.Count; i++)
                {
                    if ((int)_currentDownList [i].x >= 0)
                    {
                        GetSquareByIndex((int)_currentDownList [i].x, (int)_currentDownList [i].y).SetActive(false);
                    }
                    _currentDownList [i] = new Vector2(_currentDownList [i].x + downCount, _currentDownList [i].y);
                }
                //重新显示方块
                for (int i = 0; i < _currentDownList.Count; i++)
                {
                    if ((int)_currentDownList[i].x >= 0)
                    {
                        GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).SetActive(true);
                        GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).GetComponent <Renderer>().material.color = _currentDownColor;
                    }
                }
                _currentShapeOrigin      += new Vector2(downCount, 0);
                _fallDownTimeAccumulation = 0f;
            }
        }

        //变形
        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            //按照一定形状将下落的方块变形,如果不能变形,则没有效果
//            _currentShape.GetSwiftShapeMatrix();
            List <Vector2> tempList     = _currentShape.GetSwiftDownSquareListByOrigin(_currentShapeOrigin);
            bool           isValidSwift = true;
            for (int i = 0; i < tempList.Count; i++)
            {
                if (!CheckValidByIndex((int)tempList[i].x, (int)tempList[i].y))
                {
                    isValidSwift = false;
                    break;
                }
            }
            if (isValidSwift)
            {
                //隐藏原来的
                for (int i = 0; i < _currentDownList.Count; i++)
                {
                    if ((int)_currentDownList [i].x >= 0)
                    {
                        GetSquareByIndex((int)_currentDownList [i].x, (int)_currentDownList [i].y).SetActive(false);
                    }
                }

                _currentShape.Swift();
                _currentDownList = _currentShape.GetMapDownSquareListByOrigin(_currentShapeOrigin);
                //显示变形之后的下落方块
                for (int i = 0; i < _currentDownList.Count; i++)
                {
                    if ((int)_currentDownList [i].x >= 0)
                    {
                        GetSquareByIndex((int)_currentDownList [i].x, (int)_currentDownList [i].y).SetActive(true);
                        GetSquareByIndex((int)_currentDownList[i].x, (int)_currentDownList[i].y).GetComponent <Renderer>().material.color = _currentDownColor;
                    }
                }
            }
        }
    }