示例#1
0
    void Update()
    {
        if (ItCastHelper.IsWin)
        {
            return; //如果胜利,不需要再响应用户输入
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            #region 向上移动
            //对于第0行不需要移动,已经是最上行了
            for (int y = 1; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    person = PersonList[x, y];
                    if (person == null)
                    {
                        continue; //如果当前位置没有对象,则不需要进行任何操作
                    }
                    //如果当前位置有对象,则执行以下代码
                    //判断当前位置的前面所有位置是否有对象
                    int destPos = -1;
                    for (int y1 = y - 1; y1 >= 0; y1--)
                    {
                        personBefore = PersonList[x, y1];
                        if (personBefore != null)
                        {
                            if (person.IsEquals(personBefore)) //如果有,则不再向前找
                            {
                                destPos = -1;                  //如果是相同的对象则合并,不再需要移动当前对象
                                person.Abolition();
                                PersonList[x, y] = null;
                                personBefore.UpdateLevel(sprites.Length, sprites[personBefore.ID]);  //personBefore对象更新等级(ID)
                            }
                            break;
                        }
                        else
                        {
                            destPos = y1; //如果没有,则继续向前找
                        }
                    }
                    if (destPos > -1) //完成移动
                    {
                        //1、更新位置矩阵信息
                        PersonList[x, y]       = null;
                        PersonList[x, destPos] = person;
                        person.SetPosition(new Vector3(0, (y - destPos) * ItCastHelper.YOffset, 0)); //2、更改当前游戏对象的位置
                    }
                }
            }
            //新生成游戏对象
            CreatePerson();
            #endregion
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            #region 向下移动
            for (int y = 2; y >= 0; y--) //对于第3行不需要移动,已经是最下行了
            {
                for (int x = 0; x < 4; x++)
                {
                    person = PersonList[x, y];
                    if (person == null)
                    {
                        continue; //如果当前位置没有对象,则不需要进行任何操作
                    }
                    //如果当前位置有对象,则执行以下代码
                    //判断当前位置的前面所有位置是否有对象
                    int destPos = -1;
                    for (int y1 = y + 1; y1 < 4; y1++)
                    {
                        personBefore = PersonList[x, y1];
                        if (personBefore != null)
                        {
                            if (person.IsEquals(personBefore)) //如果有,则不再向前找
                            {
                                //如果是相同的对象则合并,不再需要移动当前对象
                                destPos = -1;
                                person.Abolition();
                                PersonList[x, y] = null;
                                personBefore.UpdateLevel(sprites.Length, sprites[personBefore.ID]);  //personBefore对象更新等级(ID)
                            }
                            break;
                        }
                        else
                        {
                            destPos = y1; //如果没有,则继续向前找
                        }
                    }
                    if (destPos > -1)                                                                //完成移动
                    {
                        PersonList[x, y]       = null;                                               //1、更新位置矩阵信息
                        PersonList[x, destPos] = person;
                        person.SetPosition(new Vector3(0, (y - destPos) * ItCastHelper.YOffset, 0)); //2、更改当前游戏对象的位置
                    }
                }
            }
            CreatePerson(); //新生成游戏对象
            #endregion
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            #region 向左移动
            for (int x = 1; x < 4; x++) //对于第0列不需要移动,已经是最左行了
            {
                for (int y = 0; y < 4; y++)
                {
                    person = PersonList[x, y];
                    if (person == null)
                    {
                        continue; //如果当前位置没有对象,则不需要进行任何操作
                    }
                    //如果当前位置有对象,则执行以下代码
                    //判断当前位置的前面所有位置是否有对象
                    int destPos = -1;
                    for (int x1 = x - 1; x1 >= 0; x1--)
                    {
                        personBefore = PersonList[x1, y];
                        if (personBefore != null)
                        {
                            if (person.IsEquals(personBefore))                                      //如果有,则不再向前找
                            {
                                destPos = -1;                                                       //如果是相同的对象则合并,不再需要移动当前对象
                                person.Abolition();                                                 //消毁
                                PersonList[x, y] = null;
                                personBefore.UpdateLevel(sprites.Length, sprites[personBefore.ID]); //personBefore对象更新等级(ID)
                            }
                            break;
                        }
                        else
                        {
                            //如果没有,则继续向前找
                            destPos = x1;
                        }
                    }
                    if (destPos > -1)                                                                //完成移动
                    {
                        PersonList[x, y]       = null;                                               //1、更新位置矩阵信息
                        PersonList[destPos, y] = person;
                        person.SetPosition(new Vector3((x - destPos) * ItCastHelper.XOffset, 0, 0)); //2、更改当前游戏对象的位置
                    }
                }
            }
            CreatePerson(); //新生成游戏对象
            #endregion
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            #region 向右移动
            for (int x = 2; x >= 0; x--) //对于第4列不需要移动,已经是最右行了
            {
                for (int y = 0; y < 4; y++)
                {
                    person = PersonList[x, y];
                    if (person == null)
                    {
                        continue; //如果当前位置没有对象,则不需要进行任何操作
                    }
                    //如果当前位置有对象,则执行以下代码
                    //判断当前位置的前面所有位置是否有对象
                    int destPos = -1;
                    for (int x1 = x + 1; x1 < 4; x1++)
                    {
                        personBefore = PersonList[x1, y];
                        if (personBefore != null)
                        {
                            if (person.IsEquals(personBefore)) //如果有,则不再向前找
                            {
                                destPos = -1;                  //如果是相同的对象则合并,不再需要移动当前对象
                                person.Abolition();
                                PersonList[x, y] = null;
                                personBefore.UpdateLevel(sprites.Length, sprites[personBefore.ID]);  //personBefore对象更新等级(ID)
                            }
                            break;
                        }
                        else
                        {
                            destPos = x1; // 如果没有,则继续向前找
                        }
                    }
                    if (destPos > -1)                  //完成移动
                    {
                        PersonList[x, y]       = null; //1、更新位置矩阵信息
                        PersonList[destPos, y] = person;
                        Vector3 pos = new Vector3((x - destPos) * ItCastHelper.XOffset, 0, 0);
                        person.SetPosition(pos); //2、更改当前游戏对象的位置
                    }
                }
            }
            CreatePerson(); //新生成游戏对象
            #endregion
        }
    }