/// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Samples.SceneManagers { using Opsive.BehaviorDesigner.Runtime; using UnityEngine; /// /// Manually ticks the behavior tree when it is the agent's turn. /// public class TurnBasedSceneManager : MonoBehaviour { [Tooltip("The player GameObject.")] [SerializeField] protected GameObject m_Player; [Tooltip("The agents against the player.")] [SerializeField] protected GameObject[] m_Agents; [Tooltip("Specifies the amount of time to wait in between turns.")] [SerializeField] protected float m_TurnDelay = 3; [Tooltip("The player instructions.")] [SerializeField] protected GameObject m_InstructionsUI; public GameObject Player { get => m_Player; } public GameObject[] Agents { get => m_Agents; } private bool m_PlayersTurn = false; private bool m_CanAttack = false; private BehaviorTree m_BehaviorTree; private TurnBaseCharacterController m_PlayerController; /// /// Initializes the default values. /// public void Awake() { m_BehaviorTree = GetComponent(); m_PlayerController = m_Player.GetComponent(); enabled = false; // Enable when it is the agent's turn. StartTurn(); } /// /// Alternates the turn between the player and the agent. /// public void StartTurn() { m_PlayersTurn = !m_PlayersTurn; if (m_PlayersTurn) { m_InstructionsUI.SetActive(true); m_CanAttack = true; } else { enabled = true; } } /// /// The specified character has been selected by the player. /// /// The selected character. public void SelectedCharacter(GameObject character) { // Wait for the player's turn. if (!m_PlayersTurn || !m_CanAttack) { return; } // The player can't shoot themself. if (character == m_Player) { return; } m_PlayerController.Attack(character); m_CanAttack = false; Invoke("StartTurn", m_TurnDelay); m_InstructionsUI.SetActive(false); } /// /// Ticks the behavior tree. /// public void Update() { m_BehaviorTree.Tick(); } /// /// The AI is done with their turn. /// public void EndAgentTurn() { enabled = false; Invoke("StartTurn", m_TurnDelay); } } }