// Called when player connects with their phone
    void InitializeNetPlayer(SpawnInfo spawnInfo)
    {
        Init();

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

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

        m_playerNameManager = new HFTPlayerNameManager(m_netPlayer);
        m_playerNameManager.OnNameChange += HandleNameChange;

        // We always get a `data` so check if it has one key from our expected message
        Dictionary <string, object> dict = spawnInfo.data as Dictionary <string, object>;

        if (dict != null && dict.ContainsKey("dir"))
        {
            // This player was transferred from another game.

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

            // Choose a starting position based on the old position
            float x = (data.pos.x < MultiMachineLevelSettings.settings.leftEdgeOfLevel.position.x)
                ? MultiMachineLevelSettings.settings.rightEdgeOfLevel.position.x - 1 : MultiMachineLevelSettings.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_playerNameManager.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);
        }
    }
Esempio n. 2
0
    public static void ShowSerializedMessage()
    {
        List <gm.sendIDMessageTP> sendIDMessageTPs = new List <gm.sendIDMessageTP>();
        Texture2D texture = Resources.Load <Texture2D>("Placeholders/test");

        Debug.Log(texture.GetPixel(0, 0));
        for (int i = 0; i < 10; i++)
        {
            sendIDMessageTPs.Add(new gm.sendIDMessageTP(i.ToString(), texture));
        }
        gm.listMessageTP listMessageTP = new gm.listMessageTP(sendIDMessageTPs);
        Debug.Log(DeJson.Serializer.Serialize(sendIDMessageTPs[0]));
        string output = DeJson.Serializer.Serialize(listMessageTP);

        Debug.Log(output);
        DeJson.Deserializer deserializer = new DeJson.Deserializer();
        listMessageTP = deserializer.Deserialize <gm.listMessageTP>(output);
        //Debug.Log(((Texture2D)listMessageTP._messages[0].playerPhoto).GetPixel(0, 0));
    }
Esempio n. 3
0
        private void StartPlayer(MessageToClient msg)
        {
            if (m_players.ContainsKey(msg.id))
            {
                return;
            }

            string name = "";

            RealNetPlayer.Options options = new RealNetPlayer.Options();
            if (msg.data != null)
            {
                DeJson.Deserializer deserializer = new DeJson.Deserializer();
                HFTPlayerStartData  startData    = deserializer.Deserialize <HFTPlayerStartData>(msg.data);
                if (startData != null)
                {
                    options.sessionId = startData.__hft_session_id__;
                    name = startData.__hft_name__;
                }
            }

            if (System.String.IsNullOrEmpty(name))
            {
                name = "Player" + (++m_totalPlayerCount);
            }

            NetPlayer player = new RealNetPlayer(this, msg.id, name, options);

            m_players[msg.id] = player;
            QueueEvent(() =>
            {
                var handler = OnPlayerConnect;
                if (handler != null)
                {
                    handler(new PlayerConnectMessageArgs(player, name, msg.data));
                }
            });
        }
Esempio n. 4
0
        private void SerializeValue(object obj)
        {
            if (obj == null)
            {
                m_builder.Append("undefined");
                return;
            }
            System.Type type = obj.GetType();

            if (type.IsArray)
            {
                SerializeArray(obj);
            }
            else if (Deserializer.IsGenericList(type)) //(type == typeof(List<Object>)) {
            {
                Type elementType = type.GetGenericArguments()[0];
                System.Reflection.MethodInfo castMethod    = typeof(System.Linq.Enumerable).GetMethod("Cast").MakeGenericMethod(new System.Type[] { elementType });
                System.Reflection.MethodInfo toArrayMethod = typeof(System.Linq.Enumerable).GetMethod("ToArray").MakeGenericMethod(new System.Type[] { elementType });
                var castedObjectEnum = castMethod.Invoke(null, new object[] { obj });
                var castedObject     = toArrayMethod.Invoke(null, new object[] { castedObjectEnum });
//            object[] oArray = ((List<object>)obj).ToArray();
//            SerializeArray(oArray);
                SerializeArray(castedObject);
            }
            else if (type.IsEnum)
            {
                SerializeString(obj.ToString());
            }
            else if (type == typeof(string))
            {
                SerializeString(obj as string);
            }
            else if (type == typeof(Char))
            {
                SerializeString(obj.ToString());
            }
            else if (type == typeof(bool))
            {
                m_builder.Append((bool)obj ? "true" : "false");
            }
            else if (type == typeof(Boolean))
            {
                m_builder.Append((Boolean)obj ? "true" : "false");
//        } else if (type.IsPrimitive) {
//            m_builder.Append(System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertToInvariantString(obj));

                m_builder.Append(Convert.ChangeType(obj, typeof(string)));
            }
            else if (type == typeof(int))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(Byte))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(SByte))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(Int16))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(UInt16))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(Int32))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(UInt32))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(Int64))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(UInt64))
            {
                m_builder.Append(obj);
            }
            else if (type == typeof(Single))
            {
                m_builder.Append(((Single)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (type == typeof(Double))
            {
                m_builder.Append(((Double)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (type == typeof(float))
            {
                m_builder.Append(((float)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (type == typeof(double))
            {
                m_builder.Append(((double)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (type.IsValueType)
            {
                SerializeObject(obj);
            }
            else if (type.IsClass)
            {
                SerializeObject(obj);
            }
            else
            {
                throw new System.InvalidOperationException("unsupport type: " + type.Name);
            }
        }
Esempio n. 5
0
        private object ConvertToType(object value, System.Type type, Dictionary <string, object> src)
        {
            if (type.IsArray)
            {
                return(ConvertToArray(value, type, src));
            }
            else if (Deserializer.IsGenericList(type))
            {
                return(ConvertToList(value, type, src));
            }
            else if (Deserializer.IsGenericDictionary(type))
            {
                return(ConvertToDictionary(value, type, src));
//        } else if (type == typeof(List<object>)) {
//            object[] oArray = ((List<object>)value).ToArray();
//            return ConvertToArray(oArray, type, src);
//        } else if (type.IsPrimitive) {
//           return System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value.ToString ());
            }
            else if (type.IsEnum)
            {
                return(System.Enum.Parse(type, Convert.ToString(value)));
            }
            else if (type == typeof(string))
            {
                return(Convert.ToString(value));
            }
            else if (type == typeof(Byte))
            {
                return(Convert.ToByte(value));
            }
            else if (type == typeof(SByte))
            {
                return(Convert.ToSByte(value));
            }
            else if (type == typeof(Int16))
            {
                return(Convert.ToInt16(value));
            }
            else if (type == typeof(UInt16))
            {
                return(Convert.ToUInt16(value));
            }
            else if (type == typeof(Int32))
            {
                return(Convert.ToInt32(value));
            }
            else if (type == typeof(UInt32))
            {
                return(Convert.ToUInt32(value));
            }
            else if (type == typeof(Int64))
            {
                return(Convert.ToInt64(value));
            }
            else if (type == typeof(UInt64))
            {
                return(Convert.ToUInt64(value));
            }
            else if (type == typeof(Char))
            {
                return(Convert.ToChar(value));
            }
            else if (type == typeof(Double))
            {
                return(Convert.ToDouble(value));
            }
            else if (type == typeof(Single))
            {
                return(Convert.ToSingle(value));
            }
            else if (type == typeof(int))
            {
                return(Convert.ToInt32(value));
            }
            else if (type == typeof(float))
            {
                return(Convert.ToSingle(value));
            }
            else if (type == typeof(double))
            {
                return(Convert.ToDouble(value));
            }
            else if (type == typeof(bool))
            {
                return(Convert.ToBoolean(value));
            }
            else if (type == typeof(Boolean))
            {
                return(Convert.ToBoolean(value));
            }
            else if (type.IsValueType)
            {
                return(DeserializeO(type, (Dictionary <string, object>)value, src));
            }
            else if (type == typeof(System.Object))
            {
                return(value);
            }
            else if (type.IsClass)
            {
                return(DeserializeO(type, (Dictionary <string, object>)value, src));
            }
            else
            {
                // Should we throw here?
            }
            return(value);
        }