public Vector3 getDirection(float x, float y) { float sx = x / mapScale; float sy = y / mapScale; int ChunkX = (int)(sx / FlowGrid.gridResolution); int ChunkY = (int)(sy / FlowGrid.gridResolution); int gridX = (int)(sx % FlowGrid.gridResolution); int gridY = (int)(sy % FlowGrid.gridResolution); FlowGrid grid = chunkSteps[new Vector2Int(ChunkX, ChunkY)]; return(FlowGrid.GetDirection(grid.directionField[gridX, gridY])); }
void Start() { _grid = GetComponent <FlowGrid>(); StartCoroutine(CastRays(Frequency)); }
public PathRequest calcPath(Vector2 start, Vector2 end, bool reset = false, PathRequest appendTarget = null) { //TODO generating portal maps PathRequest request = (appendTarget == null) ? new PathRequest(grid.getSpacing()) : appendTarget; int startChunkX = (int)(start.x / mapSize); int startChunkY = (int)(start.y / mapSize); int startX = (int)(start.x % mapSize); int startY = (int)(start.y % mapSize); int endChunkX = (int)(end.x / mapSize); int endChunkY = (int)(end.y / mapSize); int endX = (int)(end.x % mapSize); int endY = (int)(end.y % mapSize); //TODO add in detection for which start nodes it has access to. PortalGraph.PortalNode startNode = null; PortalGraph.PortalNode endNode = null; //TODO add an index to the MapDataChunk that stores the index of which portal each square has access to. foreach (int i in navGraph.associatedNodes[startChunkX, startChunkY]) { PortalGraph.PortalNode testNode = navGraph.nodes[i]; int nodeX; int nodeY; if (testNode.isHorizontal) { nodeY = testNode.side ? 0 : FlowGrid.gridResolution - 1; nodeX = testNode.startIndex; } else { nodeX = testNode.side ? 0 : FlowGrid.gridResolution - 1; nodeY = testNode.startIndex; } var mapChunk = mapData[startChunkX, startChunkY]; if (pathfinding.calculatePath(mapChunk, mapChunk.AStarGrid[startX, startY], mapChunk.AStarGrid[nodeX, nodeY], true)) { startNode = testNode; break; } } foreach (int i in navGraph.associatedNodes[endChunkX, endChunkY]) { PortalGraph.PortalNode testNode = navGraph.nodes[i]; int nodeX; int nodeY; if (testNode.isHorizontal) { nodeY = testNode.side ? 0 : FlowGrid.gridResolution - 1; nodeX = testNode.startIndex; } else { nodeX = testNode.side ? 0 : FlowGrid.gridResolution - 1; nodeY = testNode.startIndex; } var mapChunk = mapData[endChunkX, endChunkY]; if (pathfinding.calculatePath(mapChunk, mapChunk.AStarGrid[endX, endY], mapChunk.AStarGrid[nodeX, nodeY], true)) { endNode = testNode; break; } } if (startNode == null) { Debug.LogError("Unit is trapped!"); } if (endNode == null) { Debug.LogError("Requested Destination is inaccessible"); } pathfinding.calculatePath(navGraph, startNode, endNode, reset); List <AStarNode> path = pathfinding.path; FlowGrid finalGrid; Vector2Int pos = new Vector2Int(endChunkX, endChunkY); finalGrid = (!request.chunkSteps.ContainsKey(pos)) ? new FlowGrid() : request.chunkSteps[pos]; finalGrid.integrationField[(int)(end.x % FlowGrid.gridResolution), (int)(end.y % FlowGrid.gridResolution)] = 0; finalGrid.dirtySquares.Add(new Vector2Int((int)(end.x % FlowGrid.gridResolution), (int)(end.y % FlowGrid.gridResolution))); finalGrid.chunkX = endChunkX; finalGrid.chunkY = endChunkY; finalGrid.compute(); if (!request.chunkSteps.ContainsKey(pos)) { request.chunkSteps.Add(pos, finalGrid); } else { request.chunkSteps[pos] = finalGrid; } FlowGrid lastGrid = finalGrid; for (int i = 1; i < path.Count; i++)//trace from the end to the start { PortalGraph.PortalNode last = (PortalGraph.PortalNode)path[i - 1]; PortalGraph.PortalNode current = (PortalGraph.PortalNode)path[i]; Vector2Int portalIndex = new Vector2Int(last.portalX, last.portalY); if (last.gridX != current.gridX || last.gridY != current.gridY) { FlowGrid newGrid; bool hasVisited = request.chunkSteps.ContainsKey(new Vector2Int(current.gridX, current.gridY)); newGrid = hasVisited ? request.chunkSteps[new Vector2Int(current.gridX, current.gridY)] : new FlowGrid(); newGrid.chunkX = current.gridX; newGrid.chunkY = current.gridY; if (last.isHorizontal) { int yIndex = !last.side ? 0 : FlowGrid.gridResolution - 1; int yIndexNot = last.side ? 0 : FlowGrid.gridResolution - 1; for (int j = 0; j < last.size; j++) { newGrid.integrationField[last.startIndex + j, yIndex] = lastGrid.integrationField[last.startIndex + j, yIndexNot] + 1; newGrid.dirtySquares.Add(new Vector2Int(last.startIndex + j, yIndex)); } } else { int xIndex = !last.side ? 0 : FlowGrid.gridResolution - 1; int xIndexNot = last.side ? 0 : FlowGrid.gridResolution - 1; for (int j = 0; j < last.size; j++) { newGrid.integrationField[xIndex, last.startIndex + j] = lastGrid.integrationField[xIndexNot, last.startIndex + j] + 1; newGrid.dirtySquares.Add(new Vector2Int(xIndex, last.startIndex + j)); } } newGrid.compute(); if (last.isHorizontal) { int yIndex = !last.side ? 0 : FlowGrid.gridResolution - 1; for (int j = 0; j < last.size; j++) { newGrid.directionField[last.startIndex + j, yIndex] = last.side ? 4 : 0; } } else { int xIndex = !last.side ? 0 : FlowGrid.gridResolution - 1; for (int j = 0; j < last.size; j++) { newGrid.directionField[xIndex, last.startIndex + j] = last.side ? 2 : 6; } } if (!hasVisited) { request.chunkSteps.Add(new Vector2Int(current.gridX, current.gridY), newGrid); } else { request.chunkSteps[new Vector2Int(current.gridX, current.gridY)] = newGrid; } lastGrid = newGrid; } } return(request); }