Exemplo n.º 1
0
 public void ProcessPath(IList <Vector2> path)
 {
     if (path == null)
     {
         return;
     }
     if (decoratedProcessor != null)
     {
         decoratedProcessor.ProcessPath(path);
     }
     ProcessPathConcrete(path);
 }
    public IList <Vector2> GetPathTo(Vector2 from, Vector2 to)
    {
        if (pathFindingAlgorithm == null)
        {
            pathFindingAlgorithm = new AStarAlgorithm(this, new EuclidianHeuristic());
        }
        ILevelTile fromTile = LevelController.instance.GetTileAtWorldPos(from);
        ILevelTile toTile   = LevelController.instance.GetTileAtWorldPos(to);

        if (cache.TryGetPath(fromTile, toTile, out IList <ILevelTile> path) == false)
        {
            path = pathFindingAlgorithm.CalculatePath(fromTile, toTile);
            cache.CachePath(path);
        }
        if (path == null)
        {
            return(null);
        }
        // Convert Tile to Point-Path
        IList <Vector2> ret = new List <Vector2>();

        foreach (ILevelTile tile in path)
        {
            ret.Add(tile.CenterPos);
        }
        // Add last if not included in tilepath
        if (ret.Count != 0)
        {
            if ((ret[ret.Count - 1] - to).magnitude > 0.1f)
            {
                ret.Add(to);
            }
        }
        // insert start point in between for path processing, then remove it again as agents shoul always move to the next pont.
        ret.Insert(0, from);
        pathProcessor.ProcessPath(ret);
        ret.RemoveAt(0);

        return(ret);
    }