using System;
using System.Collections;
using GameFramework.Data;
using UnityEngine;
namespace GameFramework.GamePhases
{
///
/// Base class for game phases in the game framework.
///
public abstract class GamePhaseBase
{
///
/// The game context data for this game phase, containing all necessary information about the game state.
///
protected GameContextData Context { get; private set; }
///
/// The current phase of the game.
///
protected GamePhase CurrentPhase { get; private set; }
///
/// The last player who took an action in the game, if applicable.
///
protected GamePlayer LastPlayer { get; private set; }
///
/// The card data for the optionally selected card in this phase, if applicable.
///
protected CardData SelectedCard { get; private set; }
///
/// Action to be invoked when the current phase is completed, passing the next phase to transition to
/// and the last player who took action, if applicable.
///
protected Action OnPhaseComplete;
///
/// Initializes the game phase with the provided context and current phase, and optional last player
/// and/or selected card. Runs the phase logic in the next frame, and when complete, runs the End method.
///
/// The game context data for this game.
/// The current phase of the game.
/// The action to complete upon ending this phase.
/// An optional GamePlayer last player variable indicating who took an action in the game.
/// An optional CardData object of a selected card.
public IEnumerator Begin(
GameContextData context, GamePhase currentPhase, Action onPhaseComplete,
GamePlayer lastPlayer = GamePlayer.None,
CardData selectedCard = null
)
{
Context = context;
CurrentPhase = currentPhase;
OnPhaseComplete = onPhaseComplete;
LastPlayer = lastPlayer;
SelectedCard = selectedCard;
// Allow phase to run in the next frame
yield return RunPhaseLogic();
End(CurrentPhase, LastPlayer);
}
///
/// Runs the logic for the current game phase. This method should be overridden in derived classes to implement specific phase behavior.
///
protected abstract IEnumerator RunPhaseLogic();
///
/// Sets the last player that took an action.
///
/// The last player who took an action.
public void SetLastPlayer(GamePlayer player)
{
LastPlayer = player;
}
///
/// Sets the current game phase property.
///
/// The new game phase to set.
public void SetPhase(GamePhase phase)
{
//Debug.LogWarning($"Setting phase to {phase} from {CurrentPhase}");
CurrentPhase = phase;
}
///
/// Attempts to set the active state of a GameObject based on the provided boolean value.
/// Does not call SetActive method if current state matches requested state.
///
/// The Unity GameObject to call SetActive upon.
/// The active state to set.
public void TrySetActiveState(GameObject gameObject, bool isActive)
{
if (gameObject.activeSelf != isActive)
{
gameObject.SetActive(isActive);
}
}
///
/// Ends the current game phase and fires the OnPhaseComplete action supplied in the Begin method.
///
/// The next game phase proposed by this phase ending.
/// An optional GamePlayer last player variable indicating who took an action in the game.
private void End(GamePhase nextPhase, GamePlayer lastPlayer)
{
OnPhaseComplete?.Invoke(nextPhase, lastPlayer);
}
}
}