示例#1
0
    void UpdatePlayers(List <PlayerData> playerData)
    {
        // First mark all players as dead. Any one that's updated we'll undo this, leaving the ones left out as dead.
        foreach (Piece player in players)
        {
            player.Dead = true;
        }

        foreach (PlayerData playerDatum in playerData)
        {
            Piece matchingPlayer = players.SingleOrDefault(p => p.Id == playerDatum.id);
            if (matchingPlayer == null)
            {
                // Add new player.
                Piece piece  = possiblePieces[playerDatum.type.letter];
                Piece player = Instantiate(
                    piece,
                    playerDatum.x * Vector3.right
                    + playerDatum.y * Vector3.forward
                    + 0.2f * Vector3.up,
                    Quaternion.identity);
                player.GamePosition = new Vector2(playerDatum.x, playerDatum.y);
                player.Id           = playerDatum.id;
                player.Type         = (PieceType)playerDatum.type.letter;
                player.Moves        = playerDatum.moves;
                players.Add(player);
            }
            else
            {
                if (matchingPlayer == playerController.Player)
                {
                    Debug.Log(playerDatum.moves);
                }
                // Move the player
                matchingPlayer.Moves = playerDatum.moves;
                // TODO: Update the type here maybe (like if a boy becomes a queen)
                matchingPlayer.GamePosition = new Vector2(playerDatum.x, playerDatum.y);
                matchingPlayer.Dead         = false;
            }
        }

        // Remove all the dead players?
        foreach (Piece player in players)
        {
            if (player.Dead)
            {
                // Do a splosion!!!
                Transform sploSplo = Instantiate(
                    Splode,
                    player.transform.position + 0.05f * Vector3.down,
                    Quaternion.identity);
                // RIP
                Destroy(sploSplo.gameObject, 0.5f);
                // Do a splosion!!!
                Transform booshBoosh = Instantiate(
                    Boosh,
                    player.transform.position + 0.05f * Vector3.down,
                    Quaternion.identity);
                // RIP
                Destroy(booshBoosh.gameObject, 2f);

                Destroy(player.gameObject);
            }
        }
        players = players.Where(p => !p.Dead).ToList();

        // Update player controller

        Piece controlledPlayer = players.SingleOrDefault(p => p.Id == playerController.Id);

        if (controlledPlayer != null)
        {
            // Should be ok to constantly set this?
            playerController.Player = controlledPlayer;
            playerController.UpdateSquares();
        }

        // Update currently controlled player. Also works if destroyed, surprisingly
        if (playerController.Player == null)
        {
            // Request a new player?
            string id = System.Guid.NewGuid().ToString();
            playerController.Id     = id;
            playerController.Player = null;
            socketBoy.AddPlayer(id);
        }
    }