Beispiel #1
0
    public List <ColliderGroup> SetUpAllGroups(float max_size) //設定所有group
    {
        colliderGroups.Clear();
        List <ColliderGroup> groups     = new List <ColliderGroup>();
        List <ColliderGroup> groups_tmp = new List <ColliderGroup>();

        //TODO:分群演算法
        MyCollider[] allColliders = FindObjectsOfType <MyCollider>();
        //一開始所有物件都自己一組
        foreach (MyCollider c in allColliders)
        {
            ColliderGroup tmp = new ColliderGroup();
            tmp.colliders.Add(c);
            tmp.UpdateCenter();
            c.belongGroup = tmp;
            groups_tmp.Add(tmp);
        }

        //===開始合併===
        groups.Add(groups_tmp[0]);
        groups_tmp.RemoveAt(0);
        for (int i = 0; i < groups.Count; i++)
        {
            for (int j = 0; j < groups_tmp.Count; j++)
            {
                if (groups[i].colliders.Exists(a => a == groups_tmp[j].colliders[0]))
                {
                    continue;
                }
                if (CheckIsInsideBoundsCircle(groups[i].center, groups_tmp[j].colliders[0].spr.bounds.extents, groups_tmp[j].colliders[0]))
                {
                    groups[i] = MergeGroup(groups[i], groups_tmp[j].colliders[0]);
                    groups[i].UpdateCenter();
                }
                else if (i == 0)
                {
                    groups.Add(groups_tmp[j]);
                }
            }
        }

        groups.Distinct();

        return(groups);
    }
Beispiel #2
0
    ///<summary>若from組轉移後剩0個collider會自動刪掉</summary>
    public void TransferGroup(ColliderGroup from, ColliderGroup to, MyCollider collider)
    {
        if (!colliderGroups.Contains(to))
        {
            colliderGroups.Add(to);
        }
        if (to.colliders.Contains(collider))
        {
            return;
        }

        //若轉移目標group太多物件了 就踢掉一個叫它自創
        if (to.colliders.Count > group_max_colliders)
        {
            ColliderGroup newto = new ColliderGroup();
            TransferGroup(to, newto, to.colliders[0]);

            return;
        }

        //轉移
        from.colliders.Remove(collider);
        collider.belongGroup = to;
        to.colliders.Add(collider);


        to.UpdateCenter();
        if (from.colliders.Count < 1)
        {
            colliderGroups.Remove(from);
        }
        else
        {
            from.UpdateCenter();
        }
    }