SemlerPDX icon

GamePhasesBase.cs

SemlerPDX | PRO | 06/17/25 07:02:16 PM UTC | 0 ⭐ | 13531 👁️ | Never ⏰ | [Unity]
C# |

4.72 KB

|

Gaming

|

0 👍

/

0 👎

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

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎