Exemple #1
0
        private void InitGameplay()
        {
            _currentOnlineState = OnlineMatchState.Gameplay;
            var firstFrameBlankInputs = new SingleFrameInputValidation();

            firstFrameBlankInputs.FrameNumber = 0;
            _inputHistory.Add(firstFrameBlankInputs);
        }
Exemple #2
0
        public override void Update(Inputs _inputs)
        {
            var comsFromOpponent = NetworkManager.Instance.GetUnreadCommunications();

            if (!_initializedNetworkManager)
            {
                _initializedNetworkManager = true;
                NetworkManager.Instance.StartListening();
            }
            switch (_currentOnlineState)
            {
            case OnlineMatchState.WaitingForGuest:
                //wait for message from guest containing his IP so we can begin synchronizing
                foreach (var oppCom in comsFromOpponent)
                {
                    if (oppCom.Communication_Type == Communication.CommunicationType.ClientConnectingToHost)
                    {
                        //get opponent info
                        ConnectToHostMessage msg = JsonUtility.FromJson(oppCom.Message, typeof(ConnectToHostMessage)) as ConnectToHostMessage;
                        GameManager.Instance.NetplayState.OpponentIp = msg.MyIP;
                        m_setData.InitData.P2_Character = CharacterSelectScreen.GetCharacterFromString(msg.MyCharacter);
                        _currentOnlineState             = OnlineMatchState.Synchronizing;
                        _preMatchSyncCountdown          = _preMatchSyncMax;

                        //send ready
                        ConnectToGuestMessage toGuest = new ConnectToGuestMessage()
                        {
                            MyCharacter = CharacterSelectScreen.GetMatchingCharacterName(m_setData.InitData.P1_Character)
                        };
                        Communication comToGuest = new Communication()
                        {
                            Communication_Type = Communication.CommunicationType.ReadyToBeginSync, Message = JsonUtility.ToJson(toGuest)
                        };
                        comToGuest.Send();
                        break;
                    }
                }
                break;

            case OnlineMatchState.WaitingForHost:
                //keep spamming host until we get his reply that he got our ip (and therefore is ready to sync)
                Communication.CreateGuestFirstContact(CharacterSelectScreen.GetMatchingCharacterName(m_setData.InitData.P1_Character)).Send();

                foreach (var oppCom in comsFromOpponent)
                {
                    if (oppCom.Communication_Type == Communication.CommunicationType.ReadyToBeginSync)
                    {
                        _currentOnlineState    = OnlineMatchState.Synchronizing;
                        _preMatchSyncCountdown = _preMatchSyncMax;
                        break;
                    }
                }
                break;

            case OnlineMatchState.Synchronizing:
                SendSyncMessage(_preMatchSyncCountdown);
                var syncMessages = comsFromOpponent.Where(o => o.Communication_Type == Communication.CommunicationType.Synchronization).Select(o => o.Message).ToList();
                Synchronize(syncMessages);
                _preMatchSyncCountdown--;
                if (_preMatchSyncCountdown < 0)
                {
                    InitGameplay();
                }
                break;

            case OnlineMatchState.Gameplay:
                //estimate that opponent's inputs are the same as previous inputs
                var currentInputs = SingleFrameInputValidation.CreateNextInputs(_inputHistory.Last());
                currentInputs.P1_Inputs = _inputs.P1_Inputs;
                _inputHistory.Add(currentInputs);

                //serialize input history
                var serializedInputHistory = CreateInputSerializedString();
                //send inputs to other player
                Communication latstInputsHistory = new Communication()
                {
                    Message = serializedInputHistory, Communication_Type = Communication.CommunicationType.InputHistory
                };
                latstInputsHistory.Send();
                //handle opponent inputs
                foreach (var inputComFromOpponent in comsFromOpponent.Where(o => o.Communication_Type == Communication.CommunicationType.InputHistory))
                {
                    var inputHistoryFromOpponentStr = JsonHelper.FromJson <string>(inputComFromOpponent.Message);
                    var inputHistoryFromOpponent    = new List <SingleFrameInputValidation>();
                    foreach (var v in inputHistoryFromOpponentStr)
                    {
                        var opponentInputInfo = JsonUtility.FromJson(v, typeof(SingleFrameInputValidation)) as SingleFrameInputValidation;
                        //if both are same side, switch opponent info
                        if (currentInputs.P1_Source == opponentInputInfo.P1_Source)
                        {
                            var temp = opponentInputInfo.P2_Inputs;
                            opponentInputInfo.P2_Inputs = opponentInputInfo.P1_Inputs;
                            opponentInputInfo.P1_Inputs = temp;
                            opponentInputInfo.P1_Source = !opponentInputInfo.P1_Source;
                        }
                        inputHistoryFromOpponent.Add(opponentInputInfo);
                    }
                    AdjustStateForNetplay(inputHistoryFromOpponent);
                }
                //gameplay proceeds as usual
                base.Update(_inputs);
                break;

            case OnlineMatchState.MatchOver:
                break;
            }
        }