Ejemplo n.º 1
0
    private void LateUpdate()
    {
        _webClient.ProcessMessageQueue(this);

        if (_webClient.ConnectionState == ClientState.Connected && Time.time >= _timeToSendNextUpdate)
        {
            _timeToSendNextUpdate = Time.time + (1f / Constants.CLIENT_TICKRATE);

            // GUARD, DONT SEND POSITION IF WE DIDN'T MOVE
            if (!_overrideDirtySendRule && (_previousPlayerPosition - LocalPlayerTransform.position).magnitude < 0.1f)
            {
                return;
            }


            // Send our position to server
            _bitBuffer.Clear();
            _bitBuffer.AddByte(1);

            QuantizedVector3 qPosition = BoundedRange.Quantize(LocalPlayerTransform.position, Constants.WORLD_BOUNDS);
            _bitBuffer.AddUInt(qPosition.x);
            _bitBuffer.AddUInt(qPosition.y);

            _bitBuffer.ToArray(_buffer);
            _webClient.Send(new ArraySegment <byte>(_buffer, 0, 9));

            _previousPlayerPosition = LocalPlayerTransform.position;
            _overrideDirtySendRule  = false;
        }
    }
Ejemplo n.º 2
0
    void LateUpdate()
    {
        _ws.ProcessMessageQueue(this);

        if (_ws.ConnectionState == ClientState.Connected)
        {
            if (Time.time - _timeSinceLastSend > SendInterval)
            {
                _timeSinceLastSend = Time.time;

                QuantizedVector3    qPosition = BoundedRange.Quantize(_modelTransform.position, Constants.WORLD_BOUNDS);
                QuantizedQuaternion qRotation = SmallestThree.Quantize(_modelTransform.rotation);
                ushort qBestTime = HalfPrecision.Quantize(GameTimer.BestTime);
                _bitBuffer.Clear();
                _bitBuffer.AddUInt(qPosition.x)
                .AddUInt(qPosition.y)
                .AddUInt(qPosition.z)
                .AddUInt(qRotation.m)
                .AddUInt(qRotation.a)
                .AddUInt(qRotation.b)
                .AddUInt(qRotation.c)
                .AddUShort(qBestTime)
                .ToArray(_byteBuffer);
                _ws.Send(new ArraySegment <byte>(_byteBuffer, 0, 28));
            }
        }
    }
Ejemplo n.º 3
0
    private void WsOnonData(ArraySegment <byte> obj)
    {
        _bitBuffer.Clear();
        _bitBuffer.FromArray(obj.Array, obj.Count);

        ushort messageId = _bitBuffer.ReadUShort();

        switch (messageId)
        {
        case 0:
        {
            ushort id = _bitBuffer.ReadUShort();
            if (_ghostCars.ContainsKey(id))
            {
                Destroy(_ghostCars[id].gameObject);
                _ghostCars.Remove(id);
            }

            break;
        }

        case 1:
        {
            ushort count = _bitBuffer.ReadUShort();
            for (int i = 0; i < count; i++)
            {
                ushort id = _bitBuffer.ReadUShort();

                QuantizedVector3    qPosition = new QuantizedVector3(_bitBuffer.ReadUInt(), _bitBuffer.ReadUInt(), _bitBuffer.ReadUInt());
                QuantizedQuaternion qRotation = new QuantizedQuaternion(_bitBuffer.ReadUInt(), _bitBuffer.ReadUInt(), _bitBuffer.ReadUInt(), _bitBuffer.ReadUInt());
                ushort qBestTime = _bitBuffer.ReadUShort();

                // Ignore it if it is the transform for my own car
                if (_myId == id || id == 0)
                {
                    continue;
                }

                Vector3    postion  = BoundedRange.Dequantize(qPosition, Constants.WORLD_BOUNDS);
                Quaternion rotation = SmallestThree.Dequantize(qRotation);

                if (!_ghostCars.ContainsKey(id))
                {
                    GameObject newCar = Instantiate(GhostCarPrefab, postion, rotation);
                    _ghostCars[id] = newCar.GetComponent <GhostCarBehavior>();
                }
                else
                {
                    _ghostCars[id].UpdateTransform(postion, rotation);
                    float bestTime = HalfPrecision.Dequantize(qBestTime);
                    _ghostCars[id].UpdatebestTime(bestTime);
                }
            }

            break;
        }

        case 2:
        {
            ushort id = _bitBuffer.ReadUShort();

            _myId = id;

            break;
        }
        }
    }