/// <summary> /// Move the unit to destination grid position /// </summary> /// <param name="destination">coords to move to</param> /// <returns>true if move successful, false otherwise</returns> public bool Move(Coords destination) { //unhighlight the move tiles when moving UnhighlightTiles(); //create a new Breadth-first search object BFS bfs = new BFS(); //check is unit has no path to destination if (!HasPath(destination, ref bfs, moveRange)) { return(false); } //set new position transform.position = Grid.CoordsToWorldSpace(destination); return(true); }
/// <summary> /// Determine the best move towards attacking range of unit /// </summary> /// <param name="movingUnitController">the unit that will be moving</param> /// <param name="targetUnitController">the unit to move towards</param> private void MoveTowardsUnit(UnitController movingUnitController, UnitController targetUnitController) { Transform movingUnitTransform = movingUnitController.GetComponent <Transform>(); //transform of moving unit Coords movingUnitCoords = movingUnitController.GetUnitCoords(); Coords targetUnitCoords = targetUnitController.GetUnitCoords(); int attackRange = movingUnitController.GetAttackRange(); int moveRange = movingUnitController.GetMoveRange(); //Determine best coordinates to move to to be within attack range //we will get a couple of coords and then check to see which one is shortest distance from moving unit //check the spaces next to target for best space eligibility List <Coords> coordsToCheck = new List <Coords>(); //a list of coords to check for best space eligibility //look at spaces left of target //move from furthest space attackable from to closer to target unit for (int x = targetUnitCoords.X - attackRange; x < targetUnitCoords.X; x += 2) //increment by two because if a space isn't good then the next one won't be either { Coords testCoords = new Coords(x, targetUnitCoords.Y); if (targetUnitController.HasDirectPath(testCoords)) { //coords close to target and has direct path, add them to list of coords to check coordsToCheck.Add(testCoords); } } //spaces right of target for (int x = targetUnitCoords.X + attackRange; x > targetUnitCoords.X; x -= 2) { Coords testCoords = new Coords(x, targetUnitCoords.Y); if (targetUnitController.HasDirectPath(testCoords)) { //coords close to target and has direct path, add them to list of coords to check coordsToCheck.Add(testCoords); } } //spaces above target for (int y = targetUnitCoords.Y + attackRange; y > targetUnitCoords.Y; y -= 2) { Coords testCoords = new Coords(targetUnitCoords.X, y); if (targetUnitController.HasDirectPath(testCoords)) { //coords close to target and has direct path, add them to list of coords to check coordsToCheck.Add(testCoords); } } //spaces below target for (int y = targetUnitCoords.Y - attackRange; y < targetUnitCoords.Y; y += 2) { Coords testCoords = new Coords(targetUnitCoords.X, y); if (targetUnitController.HasDirectPath(testCoords)) { //coords close to target and has direct path, add them to list of coords to check coordsToCheck.Add(testCoords); } } //evaluate coords that were added to list //look for the one with the shortest path from moving unit int shortestPathLength = int.MaxValue; //the length of the path to the coords that have the shortest path from moving to target BFS shortestPathBFS = new BFS(); //the bfs of the shortest path to coords foreach (var coords in coordsToCheck) { BFS bfs = new BFS(); if (movingUnitController.HasPath(coords, ref bfs)) { int solutionLength = bfs.GetSolutionLength(); if (solutionLength < shortestPathLength) { shortestPathLength = solutionLength; shortestPathBFS = bfs; } } } //Move (partially or fully) //traverse through bfs int stepsTaken = 0; //the number of space moved already Coords coordsToMoveTo = movingUnitCoords; //the coords to move to (within moveRange) shortestPathBFS.Reset(); while (stepsTaken < moveRange && shortestPathBFS.HasNext()) { //step along path to destination coordsToMoveTo = coordsToMoveTo.Get(shortestPathBFS.Next()); stepsTaken++; //unit has moved 1 space } //set new position for moving unit movingUnitTransform.position = Grid.CoordsToWorldSpace(coordsToMoveTo); //update unit location on grid grid.SetGridArray(coordsToMoveTo, movingUnitController.gameObject); grid.SetGridArray(movingUnitCoords, null); }