// For some reason tile coordinates do not round down immediately, // it takes 1-2 frames to round down. public void UpdatePosition() { tile = new Vector2(Mathf.Floor(transform.position.x), Mathf.Floor(Math.Abs(transform.position.y))); // Current Center Tile. Debug.DrawLine(new Vector3(tile.x, -1 * tile.y), new Vector3(tile.x + 1, -1 * tile.y), new Color(130, 0, 255, 255), 0.0f, false); Debug.DrawLine(new Vector3(tile.x, -1 * tile.y - 1), new Vector3(tile.x + 1, -1 * tile.y - 1), new Color(130, 0, 255, 255), 0.0f, false); Debug.DrawLine(new Vector3(tile.x, -1 * tile.y), new Vector3(tile.x, -1 * tile.y - 1), new Color(130, 0, 255, 255), 0.0f, false); Debug.DrawLine(new Vector3(tile.x + 1, -1 * tile.y), new Vector3(tile.x + 1, -1 * tile.y - 1), new Color(130, 0, 255, 255), 0.0f, false); if (tile.x + input.x > 0 && tile.x + input.x < gameController.pacTiles.GetLength(0) - 1 && tile.y - input.y > 0 && tile.y + input.y < gameController.pacTiles.GetLength(1) - 1) { PacTile targetTile = gameController.GetPacTile(new Vector2(tile.x + input.x, tile.y - input.y)); } }
protected Directions FindNextDirection(Vector2 startPosition, Directions direction, Modes mode) { var targetedTile = new Vector2(); var nextPosition = new Vector2(); var directions = new sbyte[4, 2] { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } }; var allowedDirections = new List <Directions> { }; var newDirection = new Directions(); double lowestEuclidean = -1; // Set the new targeted tile according to the current mode. switch (mode) { case (Modes.Chase): targetedTile = targetTile; break; case (Modes.Scatter): targetedTile = scatterTile; break; case (Modes.Frightened): break; case (Modes.Respawning): targetedTile = respawnTile; break; } // Set the new square to move to. switch (currentDirection) { case (Directions.Up): if (isFirstMove) { nextPosition = new Vector2(startPosition.x, startPosition.y + 0.5f); } else { nextPosition = new Vector2(startPosition.x, startPosition.y + 1f); } break; case (Directions.Left): if (isFirstMove) { nextPosition = new Vector2(startPosition.x - 0.5f, startPosition.y); } else { nextPosition = new Vector2(startPosition.x - 1f, startPosition.y); } break; case (Directions.Down): if (isFirstMove) { nextPosition = new Vector2(startPosition.x, startPosition.y - 0.5f); } else { nextPosition = new Vector2(startPosition.x, startPosition.y - 1f); } break; case (Directions.Right): if (isFirstMove) { nextPosition = new Vector2(startPosition.x + 0.5f, startPosition.y); } else { nextPosition = new Vector2(startPosition.x + 1f, startPosition.y); } break; } // Special check for warp zones if ((tile.x <= 0 && currentDirection == Directions.Left) || (tile.x >= gameController.pacTiles.GetLength(0) - 2 && currentDirection == Directions.Right) || (tile.y <= 0 && currentDirection == Directions.Up) || (tile.y >= gameController.pacTiles.GetLength(1) - 2 && currentDirection == Directions.Down)) { isWarping = true; return(currentDirection); } // If the ghost is out of the map, continue on the same direction if (tile.x < 0 || tile.x >= gameController.pacTiles.GetLength(0) || tile.y < 0 || tile.y >= gameController.pacTiles.GetLength(1)) { return(currentDirection); } // Check if the endPosition Tile is an intersection. if (gameController.GetPacTile((int)nextPosition.x, (int)Math.Abs(nextPosition.y)).allowedDirections.Count > 0) { // If the ghost is currently Frightened, // remove the inverted direction and randomize. if (mode == Modes.Frightened) { var tempAllowedDirections = new List <Directions> { }; Directions invertedDirection = Directions.Up; Directions frightDirection = Directions.Up; tempAllowedDirections = gameController.GetPacTile((int)nextPosition.x, (int)Math.Abs(nextPosition.y)).allowedDirections; switch (direction) { case (Directions.Up): invertedDirection = Directions.Down; break; case (Directions.Left): invertedDirection = Directions.Right; break; case (Directions.Down): invertedDirection = Directions.Up; break; case (Directions.Right): invertedDirection = Directions.Left; break; } frightDirection = invertedDirection; while (frightDirection == invertedDirection) { frightDirection = tempAllowedDirections[UnityEngine.Random.Range(0, tempAllowedDirections.Count)]; } return(frightDirection); } allowedDirections = gameController.GetPacTile((int)nextPosition.x, (int)Math.Abs(nextPosition.y)).allowedDirections; // Check the lowest Euclidean on all allowed Directions // We start from the end so that the priority becomes Up, Left, Down, Right for (int i = allowedDirections.Count - 1; i >= 0; i--) { var tempPosition = new Vector2(); switch (allowedDirections[i]) { case (Directions.Up): // If the current direction is Up, it can't go Down. if (currentDirection != Directions.Down) { tempPosition = new Vector2(nextPosition.x, nextPosition.y + 1); if (Euclidean(tempPosition, targetedTile) <= lowestEuclidean || lowestEuclidean == -1) { lowestEuclidean = Euclidean(tempPosition, targetedTile); newDirection = Directions.Up; } } break; case (Directions.Left): // If the current direction is Right, it can't go Left. if (currentDirection != Directions.Right) { tempPosition = new Vector2(nextPosition.x - 1, nextPosition.y); if (Euclidean(tempPosition, targetedTile) <= lowestEuclidean || lowestEuclidean == -1) { lowestEuclidean = Euclidean(tempPosition, targetedTile); newDirection = Directions.Left; } } break; case (Directions.Down): // If the current direction is Up, it can't go Down. if (currentDirection != Directions.Up) { tempPosition = new Vector2(nextPosition.x, nextPosition.y - 1); if (Euclidean(tempPosition, targetedTile) <= lowestEuclidean || lowestEuclidean == -1) { lowestEuclidean = Euclidean(tempPosition, targetedTile); newDirection = Directions.Down; } } break; case (Directions.Right): // If the current direction is Left, it can't go Right. if (currentDirection != Directions.Left) { tempPosition = new Vector2(nextPosition.x + 1, nextPosition.y); if (Euclidean(tempPosition, targetedTile) <= lowestEuclidean || lowestEuclidean == -1) { lowestEuclidean = Euclidean(tempPosition, targetedTile); newDirection = Directions.Right; } } break; } /*if (DebugPath) * { * print(lowestEuclidean + " | " + tempPosition + " | " + allowedDirections[i] + " | " + Euclidean(tempPosition, targetedTile)); * print("Direction Chosen: " + nextDirection + " INDEX: " + i + " COUNT: " + (allowedDirections.Count - 1)); * }*/ } } else { // If not, follow the only possible path (Without going backwards). for (int i = 0; i < directions.GetLength(0); i++) { var tempPosition = new Vector2(nextPosition.x + directions[i, 0], nextPosition.y + directions[i, 1]); PacTile tempTile = gameController.GetPacTile((int)tempPosition.x, (int)(-1 * tempPosition.y)); switch (VectorToDirection(nextPosition, tempPosition)) { case (Directions.Up): if (tempTile.cost != 0 && currentDirection != Directions.Down) { newDirection = Directions.Up; } break; case (Directions.Left): if (tempTile.cost != 0 && currentDirection != Directions.Right) { newDirection = Directions.Left; } break; case (Directions.Down): if (tempTile.cost != 0 && tempTile.cost != 2 && currentDirection != Directions.Up) { newDirection = Directions.Down; } break; case (Directions.Right): if (tempTile.cost != 0 && currentDirection != Directions.Left) { newDirection = Directions.Right; } break; } } } return(newDirection); }
// Update is called once per frame void Update() { if (tile == null) { tile = gameObject.GetComponent<PacTile>(); } if (tile.allowedDirections.Count > 0) { Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); } for (int i = 0; i < tile.allowedDirections.Count; i++) { switch(tile.allowedDirections[i]) { case(GhostAI.Directions.Up): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y + 0.5f), Color.red, 0.1f); break; case(GhostAI.Directions.Left): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x - 0.5f, gameObject.transform.position.y - 0.5f), Color.red); break; case(GhostAI.Directions.Down): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 1.5f), Color.red, 0.1f); break; case(GhostAI.Directions.Right): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 1.5f, gameObject.transform.position.y - 0.5f), Color.red); break; } } /*if (tile.isIntersection) { Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); if (tile.allowUpwards) { Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y + 0.5f), Color.red, 0.1f); } if (tile.allowBottom) { Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 1.5f), Color.red, 0.1f); } if (tile.allowLeft) { Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x - 0.5f, gameObject.transform.position.y - 0.5f), Color.red); } if (tile.allowRight) { Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 1.5f, gameObject.transform.position.y - 0.5f), Color.red); } }*/ }
public void AddPacTile(int x, int y, PacTile pacTile) { pacTiles[x, y] = pacTile; }
// Update is called once per frame void Update() { if (tile == null) { tile = gameObject.GetComponent <PacTile>(); } if (tile.allowedDirections.Count > 0) { Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), Color.green, 0.1f); Debug.DrawLine(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); } for (int i = 0; i < tile.allowedDirections.Count; i++) { switch (tile.allowedDirections[i]) { case (GhostAI.Directions.Up): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y + 0.5f), Color.red, 0.1f); break; case (GhostAI.Directions.Left): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x - 0.5f, gameObject.transform.position.y - 0.5f), Color.red); break; case (GhostAI.Directions.Down): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 1.5f), Color.red, 0.1f); break; case (GhostAI.Directions.Right): Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 1.5f, gameObject.transform.position.y - 0.5f), Color.red); break; } } /*if (tile.isIntersection) * { * Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), Color.green, 0.1f); * Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); * Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 1), Color.green, 0.1f); * Debug.DrawLine(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y), new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y - 1), Color.green, 0.1f); * * if (tile.allowUpwards) * { * Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y + 0.5f), Color.red, 0.1f); * } * * if (tile.allowBottom) * { * Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 1.5f), Color.red, 0.1f); * } * * if (tile.allowLeft) * { * Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x - 0.5f, gameObject.transform.position.y - 0.5f), Color.red); * } * * if (tile.allowRight) * { * Debug.DrawLine(new Vector3(gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f), new Vector3(gameObject.transform.position.x + 1.5f, gameObject.transform.position.y - 0.5f), Color.red); * } * }*/ }