Advertisement
SemlerPDX

GamePhasesBase.cs

Jun 17th, 2025
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. using GameFramework.Data;
  5.  
  6. using UnityEngine;
  7.  
  8. namespace GameFramework.GamePhases
  9. {
  10.     /// <summary>
  11.     /// Base class for game phases in the game framework.
  12.     /// </summary>
  13.     public abstract class GamePhaseBase
  14.     {
  15.         /// <summary>
  16.         /// The game context data for this game phase, containing all necessary information about the game state.
  17.         /// </summary>
  18.         protected GameContextData Context { get; private set; }
  19.  
  20.         /// <summary>
  21.         /// The current phase of the game.
  22.         /// </summary>
  23.         protected GamePhase CurrentPhase { get; private set; }
  24.  
  25.         /// <summary>
  26.         /// The last player who took an action in the game, if applicable.
  27.         /// </summary>
  28.         protected GamePlayer LastPlayer { get; private set; }
  29.  
  30.         /// <summary>
  31.         /// The card data for the optionally selected card in this phase, if applicable.
  32.         /// </summary>
  33.         protected CardData SelectedCard { get; private set; }
  34.  
  35.         /// <summary>
  36.         /// Action to be invoked when the current phase is completed, passing the next phase to transition to<br/>
  37.         /// and the last player who took action, if applicable.
  38.         /// </summary>
  39.         protected Action<GamePhase, GamePlayer> OnPhaseComplete;
  40.  
  41.         /// <summary>
  42.         /// Initializes the game phase with the provided context and current phase, and optional last player<br/>
  43.         /// and/or selected card. Runs the phase logic in the next frame, and when complete, runs the End method.
  44.         /// </summary>
  45.         /// <param name="context">The game context data for this game.</param>
  46.         /// <param name="currentPhase">The current phase of the game.</param>
  47.         /// <param name="onPhaseComplete">The action to complete upon ending this phase.</param>
  48.         /// <param name="lastPlayer">An optional GamePlayer last player variable indicating who took an action in the game.</param>
  49.         /// <param name="selectedCard">An optional CardData object of a selected card.</param>
  50.         public IEnumerator Begin(
  51.             GameContextData context, GamePhase currentPhase, Action<GamePhase, GamePlayer> onPhaseComplete,
  52.             GamePlayer lastPlayer = GamePlayer.None,
  53.             CardData selectedCard = null
  54.         )
  55.         {
  56.             Context = context;
  57.             CurrentPhase = currentPhase;
  58.             OnPhaseComplete = onPhaseComplete;
  59.             LastPlayer = lastPlayer;
  60.             SelectedCard = selectedCard;
  61.  
  62.             // Allow phase to run in the next frame
  63.             yield return RunPhaseLogic();
  64.  
  65.             End(CurrentPhase, LastPlayer);
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Runs the logic for the current game phase. This method should be overridden in derived classes to implement specific phase behavior.
  70.         /// </summary>
  71.         protected abstract IEnumerator RunPhaseLogic();
  72.  
  73.         /// <summary>
  74.         /// Sets the last player that took an action.
  75.         /// </summary>
  76.         /// <param name="player">The last player who took an action.</param>
  77.         public void SetLastPlayer(GamePlayer player)
  78.         {
  79.             LastPlayer = player;
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Sets the current game phase property.
  84.         /// </summary>
  85.         /// <param name="phase">The new game phase to set.</param>
  86.         public void SetPhase(GamePhase phase)
  87.         {
  88.             //Debug.LogWarning($"Setting phase to {phase} from {CurrentPhase}");
  89.             CurrentPhase = phase;
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Attempts to set the active state of a GameObject based on the provided boolean value.<br/>
  94.         /// Does not call SetActive method if current state matches requested <see cref="isActive"/> state.
  95.         /// </summary>
  96.         /// <param name="gameObject">The Unity GameObject to call SetActive upon.</param>
  97.         /// <param name="isActive">The active state to set.</param>
  98.         public void TrySetActiveState(GameObject gameObject, bool isActive)
  99.         {
  100.             if (gameObject.activeSelf != isActive)
  101.             {
  102.                 gameObject.SetActive(isActive);
  103.             }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Ends the current game phase and fires the OnPhaseComplete action supplied in the Begin method.
  108.         /// </summary>
  109.         /// <param name="nextPhase">The next game phase proposed by this phase ending.</param>
  110.         /// <param name="lastPlayer">An optional GamePlayer last player variable indicating who took an action in the game.</param>
  111.         private void End(GamePhase nextPhase, GamePlayer lastPlayer)
  112.         {
  113.             OnPhaseComplete?.Invoke(nextPhase, lastPlayer);
  114.         }
  115.     }
  116. }
  117.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement