/// <summary>
 /// Get Instance of this class
 /// </summary>
 public static Packet_Deserialisation GetInstance()
 {
     if (Instance == null)
     {
         Instance = new Packet_Deserialisation();
     }
     return(Instance);
 }
Exemple #2
0
    /* Receive data of events from server */
    private void OnEventHandler(byte eventCode, object content, int senderId)
    {
        switch (eventCode)
        {
            #region Assignment 1
        //case 2:
        //    Debug.Log(string.Format("Message from Server : {0}", (string)content));
        //    break;
        //case 3:
        //    m_PlayerPosition = (string)content;
        //    Debug.Log("position : " + m_PlayerPosition);
        //    break;
        case 4:
            if (content != null)
            {
                m_PlayerPosition = content.ToString();
            }
            //Debug.Log("position : " + m_PlayerPosition);
            break;

        case 5:
            if (content != null)
            {
                m_PlayerHealth = content.ToString();
            }
            //Debug.Log("health : " + m_PlayerHealth);
            break;

        case 7:
            if (content != null)
            {
                m_PlayerScore = content.ToString();
            }
            //Debug.Log("Score: " + m_PlayerScore);
            //LoadPlayer();
            break;

            #endregion
        // --------------------------- Assignment 2
        case 12:     // spawn Enemy AI
            if (content == null)
            {
                Debug.Log("content is null");
                break;
            }

            // Deserialise info received from server
            GameObject enemy = Packet_Deserialisation.GetInstance().DeserializeCustomAI((byte[])content) as GameObject;

            // Check if Enemy is already spawned
            foreach (GameObject _enemy in GameObject.FindGameObjectsWithTag("Enemy"))
            {
                if (_enemy.GetComponent <EnemyAI>().GetName() == enemy.GetComponent <EnemyAI>().GetName())
                {
                    Debug.Log("Already in Scene!");
                    Debug.Log("Enemy name : " + enemy.GetComponent <EnemyAI>().GetName());
                    Debug.Log("Enemy in scene name : " + _enemy.GetComponent <EnemyAI>().GetName());
                    return;
                }
            }
            // instantiate enemies
            GameObject newEnemy = Instantiate(enemyPrefab, GameObject.Find("EnemyHolder").transform);
            // set variables
            newEnemy.GetComponent <EnemyAI>().SetTargetPacket(enemy.GetComponent <EnemyAI>().GetTargetPacket());
            newEnemy.GetComponent <EnemyAI>().SetName(enemy.GetComponent <EnemyAI>().GetName());
            newEnemy.GetComponent <EnemyAI>().SetPosition(enemy.GetComponent <EnemyAI>().GetPosition());
            newEnemy.GetComponent <EnemyAI>().SetHealth(enemy.GetComponent <EnemyAI>().GetHealth());
            newEnemy.GetComponent <EnemyAI>().SetGrid(enemy.GetComponent <EnemyAI>().GetGrid());
            newEnemy.GetComponent <EnemyAI>().SetPrevPos(enemy.GetComponent <EnemyAI>().GetPrevPos());
            newEnemy.GetComponent <EnemyAI>().SetNearestPos(enemy.GetComponent <EnemyAI>().GetNearestPos());

            // set position of newEnemy
            newEnemy.transform.position = newEnemy.GetComponent <EnemyAI>().GetPosition();
            Debug.Log("Position : " + newEnemy.transform.position);

            // destroy the enemy, which will then destroy the gameobject created in deserialise
            DestroyImmediate(enemy);
            break;

        case 13:     // updating the enemy AI
            if (content == null)
            {
                Debug.Log("Content is null");
                return;
            }

            // Deserialise info received from server
            GameObject infoEnemy = Packet_Deserialisation.GetInstance().DeserializeCustomAI((byte[])content) as GameObject;
            Debug.Log("enemy in server info : " + infoEnemy.GetComponent <EnemyAI>().GetName());

            // Check if infoEnemy has same name as Enemy in Scene
            foreach (GameObject _enemy in GameObject.FindGameObjectsWithTag("Enemy"))
            {
                Debug.Log("enemy in scene : " + _enemy.GetComponent <EnemyAI>().GetName());
                if (infoEnemy.GetComponent <EnemyAI>().GetName() == _enemy.GetComponent <EnemyAI>().GetName())
                {
                    Debug.Log("Enemy has same name as sent info");
                    _enemy.transform.position = infoEnemy.GetComponent <EnemyAI>().GetPosition();
                }
            }
            DestroyImmediate(infoEnemy);

            break;

        case 99:
            //Decode string packet
            string serverMessage = "";
            try
            {
                serverMessage = string.Format("{0}", (string)content);
                Debug.Log("From Server: " + serverMessage);
            }
            catch (System.Exception e)
            {
                Debug.Log("Exception : " + e.ToString());
            }
            break;
        }
    }