/// <summary>
    /// 按照给定的偏移量计算是否这个邻居是临近的状态,如果是就组在一起
    /// </summary>
    /// <param name="neighbour">The neighbour.</param>
    /// <param name="neighbourOffset">The neighbour offset.</param>
    /// <returns>临近返回true,否则false</returns>
    bool CheckNearNeighbour(GameObject neighbour, Vector2 neighbourOffset)
    {
        if (neighbour == null)
        {
            return(false);
        }
        PieceController neighbourController = neighbour.GetComponent <PieceController>();

        //处在同一个组内部,不需要再组了,也不需要移动位置,所以直接返回true
        if (isInGroup && neighbourController.groupController == groupController)
        {
            return(true);
        }
        //其他情况
        Vector2 pos = transform.position;
        //计算临近状态代表的一个rect,中心就是邻居的旁边,rect的大小是允许的误差值,在这个rect内就算是临近
        Rect    automoveRange = new Rect(0, 0, 2 * boardManager.maxAutoMoveDistance, 2 * boardManager.maxAutoMoveDistance);
        Vector2 neighbourPos  = neighbour.transform.position;

        automoveRange.center = neighbourPos + neighbourOffset;
        if (automoveRange.Contains(pos))
        {
            if (isInGroup == false)
            {
                //两个都不在组里
                if (neighbourController.isInGroup == false)
                {
                    transform.position = automoveRange.center;
                    //把两个组成一个新的group
                    GroupControler.CreatePieceGroup(this, neighbour.GetComponent <PieceController>());
                }
                //对方在组里
                else
                {
                    transform.position = automoveRange.center;
                    //把自己加入邻居的group
                    neighbourController.groupController.AddChild(this);
                }
            }
            else
            {
                Vector3 offset = new Vector3(automoveRange.center.x, automoveRange.center.y) - transform.position;
                //两个都在组里但是不在同一个组,将自己的组内成员全部转移到对方组内,同时位移,然后删除原来的组
                if (neighbourController.isInGroup == true)
                {
                    groupController.ChangeGroupThenSelfdestroy(neighbourController.groupController, offset);
                }
                //对方不在组里,将其加入自己的组内
                else
                {
                    groupController.MoveAllChild(offset);
                    groupController.AddChild(neighbourController);
                }
            }
            return(true);
        }
        return(false);
    }
Exemple #2
0
 /// <summary>
 /// Changes the group then selfdestroy.将自己的所有child转移到另一个group,进行位移,然后删除自己
 /// </summary>
 /// <param name="changeTo">The change to.</param>
 /// <param name="moveOffset">The move offset.</param>
 public void ChangeGroupThenSelfdestroy(GroupControler changeTo, Vector3 moveOffset)
 {
     for (int i = 0; i < child.Count; i++)
     {
         child[i].transform.position = child[i].transform.position + moveOffset;
         changeTo.AddChild(child[i]);
     }
     Destroy(gameObject);
 }
Exemple #3
0
    /// <summary>
    /// Creates the piece group.
    /// </summary>
    /// <param name="original">The original.</param>
    /// <param name="neighbour">The neighbour.</param>
    public static void CreatePieceGroup(PieceController original, PieceController neighbour)
    {
        //创建一个group对象作为父节点,其坐标为0
        GameObject groupPiece = new GameObject();

        groupPiece.transform.SetParent(original.transform.parent);
        groupPiece.transform.position = Vector3.zero;
        groupPiece.AddComponent <GroupControler>();
        GroupControler controller = groupPiece.GetComponent <GroupControler>();

        //将这两个对象的父节点都修改为这个
        original.transform.SetParent(groupPiece.transform);
        neighbour.transform.SetParent(groupPiece.transform);
        //修改这两个对象的标志位
        original.isInGroup        = true;
        neighbour.isInGroup       = true;
        original.groupController  = controller;
        neighbour.groupController = controller;
        //记录这两个对象
        controller.child.Add(original);
        controller.child.Add(neighbour);
    }