public void Init(int level, string id, GameObject edge) { myLevel = level; ID = id; Edge = edge; billboard = gameObject.GetComponent <Billboard>(); billboard.enabled = false; if (CovidNetworkManager.Node_MetaInfo_Dictionary.ContainsKey(ID)) { metaInfo = CovidNetworkManager.Node_MetaInfo_Dictionary[ID]; } else { metaInfo = new NodeMetaInfo(); } //Assigning Countryname as label if (metaInfo != null) { Date = metaInfo.Collection_Data; if (Date != "" && Date != null) { MyCollectionDate = DateTime.Parse(Date); } if (metaInfo.Country == null || metaInfo.Country == "") { Country.text = "Unknown"; } else { Country.text = metaInfo.Country; //Assigning Colors based on Countries Debug.Log("CountryInfo : " + metaInfo.Country); if (CovidNetworkManager.CountryAndCounts.ContainsKey(metaInfo.Country)) { NetworkCountries countryInfo = CovidNetworkManager.CountryAndCounts[metaInfo.Country]; Material mat = countryInfo.mat; Renderer rand = NodeRep.GetComponent <Renderer>(); mat.mainTexture = rand.material.mainTexture; rand.material = mat; //myColor = countryInfo.Color; if (Edge != null) { LineRenderer lr = Edge.GetComponent <LineRenderer>(); lr.SetColors(countryInfo.Color, countryInfo.Color); initWidth = lr.startWidth; myColor = lr.startColor; Edge.GetComponent <GraphLineTextureAnimation>().enabled = false; } } } } Country.gameObject.SetActive(false); }
public Path AStar(Vector2 pos, Vector2 target, bool debug) { try { int maxSize = gridManager.gridSize * gridManager.gridSize; //int maxSize = (int)((target-pos).sqrMagnitude / 2); List <NodeMetaInfo> toExplore = new List <NodeMetaInfo>(maxSize); // might be a good candidate for pooling if we have constant requests Dictionary <Vector2, NodeMetaInfo> nodeMetas = new Dictionary <Vector2, NodeMetaInfo>(maxSize); // need to track costFromStart separatelly, since we're recreating nodes on the fly // we're looking for a path in grid space, so have to convert it Vector2 targetInd = gridManager.GetIndices(target); Node targetNode = new Node(); targetNode.loc = targetInd; { Node startNode = new Node(); startNode.loc = gridManager.GetIndices(pos); NodeMetaInfo metaInfo = new NodeMetaInfo(); metaInfo.costFromStart = 0; metaInfo.estCostToEnd = EstimatedCostToTarget(startNode, targetNode); metaInfo.totalCost = metaInfo.costFromStart + metaInfo.estCostToEnd; metaInfo.parent = null; metaInfo.node = startNode; toExplore.Add(metaInfo); nodeMetas[startNode.loc] = metaInfo; } Path path = null; Rect bounds = new Rect(Vector2.zero, Vector2.one * gridManager.gridSize); while (toExplore.Count > 0) { NodeMetaInfo nodeMeta = toExplore[0]; toExplore.RemoveAt(0); Node node = nodeMeta.node; if (node.loc == targetInd) { // we found our path - need to unroll it path = new Path(); path.hasDebug = debug; while (node != null) { path.Add(node.loc); node = nodeMetas[node.loc].parent; } path.Reverse(); path.totalVisited = nodeMetas.Count; if (debug) { path.allVisited = new List <NodeMetaInfo>(); foreach (NodeMetaInfo info in nodeMetas.Values) { path.allVisited.Add(info); } } break; } else { // probably not the best way to do it, but don't see a better way right now for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x == 0 && y == 0) // avoid creating the same node { continue; } Vector2 newPos = node.loc + new Vector2(x, y); if (!bounds.Contains(newPos)) // avoid out of bounds cases { continue; } Node newNode = new Node(); newNode.loc = newPos; NodeMetaInfo newNodeMeta; // checking to see whether the new cost that we calculated for this is better float newCost = nodeMeta.costFromStart + CostOfSingleStep(node, newNode); bool inSorted = false; float oldTotalCost = 0; if (nodeMetas.TryGetValue(newNode.loc, out newNodeMeta)) { // we visited this node before - means we have calculated the cost before once // check whether it's faster to get to new node from current position, rather than old if (newCost < newNodeMeta.costFromStart) { // do early search (relies on totalCost internally for lookup) inSorted = toExplore.ContainsSorted(newNodeMeta); oldTotalCost = newNodeMeta.totalCost; // the node already has an estimate to target calculated, and current node set // meaning we need to update only part of the info newNodeMeta.parent = node; newNodeMeta.costFromStart = newCost; newNodeMeta.totalCost = newNodeMeta.costFromStart + newNodeMeta.estCostToEnd; if (inSorted) { toExplore.UpdateInSorted(newNodeMeta, oldTotalCost); } else { toExplore.AddSorted(newNodeMeta); } } } else { newNodeMeta = new NodeMetaInfo(); newNodeMeta.costFromStart = newCost; nodeMetas[newNode.loc] = newNodeMeta; // this node is explored for the first time // either way, update it's information newNodeMeta.node = newNode; newNodeMeta.parent = node; newNodeMeta.estCostToEnd = EstimatedCostToTarget(newNode, targetNode); newNodeMeta.totalCost = newNodeMeta.costFromStart + newNodeMeta.estCostToEnd; toExplore.AddSorted(newNodeMeta); } } } } } return(path); } catch (System.Exception e) { Dispatcher.Instance.Add(() => { Debug.LogError(e.ToString()); }); return(null); } }