示例#1
0
    // Checks which direction to go in every once in a while

    void Update()
    {
        /* TODO We can tweak the update rate at which we send whisp updates
         *      updates based on the player's position
         *      sending really few updates for players that are far away
         *      and relatively many updates to the players that are close by */
        if (_clock.CurrentTime > _lastUpdateTime + _navigationInterval)
        {
            _direction = Vector3.Slerp(_direction, Random.insideUnitSphere, _randomness).normalized;

            RaycastHit hitInfo;
            bool       hit = Physics.Raycast(transform.position, Vector3.down, out hitInfo, _raycastLength);
            if (hit)
            {
                float normalizedAltitude = Mathf.Clamp01((transform.position.y - hitInfo.point.y) / _maxAltitude);

                _direction = Vector3.Lerp(_direction, Vector3.down, Mathf.Pow(normalizedAltitude, _altitudeLimitPower));
                _direction = Vector3.Lerp(_direction, Vector3.up, Mathf.Pow(1f - normalizedAltitude, _altitudeLimitPower));
            }

            _lastUpdateTime = _clock.CurrentTime;

            var updateDirectionMessage = _directionMessages.Create();
            updateDirectionMessage.Content.Position  = transform.position;
            updateDirectionMessage.Content.Direction = _direction;
            _messageSender.Send(updateDirectionMessage, ObjectRoles.NonAuthoritive);
        }

        _transform.position = WhispSimulation.Simulate(_transform.position, _direction, _configuration, _clock.DeltaTime);
    }
示例#2
0
        public void Delete(ConnectionId connectionId, ObjectId objectId)
        {
            var deleteObjectMsg = _deleteMessages.Create();

            deleteObjectMsg.Content.ObjectId = objectId;
            _messageSender.Send(connectionId, deleteObjectMsg);
        }
示例#3
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Y))
     {
         var requestRace = _requestRaceMessages.Create();
         _messageSender.Send(requestRace, ObjectRole.Authority);
     }
 }
    private void SendMessage()
    {
        var message   = _stateUpdateMessages.Create();
        var bodyState = _simulation.UnityBody.State;

        message.Content.State = bodyState;
        _messageSender.Send(message, ObjectRoles.Everyone);
    }
    private void OnInputReceived(PlayerMessage.InputUpdate message)
    {
        _simulation.SetInput(message.Input);

        // Route the input to other players
        var peerMessage = _inputMessages.Create();

        peerMessage.Content.Input = message.Input;
        _messageSender.Send(peerMessage, ObjectRole.Others);
    }
示例#6
0
    private void RequestClockSync(ConnectionId hostId)
    {
        var pingMessage = _pingMessages.Create();

        pingMessage.Content.Timestamp      = _synchedGameClock.CurrentTime;
        pingMessage.Content.FixedTimestamp = _syncedFixedClock.CurrentTime; // Todo: don't use this for ping
        _ramnet.MessageSender.Send(hostId, pingMessage);

        //Debug.Log("Clock sync frame: was sent on render frame: " + Time.frameCount);
    }
示例#7
0
//    private void OnHostInfoRequest(IPEndPoint endPoint, BasicMessage.RequestHostInfo request) {
//        var response = _ramnet.MessagePool.GetMessage<BasicMessage.HostInfoResponse>();
//        response.Content.SenderTimestamp = request.Timestamp;
//        response.Content.PlayerCount = 0;
//        _ramnet.ConnectionlessMessageSender.Send(endPoint, response);
//    }

    private void OnClientClockSyncRequest(ConnectionId connectionId, BasicMessage.Ping ping)
    {
        var pong = _pongMessages.Create();

        pong.Content.SenderTimestamp      = ping.Timestamp;
        pong.Content.SenderFixedTimestamp = ping.FixedTimestamp;
        pong.Content.Timestamp            = _syncedGameClock.CurrentTime;
        pong.Content.FixedTimestamp       = _syncedFixedClock.CurrentTime;
        _ramnet.MessageSender.Send(connectionId, pong);
    }
示例#8
0
//            public static Action<IPEndPoint, BasicMessage.Ping> UnconnectedPing(
//                IClock clock,
//                IClock fixedClock,
//                INetworkMessagePool<BasicMessage.Pong> messagePool,
//                IConnectionlessMessageSender messageSender) {
//
//                return (endpoint, ping) => {
//                    var pong = messagePool.Create();
//                    pong.Content.SenderTimestamp = ping.Timestamp;
//                    pong.Content.SenderFixedTimestamp = ping.FixedTimestamp;
//                    pong.Content.Timestamp = clock.CurrentTime;
//                    pong.Content.FixedTimestamp = fixedClock.CurrentTime;
//                    messageSender.Send(endpoint, pong);
//                };
//            }

            public static Action <MessageMetaData, BasicMessage.RequestHostInfo> HostInfoRequest(
                INetworkMessagePool <BasicMessage.HostInfoResponse> messagePool,
                Func <ushort> playerCount,
                IConnectionlessMessageSender messageSender)
            {
                return((metadata, message) => {
                    var response = messagePool.Create();
                    response.Content.SenderTimestamp = message.Timestamp;
                    response.Content.PlayerCount = playerCount();
                    messageSender.Send(metadata.EndPoint, response);
                });
            }
示例#9
0
 public static Action <MessageMetaData, BasicMessage.Ping> Ping(
     IClock clock,
     IClock fixedClock,
     INetworkMessagePool <BasicMessage.Pong> messagePool,
     IMessageSender messageSender)
 {
     return((metadata, ping) => {
         var pong = messagePool.Create();
         pong.Content.FrameId = ping.FrameId;
         pong.Content.Timestamp = clock.CurrentTime;
         pong.Content.FixedTimestamp = fixedClock.CurrentTime;
         messageSender.Send(metadata.ConnectionId, pong);
     });
 }
示例#10
0
    public void OnFixedUpdate(IClock clock)
    {
//        Debug.Log("owner fixed update " + clock.FrameCount);

        /* Sample input once per render frame, but do it here instead of in Update to avoid frame lag.
         * Send to local simulation and to server */
        if (Time.renderedFrameCount > _lastSampledFrame)
        {
            var inputMessage = _inputMessages.Create();
            inputMessage.Content.Input = SampleInput(clock);
            _messageSender.Send(inputMessage, ObjectRole.Authority | ObjectRole.Owner);

            _lastSampledFrame = Time.renderedFrameCount;
        }
    }
示例#11
0
 public static Action <IPEndPoint, BasicMessage.Ping> UnconnectedPing(
     IClock clock,
     IClock fixedClock,
     INetworkMessagePool <BasicMessage.Pong> messagePool,
     IConnectionlessMessageSender messageSender)
 {
     return((endpoint, ping) => {
         var pong = messagePool.Create();
         pong.Content.SenderTimestamp = ping.Timestamp;
         pong.Content.SenderFixedTimestamp = ping.FixedTimestamp;
         pong.Content.Timestamp = clock.CurrentTime;
         pong.Content.FixedTimestamp = fixedClock.CurrentTime;
         messageSender.Send(endpoint, pong);
     });
 }
示例#12
0
 public static Action <ConnectionId, BasicMessage.Ping> Ping(
     IClock clock,
     IClock fixedClock,
     INetworkMessagePool <BasicMessage.Pong> messagePool,
     IMessageSender messageSender)
 {
     return((connectionId, ping) => {
         var pong = messagePool.Create();
         pong.Content.SenderTimestamp = ping.Timestamp;
         pong.Content.SenderFixedTimestamp = ping.FixedTimestamp;
         pong.Content.Timestamp = clock.CurrentTime;
         pong.Content.FixedTimestamp = fixedClock.CurrentTime;
         messageSender.Send(connectionId, pong);
     });
 }
示例#13
0
        public void Replicate(ConnectionId receiver, ReplicatedObject instance)
        {
            var objectRole  = ObjectRole.Nobody;
            var isOwner     = receiver == instance.OwnerConnectionId;
            var isAuthority = receiver == instance.AuthorityConnectionId;

            objectRole = objectRole | (isOwner ? ObjectRole.Owner : ObjectRole.Nobody);
            objectRole = objectRole | (isAuthority ? ObjectRole.Authority : ObjectRole.Nobody);
            objectRole = objectRole | (!isOwner && !isAuthority ? ObjectRole.Others : ObjectRole.Nobody);

            Debug.Log("replicating to " + receiver + " role: " + objectRole);

            if (instance.IsPreExisting)
            {
                var replicatePreExistingMsg = _replicatePreExistingMessages.Create();
                replicatePreExistingMsg.Content.GlobalObjectId.CopyFrom(instance.GlobalObjectId);
                replicatePreExistingMsg.Content.NetworkObjectId = instance.Id;
                replicatePreExistingMsg.Content.ObjectRole      = objectRole;
                _messageSender.Send(receiver, replicatePreExistingMsg);
            }
            else
            {
                var createObjectMsg = _createMessages.Create();
                createObjectMsg.Content.ObjectId   = instance.Id;
                createObjectMsg.Content.ObjectRole = objectRole;
                createObjectMsg.Content.ObjectType = instance.Type.Value;
                createObjectMsg.Content.Position   = instance.GameObject.transform.position;
                createObjectMsg.Content.Rotation   = instance.GameObject.transform.rotation;

                // Add any additional messages that are required to atomically construct the object
                createObjectMsg.Content.AdditionalData.Reset();
                instance.ReplicationConstructor.SerializeInitialState(createObjectMsg.Content.AdditionalData);

                _messageSender.Send(receiver, createObjectMsg);
            }
        }
示例#14
0
    private void OnCollisionEnter(Collision _)
    {
        var message = _collisionEventMessages.Create();

        _messageSender.Send(message, ObjectRoles.Everyone);
    }