public IEnumerator ChaseItemOld() { Tile objTile = MazeManager.GetTile(transform.position - new Vector3(0, Tile.size / 2, 0)); Tile playerTile = Player.instance.character.currentTile; GridPath path = PathFinder.FindPath(objTile, playerTile, moveDistance); if (path != null) { path = path.PreviousSteps; while (path != null) { if (!Player.instance.canMove || Player.instance.character.damaging) { break; } Tile t = path.LastStep; Vector2 nextPosition = (Vector2)MazeManager.TileToWorldPos(t.coordinates) + new Vector2(0, Tile.size / 2); Player.instance.character.TurnTo(nextPosition); yield return(Player.instance.character.MoveTo(nextPosition, true)); path = path.PreviousSteps; } } }
public void FinishedProcessingPath(GridPath path, bool success) { Debug.Log("FinishedProcessingPath"); currentPathRequest.callback(path, success); isProcessingPath = false; //TryProcessNext(); }
protected virtual Tile ClosestToPlayer() { if (Player.visible) { Tile playerTile = Player.instance.character.currentTile; Tile myTile = character.currentTile; if (PathFinder.EstimateCost(myTile, playerTile) >= vision) { return(null); } if (!playerTile.isWalkable) { Vector2 playerPos = Player.instance.transform.position; Vector2 tileCenter = MazeManager.TileToWorldPos(playerTile.coordinates) + new Vector3(0, Tile.size / 2, 0); Vector2 d = playerPos - tileCenter; if (Mathf.Abs(d.x) > Mathf.Abs(d.y)) { int dx = d.x > 0 ? 1 : -1; playerTile = MazeManager.maze.tiles[playerTile.x + dx, playerTile.y]; } else { int dy = d.y > 0 ? 1 : -1; playerTile = MazeManager.maze.tiles[playerTile.x, playerTile.y + dy]; } } GridPath path = PathFinder.FindPath(playerTile, myTile, vision); if (path == null) { float best = vision + 1; GridPath bestp = null; foreach (Tile n in myTile.GetNeighbours4Walkeable()) { path = PathFinder.FindPath(n, playerTile, vision - 1); if (path != null && path.TotalCost < best) { best = path.TotalCost; bestp = path; } } path = bestp; } if (path != null) { if (path.PreviousSteps != null && path.PreviousSteps.LastStep.isWalkable) { return(path.PreviousSteps.LastStep); } else { return(path.LastStep); } } } return(null); }
public static void GeneratePath(GridPath path, GridArea gridArea) { var start = path.Start; var end = path.Finish; var pathResult = new List <GridAreaLocation>(); pathResult.Add(path.StartEntrance); bool isComplete = false; GridAreaLocation previous = path.StartEntrance; GridAreaLocation currentLocation = path.StartEntrance; while (!isComplete) { var cell = gridArea.GetCell(previous); // for (int i = 0; i < 4; i++) // { // var n = cell.Neighbors[i]; // // } var preferredNext = PathAlgorithm.SemiGeneticGetNextLocation(gridArea, currentLocation, path.FinishEntrance, previous); bool added = false; foreach (var i in preferredNext) { var c = gridArea.GetCell(i.Add(currentLocation)); if (c == null) { continue; } if (c.State == CellState.None) { previous = currentLocation; currentLocation = i.Add(currentLocation); if (currentLocation.X < 0 || currentLocation.Y < 0) { return; } c.State = CellState.Path; pathResult.Add(currentLocation); added = true; break; } } if (!added) { path.Path = pathResult; return; } if (pathResult.Last() == path.FinishEntrance) { path.Path = pathResult; return; } } pathResult.Add(path.FinishEntrance); }
public void OnPathFound(GridPath _path, bool pathSuccess) { Debug.Log("OnPathFound"); if (pathSuccess) { currentPath = _path; grid.vrLine.SetPositions(currentPath.getNodeVectorPositions()); } }
///////////////////// /// Base Constructors /// public GridSearchAStar(GridMap grid, GridPoint source, GridPoint destination) : base(grid) { this.source = source; this.destination = destination; explored = new Dictionary<int, GridState>(); frontier = new PriorityQueue<GridState>(); path = new GridPath(); }
public void FinishedProcessingPath(GridPath foundPath) { if(foundPath.path.pathSuccess) { foundPath.WriteResults(foundPath.path.pathTime, foundPath.path.pathTime + foundPath.path.totalMs, foundPath.path.pathType, foundPath.path.totalNodes, foundPath.path.waypoints.Length, Vector3.Distance(foundPath.path.waypoints[0], foundPath.path.waypoints[foundPath.path.waypoints.Length-1])); } foundPath.callback(foundPath.path.waypoints, foundPath.path.pathSuccess); enemysInQueue.Remove(foundPath.path.PathID); numWorkingPaths --; TryProcessNext(); }
protected override void Start() { if (havePath) { gridPath = pathfinding.GetComponent <GridPath>(); } counter = 1; GameManager.instance.AddEnemyToList(this); animator = GetComponentInChildren <Animator>(); target = GameObject.FindGameObjectWithTag("Player").transform; player = target.GetComponent <Player>(); sr = GetComponentInChildren <SpriteRenderer>(); onLeft = true; base.Start(); }
// A* alrogithm. public static GridPath FindPath(Tile initial, Tile final, int maxDistance) { // Fila de prioridade com os menores custos para o tile final PriorityQueue <GridPath> queue = new PriorityQueue <GridPath>(); queue.Enqueue(0, new GridPath(initial)); if (initial == final) { return(queue.Dequeue()); } // Para armazenar se um tile foi visitado ou não bool[,] closedTiles = new bool[MazeManager.maze.width, MazeManager.maze.height]; for (int i = 0; i < MazeManager.maze.width; i++) { for (int j = 0; j < MazeManager.maze.height; j++) { closedTiles [i, j] = false; } } while (!queue.IsEmpty) { GridPath currentPath = queue.Dequeue(); Tile currentTile = currentPath.LastStep; if (closedTiles [currentTile.x, currentTile.y] == false) { closedTiles [currentTile.x, currentTile.y] = true; foreach (Tile neighbour in currentTile.GetNeighbours4()) { if (neighbour.isWalkable && (currentPath.TotalCost + 1 <= maxDistance)) { GridPath newPath = currentPath.AddStep(neighbour, 1); if (neighbour == final) { return(newPath); } queue.Enqueue(newPath.TotalCost + EstimateCost(neighbour, final), newPath); } } } } return(null); }
static bool ValidatePath(GridPath gridPath) { GridAreaLocation previous = null; foreach (var l in gridPath.Path) { if (previous == null) { previous = l; continue; } if (Math.Abs(l.X - previous.X) + Math.Abs(l.Y - previous.Y) > 1) { return(false); //error } previous = l; } return(true); }
void GenerateRandomGrid() { var gridArea = TestGrid.GetGridArea(); var width = gridArea.Width; var height = gridArea.Height; var grid = new List <GridTile> (); var gridModels = new List <GridModel> (); ClearGridParent(); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { var gt = Instantiate(gridPrefab, GridParent); gt.name = (x + "," + y); gt.transform.localPosition = new Vector3(x, y, GridParent.transform.position.z); grid.Add(gt); gridModels.Add(new GridModel(gt)); } } gridArea.SetGrids(gridModels); var cpu = TestGrid.GetCpu(); var cpuModel = GridUtility.GetEmptyGrid(gridArea); var cpuLocation = gridArea.GetCellLocation(cpuModel.Cells.First()); //need PORTS and ENTRANCE cpuModel.GridComponents.Add(cpu); var componentScript = cpuModel.GridTile.gameObject.GetComponent <ComponentScript>(); componentScript.CreateComponent(cpu); //empty.GridTile.SetState(empty); var boostChip = TestGrid.GetBoostChip(); var boostModel = GridUtility.GetEmptyGrid(gridArea); var boostChipLocation = gridArea.GetCellLocation(boostModel.Cells.First());//need PORTS and ENTRANCE boostModel.AddComponent(boostChip); componentScript = boostModel.GridTile.gameObject.GetComponent <ComponentScript>(); componentScript.CreateComponent(boostChip); boostModel.GridTile.SetState(boostModel); var componentCount = 4; var usedTiles = new List <GridTile> (); var max = grid.Count; int i = 0; List <GridAreaLocation> recvrLocations = new List <GridAreaLocation>(); while (i < componentCount) { var receiverModel = GridUtility.GetEmptyGrid(gridArea); var gridComponent = new GridComponent(); gridComponent.Color = GameUtility.GetRandomColor(); var c = new CellLocation(Random.Range(0, 3), Random.Range(0, 3)); var rCell = receiverModel.GetCell(c);// testing, picking first cell of the grid recvrLocations.Add(gridArea.GetCellLocation(rCell)); gridComponent.CellLocations.Add(c); receiverModel.AddComponent(gridComponent); componentScript = receiverModel.GridTile.gameObject.GetComponent <ComponentScript>(); componentScript.CreateComponent(gridComponent); ++i; receiverModel.GridTile.SetState(receiverModel); } List <GridPath> paths = new List <GridPath>(); foreach (var r in recvrLocations) { var gp = new GridPath(); gp.StartEntrance = r; gp.FinishEntrance = cpuLocation; AStarPath aStar = new AStarPath(); var path = aStar.FindPath(r, cpuLocation, gridArea); gp.Path = path; //GridUtility.GeneratePath(gp, gridArea); paths.Add(gp); } GridUtility.SetCellStates(paths, gridArea); gridModels.ForEach(x => x.GridTile.SetState(x)); //gridComponent.Cell.Location = new CellLocation { X = 0, Y = 1 }; //componentScript.CreateComponent (gridComponent); //g.gameObject.AddComponent<ComponentScript> (); }
private GridPath(Tile lastStep, GridPath previousSteps, float totalCost) { LastStep = lastStep; PreviousSteps = previousSteps; TotalCost = totalCost; }
void Awake() { grid = GetComponent <GridPath>(); }
protected override void Start() { gridPath = pathfinding.GetComponent <GridPath>(); counterDark = -1; base.Start(); }
void Awake() { instance = this; grid = GetComponent <GridSystem>(); gridPath = GetComponent <GridPath>(); }