Esempio n. 1
0
    public void UpdateCollider()
    {
        for (int i = 0; i < colliderObjs.Count; i++)
        {
            GameObject.DestroyImmediate(colliderObjs[i]);
        }
        colliderObjs.Clear();

        int index = layers.IndexOf(MapEditorSortLayer.Floor1);
        Dictionary <MapEditorItemType, List <int> > itemType = new Dictionary <MapEditorItemType, List <int> >();
        int count = layerItems[index].items.Count;

        for (int i = 0; i < count; i++)
        {
            MapEditorItemType mapEditorItemType = layerItems[index].items[i].itemType;
            if (mapEditorItemType == MapEditorItemType.None || mapEditorItemType == MapEditorItemType.Deco)
            {
                continue;
            }
            mapEditorItemType = mapEditorItemType == MapEditorItemType.StoneWall ? MapEditorItemType.Wall : mapEditorItemType;
            if (itemType.ContainsKey(mapEditorItemType))
            {
                itemType[mapEditorItemType].Add(layerItems[index].posList[i]);
            }
            else
            {
                itemType.Add(mapEditorItemType, new List <int>());
                itemType[mapEditorItemType].Add(layerItems[index].posList[i]);
            }
        }

        Dictionary <MapEditorItemType, List <List <int> > > colliders = new Dictionary <MapEditorItemType, List <List <int> > >();

        foreach (var items in itemType)
        {
            List <int> allPos = items.Value;
            while (allPos.Count > 0)
            {
                List <int> list = new List <int>();
                list.Add(allPos[0]);
                allPos.RemoveAt(0);
                List <int> close = new List <int>();
                while (list.Count > 0)
                {
                    int x    = list[0] / 10000;
                    int y    = list[0] % 10000;
                    int pos1 = (x - 1) * 10000 + y;
                    if (allPos.IndexOf(pos1) != -1)
                    {
                        list.Add(pos1);
                        allPos.Remove(pos1);
                    }
                    pos1 = (x + 1) * 10000 + y;
                    if (allPos.IndexOf(pos1) != -1)
                    {
                        list.Add(pos1);
                        allPos.Remove(pos1);
                    }
                    pos1 = x * 10000 + (y - 1);
                    if (allPos.IndexOf(pos1) != -1)
                    {
                        list.Add(pos1);
                        allPos.Remove(pos1);
                    }
                    pos1 = x * 10000 + (y + 1);
                    if (allPos.IndexOf(pos1) != -1)
                    {
                        list.Add(pos1);
                        allPos.Remove(pos1);
                    }
                    if (close.IndexOf(list[0]) == -1)
                    {
                        close.Add(list[0]);
                    }
                    list.RemoveAt(0);
                }
                if (colliders.ContainsKey(items.Key))
                {
                    colliders[items.Key].Add(close);
                }
                else
                {
                    colliders.Add(items.Key, new List <List <int> >());
                    colliders[items.Key].Add(close);
                }
            }
        }

        foreach (var collider in colliders)
        {
            List <List <int> > posList = collider.Value;
            int c = posList.Count;
            for (int i = 0; i < c; i++)
            {
                List <Vector3>        vertices = new List <Vector3>();
                List <Vector2[]>      polygons = new List <Vector2[]>();
                List <Collider2DEdge> edges    = new List <Collider2DEdge>();
                int cc = posList[i].Count;
                for (int j = 0; j < cc; j++)
                {
                    int x = posList[i][j] / 10000;
                    int y = posList[i][j] % 10000;

                    count = vertices.Count / 4;
                    vertices.AddRange(new Vector3[4]);
                    vertices[count * 4]     = new Vector3(x, y);
                    vertices[count * 4 + 1] = new Vector3(x + 1, y);
                    vertices[count * 4 + 2] = new Vector3(x + 1, y + 1);
                    vertices[count * 4 + 3] = new Vector3(x, y + 1);

                    if (posList[i].IndexOf(x * 10000 + (y + 1)) == -1)
                    {
                        AddEdge(edges, vertices[count * 4 + 2], vertices[count * 4 + 3]);
                    }

                    if (posList[i].IndexOf(x * 10000 + (y - 1)) == -1)
                    {
                        AddEdge(edges, vertices[count * 4], vertices[count * 4 + 1]);
                    }

                    if (posList[i].IndexOf((x - 1) * 10000 + y) == -1)
                    {
                        AddEdge(edges, vertices[count * 4 + 3], vertices[count * 4]);
                    }

                    if (posList[i].IndexOf((x + 1) * 10000 + y) == -1)
                    {
                        AddEdge(edges, vertices[count * 4 + 1], vertices[count * 4 + 2]);
                    }
                }

                GameObject        go = new GameObject();
                PolygonCollider2D polygonCollider2D = go.AddComponent <PolygonCollider2D>();

                go.layer = LayerUtil.LayerToObstacle();

                colliderObjs.Add(go);
                PolygoniseEdges(polygons, edges);
                polygonCollider2D.pathCount = polygons.Count;
                for (int n = 0; n < polygons.Count; n++)
                {
                    polygonCollider2D.SetPath(n, polygons[n]);
                }
            }
        }
    }