Exemplo n.º 1
0
    public IEnumerator CopTurn()
    {
        //Notifies the UI Controller to update relevant banner information
        uiController.UpdateTurnUI(false);
        uiController.DisplayTip();
        //Iterates over the cop cars in the level
        for (int i = 0; i < copCars.Count; i++)
        {
            //Select the relevant cop car
            Car copCar = copCars[i];
            //Highlight the squares that the cop can move
            HighlightAvailableSquares(copCar);
            //Set the current camera target
            cameraController.currentTargetCar = copCar;
            //Update banner information for this specific cop
            uiController.UpdateBanner("Cop " + copCar.carName + "'s turn");
            //Declare a coordinate (clone) of the cops current coordinates
            GridCoord currentClone = copCar.gridCoord;
            //Get a dice roll
            int diceRoll = GetDiceRoll();
            //Update the move count on the banner information
            uiController.UpdateMoveCount(diceRoll);
            //Wait for the camera to move to the cop cars position
            yield return(new WaitForSeconds(0.8f));

            //For every move on this dice roll
            for (int m = diceRoll; m > 0; m--)
            {
                //Try 10 times to move
                int remainingTries = 10;
                //Declare a dictionary that will help us find the coordinate that reduces distance
                //to the player the most.
                Dictionary <float, GridCoord> distanceDict = new Dictionary <float, GridCoord>();
                //(All below) Randomly move the coordinate by up to a unit in any direction
                do
                {
                    currentClone = copCar.gridCoord;
                    int dir = Random.Range(0, 4);
                    switch (dir)
                    {
                    case 0:
                        currentClone.x++;
                        break;

                    case 1:
                        currentClone.x--;
                        break;

                    case 2:
                        currentClone.y++;
                        break;

                    case 3:
                        currentClone.y--;
                        break;
                    }
                    //Get the distance to the player if the cop car were to move here
                    float distance = GridCoord.Distance(currentClone, playerCar.gridCoord);
                    //Check invalid grid positions
                    if (currentClone.x < 0 || currentClone.y < 0 || currentClone.x >= gridGenerator.dimensionsX || currentClone.y >= gridGenerator.dimensionsY)
                    {
                        continue;
                    }
                    //Check if the grid is occupied by a building
                    if (gridGenerator.GetSquare(currentClone).squareType != Square.SquareType.ROAD && gridGenerator.GetSquare(currentClone).squareType != Square.SquareType.HIGHWAY_EXIT)
                    {
                        continue;
                    }
                    //Insert the distance and coordinate into the dictionary
                    distanceDict[distance] = currentClone;
                }while(--remainingTries > 0);
                //Find the smallest result
                var       result     = distanceDict.OrderBy(x => x.Key);
                GridCoord lowestDist = result.FirstOrDefault().Value;
                //Check the dictionary result isn't empty
                if (lowestDist != null)
                {
                    Square s = gridGenerator.GetSquare(lowestDist);
                    //Finally! Move to the square
                    if (CanMoveTo(copCar, s))
                    {
                        MoveToSquare(copCar, s);
                    }
                }
                //Update the move banner count
                uiController.UpdateMoveCount(m);
                //Wait for a quarter of a second to let the cop car to move to the position
                yield return(new WaitForSeconds(0.25f));
            }
        }
        //Change to the players turn
        PlayerTurn();
    }