void Checkdirections(EnemyWP ewp, Vector2 _target) { targetTile = GetTileFromPos(_target); //make new List copy validDirections -direction you came from List <EnemyWP> NeighbourWp = new List <EnemyWP>(); List <WP> _neighbourTile = new List <WP> (); foreach (WP tile in ewp.neighbourTile) { _neighbourTile.Add(tile); } foreach (EnemyWP enemyWP in ewp.neighbours) { NeighbourWp.Add(enemyWP); } List <Vector2> ApprovedDirection = new List <Vector2>(); foreach (Vector2 dir in ewp.validDirection) { ApprovedDirection.Add(dir); } for (int i = 0; i < NeighbourWp.Count; i++) { if (ApprovedDirection[i] == direction * -1) { ApprovedDirection.Remove(ApprovedDirection[i]); NeighbourWp.Remove(NeighbourWp [i]); _neighbourTile.Remove(_neighbourTile [i]); } } float lowestDist = 1000000f; for (int i = 0; i < ApprovedDirection.Count; i++) { if (ApprovedDirection[i] != Vector2.zero) { float distance = GetDistanceTotal(_neighbourTile[i].Pos, targetTile.Pos); if (distance < lowestDist) { lowestDist = distance; newDirection = ApprovedDirection [i]; // print (targetTile + " N:" + NeighbourWp [i].EwpID + "direction" + ApprovedDirection[i]); } } } ChangeDirection(); //remove }
void targetWP() { Target = null; for (int i = 100; ;) { setTarget(EnemyWP.getP()); if (TargetNode != null) { break; } if (i-- <= 0) { Debug.LogError(" can't find valid EnemyWP "); break; } } }
void Move() { if (WayPointManager.instance.tile.ContainsKey(currentTile.Pos + direction)) { if (direction != Vector2.zero) //fix if pdist < tdist { isMoving = true; WP nextTile = GetTileFromPos(currentTile.Pos + direction); RotateUnit(direction); if (GetDistanceDirection(nextTile.Pos) < 0.01f) //Debug direction should only be changed at this point { currentTile = nextTile; if (currentTile.GetComponent <EnemyWP> () != null) //has EWP { currentWp = currentTile.GetComponent <EnemyWP>(); //Begin to look for new direction if (WayPointManager.instance.tile.ContainsKey(currentTile.Pos + newDirection)) { Checkdirections(currentWp, getTargetPos()); } } } float newX = direction.x * uStats.BaseSpeed; float newZ = direction.y * uStats.BaseSpeed; transform.position += new Vector3(newX, 0, newZ) * Time.deltaTime; } } else { isMoving = false; Checkdirections(currentWp, getTargetPos()); } }