int GetDistance(PhysicalNode node, PhysicalNode otherNode) { int xDistance = Mathf.Abs(node.onGridX - otherNode.onGridX); int yDistance = Mathf.Abs(node.onGridY - otherNode.onGridY); if (xDistance > yDistance) { return(14 * yDistance + 10 * (xDistance - yDistance)); } return(14 * xDistance + 10 * (yDistance - xDistance)); }
void RetracePath(PhysicalNode startNode, PhysicalNode endNode) { List <PhysicalNode> path = new List <PhysicalNode>(); PhysicalNode currentNode = endNode; while (currentNode != startNode) { path.Add(currentNode); currentNode = currentNode.parent; } path.Reverse(); grid.path = path; }
void GenerateGrid() { grid = new PhysicalNode[localSize.x, localSize.y]; for (int y = 0; y < localSize.y; y++) { for (int x = 0; x < localSize.x; x++) { Vector3 onGridPos = initPointPos + (Vector3.right * (x * nodeDiameter + nodeRadius)) + (Vector3.forward * (y * nodeDiameter + nodeRadius)); bool walkable = !(Physics.CheckSphere(onGridPos, nodeRadius, unwalkableMask)); grid[x, y] = new PhysicalNode(onGridPos, walkable, x, y); } } }
void OnDrawGizmos() { //Grid Gizmos.color = Color.white; Gizmos.DrawWireCube(transform.position, new Vector3(worldSize.x, 0.5f, worldSize.y)); //InitCorner Gizmos.color = Color.green; Gizmos.DrawWireSphere(initPointPos, 0.2f); //Nodes if (displayGrid == true) { if (grid != null) { PhysicalNode playerNode = GetNode(player.position); foreach (PhysicalNode n in grid) { if (playerNode == n) { Gizmos.color = Color.cyan; Gizmos.DrawCube(n.worldPos, new Vector3(nodeDiameter - 0.1f, 0.1f, nodeDiameter - 0.1f)); } else { Gizmos.color = (n.walkable) ? Color.white : Color.red; if (path != null) { if (path.Contains(n)) { Gizmos.color = Color.black; } } Gizmos.DrawCube(n.worldPos, new Vector3(nodeDiameter - 0.1f, 0.1f, nodeDiameter - 0.1f)); } } } } }
public List <PhysicalNode> GetNodeNeighbours(PhysicalNode node) { List <PhysicalNode> neighbours = new List <PhysicalNode>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; } int auxX = node.onGridX + x; int auxY = node.onGridY + y; if (auxX >= 0 && auxX < localSize.x && auxY >= 0 && auxY < localSize.y) { neighbours.Add(grid[auxX, auxY]); } } } return(neighbours); }
void FindPath(Vector3 startPos, Vector3 endPos) { PhysicalNode startNode = grid.GetNode(startPos); PhysicalNode endNode = grid.GetNode(endPos); List <PhysicalNode> openSet = new List <PhysicalNode>(); HashSet <PhysicalNode> closedSet = new HashSet <PhysicalNode>(); openSet.Add(startNode); //current = node in OPEN with the lowest f_cost while (openSet.Count > 0) { PhysicalNode currentNode = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost) { if (openSet[i].hCost < currentNode.hCost) { currentNode = openSet[i]; } } } // remove current from OPEN openSet.Remove(currentNode); // add current to CLOSED closedSet.Add(currentNode); // if current is the target node //path has been found return if (currentNode == endNode) { RetracePath(startNode, endNode); return; } foreach (PhysicalNode neighbour in grid.GetNodeNeighbours(currentNode)) { // if neighbour is not traversable or neighbour is in CLOSED if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newPathToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); // if new path to neighbour is shorter OR neighbour is not in OPEN if (newPathToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newPathToNeighbour; neighbour.hCost = GetDistance(neighbour, endNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }
// GET: api/Physical public IEnumerable <PhysicalNode> GetNode() { IEnumerable <CCTVServerInfo> servers = ServerPersistence.Instance.GetAllInfos(); IEnumerable <CCTVStaticInfo> videos = StaticPersistence.Instance.GetAllInfos(); IEnumerable <CCTVDeviceInfo> devices = DevicePersistence.Instance.GetAllInfos(); List <PhysicalNode> pServers = new List <PhysicalNode>(); List <PhysicalNode> aloneNodes = new List <PhysicalNode>(); Dictionary <string, List <PhysicalNode> > dServerChildren = new Dictionary <string, List <PhysicalNode> >(); Dictionary <string, CCTVDeviceInfo> dDevices = new Dictionary <string, CCTVDeviceInfo>(); foreach (CCTVServerInfo si in servers) { dServerChildren[si.ServerId] = new List <PhysicalNode>(); pServers.Add(new PhysicalNode() { Id = si.ServerId, Name = si.Name, Type = NodeType.Server }); } foreach (CCTVDeviceInfo di in devices) { dDevices[di.VideoId] = di; } foreach (CCTVStaticInfo si in videos) { var pn = new PhysicalNode() { Id = si.VideoId, Name = si.Name, Type = NodeType.Video }; if (dDevices.ContainsKey(si.VideoId) && dServerChildren.ContainsKey(dDevices[si.VideoId].PreferredServerId)) { dServerChildren[dDevices[si.VideoId].PreferredServerId].Add(pn); } else { aloneNodes.Add(pn); } } foreach (PhysicalNode pn in pServers) { pn.Children = dServerChildren[pn.Id].ToArray(); } List <PhysicalNode> allNodes = new List <PhysicalNode>(); if (aloneNodes.Count > 0) { allNodes.Add(new PhysicalNode() { Id = Guid.NewGuid().ToString(), Name = "未分配", Type = NodeType.Server, Children = aloneNodes.ToArray() }); } allNodes.AddRange(pServers); return(allNodes); }
void StartFire(PhysicalNode node) { int rand = Random.Range(0, 360); Instantiate(flamePrefab, node.worldPos, Quaternion.Euler(270, rand, rand)); }