/// <summary>
 /// Update the Score Text
 /// </summary>
 public void UpdateTurnText(GameStateManager.GameTurn newTurn)
 {
     //Update Turn Text to whatever is in the dictonary
     //for the given turn
     if (turnNamePairs.ContainsKey(newTurn))
     {
         currentTurnText.text = turnNamePairs[newTurn];
     }
 }
示例#2
0
    /// <summary>
    /// End a turn after a number of seconds has passed
    /// </summary>
    /// <param name="turnToEnd">Turn To End (Player or AI)</param>
    /// <param name="time">Time (seconds) to wait</param>
    /// <returns></returns>
    protected IEnumerator EndTurnAfterTime(GameStateManager.GameTurn turnToEnd, float time)
    {
        //Wait for time
        TurnInteractionEnded = true;
        yield return(new WaitForSecondsRealtime(time));

        //End Turn
        EndTurn?.Invoke(turnToEnd);
        TurnInteractionEnded = false;
    }
    /// <summary>
    /// Work out the new state for the AI to be in
    /// </summary>
    /// <returns>New State</returns>
    private void UpdateAI(GameStateManager.GameTurn newTurn)
    {
        //If it is not our turn then return
        if (newTurn != GameStateManager.GameTurn.TURN_AI)
        {
            return;
        }

        //Null Check Player and early out
        if (!playerObject)
        {
            return;
        }

        //Get a new state, based on current conditions
        currentAIState = GetNewAIState();

        //Set the prev target pos to the current pos,
        //so we can check if the target pos hasn't moved
        prevTargetPos = targetPos;

        switch (currentAIState)
        {
        case AI_STATE.AI_STATE_SEEK_TO_PLAYER:
        {
            //Set our target position to be the player
            targetPos = playerObject.transform.position;
            //Update the last known player position, set it to our y pos
            //as neither object moves in the y direction
            lastKnownPlayerPos = new Vector3(targetPos.x, transform.position.y, targetPos.z);
            break;
        }

        case AI_STATE.AI_STATE_SEEK_TO_LAST_POS:
        {
            //Set the target pos to the players
            //last know position
            targetPos = lastKnownPlayerPos;

            break;
        }

        case AI_STATE.AI_STATE_WANDER:
        {
            //Find a point on the map that we should
            //go to, then set it as our target pos
            targetPos = GetWanderTargetPos();
            break;
        }
        }

        //If the target pos is not the same as the previous
        //and it is not the current pos
        //target pos find a route to the target
        if (targetPos != prevTargetPos && targetPos != transform.position)
        {
            lastPathfindResults = pathfinder.GetAStarPath(transform.position, targetPos);
        }

        //Check that we have a path to follow
        if (lastPathfindResults.Count > 0)
        {
            //Get the next pos from the list and keep our y the same
            //because the grid is flat to the x/z plane
            Vector3 nextPos = lastPathfindResults[0].transform.position;
            nextPos.y = transform.position.y;

            //Rotate and move towards next position
            transform.LookAt(nextPos);

            //Check that the tile at the next pos is not occupied by the player
            WalkableTile nextTile = lastPathfindResults[0].GetComponentInChildren <WalkableTile>();
            if (nextTile)
            {
                if (!nextTile.IsOccupied || nextTile.Occupier.GetComponent <PlayerMovement>())
                {
                    //Attempt to reserve the space and only move if we do this succcessfully
                    if (nextTile.AttemptToReserve(this.gameObject))
                    {
                        StartCoroutine(MoveOverTime(nextPos, turnDuration));
                    }
                }
            }

            //Remove the pos from the list so we don't
            //visit it again
            lastPathfindResults.RemoveAt(0);
        }

        //Debug Test for State
        Debug.Log("FOX :: " + currentAIState.ToString());

        //End the turn after turn timer is up - call event
        StartCoroutine(EndTurnAfterTime(GameStateManager.GameTurn.TURN_AI, turnDuration));
    }