private void InitiatePlayerHits(PlayerBehaviour player)
 {
     StartCoroutine(Util.WaitWithDelegate(1, () =>
     {
         var players    = Global.PlayersHit.Count > 0 ? Global.PlayersHit.Count(x => x.State != PlayerState.IsDead) : 0;
         var playersHit = _players.Where(x => x.PlayerBeenHit && x.State != PlayerState.IsDead).ToArray();
         if (playersHit.Count() != players || playersHit.Any(x => !x.FallDamageCalculationComplete()))
         {
             Debug.Log(playersHit.Count() + " and " + players);
             InitiatePlayerHits(player);
             return;
         }
         SetPlayerHits(player);
     }));
 }
        private void SetNextActivePlayer(int currentPlayerId = 0)
        {
            CameraLock();
            this._playersToTrigger = new List <PlayerBehaviour>();
            Global.PlayersHit      = new List <PlayerBehaviour>();
            currentPlayerId        = currentPlayerId > 0 ? currentPlayerId : this._currentActivePlayer.Id;
            var rearrangeList = this._players.Where(x => x.Id > currentPlayerId).OrderBy(x => x.Id).ToList();

            rearrangeList.AddRange(this._players.Where(x => x.Id <= currentPlayerId).OrderBy(x => x.Id));

            bool firstIteration = true;

            foreach (PlayerBehaviour player in rearrangeList)
            {
                if (player.State == PlayerState.IsDead)
                {
                    continue;
                }

                if (firstIteration)
                {
                    if (!Global.IsGameOver)
                    {
                        DisplayMessage("Your turn, " + player.Name + "!", 2);
                    }

                    this._currentActivePlayer = player;
                    Global.CurrentPlayerId    = player.Id;
                    Global.CurrentPlayer      = player;
                    player.ChangeState(PlayerState.IsActive);
                    player.SetMovementAllowance(true);
                    player.SetPlayerBar(false);

                    CameraBehaviour.SetObjectToTrack(player.gameObject);
                    CameraBehaviour.SetTarget(player.gameObject.transform.position);

                    firstIteration = false;
                }
                else
                {
                    player.State = PlayerState.Idle;
                    player.SetMovementAllowance(false);
                    player.SetPlayerBar(true);
                }

                player.PlayerBeenHit = false;
            }
        }
        private void EndActivePlayerTurn(PlayerBehaviour player)
        {
            CameraBehaviour.SetTarget(player.transform.position);

            foreach (var obj in this._players.Where(x => x.Id != player.Id))
            {
                obj.SetMovementAllowance(false);
            }

            CameraLock();
            player.AllowMovement = true;

            DisplayMessage("Your turn is ending in 3 seconds.", 2);
            SetRoundTime(4);
            SetTimerCoroutine(4, () =>
            {
                if (player.State == PlayerState.IsEndingTurn)
                {
                    player.ChangeState(PlayerState.IsDone);
                }
            });
        }
        public void ChangePlayerState(PlayerState state, PlayerBehaviour player, bool internalStateChange = false)
        {
            this._actionToRun = null;


            if (!internalStateChange)
            {
                if (this._timingCoroutine != null)
                {
                    StopCoroutine(this._timingCoroutine);
                }
            }

            switch (state)
            {
            case PlayerState.IsActive:
                if (!internalStateChange)
                {
                    SetTimerCoroutine(Global.CurrentRoundTime, () =>
                    {
                        if (player.State == PlayerState.IsActive || player.State == PlayerState.IsUsingUtility)
                        {
                            player.ChangeState(PlayerState.IsDone);
                        }
                    });
                }
                break;

            case PlayerState.IsUsingUtility:
                if (player.HasUsedUtility)
                {
                    DisplayMessage("You have already used a utility in this round.", 2);
                }
                player.ChangeState(PlayerState.IsActive, true, true);
                break;

            case PlayerState.IsFiring:
                CameraBehaviour.Locked = true;
                UiController.TimerController.Freeze = true;

                foreach (var obj in this._players.Where(x => x.Id != player.Id))
                {
                    obj.SetMovementAllowance(true);
                }

                this._actionToRun = () =>
                {
                    if (Global.TrackWeaponPosition)
                    {
                        CameraBehaviour.SetTarget(Global.NextProjectileCollision);
                    }
                    else if (Global.TrackProjectilePosition && player.WeaponController.ProjectileToTrack != null)
                    {
                        CameraBehaviour.SetTarget(player.WeaponController.ProjectileToTrack.transform.position);
                    }
                };
                break;

            case PlayerState.IsFiringWithTrigger:
                UiController.TimerController.Freeze = true;

                foreach (var obj in this._players.Where(x => x.Id != player.Id))
                {
                    obj.SetMovementAllowance(true);
                }
                break;

            case PlayerState.IsEndingTurn:
                InitiatePlayerHits(player);
                break;

            case PlayerState.HasTriggeredHealthLoss:
                this._playersToTrigger.Remove(player);
                player.PlayerBeenHit = false;

                if (player.Id != _currentActivePlayer.Id)
                {
                    player.ChangeState(PlayerState.Idle);
                }

                if (this._playersToTrigger.Count > 0)
                {
                    this._playersToTrigger.First().ChangeState(PlayerState.HasLostHealth);
                }
                else
                {
                    if (player.Id == _currentActivePlayer.Id)
                    {
                        player.ChangeState(PlayerState.IsDone);
                        break;
                    }

                    EndActivePlayerTurn(_currentActivePlayer);
                }
                break;

            case PlayerState.IsDone:
                NextRound();
                break;

            case PlayerState.IsDead:
                DisplayMessage(player.Name + " has been killed.", 2);

                if (_players.Count(x => x.State == PlayerState.IsDead) >= (this._players.Count) - 1)
                {
                    NextRound();
                    Global.IsGameOver = true;
                    break;
                }

                if (this._playersToTrigger.Contains(player))
                {
                    this._playersToTrigger.Remove(player);
                    player.PlayerBeenHit = false;
                    player.SetMovementAllowance(false);

                    if (this._playersToTrigger.Count > 0)
                    {
                        this._playersToTrigger.First().ChangeState(PlayerState.HasLostHealth);
                    }
                    else
                    {
                        if (player.Id == _currentActivePlayer.Id)
                        {
                            NextRound();
                            break;
                        }

                        EndActivePlayerTurn(_currentActivePlayer);
                    }
                }

                if (player.Id == this._currentActivePlayer.Id)
                {
                    player.PlayerBeenHit = false;
                    player.SetMovementAllowance(false);
                    NextRound();
                }
                break;
            }
        }