Advertisement
jwow22

Clean - AI movement

Jan 5th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1.     // To perform every tick
  2.     public override void MovementTick()
  3.     {
  4.        GridObject lowestCostObject = aiFinder.LowestCostObject();
  5.        Tile targetTile = lowestCostObject.currentTile;
  6.        currentTilePath = pathfinding.FindPath(headBody.gridObject.currentTile, targetTile);
  7.  
  8.        if (currentTilePath != null)
  9.        {
  10.            MoveHeadToTile(currentTilePath[0]);
  11.            return;
  12.        }
  13.        
  14.        // If can't find an object, it will try to move to a random valid neighbour
  15.        Tile randomValidTile = RandomValidTile();
  16.        if (randomValidTile == null)
  17.        {
  18.            // Move up and die if there's no valid neighbours
  19.            MoveHeadToTile(headBody.gridObject.currentTile.neighbourTiles[(int)Direction.Up]);
  20.            return;
  21.        }
  22.        // Move to random tile if path wasn't found
  23.        MoveHeadToTile(randomValidTile);
  24.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement