Exemple #1
0
    void Update()
    {
        if (_levelStarted)
        {
            if (Input.GetKeyDown(KeyCode.P))
            {
                _gamePause = !_gamePause;
                if (OnGamePause != null)
                {
                    OnGamePause(this, new BoolEventArgs(_gamePause));
                }
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                GameOver();
            }

            if (!_gamePause && !_levelPause)
            {
                if (Input.GetKeyDown(KeyCode.R))
                {
                    PacManController pcm = _personas[0].GetComponent <PacManController>();
                    HandlePlayerDead(pcm);
                }
                UpdateTimers();
                foreach (Persona p in _personas)
                {
                    p.OnUpdate();
                }
            }
        }
    }
    private void doubleRedtoPacPlusTwo() ///Decision making algorithm: that choose the shortest path to the position obtained by doubling the vector from Blinky(red) to PacMan+2units.
    {
        Vector2 target = Vector2.zero;

        PacManController Pac       = GameObject.FindGameObjectWithTag("PacMan").GetComponent <PacManController>(); //we find pacman to later access his position
        Direction        PacFacing = Pac.getFacing();                                                              //get the direction he is facing

        GameObject red = GameObject.Find("Blinky");                                                                //find red/blinky to use his position

        //Now we need to use the vector coordinates of pac and red to find the vector coordinates of the target node
        Vector2 pacPos = Pac.transform.position; //get the coordinates of pacman
        Vector2 redPos = red.transform.position; //get the coordinates of red

        Vector2 pacPosPlusTwo = Vector2.zero;

        //choose how to add +2 to pac's coordinates and assgin it tt pacPosPlusTwo
        if (PacFacing == Direction.Down)
        {
            pacPosPlusTwo = new Vector2(pacPos.x, pacPos.y - 2);
        }
        else if (PacFacing == Direction.Left)
        {
            pacPosPlusTwo = new Vector2(pacPos.x - 2, pacPos.y);
        }
        else if (PacFacing == Direction.Right)
        {
            pacPosPlusTwo = new Vector2(pacPos.x + 2, pacPos.y);
        }
        else //implies that PacFacing == Direction.Up
        {
            pacPosPlusTwo = new Vector2(pacPos.x, pacPos.y + 2);
        }

        //Now use pacPosPlusTwo and redPos to find and assign target

        target = 2 * (pacPosPlusTwo - redPos);  //target is double the vector from red to pac+2 (vector algebra)

        shortestPathTo(targetPosition: target); //now that inky has his target position, just take the shortest path to it.
    }
    private void nAheadOfPacMan()                                                                                  //Decision making algorithm: ghost finds his neighbor node closest to n pills ahead of PacMan as a vector and chooses that node as his next direction
    {
        PacManController Pac       = GameObject.FindGameObjectWithTag("PacMan").GetComponent <PacManController>(); //we find pacman to later access his position
        Direction        PacFacing = Pac.getFacing();                                                              //get the direction he is facing

        if (getNodeAtPosition(transform.position) != null)                                                         //run only if on a node
        {
            float   minDistance     = 9999;                                                                        //initialize minDistance to a random big value that's greater than any ghost-pacman distance possible
            Vector2 tempDirection   = Vector2.zero;                                                                //initialize the direction vector the ghost will take
            Node    currentPosition = getNodeAtPosition(transform.position);                                       //get current position to then find my neighbors
            Node[]  myNeighbors     = currentPosition.neighbors;                                                   //get my neighbors, store them in an array of nodes called myNeighbors

            Vector2 PacNAheadPosition = Vector2.zero;

            for (int i = 0; i < myNeighbors.Length; i++)             //iterate over the neighbors to find the shortest one to pacman
            {
                if (direction * (-1) == currentPosition.validDir[i]) //Mate must document
                {
                    continue;
                }

                Node neighborNode = myNeighbors[i];

                Vector2 nodePos = neighborNode.transform.position; //get the coordinates of the node
                Vector2 pacPos  = Pac.transform.position;          //get the coordinates of pacman

                //if statement to choose the right n pills ahead position depending on which direction pacman is going
                //can be rewritten using a switch statement
                //this is the most significant difference with shortestPathToPacMan()
                if (PacFacing == Direction.Down)
                {
                    PacNAheadPosition = new Vector2(pacPos.x, pacPos.y - nAhead);
                }
                else if (PacFacing == Direction.Left)
                {
                    PacNAheadPosition = new Vector2(pacPos.x - nAhead, pacPos.y);
                }
                else if (PacFacing == Direction.Right)
                {
                    PacNAheadPosition = new Vector2(pacPos.x + nAhead, pacPos.y);
                }
                else //implies that PacFacing == Direction.Up
                {
                    PacNAheadPosition = new Vector2(pacPos.x, pacPos.y + nAhead);
                }

                //now that we have the right PacFourAheadPosition we want, we can find its distance to the ghost...

                float tempDistance = (PacNAheadPosition - nodePos).sqrMagnitude; //distance from n pills ahead of pacman to the node we are currently iterating over

                if (tempDistance < minDistance)                                  //if the vector distance between the neighbor is the min, set Ghost to go towards that Node
                {
                    //Access the valid directions of the node we are currently on.
                    minDistance   = tempDistance;
                    tempDirection = currentPosition.validDir[i];
                }
            }
            //ghost chooses to go to the position of tempDirection store after the for-loop
            ChangePosition(tempDirection); //similar to randomInput() and shortestPathToPacMan()
        }
    }
 //used to crreate an instance of pacman
 private void Awake()
 {
     instPc = this;
 }