public static void GoToNewPosition(GameObject obj) { float yAngle = obj.transform.rotation.eulerAngles.y; Vector3 newPosition; Vector3 currentPosition = obj.transform.position; if (TankUtils.FloatsAreEqual(yAngle, Constants.AngleUp, Constants.MaxDifferenceForAngles)) { newPosition = new Vector3(currentPosition.x - Constants.MoveLength, currentPosition.y, currentPosition.z); } else if (TankUtils.FloatsAreEqual(yAngle, Constants.AngleDown, Constants.MaxDifferenceForAngles)) { newPosition = new Vector3(currentPosition.x + Constants.MoveLength, currentPosition.y, currentPosition.z); } else if (TankUtils.FloatsAreEqual(yAngle, Constants.AngleLeft, Constants.MaxDifferenceForAngles)) { newPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z - Constants.MoveLength); } else { newPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z + Constants.MoveLength); } obj.transform.position = newPosition; }
void UpdatePosition() { CheckForPlayerOnTheWay(); CheckForCastleOnTheWay(); int xCoordinate = (int)Math.Round(transform.position.x); int zCoordinate = (int)Math.Round(transform.position.z); if (TankUtils.IsCenterOfField(gameObject, xCoordinate, zCoordinate)) { currentIndex++; if (currentIndex == path.Count) { bool stayInPlace = ArrivedToDestination(); if (stayInPlace) { return; } } Vector3 nextRotation = TankUtils.FindNextRotation(gameObject, path[currentIndex]); nextRotation.y -= transform.rotation.eulerAngles.y; transform.Rotate(nextRotation); } TankUtils.GoToNewPosition(gameObject); Invoke("UpdatePosition", Constants.UpdateMethodDelay); }
void UpdatePosition() { CheckEdgePositions(); CheckForPlayerOnTheWay(); CheckForCastleOnTheWay(); int xCoordinate = (int)Math.Round(transform.position.x); int zCoordinate = (int)Math.Round(transform.position.z); if (TankUtils.IsCenterOfField(gameObject, xCoordinate, zCoordinate)) { stepCounter--; Vector2 nextField = FindNextField(); if (nextField.Equals(Vector2.negativeInfinity) || mapManagerScript.obstacleMatrix[(int)nextField.x, (int)nextField.y]) { RotateToFreeNeighbour(xCoordinate, zCoordinate); if (stepCounter == 0) { stepCounter = 5; } } else if (stepCounter == 0) { stepCounter = 5; RotateToFreeNeighbour(xCoordinate, zCoordinate); } } TankUtils.GoToNewPosition(gameObject); }
Vector2 FindNextField() { Vector2 nextField = Vector2.negativeInfinity; float yAngle = transform.rotation.eulerAngles.y; int xCoordinate = (int)Math.Round(transform.position.x); int zCoordinate = (int)Math.Round(transform.position.z); int row = (xCoordinate - 1) / 2; int column = (zCoordinate - 1) / 2; if (row > 0 && TankUtils.FloatsAreEqual(yAngle, Constants.AngleUp, Constants.MaxDifferenceForAngles)) { nextField = new Vector2(row - 1, column); } else if (row < 19 && TankUtils.FloatsAreEqual(yAngle, Constants.AngleDown, Constants.MaxDifferenceForAngles)) { nextField = new Vector2(row + 1, column); } else if (column > 0 && TankUtils.FloatsAreEqual(yAngle, Constants.AngleLeft, Constants.MaxDifferenceForAngles)) { nextField = new Vector2(row, column - 1); } else if (column < 19 && TankUtils.FloatsAreEqual(yAngle, Constants.AngleRight, Constants.MaxDifferenceForAngles)) { nextField = new Vector2(row, column + 1); } return(nextField); }
void SpawnRocket() { if (Time.time >= timeForSpawn) { timeForSpawn = Time.time + spawnDelay; TankUtils.SpawnRocket(gameObject); } }
void RotateToFreeNeighbour(int xCoordinate, int zCoordinate) { stepCounter = 5; List <Vector2> freeFields = FindFreeNeighbourFields((xCoordinate - 1) / 2, (zCoordinate - 1) / 2); if (freeFields.Count > 0) { int randomIndex = GetRandom(freeFields.Count - 1); Vector3 nextRotation = TankUtils.FindNextRotation(gameObject, freeFields[randomIndex]); nextRotation.y -= transform.rotation.eulerAngles.y; transform.Rotate(nextRotation); } else { Debug.Log("Nema slobodnog komsije!"); transform.Rotate(new Vector3(0, Constants.AngleLeft, 0)); return; } }
void RotateToCastle() { Vector2 castleField; int currentRow = ((int)transform.position.x - 1) / 2; int currentColumn = ((int)transform.position.z - 1) / 2; if (currentRow == Constants.CastleMinRow) { castleField = new Vector2(currentRow + 1, currentColumn); } else if (currentColumn == Constants.CastleMinColumn) { castleField = new Vector2(currentRow, currentColumn + 1); } else { castleField = new Vector2(currentRow, currentColumn - 1); } Vector3 nextRotation = TankUtils.FindNextRotation(gameObject, castleField); nextRotation.y -= transform.rotation.eulerAngles.y; transform.Rotate(nextRotation); }
List <Vector2> FindFreeNeighbourFields(int row, int column) { List <Vector2> fields = new List <Vector2>(); if (column - 1 >= 0 && !mapManagerScript.obstacleMatrix[row, column - 1] && !TankUtils.FloatsAreEqual(transform.rotation.eulerAngles.y, 360, Constants.MaxDifferenceForAngles) && !TankUtils.FloatsAreEqual(transform.rotation.eulerAngles.y, 0, Constants.MaxDifferenceForAngles)) { fields.Add(new Vector2(row, column - 1)); } if (column + 1 < Constants.NumOfColums && !mapManagerScript.obstacleMatrix[row, column + 1] && !TankUtils.FloatsAreEqual(transform.rotation.eulerAngles.y, 180, Constants.MaxDifferenceForAngles)) { fields.Add(new Vector2(row, column + 1)); } if (row - 1 >= 0 && !mapManagerScript.obstacleMatrix[row - 1, column] && !TankUtils.FloatsAreEqual(transform.rotation.eulerAngles.y, 90, Constants.MaxDifferenceForAngles)) { fields.Add(new Vector2(row - 1, column)); } if (row + 1 < Constants.NumOfRows && !mapManagerScript.obstacleMatrix[row + 1, column] && !TankUtils.FloatsAreEqual(transform.rotation.eulerAngles.y, 270, Constants.MaxDifferenceForAngles)) { fields.Add(new Vector2(row + 1, column - 1)); } return(fields); }