/// <summary> /// Sets a concrete phase of the game /// </summary> /// <param name="phase">Phase instance</param> private void SetPhase(IGamePhase phase) { if (phase == null) { throw new ArgumentNullException(nameof(phase)); } this._currentGamePhase?.EndPhase(); var previousPhase = this._currentGamePhase; OnDebuggingMessage("Now moving to " + phase.Type); this._currentGamePhase = phase; //there was no phase previously - we are just initializing if (previousPhase != null) { GamePhaseChanged?.Invoke(this, new GamePhaseChangedEventArgs(previousPhase, this._currentGamePhase)); } //inform agents about new phase and provide them access foreach (var player in this.Players) { player.Agent.GamePhaseChanged(phase.Type); } //start the new phase this._currentGamePhase.StartPhase(); GamePhaseStarted?.Invoke(this, this._currentGamePhase); }
// thread-safe, lock-free, performs roll-back on to_factory failure. // Intent: I dont want to call the factory before I can successfully assign it. // (I could have used a lock, but I didn't...) private bool TryTransition(GamePhase from, IGamePhase via, Func <IGamePhase> to_factory) { // null-checks + transition direction check + via.State is an even (i.e. is transitional) check. Debug.Assert(via != null && to_factory != null && (via.Phase - 1 == from) && ((int)via.Phase & 1) == 0); var old = _phase; if (old.Phase == from && ReferenceEquals(old, Interlocked.CompareExchange(ref _phase, via, old))) { // we have successfully entered the transition state (i.e. via)! // where we have succeeded all others shall fail! // ...which is a fancy way of saying only one thread can be in this block at a time. // simply put: for as long as _game is in the via state, we have "lock". try { //var data = old.FinalLockDown(); // TODO <----------------------------------- var next = to_factory(); if (next != null && (via.Phase + 1 == next.Phase)) { _phase = next; // success, and "lock" released. return(true); } return(false); // (false will be returned *after* finally has executed.) } finally { if (ReferenceEquals(via, _phase)) // we failed to transition to next? { _phase = old; // performing roll-back, and "lock" released. } } } return(false); // some other thread beat us to the punch. }
public void Initialize() { m_killedCount = 0; m_bonusPower = 1; m_limitTime = 60f; m_currentPhase = new IntroductionPhase(); m_currentPhase.OnEnter(this); }
private async void Assistant_Controller_GamePhaseStarted(object sender, IGamePhase e) { if (e.Type == GamePhaseType.LifeDeathDetermination) { if (ProvidesFinalEvaluation && !_isOnlineGame) { var deads = await GetDeadPositions(); foreach (var dead in deads) { _uiConnector.RequestLifeDeathKillGroup(dead); } } } }
private void ChangePhase(IGamePhase newPhase) { if (newPhase == null) { Debug.Log("newState is Null"); return; } if (currentPhase != null) { //only exit the current phase if it exists currentPhase.Exit(); } //now we can change the phase to the new phase, we exitted that old one, and now we enter the new one currentPhase = newPhase; currentPhase.Enter(); nextPhase = null; }
internal TransitionPhase(GamePhase newPhase, IGamePhase oldPhase) { _newPhase = newPhase; _oldPhase = oldPhase; }
private void EndLifeAndDeathPhase(IGamePhase phase) { ILifeAndDeathPhase lifeAndDeath = (ILifeAndDeathPhase)phase; lifeAndDeath.LifeDeathTerritoryChanged -= LifeDeath_TerritoryChanged; }
public void PhaseTransition(IGamePhase arg_nextPhase) { m_currentPhase.OnExit(this); m_currentPhase = arg_nextPhase; m_currentPhase.OnEnter(this); }
/// <summary> /// Creates a change of game phases /// </summary> /// <param name="previousPhase">Previously set phase</param> /// <param name="newPhase">New phase</param> public GamePhaseChangedEventArgs(IGamePhase previousPhase, IGamePhase newPhase) { PreviousPhase = previousPhase; NewPhase = newPhase; }