コード例 #1
0
    void SwitchGame(int dir)
    {
        MessageSwitchGame data = new MessageSwitchGame();

        data.pos   = transform.position;
        data.vel   = m_rigidbody2d.velocity;
        data.name  = m_playerName;
        data.color = m_color;
        data.dir   = m_direction;

        // Look up the id of the game where in. Something like "level3"
        string gameId = LevelSettings.settings.playerSpawner.server.Id;
        // Extract the number part
        int numNdx  = gameId.IndexOfAny("0123456789".ToCharArray());
        int gameNum = System.Convert.ToInt32(gameId.Substring(numNdx));

        // Increment or Decrement the number wraping at the ends
        gameNum = (gameNum + LevelSettings.settings.numGames + dir) % LevelSettings.settings.numGames;

        // Build the ID of the game we want to switch to
        gameId = gameId.Substring(0, numNdx) + gameNum;

        // Send this player to that game.
        // Note: No more messages will be received or sent to this player.
        m_netPlayer.SwitchGame(gameId, data);
    }
コード例 #2
0
    // Called when player connects with their phone
    void InitializeNetPlayer(SpawnInfo spawnInfo)
    {
        Init();

        m_netPlayer = spawnInfo.netPlayer;
        m_netPlayer.OnDisconnect += Remove;
        m_netPlayer.OnNameChange += ChangeName;

        // Setup events for the different messages.
        m_netPlayer.RegisterCmdHandler <MessageMove>("move", OnMove);
        m_netPlayer.RegisterCmdHandler <MessageJump>("jump", OnJump);

        // We always get a `data` so check if it has one key from our expected message
        if (spawnInfo.data != null && spawnInfo.data.ContainsKey("dir"))
        {
            // This player was transferred from another game.

            // Turn the data back into our structure
            DeJson.Deserializer deserializer = new DeJson.Deserializer();
            MessageSwitchGame   data         = deserializer.Deserialize <MessageSwitchGame>(spawnInfo.data);

            // Choose a starting position based on the old position
            float x = (data.pos.x < LevelSettings.settings.leftEdgeOfLevel.position.x)
                ? LevelSettings.settings.rightEdgeOfLevel.position.x - 1 : LevelSettings.settings.leftEdgeOfLevel.position.x + 1;
            transform.localPosition = new Vector3(x, data.pos.y, 0f);

            // Set the initial velocity
            m_rigidbody2d.velocity = data.vel;

            // And the direction
            m_direction = data.dir;

            SetName(data.name);
            SetColor(data.color);
        }
        else
        {
            // This player just joined.
            MoveToRandomSpawnPoint();
            SetName(m_netPlayer.Name);
            float           hue   = Random.value;
            float           sat   = (float)Random.Range(0, 3) * -0.25f;
            MessageSetColor color = new MessageSetColor(
                hue,
                sat,
                0.0f,
                m_material.GetFloat("_HSVRangeMin"),
                m_material.GetFloat("_HSVRangeMax"));
            SetColor(color);

            // Send it to the phone
            m_netPlayer.SendCmd("setColor", color);
        }
    }