// Calculate the next point on the Creatures Patrol Point NextPointOnPatrol() { // The Target Coord is determined by the Current Leg of the Creature's Patrol int targetXCoord = patrolEnd.x; int targetYCoord = patrolEnd.y; if (onReturnLegOfPatrol) { targetXCoord = patrolStart.x; targetYCoord = patrolStart.y; } // Determine the Starting Position and the Target Position int currentX = (int)transform.position.x; int currentY = (int)transform.position.y; Point startPos = new Point(currentX, currentY); Point targetPos = new Point(targetXCoord, targetYCoord); // Calculate the Best Path to the Target Point List <Point> path = Pathfinding.FindPath(pathingCosts, startPos, targetPos); Point pointToMoveTo = path [0]; // Update the Leg of the Patrol if (pointToMoveTo.x == targetPos.x && pointToMoveTo.y == targetPos.y) { onReturnLegOfPatrol = !onReturnLegOfPatrol; } return(pointToMoveTo); }
IEnumerator Wandering() { List <PathFind.Point> path = new List <PathFind.Point>(); Vector3Int pos = TileManager.instance.floor.WorldToCell(transform.position); PathFind.Point _start = new PathFind.Point(pos.x, pos.y); // Get a valid destination with path while (path.Count < 1) { Vector2 destination = RandomDestination(); PathFind.Point _dest = new PathFind.Point((int)destination.x, (int)destination.y); path = PathFind.Pathfinding.FindPath(TileManager.instance.grid, _start, _dest); } // Travel along path while (path.Count > 1) { for (int i = 0; i < pointsToTravelBeforeDecision || i < path.Count - 1; i++) { int x = path[0].x; int y = path[0].y; destination = new Vector2(x, y); path.RemoveAt(0); // Wait until at point while (position.position != new Vector3(x, y)) { } } yield return(new WaitForSeconds(decisionTime)); } }
private void Start() { GetGrid(); PathFind.Point _from = new PathFind.Point(10, 1); PathFind.Point _to = new PathFind.Point(10, 10); List <PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to); Debug.Log(path); }
private void FindPath() { if (Path.Count == 0) { PathFind.Grid grid = new PathFind.Grid(ValueMap); PathFind.Point _from = new PathFind.Point(StartBlock.x, StartBlock.y); PathFind.Point _to = new PathFind.Point(EndBlock.x, EndBlock.y); Path = PathFind.Pathfinding.FindPath(grid, _from, _to, PathFind.Pathfinding.DistanceType.Manhattan); GetNextWayPoint(); } }
private void AddEnemies(int count) { //generate a* map _map = _grid.GenerateMap(); //generate grid NesScripts.Controls.PathFind.Grid grid = new NesScripts.Controls.PathFind.Grid(_map); //path tests NesScripts.Controls.PathFind.Point _from = new NesScripts.Controls.PathFind.Point(_player.x, _player.y); NesScripts.Controls.PathFind.Point _to = new NesScripts.Controls.PathFind.Point(_target.x, _target.y); int _c = count; List <Tile> _uTiles = _grid.GetUnoccupiedTiles(null, null, new GridObject[] { _player.current, _target.current }); for (int i = 0; i < _uTiles.Count; i++) { Tile tile = _uTiles[i]; _map[tile.x, tile.y] = false; grid.UpdateGrid(_map); if (NesScripts.Controls.PathFind.Pathfinding.FindPath(grid, _from, _to, Pathfinding.DistanceType.Manhattan).Count > 0) { //occupy tile tile.Occupy(); //add and move enemy Enemy e = Instantiate(_enemyPrefab, _enemyContainer.transform); e.Move(tile); _c--; if (_c == 0) { break; } } else { _map[tile.x, tile.y] = true; } } }
// Update is called once per frame void Update() { NesScripts.Controls.PathFind.Point theFrom = new NesScripts.Controls.PathFind.Point((int)transform.position.x, (int)transform.position.y); NesScripts.Controls.PathFind.Point theTo = new NesScripts.Controls.PathFind.Point((int)target.position.x, (int)target.position.y); List <NesScripts.Controls.PathFind.Point> path = NesScripts.Controls.PathFind.Pathfinding.FindPath(grid, theFrom, theTo); // Debug.Log("Path Count" + path.Count); for (int x = 0; x < 30; x++) { for (int y = 0; y < 30; y++) { if (tilesmap[x, y]) { myGrid[x, y].GetComponent <SpriteRenderer>().sprite = tile.GetComponent <SpriteRenderer>().sprite; } } } for (int i = 0; i < path.Count; i++) { myGrid[path[i].x, path[i].y].GetComponent <SpriteRenderer>().sprite = tilePath.GetComponent <SpriteRenderer>().sprite; } if (path.Count > 0) { transform.position = Vector3.MoveTowards(transform.position, new Vector3(path[0].x, path[0].y, 0), .05f); } if (Time.time >= nextMove) { nextMove = Time.time + 1f / moveRate; // transform.position = Vector3.Lerp(transform.position, new Vector3(path[nextIndex].x, path[nextIndex].y, transform.position.z), .2f); // nextIndex++; } if (currentHealth <= 0) { Kill(); } }
// Start is called before the first frame update void Start() { currentHealth = maxHealth; for (int x = 0; x < 30; x++) { for (int y = 0; y < 30; y++) { bool isBlock = Random.Range(1, 10) == 1; tilesmap[x, y] = !isBlock; myGrid[x, y] = Instantiate(isBlock ? tileBlock : tile) as GameObject; myGrid[x, y].transform.position = new Vector3(x, y, 0); } } grid = new NesScripts.Controls.PathFind.Grid(tilesmap); _from = new NesScripts.Controls.PathFind.Point(1, 1); _to = new NesScripts.Controls.PathFind.Point(15, 15); // List<NesScripts.Controls.PathFind.Point> path = NesScripts.Controls.PathFind.Pathfinding.FindPath(grid, _from, _to); }
//Move is called by the GameManger each turn to tell each Enemy to try to move towards the player. public void Move() { Point nextPoint = NextPointOnPatrol(); MoveTo(nextPoint.x, nextPoint.y); }
public void SetPatrolStart(int x, int y) { patrolStart = new Point(x, y); patrolEnd = new Point(x + targXOffset, y + targYOffset); }