コード例 #1
0
    private void SendInput()
    {
        var angles = GetRotation();
        var move   = GetMove();
        var shoot  = GetShot();

        _prediction.AddInputs(_sequence, (int)move.x, (int)move.y, (int)angles.y);
        BitBuffer bitBuffer = new BitBuffer();

        //TODO: cambiar rango de player id
        bitBuffer.InsertInt(_engine.playerId, 0, 10);

        bitBuffer.InsertInt((int)angles.x, 0, 360);
        bitBuffer.InsertInt((int)angles.y, 0, 360);
        bitBuffer.InsertInt((int)angles.z, 0, 360);

        bitBuffer.InsertInt((int)move.x, -1, 1);
        bitBuffer.InsertInt((int)move.y, -1, 1);

        bitBuffer.InsertBit(shoot);

        _packetProcessor.SendReliableFastData(bitBuffer.GetByteArray(), _serverIpEndPoint, MessageType.Input, _sequence);
    }
コード例 #2
0
    private void ReadMessage(Message message)
    {
        if (message.messageType == MessageType.Join)
        {
            var idPlayer = idCounter++;

            var playerScript = CreatePlayer();
            playerScript.id             = idPlayer;
            playerScript.playerHealth   = playerScript.GetComponent <PlayerHealth>();
            playerScript.playerMovement = playerScript.GetComponent <PlayerMovement>();
            _players.Add(idPlayer, playerScript);
            playersTransforms.Add(playerScript.playerMovement.transform);
            playersHealth.Add(playerScript.playerHealth);
            playersObjects.Add(playerScript.gameObject);

            _ipEndPoints.Add(message.from);

            BitBuffer buffer = new BitBuffer();
            buffer.InsertInt(idPlayer, 0, 10);

            _packetProcessor.SendReliableFastData(buffer.GetByteArray(), message.from, MessageType.JoinACK, _sequence);
            return;
        }

        BitBuffer bitBuffer = new BitBuffer(message.message);

        var id = bitBuffer.GetInt(0, 10);

        // Prevent ReliableFast double input problem
        if (_lastMessageReceived.ContainsKey(id) && _lastMessageReceived[id] >= message.messageId)
        {
            return;
        }

        _lastMessageReceived[id] = message.messageId;

        if (message.messageType == MessageType.Input)
        {
            var x = bitBuffer.GetInt(0, 360);
            var y = bitBuffer.GetInt(0, 360);
            var z = bitBuffer.GetInt(0, 360);

            var h = bitBuffer.GetInt(-1, 1);
            var v = bitBuffer.GetInt(-1, 1);

            var shoot = bitBuffer.GetBit();

            _players[id].playerMovement.Rotation(x, y, z);
            _players[id].playerMovement.Move(h, v);
            if (shoot)
            {
                _players[id].playerShooting.Shoot();
                _players[id].isShooting = true;
            }
        }

        if (_players[id].lastInputSequence < message.sequence)
        {
            _players[id].lastInputSequence = message.sequence;
        }
    }