// Exits the recovery state and reverts the ghosts to normal. void ExitRecoveryMode() { modeChangeTimer = 0; stateIteration = 1; ChangeMode(modes[stateIteration]); ghostState = AnimationController.State.RECOVERED; frightenedFactor = 1f; }
// Enters the frightened state after a power pill is eaten. public void EnterFrightenedMode() { if (currentMode != Mode.IDLE && currentMode != Mode.RECOVERY) { consumedGhosts = 0; frightenedTimer = 0; ChangeMode(Mode.FRIGHTENED); ghostState = AnimationController.State.FRIGHTENED; frightenedFactor = frightenedSlow; ReverseDirection(); } }
// Move Pacman in the player's current direction. void Move() { if (targetNode != currentNode && targetNode != null) // If the game thinks we have not yet reached our target node. { if (gameBoard.Overshot(previousNode, targetNode, transform.localPosition)) // If we reached our target node. { currentNode = targetNode; // Update the node information. // Handling left/right portals. if (currentNode.GetComponent <Tile>() != null && currentNode.GetComponent <Tile>().isPortal) { this.transform.localPosition = currentNode.GetComponent <Tile>().portalReceiver.transform.position; currentNode = currentNode.GetComponent <Tile>().portalReceiver.GetComponent <Node>(); } else { this.transform.localPosition = currentNode.transform.position; } Node moveToNode = CanMove(nextDirection); // Updates the direction to the desired one if it's valid, or checks if Pacman can keep going if the desired direction isn't valid. if (moveToNode != null) { playerDirection = nextDirection; } else { moveToNode = CanMove(playerDirection); } // Updates node information if there's a node Pacman can go to or else brings him to a halt. if (moveToNode != null) { targetNode = moveToNode; previousNode = currentNode; currentNode = null; } else { playerState = AnimationController.State.STILL; } } else { transform.localPosition += (Vector3)(playerDirection * playerSpeed) * Time.deltaTime * (playerState == AnimationController.State.STILL ? 0.0f : 1.0f); // Move Pacman. } animator.SetAnimatorDirection(playerDirection); animator.SetAnimatorState(playerState); } }
// Restarts Pacman's state. public void Restart() { SetDifficulty(); this.transform.position = startingPosition.transform.position; playerDirection = Vector2.right; nextDirection = Vector2.right; animator.SetAnimatorDirection(playerDirection); playerState = AnimationController.State.ALIVE; animator.SetAnimatorState(playerState); currentNode = startingPosition; ChangePosition(playerDirection); }
// Method for updating the behaviour of each ghost. void ModeUpdate() { if (currentMode != Mode.FRIGHTENED && currentMode != Mode.RECOVERY) // If ghosts are not in modes that interrupt their normal behaviour. { modeChangeTimer += Time.deltaTime; // Increment the behaviour timer. if (stateIteration == 1) // If the ghost has exited idle state. { ghostState = AnimationController.State.MOVING; } if (stateIteration == timers.Length) // If the ghost has entered permanent chase state. { ChangeMode(modes[timers.Length]); } else { if (modeChangeTimer > timers[stateIteration]) // If it's time to change modes. { stateIteration++; modeChangeTimer = 0; ChangeMode(modes[stateIteration]); } } } else if (currentMode == Mode.FRIGHTENED) { frightenedTimer += Time.deltaTime; if (frightenedTimer >= frightenedTime * 0.7 && ghostState == AnimationController.State.FRIGHTENED) { EnterFrightened2Mode(); } if (frightenedTimer >= frightenedTime) { ExitFrightenedMode(); } } else { if (previousNode == ghostHouse.GetComponent <Node>()) { ExitRecoveryMode(); } } }
// Called when the game starts. void Start() { pacMan = GameObject.Find("Pacman"); blinky = GameObject.Find("Blinky"); ghostHouse = GameObject.Find("StartNodePinky"); gameBoard = GameObject.Find("GameBoard").GetComponent <GameBoard>(); animator = this.GetComponent <AnimationController>(); SetDifficulty(); currentMode = Mode.IDLE; ghostState = AnimationController.State.STILL; currentNode = startingPosition; previousNode = currentNode; targetNode = ChooseNextNode(); }
// Called when the game starts. void Start() { // Initialising values. animator = this.GetComponent <AnimationController>(); gameBoard = GameObject.Find("GameBoard").GetComponent <GameBoard>(); ghosts[0] = GameObject.Find("Blinky"); ghosts[1] = GameObject.Find("Pinky"); ghosts[2] = GameObject.Find("Inky"); ghosts[3] = GameObject.Find("Clyde"); SetDifficulty(); playerDirection = Vector2.right; playerState = AnimationController.State.MOVING; currentNode = startingPosition; ChangePosition(playerDirection); }
// Handles player input (arrow keys). void CheckInput() { if (Input.GetKeyDown(KeyCode.RightArrow)) { ChangePosition(Vector2.right); playerState = AnimationController.State.MOVING; } else if (Input.GetKeyDown(KeyCode.UpArrow)) { ChangePosition(Vector2.up); playerState = AnimationController.State.MOVING; } else if (Input.GetKeyDown(KeyCode.LeftArrow)) { ChangePosition(Vector2.left); playerState = AnimationController.State.MOVING; } else if (Input.GetKeyDown(KeyCode.DownArrow)) { ChangePosition(Vector2.down); playerState = AnimationController.State.MOVING; } // Restart. /*else if (Input.GetKeyDown(KeyCode.R)) * { * StartCoroutine(gameBoard.ProcessGameOver(1f)); * }*/ // Cheating! /*else if (Input.GetKeyDown(KeyCode.C)) * { * foreach (GameObject ghost in ghosts) * { * ghost.GetComponent<GhostController>().canMove = !ghost.GetComponent<GhostController>().canMove; * } * * playerSpeed = playerSpeed == 1.5f ? GameBoard.levels[GameBoard.level - 1].GetPacmanSpeed() : 1.5f; * * Lives.AddALife(); * }*/ }
// Resets all ghost variables. public void Restart() { SetDifficulty(); transform.position = startingPosition.transform.position; modeChangeTimer = 0; frightenedTimer = 0; stateIteration = 0; frightenedFactor = 1f; currentMode = Mode.IDLE; previousMode = Mode.IDLE; ghostState = AnimationController.State.RECOVERED; animator.SetAnimatorState(ghostState); ghostState = AnimationController.State.STILL; animator.SetAnimatorState(ghostState); animator.SetAnimatorDirection(Vector2.up); currentNode = startingPosition; previousNode = currentNode; targetNode = ChooseNextNode(); }
// Exits the frightened state and reverts the ghosts to normal. void ExitFrightenedMode() { ChangeMode(previousMode); ghostState = AnimationController.State.RECOVERED; frightenedFactor = 1f; }
// Enters the recovery state (eyes) after being eaten. public void EnterRecoveryMode() { ChangeMode(Mode.RECOVERY); ghostState = AnimationController.State.RECOVERY; frightenedFactor = recoveryFactor; }
// Enter blinking state indicating frightened mode is nearly over. public void EnterFrightened2Mode() { ghostState = AnimationController.State.FRIGHTENED2; }