// Use this for initialization void Start() { spawner = FindObjectOfType <SpawnerAI>(); text = GetComponent <Text>(); anim = GetComponent <Animator>(); wave = spawner.wave; }
/// <summary> /// A* Pathfinding Algorithm /// </summary> /// <param name="spawner">The spawner.</param> /// <param name="flying">if set to <c>true</c> [flying].</param> /// <param name="teamId">The team identifier.</param> /// <returns> /// List of Vectors that the Monsters will follow till they reach their destination /// </returns> LinkedList<Vector2> pathFinding(SpawnerAI spawner, bool flying, int teamId) { // 2D Array List of possible paths monsters can take to reach their destination LinkedList<Vector2> paths = new LinkedList<Vector2>(); // List of tile indexes used for calculating waypoints LinkedList<Square> usedSquares = new LinkedList<Square>(); LinkedList<Square> openSquares = new LinkedList<Square>(); LinkedList<Square> leadingSquares = new LinkedList<Square>(); Vector2 destSquare = FindTile((teamId != this.teamId ? _opponentPoint.gameObject : _point.gameObject)); int fMin = 0, g = 0; Square spawn = createSquare(g, FindTile(spawner.gameObject), destSquare); leadingSquares.AddLast(spawn); int counter = 0; int counterMax = (_mapWidth * _mapHeight); counterMax = counterMax * counterMax; do { if (counter > counterMax) { Debug.Log("Algorithm ran for too long, counts: " + counter); return paths; } else counter++; foreach(Square square in leadingSquares){ openSquares.Remove (square); usedSquares.AddLast (square); //Debug.Log ("Square: g: " + g + " h: " + square.h + " f: " + square.f + " x: " + square.x + " y: " + square.y); g = square.g; if (square.h != 0) { // Check Adjacent Squares LinkedList<Vector2> adjacentTiles = AdjacentTiles(square.x, square.y, square.g + 1, usedSquares, openSquares, flying); foreach (Vector2 adjacentSquare in adjacentTiles) { Square temp = createSquare(g +1, adjacentSquare, destSquare); foreach(Square openSquare in openSquares) if(distSquare(temp, openSquare) == 0) continue; foreach(Square usedSquare in usedSquares) if(distSquare(temp, usedSquare) == 0) continue; openSquares.AddLast(temp); } } else // Found the goal { openSquares.Clear(); Square tempSquare = square; Square cmpSquare = square; do { paths.AddFirst(map[tempSquare.y, tempSquare.x].transform.position); usedSquares.Remove(tempSquare); g--; int smallestDist = int.MaxValue; foreach(Square usedSquare in usedSquares) { if (usedSquare.g == g) { if (distSquare(usedSquare, tempSquare) < smallestDist) { smallestDist = distSquare(usedSquare, tempSquare); cmpSquare = usedSquare; } } } tempSquare = cmpSquare; } while (g > 0); paths.AddFirst(map[spawn.y, spawn.x].transform.position); usedSquares.Remove(tempSquare); //Debug.Log("Counter Finished: " + counter); return paths; } } leadingSquares.Clear(); fMin = int.MaxValue; foreach (Square openSquare in openSquares) { if (openSquare.f < fMin) { leadingSquares.Clear (); leadingSquares.AddLast (openSquare); fMin = openSquare.f; } else if (openSquare.f == fMin){ leadingSquares.AddLast (openSquare); } } g++; } while (leadingSquares.Count > 0); Debug.Log("No possible path exists!"); return paths; }
/// <summary> /// creates a new copy of a LinkedList /// </summary> /// <param name="spawner">The spawner.</param> /// <param name="ground">if set to <c>true</c> [ground].</param> /// <returns></returns> LinkedList<Vector2> copyWaypoints(SpawnerAI spawner, bool ground) { LinkedList<Vector2> copyWaypoints = new LinkedList<Vector2>(); if (ground) foreach (Vector2 v in spawner.wayPoints) copyWaypoints.AddLast(v); else foreach(Vector2 v in spawner.flyPoints) copyWaypoints.AddLast(v); return copyWaypoints; }
/// <summary> /// Adds a spawner to a spawner list /// </summary> /// <param name="spai">The spai.</param> /// <param name="teamId">The team identifier.</param> public void addSpawnerToSpawnerList(SpawnerAI spai, int teamId) { spai.targetWaypoint = (teamId == this.teamId ? _point : _opponentPoint); spawners.Add(spai); spai.wayPoints = pathFinding(spai, false, teamId); spai.flyPoints = pathFinding(spai, true, teamId); teamSpawners[teamId - 1].Add(spai); }
/// <summary> /// Adds a spawner to a spawner list /// </summary> /// <param name="spai"></param> public void addSpawnerToSpawnerList(SpawnerAI spai) { spawners.Add(spai); spai.wayPoints = pathFinding(spai); spai.flyPoints = pathFinding(spai, true); }