Example #1
0
        protected virtual void OnMessage(object sender, PlayerIOClient.Message m)
        {
            switch (m.Type)
            {
            case "add":
            {
                int ID = m.GetInt(0);
                if (!playerList.ContainsKey(ID))
                {
                    Player temp = new Player((Bot)this, ID, m.GetString(1), m.GetInt(2), m.GetFloat(3), m.GetFloat(4), m.GetBoolean(5), m.GetBoolean(6), m.GetBoolean(7), m.GetInt(8), m.GetBoolean(10), m.GetBoolean(9), m.GetInt(11));
                    temp.x = m.GetDouble(3);
                    temp.y = m.GetDouble(4);
                    lock (playerList)
                    {
                        playerList.Add(ID, temp);
                    }
                    if (nameList.ContainsKey(temp.name))
                    {
                        nameList.Remove(temp.name);
                    }
                    nameList.Add(temp.name, ID);
                }
            }
            break;

            case "left":
            {
                int tempKey = m.GetInt(0);
                if (playerList.ContainsKey(tempKey))
                {
                    nameList.Remove(playerList[tempKey].name);
                    lock (playerList)
                    {
                        playerList.Remove(tempKey);
                    }
                }
            }
            break;

            case "m":
            {
                int   playerID     = int.Parse(m[0].ToString());
                float playerXPos   = float.Parse(m[1].ToString());
                float playerYPos   = float.Parse(m[2].ToString());
                float playerXSpeed = float.Parse(m[3].ToString());
                float playerYSpeed = float.Parse(m[4].ToString());
                float modifierX    = float.Parse(m[5].ToString());
                float modifierY    = float.Parse(m[6].ToString());
                int   xDir         = int.Parse(m[7].ToString());
                int   yDir         = int.Parse(m[8].ToString());
                if (playerList.ContainsKey(playerID))
                {
                    lock (playerList)
                    {
                        Player player = playerList[playerID];
                        player.x             = playerXPos;
                        player.y             = playerYPos;
                        player.speedX        = playerXSpeed;
                        player.speedY        = playerYSpeed;
                        player.modifierX     = modifierX;
                        player.modifierY     = modifierY;
                        player.horizontal    = xDir;
                        player.vertical      = yDir;
                        playerList[playerID] = player;
                    }
                }
            }
            break;

            case "god":
            {
                int tempKey = m.GetInt(0);
                if (playerList.ContainsKey(tempKey))
                {
                    Player player = playerList[tempKey];
                    player.isgod = m.GetBoolean(1);
                    lock (playerList)
                    {
                        playerList[tempKey] = player;
                    }
                }
            }
            break;

            case "face":
            {
                int tempKey = m.GetInt(0);
                if (playerList.ContainsKey(tempKey))
                {
                    Player player = playerList[tempKey];
                    player.frame(m.GetInt(1));
                    lock (playerList)
                    {
                        playerList[tempKey] = player;
                    }
                }
            }
            break;

            case "k":     //player got crown
            {
                int userId = m.GetInt(0);
                if (playerList.ContainsKey(userId))
                {
                    lock (playerList)
                    {
                        foreach (Player p in playerList.Values)
                        {
                            p.hascrown = false;
                        }
                        playerList[userId].hascrown = true;
                    }
                }
            }
            break;

            case "ks":     //player got silver crown
            {
                int userId = m.GetInt(0);
                if (playerList.ContainsKey(userId))
                {
                    lock (playerList)
                        playerList[userId].hascrownsilver = true;
                }
            }
            break;

            case "c":     //player took coin
            {
                int userId     = m.GetInt(0);
                int totalCoins = m.GetInt(1);
                if (playerList.ContainsKey(userId))
                {
                    lock (playerList)
                        playerList[userId].coins = totalCoins;
                }
            }
            break;

            case "levelup":
            {
                int userId = m.GetInt(0);
                int level  = m.GetInt(1);
                if (playerList.ContainsKey(userId))
                {
                    lock (playerList)
                        playerList[userId].level = level;
                }
            }
            break;

            case "tele":     //owner used reset/load
            {
                bool resetUsed = m.GetBoolean(0);
                for (int i = 1; i < m.Count; i += 3)
                {
                    int userId    = m.GetInt(1);
                    int spawnPosX = m.GetInt(2);
                    int spawnPosY = m.GetInt(3);
                    if (playerList.ContainsKey(userId))
                    {
                        lock (playerList)
                        {
                            Player p = playerList[userId];
                            p.setPosition(spawnPosX * 16, spawnPosY * 16);
                            playerList[userId] = p;
                        }
                    }
                }
            }
            break;
            }
        }
Example #2
0
    void connection_OnMessage(object sender, PlayerIOClient.Message e)
    {
        // handle incoming messages
        switch (e.Type)
        {
        // server sent us our ID
        case "SetID":
            NetworkUtils.localPlayerID = e.GetInt(0);
            break;

        // add a player to list of players in the room
        case "UserJoined":
            NetworkUtils.PlayersInRoom.Add(e.GetInt(0), e.GetString(1));
            break;

        // remove player from list of players
        case "UserLeft":
            NetworkUtils.PlayersInRoom.Remove(e.GetInt(0));
            //clean up this player's bots
            foreach (ulong botID in BotInfo.botMap.Keys)
            {
                Destroy(BotInfo.botMap[botID].gameObject);
            }
            break;

        // spawn a bot
        case "OnBotSpawned":
            int   spawnedBotOwnerID = e.GetInt(0);
            ulong spawnedBotID      = e.GetULong(1);
            float spawnedBotPosX    = e.GetFloat(2);
            float spawnedBotPosY    = e.GetFloat(3);

            GameObject bot = (GameObject)Instantiate(BotPrefab, new Vector3(spawnedBotPosX, 0f, spawnedBotPosY), Quaternion.identity);
            bot.GetComponent <BotInfo>().OwnerID = spawnedBotOwnerID;
            bot.GetComponent <BotInfo>().BotID   = spawnedBotID;
            break;

        // update a bot
        case "UpdateBot":
            ulong updateBotID     = e.GetULong(0);
            float updatePosX      = e.GetFloat(1);
            float updatePosY      = e.GetFloat(2);
            int   updateBotHealth = e.GetInt(3);

            BotInfo updateBot = BotInfo.botMap[updateBotID];
            updateBot.transform.position = new Vector3(updatePosX, 0f, updatePosY);
            updateBot.SendMessage("SetHealth", updateBotHealth, SendMessageOptions.DontRequireReceiver);
            break;

        // destroy a bot
        case "BotDied":
            // kill bot
            ulong killedBotID = e.GetULong(0);

            BotInfo killedBot = BotInfo.botMap[killedBotID];
            if (killedBot.IsMine)
            {
                // increment lost bots
                NetworkUtils.playerObject.Set("Deaths", NetworkUtils.playerObject.GetInt("Deaths") + 1);
            }

            // destroy bot obj
            GameObject.Destroy(killedBot);
            break;

        // local player got a kill
        case "GotKill":
            // increment kills
            NetworkUtils.playerObject.Set("Kills", NetworkUtils.playerObject.GetInt("Kills") + 1);
            break;

        // one of local player's bots took damage
        case "TookDamage":
            Debug.Log("Taking damage!");
            break;
        }
    }