Esempio n. 1
0
    private void WebClientOnonData(ArraySegment <byte> data)
    {
        _bitBuffer.Clear();
        _bitBuffer.FromArray(data.Array, data.Count);
        byte messageId = _bitBuffer.ReadByte();

        switch (messageId)
        {
        case 2:
        {
            _myId = _bitBuffer.ReadUShort();
            // Put my name into _names too
            _names[_myId] = ConnectUIController.DisplayName;

            _currentState = (GameState)_bitBuffer.ReadByte();
            // Read all the ids and names it gives me and store it into _names to be used when players get spawned
            ushort count = _bitBuffer.ReadUShort();
            _scoreboardController.ResetScores();
            for (int i = 0; i < count; i++)
            {
                ushort id    = _bitBuffer.ReadUShort();
                string name  = _bitBuffer.ReadString();
                short  score = _bitBuffer.ReadShort();
                _names[id] = name;

                Debug.Log("id: " + id + ", name: " + name);

                _scoreboardController.UpdateEntry(id, name, score);
            }
            _scoreboardController.DrawBoard();

            StatusText.enabled = true;
            if (_currentState == GameState.Waiting)
            {
                StatusText.text = "Waiting for at least two players";
            }
            else
            {
                StatusText.text = "Waiting for current round to end...";
                _musicController.Pause();
            }

            _handShakeComplete = true;
            break;
        }

        case 3:
        {
            // GUARD FROM SETTING PLAYER POSITIONS UNTIL WE HAVE OUR ID
            if (!_handShakeComplete)
            {
                break;
            }

            ushort count = _bitBuffer.ReadUShort();
            for (int i = 0; i < count; i++)
            {
                ushort id = _bitBuffer.ReadUShort();
                uint   qX = _bitBuffer.ReadUInt();
                uint   qY = _bitBuffer.ReadUInt();

                // GUARD FROM CHANGING LOCAL PLAYERS POSITION
                if (id == _myId)
                {
                    continue;
                }

                QuantizedVector2 qPosition = new QuantizedVector2(qX, qY);
                Vector2          position  = BoundedRange.Dequantize(qPosition, Constants.WORLD_BOUNDS);

                if (!_otherPlayers.ContainsKey(id))
                {
                    // Create new player
                    GameObject newPlayer = Instantiate(OtherPlayerPrefab, position, Quaternion.identity);
                    Destroy(newPlayer.GetComponent <Rigidbody2D>());
                    Destroy(newPlayer.GetComponent <CircleCollider2D>());
                    Destroy(newPlayer.GetComponent <PlayerController>());
                    Destroy(newPlayer.GetComponent <MusicController>());
                    PositionInterp positionInterp = newPlayer.GetComponent <PositionInterp>();
                    positionInterp.enabled = true;
                    _otherPlayers[id]      = positionInterp;
                    newPlayer.GetComponent <Nametag>().SetName(_names[id]);
                    _overrideDirtySendRule = true;
                }
                // Update the other players position
                _otherPlayers[id].PushNewPosition(position);
            }
            break;
        }

        case 4:
        {
            ushort id = _bitBuffer.ReadUShort();
            if (_otherPlayers.ContainsKey(id))
            {
                Destroy(_otherPlayers[id].gameObject);
                _otherPlayers.Remove(id);
            }

            _scoreboardController.RemoveEntry(id);
            _scoreboardController.DrawBoard();

            break;
        }

        case 5:
        {
            _currentState = (GameState)_bitBuffer.ReadByte();
            HandleStateChange(_currentState);

            break;
        }

        case 7:
        {
            ushort count = _bitBuffer.ReadUShort();
            _scoreboardController.ResetScores();
            for (int i = 0; i < count; i++)
            {
                ushort id     = _bitBuffer.ReadUShort();
                short  points = _bitBuffer.ReadShort();

                _scoreboardController.UpdateEntry(id, _names[id], points);
            }
            _scoreboardController.DrawBoard();

            break;
        }

        case 9:
        {
            ushort id   = _bitBuffer.ReadUShort();
            string name = _bitBuffer.ReadString();
            _names[id] = name;

            // Add this new guy to the scoreboard
            _scoreboardController.UpdateEntry(id, name, 0);
            _scoreboardController.DrawBoard();

            break;
        }

        case 13:
        {
            ushort id = _bitBuffer.ReadUShort();
            if (_myId != id)
            {
                _otherPlayers[id].GetComponent <PlayerAnimationController>().Explode();
                _audioSource.PlayOneShot(ExplodeSound);
            }

            break;
        }
        }
    }
Esempio n. 2
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;
        }
        }
    }