Exemple #1
0
    public void SafeLocatePlaces(List <ShipmentNode> nodes, List <ShipmentEdge> edges)
    {
        iterations = 0;
        ResetIds();
        float           edge          = Places[0].GetComponent <RectTransform>().sizeDelta.x / 2;
        float           distanceMin   = GetMinDistance();
        Vector2         mapSize       = Map.GetComponent <RectTransform>().sizeDelta / 2;
        float           xMax          = (mapSize.x - edge) * 0.95f;
        float           yMax          = (mapSize.y - edge) * 0.95f;
        List <MapPlace> locatedPlaces = new List <MapPlace>(Places.Count);

        Utils.Shuffle(Places);
        nodes.Sort((node1, node2) => node1.Type == ShipmentNodeType.Start ? -1 : node2.Type == ShipmentNodeType.Start ? 1 : ShipmentsView.instance.Model.GetEdgesByIdNode(node2.Id).Count - ShipmentsView.instance.Model.GetEdgesByIdNode(node1.Id).Count);
        SetFirstPlace(nodes[0], xMax, yMax);
        locatedPlaces.Add(Places[0]);
        Places[0].gameObject.SetActive(true);
        int i = 1;

        for (; i < nodes.Count; i++)
        {
            MapPlace mapPlace = Places[i];
            mapPlace.gameObject.SetActive(true);
            ShipmentNode node = nodes[i];
            mapPlace.SetData(node.Id, PlacesSprites[node.Id], CrosSprites[node.Type == ShipmentNodeType.Other ? 1 : 0], node.Type);
            if (iterations >= 100)
            {
                iterations = 0;

                locatedPlaces.Clear();
                foreach (ShipmentEdge a in edges)
                {
                    a.Length = 0;
                }
                SafeLocatePlaces(nodes, edges);
                return;
            }
            SafeLocatePlace(mapPlace, locatedPlaces, ShipmentsView.instance.Model.GetEdgesByIdNode(node.Id), xMax, yMax, distanceMin);
        }

        // Oculto los places no usados
        for (int j = Places.Count - 1; j >= i; j--)
        {
            Places[j].gameObject.SetActive(false);
        }

        foreach (ShipmentEdge shipmentEdge in edges)
        {
            if (shipmentEdge.Length == 0)
            {
                MapPlace place    = Places.Find(e => e.Id == shipmentEdge.IdNodeA);
                MapPlace mapPlace = Places.Find(e => e.Id == shipmentEdge.IdNodeB);
                float    distance = Vector2.Distance(place.transform.position, mapPlace.transform.position) /
                                    Ruler.GetUnityDistances();
                shipmentEdge.Length = (int)distance;
            }
        }
    }