Exemple #1
0
    void PreparePath()
    {
        bool[,] tilesmap = new bool[100, 100];
        //prepare obstaclemap

        preparedEnemyPos = this.transform.position;
        for (int x = 0; x < 100; x++)
        {
            for (int y = 0; y < 100; y++)
            {
                tilesmap [x, y] = !Physics2D.OverlapBox(new Vector2(x, y) + preparedEnemyPos, new Vector2(1, 1), 0);
                //Debug.Log (tilesmap [x, y]);
            }
        }


        PathFind.Grid grid = new PathFind.Grid(100, 100, tilesmap);
        // create source and target points
        PathFind.Point _from = new PathFind.Point(50, 50);
        PathFind.Point _to   = new PathFind.Point((int)(GameObject.Find("Player").transform.position.x - preparedEnemyPos.x),
                                                  (int)(GameObject.Find("Player").transform.position.y - preparedEnemyPos.y));

        // get path
        // path will either be a list of Points (x, y), or an empty list if no path is found.
        path = PathFind.Pathfinding.FindPath(grid, _from, _to);
        Debug.Log(path.ToArray().Length);
        foreach (PathFind.Point point in path)
        {
            Debug.Log("path x: " + point.x + " y: " + point.y);
        }
    }
Exemple #2
0
    public static Stack <PathFind.Point> GetPath(int startX, int startY, int endX, int endY)
    {
        PathFind.Point _from = new PathFind.Point(startX, startY);
        PathFind.Point _to   = new PathFind.Point(endX, endY);

        if (tilesMap[_to.x, _to.y] == false)
        {
            _to = FindNearestValidNode(_from, _to);
        }

        foundPath = false;
        Queue <PathFind.Point> q = new Queue <PathFind.Point>();

        _from.parent = null;
        q.Enqueue(_from);

        BFS(q, new bool[19, 11], _to);

        PathFind.Point         traverse = _to;
        Stack <PathFind.Point> path     = new Stack <PathFind.Point>();

        while (traverse != null)
        {
            path.Push(new PathFind.Point(traverse.x, -traverse.y));
            traverse = traverse.parent;
        }
        if (path.Count > 0)
        {
            path.Pop();
        }
        return(path);
    }
Exemple #3
0
    IEnumerator move()
    {
        foreach (PathFind.Point point in path)
        {
            //Debug.Log ("following next point");
            GameObject objAtPoint = mapManager.getObjectAt(point.x, point.y);
            if (objAtPoint != null && objAtPoint.tag == "Building")
            {
                needNewPath = true;
                rotateToTarget(objAtPoint.transform.position);
                attack(objAtPoint);
                break;
            }
            Vector2 itemPos = new Vector2(point.x, point.y);
            while (Vector2.Distance(transform.position, itemPos) > .0001)
            {
                rotateToTarget(itemPos);
                transform.position = Vector2.MoveTowards(transform.position, itemPos, speed * Time.deltaTime);
                yield return(null);
            }

            PathFind.Point actualPlayerPos = new PathFind.Point((int)player.gameObject.transform.position.x,
                                                                (int)player.gameObject.transform.position.y);
            if (path [path.Count - 1] != actualPlayerPos)               //check if the player have moved
            {
                needNewPath = true;
                break;
            }
        }
        needNewPath = true;
    }
    // ====================================================================================== //
    public static Position GetNextPosition(Position origin, Position destination)
    {
        // if pointing on creature - use the my stupid path finding
        if (destination.DungeonTile.IsBlockPath)
        {
            return(getNextStupidTile(origin, destination));
        }

        // else, using A* of 3rd party library from https://github.com/RonenNess/Unity-2d-pathfinding

        // create a grid
        PathFind.Grid grid = new PathFind.Grid(Dungeon.Instance.Width, Dungeon.Instance.Height, Dungeon.Instance.GetWalkingMap());

        // create source and target points
        PathFind.Point _from = new PathFind.Point(origin.X, origin.Y);
        PathFind.Point _to   = new PathFind.Point(destination.X, destination.Y);

        // get path
        // path will either be a list of Points (x, y), or an empty list if no path is found.
        List <PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to);

        // if no path return null position
        if (path.Count <= 0)
        {
            return(Position.NullPosition);
        }
        // otherwise, return the first position in path
        return(new Position(path[0].x, path[0].y));
    }
    public List <PathFind.Point> Astar(int startX, int startY, int targetX, int targetY)
    {
        PathFind.Grid grid = new PathFind.Grid(mapSizeX, mapSizeY, navMesh);

        PathFind.Point _from = new PathFind.Point(startX, startY);
        PathFind.Point _to   = new PathFind.Point(targetX, targetY);

        return(PathFind.Pathfinding.FindPath(grid, _from, _to));
    }
Exemple #6
0
    void Update()
    {
        // The X and Y positions of both the player and the enemy are either rounded up or down.
        // These are necessary in making the AI's path to the player's location in the grid as accurate as possible.
        float playerX, playerY, enemyX, enemyY;

        if (player.transform.position.x > (int)player.transform.position.x + .49)
        {
            playerX = Mathf.Ceil(player.transform.position.x);
        }
        else
        {
            playerX = Mathf.Floor(player.transform.position.x);
        }
        if (player.transform.position.y > (int)player.transform.position.y + .49)
        {
            playerY = Mathf.Ceil(player.transform.position.y);
        }
        else
        {
            playerY = Mathf.Floor(player.transform.position.y);
        }
        if (transform.position.y > (int)transform.position.y + .49)
        {
            enemyY = Mathf.Ceil(transform.position.y);
        }
        else
        {
            enemyY = Mathf.Floor(transform.position.y);
        }
        if (transform.position.x > (int)transform.position.x + .49)
        {
            enemyX = Mathf.Ceil(transform.position.x);
        }
        else
        {
            enemyX = Mathf.Floor(transform.position.x);
        }

        // The reason why int casting is done here instead of in the if/else block above
        // is because I had some other code that did a different thing and didn't change this.
        PathFind.Point playerPos = new PathFind.Point((int)playerX, (int)playerY);
        PathFind.Point enemyPos  = new PathFind.Point((int)enemyX, (int)enemyY);

        // Have to make a new tilemap, which is a 2D array of floats, where every index corresponds to
        // the penalty an enemy path would incur, should the enemy cross that way.
        makeNewTileMap(10, 10);
        grid = new PathFind.Grid(10, 10, tilesmap);

        // Find the shortest path to the player. If the count isn't 0, move towards player.
        enemyPath = PathFind.Pathfinding.FindPath(grid, enemyPos, playerPos);
        if (enemyPath.Count != 0)
        {
            enemyRB.MovePosition(Vector2.LerpUnclamped(new Vector2(transform.position.x, transform.position.y), new Vector2(enemyPath[0].x, enemyPath[0].y), .08f));
        }
    }
Exemple #7
0
 protected override void Start()
 {
     base.Start();
     path  = new List <PathFind.Point>();
     _to   = new PathFind.Point(X, Y);
     _from = new PathFind.Point(X, Y);
     grid  = new PathFind.Grid(_LevelController.instance.knightsTilemap.GetLength(0), _LevelController.instance.knightsTilemap.GetLength(1), _LevelController.instance.knightsTilemap);
     path  = PathFind.Pathfinding.FindPath(grid, _from, _to);
     ExtrapolatePath(path);
 }
Exemple #8
0
    public Vector3[] GetPath(Vector3 start, Vector3 target)
    {
        Vector3Int startPos = baseMap.WorldToCell(start);
        Vector3Int targetPos = baseMap.WorldToCell(target);
        PathFind.Point _from = new PathFind.Point(Mathf.Abs(startPos.x), Mathf.Abs(startPos.y));
        PathFind.Point _to = new PathFind.Point(Mathf.Abs(targetPos.x), Mathf.Abs(targetPos.y));

        // get path
        // path will either be a list of Points (x, y), or an empty list if no path is found.
        return TileToWorldList(PathFind.Pathfinding.FindPath(grid, _from, _to));
    }
    // Use this for initialization
    void Start()
    {
        actor = gameObject;

        walkableMap = GameManager.instance.walkableMap;
        gameGrid    = new PathFind.Grid(walkableMap.GetLength(0), walkableMap.GetLength(1), walkableMap);

        SetCurrentPoint(transform.position);
        targetPoint         = new PathFind.Point(currentPoint.x, currentPoint.y);
        avoidedPedestrian   = false;
        waitingForPedToPass = false;
        unvisitedShops      = new List <Vector2Int>(GameManager.instance.shops);
        unvisitedShops.Remove(new Vector2Int(currentPoint.x, currentPoint.y));
    }
    public static Vector3[] GetPath(Vector2 start, Vector2 end)
    {
        PathFind.Point startPoint = new PathFind.Point((int)start.x, (int)start.y);
        PathFind.Point endPoint   = new PathFind.Point((int)end.x, (int)end.y);

        List <PathFind.Point> path = PathFind.Pathfinding.FindPath(Instance.grid, startPoint, endPoint);

        Vector3[] points = new Vector3[path.Count];

        for (int i = 0; i < path.Count; i++)
        {
            points[i] = new Vector3(path[i].x + 0.5f, path[i].y + 0.5f, 0);
        }
        return(points);
    }
Exemple #11
0
    static PathFind.Point FindNearestValidNode(PathFind.Point start, PathFind.Point end)
    {
        int xDir, yDir;

        if (end.x - start.x < 0)
        {
            xDir = 1;
        }
        else if (end.x - start.x > 0)
        {
            xDir = -1;
        }
        else
        {
            xDir = 0;
        }

        if (end.y - start.y < 0)
        {
            yDir = 1;
        }
        else if (end.y - start.y > 0)
        {
            yDir = -1;
        }
        else
        {
            yDir = 0;
        }

        int xPos = end.x, yPos = end.y;

        while (xPos < 19 && xPos >= 0 && yPos < 11 && yPos >= 0 &&
               tilesMap[xPos, yPos] == false)
        {
            xPos += xDir;
            yPos += yDir;
        }

        return(new PathFind.Point(xPos, yPos));
    }
Exemple #12
0
    public override void OnUpdate(float deltaTime)
    {
        foreach (var entity in tilemapFilter)
        {
            tilemap = entity.GetComponent <TilemapComponent>().Tilemap;
            break;
        }

        var size = tilemap.size;

        PathFind.Grid grid = GetPathFindGrid();

        foreach (var entity in filter)
        {
            var pathFinder = entity.GetComponent <PathFinderComponent>();
            var transform  = entity.GetComponent <TransformComponent>();

            var start  = tilemap.WorldToCell(transform.Transform.position);
            var target = tilemap.WorldToCell(pathFinder.Target.position);

            start  = ToPathSystem(size, start);
            target = ToPathSystem(size, target);

            PathFind.Point _from = new PathFind.Point(start.x, start.y);
            PathFind.Point _to   = new PathFind.Point(target.x, target.y);

            List <PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to);

            ref var pathComponent = ref entity.GetComponent <PathComponent>();
            pathComponent.CurrentPoint = (Vector2Int)start;
            pathComponent.Distance     = path.Count;

            if (path.Count > 0)
            {
                pathComponent.NextPoint = new Vector2Int(path[0].x, path[0].y);
            }
            else
            {
                pathComponent.NextPoint = (Vector2Int)target;
            }
        }
Exemple #13
0
    public void FindNewPath(GridUnit fromGrid, GridUnit toGrid)
    {
        // get the script component so we can get row,col
        GridUnit fromScript = fromGrid.GetComponent <GridUnit> ();
        GridUnit toScript   = toGrid.GetComponent <GridUnit> ();

        // create source and target points
        PathFind.Point _from = new PathFind.Point(fromScript.col, fromScript.row);
        PathFind.Point _to   = new PathFind.Point(toScript.col, toScript.row);

        // get path
        // path will either be a list of Points (x, y), or an empty list if no path is found.
        List <PathFind.Point> pathList = PathFind.Pathfinding.FindPath(grid, _from, _to);

        //Debug.Log ("FindNewPath Called");

        // only set path if list is not empty
        if (pathList.Count > 0)
        {
            navPath = pathList;
        }
    }
Exemple #14
0
    // Use this for initialization
    void Start()
    {
        mapManager  = MapManager.instance;
        player      = GameManager.playerInstance;
        audioSource = gameObject.GetComponent <AudioSource>();

        int playerX = (int)gameObject.transform.position.x;
        int playerY = (int)gameObject.transform.position.y;

        self = new PathFind.Point(playerX, playerY);

        int x = (int)player.gameObject.transform.position.x;
        int y = (int)player.gameObject.transform.position.y;

        target = new PathFind.Point(x, y);

        attackDamage += (int)(GameManager.instance.getWave() * 0.1f * attackDamage);
        hp           += (int)(GameManager.instance.getWave() * 0.1f * hp);
        reward       += (int)(GameManager.instance.getWave() * 0.1f * reward);
        setupPathfinding();
        StartCoroutine(move());
    }
Exemple #15
0
    void AlternativeCatchPlayer()
    {
        Vector2 pos = transform.position;

        desiredMomentum = new Vector2(0, 0);

        PathFind.Point _from = new PathFind.Point(50, 50);
        PathFind.Point _to   = new PathFind.Point(Random.Range(0, 100), Random.Range(0, 100));

        if (Time.frameCount % 20 == 0)
        {
            bool[,] tilesmap = new bool[100, 100];
            for (int x = 0; x <= 99; x++)
            {
                for (int y = 0; y <= 99; y++)
                {
                    tilesmap [x, y] = !Physics2D.OverlapPoint(new Vector2(x, y) + pos);
                    if (Input.GetKey(KeyCode.I) && !tilesmap[x, y])
                    {
                        Debug.Log(tilesmap [x, y] + "checking x:" + (x + pos.x) + " y:" + (y + pos.y));
                    }
                }
            }

            PathFind.Grid grid = new PathFind.Grid(100, 100, tilesmap);



            path = PathFind.Pathfinding.FindPath(grid, _from, _to);
        }
        if (path != null && Time.frameCount % 20 < path.ToArray().Length)
        {
            desiredMomentum.x = (path [Time.frameCount % 20].x - 50) * speed;
            desiredMomentum.y = (path [Time.frameCount % 20].y - 50) * speed;
        }

        GetComponent <Rigidbody2D>().AddForce(desiredMomentum);
    }
 void SetCurrentPoint(Vector2 newPos)
 {
     currentPoint = new PathFind.Point((int)newPos.x, (int)newPos.y);
 }
Exemple #17
0
    // Update is called once per frame
    void Update()
    {
        if (pause_game_control.paused)
        {
            return;
        }

        CleanUpActionLine();

        if (unit_action_counter < unit_action_speed)
        {
            unit_action_counter += 1;
            return;
        }
        else
        {
            unit_action_counter = 0;
        }



        // If we have a job do something about it
        if (job != null)
        {
            // If we don't have a path we are either at the job
            // or we need to compute an new path
            if (!IsPathing())
            {
                if (IsByJob())
                {
                    // we are at the target, do work!
                    path = null;
                    WorkOnJob();
                }
                else
                {
                    // We arent't near the target, path there
                    var pos = GetPos();
                    var tgt = job.GetTarget();

                    bool[,] d_grid_mask = dispatcher.GetGridMask();
                    bool[,] grid_mask   = new bool[d_grid_mask.GetLength(0), d_grid_mask.GetLength(1)];

                    for (int i = 0; i < d_grid_mask.GetLength(0); i++)
                    {
                        for (int j = 0; j < d_grid_mask.GetLength(1); j++)
                        {
                            grid_mask[i, j] = d_grid_mask[i, j];
                        }
                    }

                    grid_mask[tgt.x, tgt.y] = true;

                    if (pathfinding_grid == null)
                    {
                        pathfinding_grid = new PathFind.Grid(grid_mask);
                    }
                    else
                    {
                        pathfinding_grid.UpdateGrid(grid_mask);
                    }

                    var _from = new PathFind.Point((int)pos.x, (int)pos.y);
                    var _to   = new PathFind.Point(tgt.x, tgt.y);
                    path = PathFind.Pathfinding.FindPath(pathfinding_grid, _from, _to);

                    if (path.Count == 0)
                    {
                        // we were not able to path to location or. Abort useful tasks, cancel idle tasks;

                        if (job is IdleJob)
                        {
                            job.Cancel();
                        }
                        else
                        {
                            job.Abort("Could not find path to job.");
                        }
                    }
                    else if (path.Count > 1)
                    {
                        // pop off the last action so we don't move onto the tile.
                        path.RemoveAt(path.Count - 1);
                    }
                }
            }

            // explicitly separate if so that we can move this turn
            // if we needed to compute a new path
            if (IsPathing())
            {
                // get the next point on the path and move there.
                var move_to = path[0];
                path.RemoveAt(0);

                var new_vec = new Vector3((float)move_to.x, (float)move_to.y, transform.position.z);
                transform.position = new_vec;

                var pos = GetPos();
                dispatcher.AcknowledgeUnitMove(pos, new_vec);
            }
        }
        else
        {
            // if we have no job, we can clear our pathing
            path = null;
        }
    }
Exemple #18
0
 public void MoveTowards(PathFind.Point point)
 {
 }
Exemple #19
0
    // returns vector2 position given pathfind point
    public Vector2 GetGridPos(PathFind.Point gridPoint)
    {
        string gridName = string.Format("GridUnit_{0}-{1}", gridPoint.y, gridPoint.x);

        return((Vector2)GameObject.Find(gridName).transform.position);
    }
Exemple #20
0
    static void BFS(Queue <PathFind.Point> queue, bool[,] visited, PathFind.Point dest)
    {
        if (queue.Count == 0)
        {
            return;
        }

        PathFind.Point p = queue.Dequeue();

        if (foundPath)
        {
            return;
        }

        if (p == dest)
        {
            //Debug.Log("Found Path");
            dest.parent = p.parent;
            foundPath   = true;
            return;
        }

        if (p.x - 1 >= 0 && !visited[p.x - 1, p.y] && tilesMap[p.x - 1, p.y])
        {
            visited[p.x - 1, p.y] = true;
            queue.Enqueue(new PathFind.Point(p.x - 1, p.y, p));
        }
        if (p.x + 1 < 19 && !visited[p.x + 1, p.y] && tilesMap[p.x + 1, p.y])
        {
            visited[p.x + 1, p.y] = true;
            queue.Enqueue(new PathFind.Point(p.x + 1, p.y, p));
        }
        if (p.y - 1 >= 0 && !visited[p.x, p.y - 1] && tilesMap[p.x, p.y - 1])
        {
            visited[p.x, p.y - 1] = true;
            queue.Enqueue(new PathFind.Point(p.x, p.y - 1, p));
        }
        if (p.y + 1 < 11 && !visited[p.x, p.y + 1] && tilesMap[p.x, p.y + 1])
        {
            visited[p.x, p.y + 1] = true;
            queue.Enqueue(new PathFind.Point(p.x, p.y + 1, p));
        }
        if (p.y + 1 < 11 && p.x + 1 < 19 && !visited[p.x + 1, p.y + 1] && tilesMap[p.x + 1, p.y + 1])
        {
            visited[p.x + 1, p.y + 1] = true;
            queue.Enqueue(new PathFind.Point(p.x + 1, p.y + 1, p));
        }
        if (p.y + 1 < 11 && p.x - 1 >= 0 && !visited[p.x - 1, p.y + 1] && tilesMap[p.x - 1, p.y + 1])
        {
            visited[p.x - 1, p.y + 1] = true;
            queue.Enqueue(new PathFind.Point(p.x - 1, p.y + 1, p));
        }
        if (p.y - 1 >= 0 && p.x + 1 < 19 && !visited[p.x + 1, p.y - 1] && tilesMap[p.x + 1, p.y - 1])
        {
            visited[p.x + 1, p.y - 1] = true;
            queue.Enqueue(new PathFind.Point(p.x + 1, p.y - 1, p));
        }
        if (p.y - 1 >= 0 && p.x - 1 >= 0 && !visited[p.x - 1, p.y - 1] && tilesMap[p.x - 1, p.y - 1])
        {
            visited[p.x - 1, p.y - 1] = true;
            queue.Enqueue(new PathFind.Point(p.x - 1, p.y - 1, p));
        }

        BFS(queue, visited, dest);
    }
 void SetNewPath(PathFind.Point target)
 {
     path = PathFind.Pathfinding.FindPath(gameGrid, currentPoint, target, true);
 }
 void TakeNextPathStep()
 {
     currentPoint = path[0];
     path.RemoveAt(0);
 }