Ejemplo n.º 1
0
        private void OnEnable()
        {
            _enemy.enabled        = true;
            _room                 = new LocalRoom(PlayerId, EnemyId);
            _room.State.OnChange += changes =>
            {
                foreach (var change in changes)
                {
                    switch (change.Field)
                    {
                    case RoomState.Phase:
                        var phase = (string)change.Value;
                        GamePhaseChanged?.Invoke(phase);
                        if (phase.Equals(RoomPhase.Result))
                        {
                            _isMatchFinished = true;
                        }
                        break;

                    case RoomState.PlayerTurn:
                        if (!_isMatchFinished && EnemyId.Equals((string)change.Value))
                        {
                            StartCoroutine(_enemy.GetShots(cells => _room.Turn(EnemyId, cells)));
                        }
                        break;
                    }
                }
            };
            _room.State.players[PlayerId].ships.OnChange += (turn, part) => _enemy.UpdatePlayerShips(part, turn);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        private void RegisterRoomHandlers()
        {
            _room.State.OnChange += OnRoomStateChange;

            void OnRoomStateChange(List <DataChange> changes)
            {
                foreach (var change in changes.Where(change => change.Field == RoomState.Phase))
                {
                    GamePhaseChanged?.Invoke((string)change.Value);
                }
            }
        }