Ejemplo n.º 1
0
    void Parse()
    {
        string message = "";

        lock (messageQueue)
        {
            if (messageQueue.Count > 0)
            {
                message = messageQueue.Dequeue();
            }
        }

        if (message != "")
        {
            try
            {
                UnityEngine.Debug.Log(message);
                JsonData data = JsonMapper.ToObject(message);
                int      type = (int)data["type"];
                switch (type)
                {
                case (int)Packet.eType.GC_CONNECT:
                {
                    uint uniqueConnID = uint.Parse(data["uniqueConnID"].ToString());
                    GlobalSetting.Instance.UniqueConnID = uniqueConnID;
                    UnityEngine.Debug.Log("Packet.eType.GC_CONNECT:" + uniqueConnID.ToString());

                    //Create My Character (Instatiate and add to dictionary)
                    GlobalSetting.Instance.MyCharacterObj = (GameObject)Instantiate(characterPrefab, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
                    GlobalSetting.Instance.MyCharacterObj.GetComponent <PlayerCharacter>().IsInputPlayer = true;
                    _dicCharacters.Add(uniqueConnID, GlobalSetting.Instance.MyCharacterObj);
                    //Set Camera Target
                    Camera.main.transform.GetComponent <CameraMove>().TargetPlayer = GlobalSetting.Instance.MyCharacterObj;
                }
                break;

                case (int)Packet.eType.GC_CHAT:
                {
                    string chatData = (string)data["chatData"];
                    //string nickName = (string)data["nickName"];
                    UIChatting.GetComponent <ChatInput>().AddText(chatData);
                }
                break;

                case (int)Packet.eType.GC_MONSTER_STATE:
                {
                    int   state       = int.Parse(data["state"].ToString());
                    int   typeID      = int.Parse(data["typeID"].ToString());
                    uint  monUniqueID = uint.Parse(data["uniqueID"].ToString());
                    float posX        = float.Parse(data["posX"].ToString());
                    float posY        = float.Parse(data["posY"].ToString());
                    float posZ        = float.Parse(data["posZ"].ToString());
                    float dirX        = float.Parse(data["dirX"].ToString());
                    float dirY        = float.Parse(data["dirY"].ToString());
                    float dirZ        = float.Parse(data["dirZ"].ToString());

                    // temp code
                    // @todo: Dic
                    //GameObject monster = GameObject.Find("Monster");
                    GameObject monster = null;
                    if (_dicMonsters.ContainsKey(monUniqueID))
                    {
                        monster = _dicMonsters[monUniqueID];
                    }
                    else
                    {
                        monster = Instantiate(monsterPrefab) as GameObject;
                        _dicMonsters.Add(monUniqueID, monster);
                    }


                    if (monster != null)
                    {
                        Vector3 pos = new Vector3(posX, posY, posZ);
                        if (state == (int)CharacterState.eState.E_IDLE)
                        {
                            monster.transform.position = pos;
                            monster.transform.rotation = Quaternion.LookRotation(new Vector3(dirX, dirY, dirZ));
                            monster.GetComponent <MonsterCharacter>().SetStateIdle();
                        }
                        else if (state == (int)CharacterState.eState.E_WALK)
                        {
                            monster.transform.position = pos;
                            monster.transform.rotation = Quaternion.LookRotation(new Vector3(dirX, dirY, dirZ));
                            monster.GetComponent <MonsterCharacter>().SetStateWalk();
                        }
                    }
                }
                break;

                case (int)Packet.eType.GC_PLAYER_STATE:
                {
                    Debug.Log("Packet.eType.GC_PLAYER_STATE");
                    uint uniqueConnID = uint.Parse(data["uniqueConnID"].ToString());
                    int  state        = int.Parse(data["state"].ToString());
                    //position
                    float posX = float.Parse(data["posX"].ToString());
                    float posY = float.Parse(data["posY"].ToString());
                    float posZ = float.Parse(data["posZ"].ToString());
                    //forward
                    float dirX  = float.Parse(data["dirX"].ToString());
                    float dirY  = float.Parse(data["dirY"].ToString());
                    float dirZ  = float.Parse(data["dirZ"].ToString());
                    float speed = float.Parse(data["speed"].ToString());

                    //Except my packet
                    if (uniqueConnID != GlobalSetting.Instance.UniqueConnID)
                    {
                        //other player setting.

                        //characterPrefab
                        //1. Find Character in Dictionary
                        //If the character is not exist,
                        //Create Character using characterPrefab
                        //(Instatiate and add to dictionary)
                        if (_dicCharacters.ContainsKey(uniqueConnID))
                        {
                            GameObject player = _dicCharacters[uniqueConnID];
                            player.transform.position = new Vector3(posX, posY, posZ);
                            player.transform.rotation = Quaternion.LookRotation(new Vector3(dirX, dirY, dirZ));

                            player.GetComponent <PlayerCharacter>().SetStateByNetwork(state);
                        }
                        else
                        {
                            Vector3 pos = new Vector3(posX, posY, posZ);

                            Quaternion rot = Quaternion.LookRotation(new Vector3(dirX, dirY, dirZ));
                            GameObject obj = (GameObject)Instantiate(characterPrefab, pos, rot);

                            _dicCharacters.Add(uniqueConnID, obj);
                        }
                    }
                }
                break;
                }
            }
            catch (Exception e)
            {
                Debug.Log(e);
            }
        }
    }