/// <summary> /// Return a list of path nodes from the start tile to the end tile ( Use RpgMapHelper class to get the tile index ) /// </summary> /// <param name="startIdx"></param> /// <param name="endIdx"></param> /// <returns></returns> public IEnumerator GetRouteFromToAsync(Vector2 startPos, Vector2 endPos) { IPathNode start = GetMapTileNode(startPos); EndNode = GetMapTileNode(endPos); return(m_pathFinding.ComputePathAsync(start, EndNode, AllowBlockedDestination)); }
void ProcessComputedPath() { //+++find closest node and take next one if possible m_curNode = m_pathNodes.First; if (m_curNode != null) { Vector2 vPos = transform.position; while (m_curNode != null && m_curNode.Next != null) { MapTileNode n0 = m_curNode.Value as MapTileNode; MapTileNode n1 = m_curNode.Next.Value as MapTileNode; float distSqr = (vPos - (Vector2)n0.Position).sqrMagnitude; float distSqr2 = (vPos - (Vector2)n1.Position).sqrMagnitude; if (distSqr2 < distSqr) { m_curNode = m_curNode.Next; } else { break; } } // take next one, avoid moving backward in the path if (m_curNode.Next != null) { m_curNode = m_curNode.Next; } } //--- }
/// <summary> /// Return a list of path nodes from the start tile to the end tile /// </summary> /// <param name="startIdx"></param> /// <param name="endIdx"></param> /// <returns></returns> public IEnumerator GetRouteFromToAsync(Vector2 startPos, Vector2 endPos) { IPathNode start = GetMapTileNode(startPos); EndNode = GetMapTileNode(endPos); return(m_pathFinding.ComputePathAsync(start, EndNode, MaxDistance > 0? MaxDistance : int.MaxValue)); }
public override float GetNeigborMovingCost(int neigborIdx) { MapTileNode neighborNode = GetNeighbor(neigborIdx) as MapTileNode; if (!m_owner.AllowBlockedDestination || m_owner.EndNode != neighborNode) { if ((m_owner.PassableDetectionMode & MapPathFinding.ePassableDetectionMode.Raycast2D) != 0) { if (RaycastCheck2D(neighborNode.Position)) { return(PathFinding.k_InfiniteCostValue); } } else if ((m_owner.PassableDetectionMode & MapPathFinding.ePassableDetectionMode.Raycast3D) != 0) { if (RaycastCheck3D(neighborNode.Position)) { return(PathFinding.k_InfiniteCostValue); } } } float fCost = 1f; //567 // //3X4 // neighbor index positions, X is the position of this node //012 if (neigborIdx == 0 || neigborIdx == 2 || neigborIdx == 5 || neigborIdx == 7) { //check if can reach diagonals as it could be not possible if flank tiles are not passable MapTileNode nodeN = GetNeighbor(1) as MapTileNode; MapTileNode nodeW = GetNeighbor(3) as MapTileNode; MapTileNode nodeE = GetNeighbor(4) as MapTileNode; MapTileNode nodeS = GetNeighbor(6) as MapTileNode; bool usingColliders = (m_owner.PassableDetectionMode & (MapPathFinding.ePassableDetectionMode.Raycast2D | MapPathFinding.ePassableDetectionMode.Raycast2D)) != 0; bool nodeNisPassable = nodeN.IsPassable() && (!usingColliders || GetNeigborMovingCost(1) != PathFinding.k_InfiniteCostValue); bool nodeWisPassable = nodeW.IsPassable() && (!usingColliders || GetNeigborMovingCost(3) != PathFinding.k_InfiniteCostValue); bool nodeEisPassable = nodeE.IsPassable() && (!usingColliders || GetNeigborMovingCost(4) != PathFinding.k_InfiniteCostValue); bool nodeSisPassable = nodeS.IsPassable() && (!usingColliders || GetNeigborMovingCost(6) != PathFinding.k_InfiniteCostValue); if ( !m_owner.AllowDiagonals || (neigborIdx == 0 && (!nodeNisPassable || !nodeWisPassable)) || // check North West (neigborIdx == 2 && (!nodeNisPassable || !nodeEisPassable)) || // check North East (neigborIdx == 5 && (!nodeSisPassable || !nodeWisPassable)) || // check South West (neigborIdx == 7 && (!nodeSisPassable || !nodeEisPassable)) // check South East ) { return(PathFinding.k_InfiniteCostValue); } else { fCost = 1.41421356237f; } } else { fCost = 1f; } fCost *= m_costFactor; return(fCost); }
/// <summary> /// Return a list of path nodes from the start tile to the end tile ( Use RpgMapHelper class to get the tile index ) /// </summary> /// <param name="startIdx"></param> /// <param name="endIdx"></param> /// <returns></returns> public IEnumerator GetRouteFromToAsync(Vector2 startPos, Vector2 endPos) { ClearNodeDictionary(); IPathNode start = GetMapTileNode(startPos); EndNode = GetMapTileNode(endPos); return(m_pathFinding.ComputePathAsync(start, EndNode)); }
/// <summary> /// Get a map tile node based on its index /// </summary> /// <param name="idx"></param> /// <returns></returns> public MapTileNode GetMapTileNode(int idx) { MapTileNode mapTileNode; bool wasFound = m_dicTileNodes.TryGetValue(idx, out mapTileNode); if (!wasFound) { mapTileNode = new MapTileNode(TilemapGroup, this, idx); m_dicTileNodes[idx] = mapTileNode; } return(mapTileNode); }
/// <summary> /// Get a map tile node based on its index /// </summary> /// <param name="idx"></param> /// <returns></returns> public MapTileNode GetMapTileNode(int idx) { MapTileNode mapTileNode; bool wasFound = m_dicTileNodes.TryGetValue(idx, out mapTileNode); if (!wasFound) { mapTileNode = new MapTileNode(TilemapGroup, this); mapTileNode.SetGridPos(idx >> 16, (int)(short)idx, CellSize); m_dicTileNodes[idx] = mapTileNode; } return(mapTileNode); }
/// <summary> /// Return a list of path nodes from the start tile to the end tile ( Use RpgMapHelper class to get the tile index ) /// </summary> /// <param name="startIdx"></param> /// <param name="endIdx"></param> /// <returns></returns> public LinkedList <IPathNode> GetRouteFromTo(Vector2 startPos, Vector2 endPos) { LinkedList <IPathNode> nodeList = new LinkedList <IPathNode>(); if (m_pathFinding.IsComputing) { Debug.LogWarning("PathFinding is already computing. GetRouteFromTo will not be executed!"); } else { IPathNode start = GetMapTileNode(startPos); EndNode = GetMapTileNode(endPos); nodeList = m_pathFinding.ComputePath(start, EndNode, AllowBlockedDestination); //NOTE: the path is given from end to start ( change the order? ) } return(nodeList); }
MapTileNode GetOrCreateMapTileNode(int idx) { MapTileNode node; int poolIdx = m_mapTileNodePool.Count - 1; if (poolIdx >= 0) { node = m_mapTileNodePool[poolIdx]; m_mapTileNodePool.RemoveAt(poolIdx); } else { node = new MapTileNode(TilemapGroup, this); } node.SetGridPos(idx >> 16, (int)(short)idx, CellSize); return(node); }
public override float GetNeigborMovingCost(int neigborIdx) { float fCost = 1f; //567 // //3X4 // neighbor index positions, X is the position of this node //012 if (neigborIdx == 0 || neigborIdx == 2 || neigborIdx == 5 || neigborIdx == 7) { //check if can reach diagonals as it could be not possible if flank tiles are not passable MapTileNode nodeN = GetNeighbor(1) as MapTileNode; MapTileNode nodeW = GetNeighbor(3) as MapTileNode; MapTileNode nodeE = GetNeighbor(4) as MapTileNode; MapTileNode nodeS = GetNeighbor(6) as MapTileNode; if ( !m_owner.AllowDiagonals || (neigborIdx == 0 && (!nodeN.IsPassable() || !nodeW.IsPassable())) || // check North West (neigborIdx == 2 && (!nodeN.IsPassable() || !nodeE.IsPassable())) || // check North East (neigborIdx == 5 && (!nodeS.IsPassable() || !nodeW.IsPassable())) || // check South West (neigborIdx == 7 && (!nodeS.IsPassable() || !nodeE.IsPassable())) // check South East ) { return(PathFinding.k_InfiniteCostValue); } else { fCost = 1.41421356237f; } } else { fCost = 1f; } return(fCost); }
public MapTileNode GetMapTileNode(int gridX, int gridY) { return(GetMapTileNode(MapTileNode.UnsafeJointTwoInts(gridX, gridY))); }