예제 #1
0
    private void UpdateAllCircles(GameStateUpdatePacket gameStatePacket)
    {
        // reset the timer used for interpolating circle position
        circleDeltaTimer = 0;

        for (var iPlayer = 0; iPlayer < gameStatePacket.PlayersLength; iPlayer++)
        {
            var player = gameStatePacket.Players(iPlayer);
            if (!player.HasValue)
            {
                continue;
            }

            if (!circles.ContainsKey(player.Value.UserId))
            {
                InitializeCircle(
                    syncServer.UserId == player.Value.UserId ? PlayerColor : OpponentColor,
                    player.Value.UserId, new Vector2(player.Value.PosX, player.Value.PosY)
                    );

                continue;
            }

            var circle = circles[player.Value.UserId];
            // We set the circlesPreviousPosition to the current circle location -- we want to interpolate from that point to the new desired location
            circlesPreviousPositions[player.Value.UserId] = new Vector2(circle.GetComponent <Rigidbody2D>().position.x, circle.GetComponent <Rigidbody2D>().position.y);
            // We set the circlesDesiredPosition to the x,y coordinates the user clicked on -- this is where our circle will interpolate towards
            circlesDesiredPositions[player.Value.UserId] = new Vector2(player.Value.PosX, player.Value.PosY);

            SetCircleOpacity(circle, player.Value.Active ? ActiveOpacity : InactiveOpacity);
        }
    }
예제 #2
0
 private void OnGameStateUpdated(GameStateUpdatePacket gameStateUpdatePacket)
 {
     gameTime = gameStateUpdatePacket.GameTime;
     HideWaitForOpponentDialog();
     UpdateAllCircles(gameStateUpdatePacket);
     playerPositionText.text = MakeInfoText();
 }
예제 #3
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isDragging = true;
        }

        if (Input.GetMouseButtonUp(0))
        {
            isDragging = false;
        }

        if (isDragging && playerInitialized)
        {
            // The game server is server authoritative. That is, it is the
            // "source of truth", so only update the UI based on the information
            // it sends from the GameStateUpdatePacket in response to PlayerUpdateStatePackets
            // it gets from each player.
            syncServer.SendPlayerPosition(ScreenToWorldPosition(new Vector2(Input.mousePosition.x, Input.mousePosition.y)));
        }

        // Process all packets that have been received by the server thus far
        byte[] data;
        while (syncServer.GetNextPacket(out data))
        {
            var packet     = PacketFactory.BytesToPacket(data);
            var byteBuffer = new ByteBuffer(data);
            switch ((Opcode)packet.Opcode)
            {
            case Opcode.Success:
                Debug.Log("Success packet received");
                break;

            case Opcode.GameStateUpdate:
                OnGameStateUpdated(GameStateUpdatePacket.GetRootAsGameStateUpdatePacket(byteBuffer));
                break;

            case Opcode.MatchSuccess:
                // All players have entered the match, the game can now be played.
                // set the tickRate so that we can properly interpolate animations on the client side
                tickRate = MatchSuccessPacket.GetRootAsMatchSuccessPacket(byteBuffer).TickRate;
                Debug.Log("Match found, joining... Tick Rate: " + tickRate);
                OnMatchReady();
                break;

            case Opcode.GameOver:
                OnGameOver(GameOverPacket.GetRootAsGameOverPacket(byteBuffer));
                break;

            default:
                Debug.Log($"Packet received with Opcode={(Opcode)packet.Opcode}");
                break;
            }
        }

        // If we've initialized our players, lets start interpolating between circlesPreviousPositions and circlesDesiredPositions
        if (playerInitialized)
        {
            DrawAllCircles();
        }
    }