Beispiel #1
0
    void Sync(SocketIOEvent e)
    {
        if (!isGameRunning)
        {
            return;
        }

        List <JSONObject> receivedPlayersList = e.data["players"].list;

        for (int i = 0; i < receivedPlayersList.Count; i++)
        {
            JSONObject player = receivedPlayersList[i];

            Player playerInList = playersList.Find(x => x.id == player["id"].str);
            if (playerInList == null)
            {
                GameObject go = Instantiate(playerPrefab, Vector2.zero, Quaternion.identity) as GameObject;
                playerInList = go.GetComponent <Player>();
                playersList.Add(playerInList);
            }
            playerInList.SetData(player);

            if (player["id"].str == this.playerId)
            {
                if (this.isPlayerAlive && player["status"].str != "alive")
                {
                    this.isPlayerAlive = false;
                    cameraBehavior.SetObserver();
                }
            }
        }

        List <JSONObject> receivedSpellsList = e.data["spells"].list;

        for (int i = 0; i < receivedSpellsList.Count; i++)
        {
            JSONObject spell       = receivedSpellsList[i];
            Spell      spellOnList = spellsList.Find(x => x.id == spell["id"].str);
            if (spellOnList == null)
            {
                string spellType = spell["type"].str;
                if (spellType == "fireball")
                {
                    spellOnList = Instantiate(fireballPrefab, JSONTemplates.ToVector2(spell["position"]), Quaternion.identity).GetComponent <Spell>();
                }
                else if (spellType == "follower")
                {
                    spellOnList = Instantiate(followerPrefab, JSONTemplates.ToVector2(spell["position"]), Quaternion.identity).GetComponent <Spell>();
                }
                else if (spellType == "boomerang")
                {
                    spellOnList = Instantiate(boomerangPrefab, JSONTemplates.ToVector2(spell["position"]), Quaternion.identity).GetComponent <Spell>();
                }
                spellsList.Add(spellOnList);
            }
            spellOnList.SetData(spell);
        }
    }