Exemplo n.º 1
0
        private void FixedUpdate()
        {
            if (_isConnected)
            {
                ReadLatencySelection();

                _playerInput.Read();
                if (_playerInput.HasInputsToSend())
                {
                    SendInputEvent();
                }

                _playerInput.AccumCooldown += Time.fixedDeltaTime;
                if (_playerInput.Shooting)
                {
                    Shoot();
                }

                _playerInput.DiscardShootsByTimeout(Time.fixedDeltaTime);
                if (_playerInput.HasShootsToSend())
                {
                    SendHits();
                }

                // if (_playerInput.CurrentInput.IsPressingSomething())
                // {
                //     HandleInputThroughPrediction();
                // }
                HandleInputThroughPrediction();

                _timeFromLastSnapshotInterpolation += Time.fixedDeltaTime;
                if (_snapshotBuffer.Count >= InterpolationBufferSize)
                {
                    _currentSnapshot = _snapshotBuffer.Dequeue();
                    if (_currentSnapshot.Contains(_id))
                    {
                        Logger.Log("ACK = " + _currentSnapshot.GetLastInputIdProcessed(_id), false, "magenta");
                    }
                    _timeFromLastSnapshotInterpolation = 0f;
                    foreach (var player in _players.Values)
                    {
                        player.RefreshLastSnapshotTransform();
                    }
                }

                if (_currentSnapshot != null)
                {
                    InterpolateSnapshots();
                    if (_currentSnapshot.Contains(_id))
                    {
                        var lastInputIdProcessedByServer = _currentSnapshot.GetLastInputIdProcessed(_id);
                        _playerInput.DiscardInputsAlreadyProcessedByServer(lastInputIdProcessedByServer);
                        if (_lastInputUsedAsBaseForConciliation < lastInputIdProcessedByServer)
                        {
                            //Conciliate(); //TODO: Fixme!!!
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void Conciliate()
 {
     _conciliationObject.transform.position = _currentSnapshot.GetPosition(_id);
     _conciliationObject.transform.rotation = _currentSnapshot.GetRotation(_id);
     Logger.Log("C: Transform took from #" + _currentSnapshot.GetLastInputIdProcessed(_id)
                + ", P = " + Printer.V3(_conciliationObject.transform.position)
                + ", R = " + Printer.Q4(_conciliationObject.transform.rotation), false, "orange");
     foreach (var input in _playerInput.GetInputsNotProcessedByServer())
     {
         Logger.Log("C: Before Executing #" + input.Key
                    + ", P = " + Printer.V3(_conciliationObject.transform.position)
                    + ", R = " + Printer.Q4(_conciliationObject.transform.rotation), false, "yellow");
         var(mov, rot) = PlayerManager.ProcessInput(input.Value, _conciliationObject, "C", "orange");
         Logger.Log("C: After  Executing #" + input.Key
                    + ", mM = " + Printer.V3(mov)
                    + ", mR = " + Printer.V3(rot)
                    + ", P = " + Printer.V3(_conciliationObject.transform.position)
                    + ", R = " + Printer.Q4(_conciliationObject.transform.rotation), false, "yellow");
     }
     Logger.Log("Conciliation: Finished"
                + ", P = " + Printer.V3(_conciliationObject.transform.position)
                + ", R = " + Printer.Q4(_conciliationObject.transform.rotation), false, "orange");
     _players[_id].GameObject.transform.position = _conciliationObject.transform.position;
     _players[_id].GameObject.transform.rotation = _conciliationObject.transform.rotation;
     _lastInputUsedAsBaseForConciliation         = _currentSnapshot.GetLastInputIdProcessed(_id);
 }
Exemplo n.º 3
0
 private void HandleInputThroughPrediction()
 {
     if (_playerInput.CurrentInput.IsPressingSomething())
     {
         var player = _players[_id].GameObject;
         Logger.Log("P: Before Executing #" + _playerInput.BiggestInputIdQueuedToSend
                    + ", P = " + Printer.V3(player.transform.position)
                    + ", R = " + Printer.Q4(player.transform.rotation), false, "cyan");
         var(mov, rot) = PlayerManager.ProcessInput(_playerInput.CurrentInput, player, "P", "grey");
         Logger.Log("P: After  Executing #" + _playerInput.BiggestInputIdQueuedToSend
                    + ", mM = " + Printer.V3(mov)
                    + ", mR = " + Printer.V3(rot)
                    + ", P = " + Printer.V3(player.transform.position)
                    + ", R = " + Printer.Q4(player.transform.rotation), false, "cyan");
     }
     _players[_id].GameObject.GetComponent <CharacterController>().Move(Physics.gravity * Time.fixedDeltaTime);
     foreach (var uiObject in FindObjectsOfType <Text>())
     {
         if (uiObject.CompareTag("LifeText"))
         {
             uiObject.text = _players[_id].Health + "%";
         }
         else if (uiObject.CompareTag("ScoreText"))
         {
             uiObject.text = "Score: " + _players[_id].Score;
         }
     }
 }
Exemplo n.º 4
0
        private void HandleHit(Packet hitPacket)
        {
            Logger.Log("C: Receiving hit response", false, "cyan");
            var buffer = hitPacket.Buffer;
            var lastShootIdProcessed = buffer.GetInt();

            _players[_id].IncrementScore(_playerInput.DiscardShootsAlreadyProcessedByServer(lastShootIdProcessed));
        }
Exemplo n.º 5
0
        private void SendHitResponsePacket(int shooter)
        {
            Logger.Log("S: Sending las processed = " + _clients[shooter].ClientInput.LastShootProcessed, false, "lime");
            var packet = Packet.Obtain();
            var buffer = packet.Buffer;

            EventSerializer.SerializeIntoBuffer(buffer, Event.Hit);
            buffer.PutInt(_clients[shooter].ClientInput.LastShootProcessed);
            buffer.Flush();
            _channel.Send(packet, _clients[shooter].EndPoint);
            packet.Free();
        }
Exemplo n.º 6
0
        private void Shoot()
        {
            Logger.Log("C: Shooting!", false);
            var origin  = _players[_id].GameObject.transform.position + Vector3.up * 1.5f;
            var forward = _players[_id].GameObject.transform.forward;
            var hitted  = Physics.Raycast(origin, forward, out var hit);

            if (hitted && hit.collider.gameObject.CompareTag("Player"))
            {
                Logger.Log("C: Player hitted", false, "blue");
                var shoot = new Shoot(_id, hit.collider.gameObject.GetComponent <Info>().Id);
                _playerInput.AddShoot(shoot);
            }
        }
Exemplo n.º 7
0
        private void SendHits()
        {
            var packet = Packet.Obtain();

            EventSerializer.SerializeIntoBuffer(packet.Buffer, Event.Hit);
            var shoots = _playerInput.GetShootsNotProcessedByServer();

            packet.Buffer.PutInt(_id);
            packet.Buffer.PutInt(shoots.Count);
            foreach (var shoot in shoots)
            {
                packet.Buffer.PutInt(shoot.Key);
                packet.Buffer.PutInt(shoot.Value.Hitted);
            }
            packet.Buffer.Flush();
            Logger.Log("C: Sending hits", false, "cyan");
            StartCoroutine(SendPacketAddingLatency(packet));
        }
Exemplo n.º 8
0
        private void HandleHitEvent(Packet hitPacket)
        {
            Logger.Log("S: Receiving hits", false, "lime");
            var buffer  = hitPacket.Buffer;
            var shooter = buffer.GetInt();

            for (var shootsToProcess = buffer.GetInt(); shootsToProcess > 0; shootsToProcess--)
            {
                var shootId = buffer.GetInt();
                var hitted  = buffer.GetInt();
                if (!_clients[shooter].ClientInput.ShootWasAlreadyProcessed(shootId))
                {
                    Logger.Log("Server: Client #" + shooter + " hitted Client #" + hitted + " in Shoot #" + shootId, false);
                    _clients[hitted].DecreaseHealth();
                    _clients[shooter].ClientInput.UpdateLastProcessedShoot(shootId);
                }
            }
            SendHitResponsePacket(shooter);
        }
Exemplo n.º 9
0
 private void HandleClientMovement()
 {
     foreach (var client in _clients)
     {
         while (client.ClientInput.NextInput())
         {
             Logger.Log("S: Before Executing #" + client.ClientInput.CurrentInputId
                        + ", P = " + Printer.V3(client.Entity.transform.position)
                        + ", R = " + Printer.Q4(client.Entity.transform.rotation), false, "lime");
             var(mov, rot) = PlayerManager.ProcessInput(client.ClientInput.CurrentInput, client.Entity, "S", "green");
             Logger.Log("S: After  Executing #" + client.ClientInput.CurrentInputId
                        + ", mM = " + Printer.V3(mov)
                        + ", mR = " + Printer.V3(rot)
                        + ", P = " + Printer.V3(client.Entity.transform.position)
                        + ", R = " + Printer.Q4(client.Entity.transform.rotation), false, "lime");
         }
         client.Entity.GetComponent <CharacterController>().Move(Physics.gravity * Time.fixedDeltaTime);
     }
 }