Exemplo n.º 1
0
    void Client_SendInput(List <Inputs> inputs)
    {
        NetInputMessage msg = new NetInputMessage
        {
            Identifier = _tickNumber,
            Inputs     = inputs
        };

        // If the current client is Master, send input to whole room
        // else send it to only Master (to be distributed later)
        var options = new RaiseEventOptions
        {
            TargetActors = PhotonNetwork.isMasterClient
                ? PhotonUtils.GetPlayerIdsInRoom()
                : new[] { PhotonNetwork.room.MasterClientId }
        };


        // If inputs are coming from the server (Master Server), they will be absolute.
        if (PhotonNetwork.isMasterClient)
        {
            var stateMsg = new NetStateMessage
            {
                Position       = transform.position,
                LastTick       = _tickNumber,
                PhotonPlayerId = PhotonNetwork.player.ID
            };

            PhotonNetwork.RaiseEvent((byte)EventCodes.STOC_ClientInputAcknowledge, stateMsg, false, options);
        }
        else
        {
            PhotonNetwork.RaiseEvent((byte)EventCodes.CTOS_ClientInput, msg, false, options);
        }
    }
Exemplo n.º 2
0
    public void ProcessInputMessages()
    {
        // Get the message
        NetInputMessage msg = _inputMessageQueue.Dequeue();

        // Find out who sent it
        PhotonPlayer photonPlayer = PhotonUtils.GetPlayerById(msg.PhotonPlayerId);

        if (photonPlayer == null)
        {
            return;
        }

        // Get remote player instance
        var remotePlayer = _remotePlayers.Find(x => x.Player.ID == photonPlayer.ID);

        if (remotePlayer == null)
        {
            Transform target = null;
            foreach (var go in GameObject.FindGameObjectsWithTag("NetworkPlayer"))
            {
                if (go.GetComponent <PhotonView>().ownerId == photonPlayer.ID)
                {
                    target = go.transform;
                }
            }

            if (target == null)
            {
                Debug.LogErrorFormat("Could not find Transform component for PhotonPlayer {0}", photonPlayer.ID);
            }
            else
            {
                remotePlayer = new ServerPlayer()
                {
                    Player = photonPlayer, Target = target
                };
                _remotePlayers.Add(remotePlayer);
            }
        }

        if (remotePlayer == null)
        {
            return;
        }

        Player player = remotePlayer.Target.GetComponent <Player>();

        player.Client_SyncServer();

        Server_ApplyInput(msg.Inputs, remotePlayer);

        player.PositionBuffer.Push(remotePlayer.Position.x, remotePlayer.Position.y, player.Syncer.ServerDelta(Time.deltaTime));

        NetStateMessage stateMsg = new NetStateMessage
        {
            Position       = remotePlayer.Position,
            LastTick       = msg.Identifier,
            PhotonPlayerId = remotePlayer.Player.ID
        };

        var options = new RaiseEventOptions {
            TargetActors = PhotonUtils.GetPlayerIdsInRoom()
        };


        // Broadcast new state of the client
        PhotonNetwork.RaiseEvent((byte)EventCodes.STOC_ClientInputAcknowledge, stateMsg, false, options);
    }