Example #1
0
 internal void init(string entityID, bool isClientOwner, float x, float y, float z, float yaw, MapAttr attrs)
 {
     this.ID            = entityID;
     this.IsClientOwner = isClientOwner;
     this.Position      = new Vector3(x, y, z);
     this.Yaw           = yaw;
     this.Attrs         = attrs;
 }
Example #2
0
        static MapAttr convertFromMsgPackObjectDictionary(MsgPack.MessagePackObjectDictionary mpobj)
        {
            MapAttr t = new MapAttr();

            MsgPack.MessagePackObjectDictionary.Enumerator e = mpobj.GetEnumerator();
            while (e.MoveNext())
            {
                MsgPack.MessagePackObject key = e.Current.Key;
                MsgPack.MessagePackObject val = e.Current.Value;
                t.put(key.AsString(), convertFromMsgPackObject(val));
            }
            return(t);
        }
Example #3
0
        internal void OnMapAttrChange(ListAttr path, string key, object val)
        {
            MapAttr t = this.getAttrByPath(path) as MapAttr;

            t.put(key, val);
            string rootkey = path != null && path.Count > 0 ? (string)path.get(0) : key;

            System.Reflection.MethodInfo callback = this.GetType().GetMethod("OnAttrChange_" + rootkey);
            if (callback != null)
            {
                callback.Invoke(this, new object[0]);
            }
        }
Example #4
0
        internal void OnMapAttrClear(ListAttr path)
        {
            System.Diagnostics.Debug.Assert(path != null && path.Count > 0);
            MapAttr t = this.getAttrByPath(path) as MapAttr;

            t.Clear();
            string rootkey = (string)path.get(0);

            System.Reflection.MethodInfo callback = this.GetType().GetMethod("OnAttrChange_" + rootkey);
            if (callback != null)
            {
                callback.Invoke(this, new object[0]);
            }
        }
Example #5
0
        internal void OnMapAttrDel(ListAttr path, string key)
        {
            MapAttr t = this.getAttrByPath(path) as MapAttr;

            if (t.ContainsKey(key))
            {
                t.Remove(key);
            }
            string rootkey = path != null && path.Count > 0 ? (string)path.get(0) : key;

            System.Reflection.MethodInfo callback = this.GetType().GetMethod("OnAttrChange_" + rootkey);
            if (callback != null)
            {
                callback.Invoke(this, new object[0]);
            }
        }
Example #6
0
        private void handleCreateEntityOnClient(Packet pkt)
        {
            bool    isClientOwner = pkt.ReadBool();
            string  entityID      = pkt.ReadEntityID();
            string  typeName      = pkt.ReadVarStr();
            float   x             = pkt.ReadFloat32();
            float   y             = pkt.ReadFloat32();
            float   z             = pkt.ReadFloat32();
            float   yaw           = pkt.ReadFloat32();
            MapAttr attrs         = pkt.ReadData() as MapAttr;

            // this.debug ("Handle Create Entity On Client: IsClientOwner = {0}, EntityID = {1}, TypeName = {2}, Position = {3},{4},{5}, Yaw = {6}, Attrs = {7}", isClientOwner, entityID, typeName, x,y,z, yaw, attrs);
            if (OnCreateEntityOnClient != null)
            {
                this.OnCreateEntityOnClient(typeName, entityID, isClientOwner, x, y, z, yaw, attrs);
            }
        }
Example #7
0
        static MsgPack.MessagePackObject convertToMsgPackObject(object v)
        {
            Type t = v.GetType();

            if (t.Equals(typeof(MapAttr)))
            {
                MapAttr ht = v as MapAttr;
                IDictionaryEnumerator e = ht.GetEnumerator();
                MsgPack.MessagePackObjectDictionary d = new MsgPack.MessagePackObjectDictionary();
                while (e.MoveNext())
                {
                    d.Add(new MsgPack.MessagePackObject(e.Key as string), convertToMsgPackObject(e.Value));
                }
                return(new MsgPack.MessagePackObject(d));
            }
            else if (t.Equals(typeof(ListAttr)))
            {
                ListAttr    al = v as ListAttr;
                IEnumerator e  = al.GetEnumerator();
                System.Collections.Generic.IList <MsgPack.MessagePackObject> l = new System.Collections.Generic.List <MsgPack.MessagePackObject>();
                while (e.MoveNext())
                {
                    l.Add(convertToMsgPackObject(e.Current));
                }
                return(new MsgPack.MessagePackObject(l));
            }
            else if (t.Equals(typeof(bool)))
            {
                return(new MsgPack.MessagePackObject((bool)v));
            }
            else if (t.Equals(typeof(string)))
            {
                return(new MsgPack.MessagePackObject((string)v));
            }
            else
            {
                Debug.Assert(false, "Unknwon type: " + t.Name);
                return(new MsgPack.MessagePackObject());
            }
        }
Example #8
0
        public void Destroy()
        {
            if (this.IsDestroyed)
            {
                return;
            }

            EntityManager.Instance.delEntity(this);

            try
            {
                this.OnDestroy();
            }
            catch (Exception e)
            {
                GoWorldLogger.Error(this.ToString(), e.ToString());
            }

            this.IsDestroyed = true;
            this.Attrs       = null;
            GameObject.Destroy(gameObject);
        }
Example #9
0
 private static void OnCreateEntityOnClient(string typeName, string entityID, bool isClientOwner, float x, float y, float z, float yaw, MapAttr attrs)
 {
     GoWorldLogger.Debug("GoWorld", "OnCreateEntityOnClient {0}<{1}>, IsClientOwner={2}, Attrs={3}, Position={4},{5},{6} ...", typeName, entityID, isClientOwner, attrs, x, y, z);
     ClientEntity e = EntityManager.CreateEntity(typeName, entityID, isClientOwner, x, y, z, yaw, attrs);
 }
Example #10
0
 public static new GameObject CreateGameObject(MapAttr attrs)
 {
     return(new GameObject("ClientSpace", typeof(ClientSpace)));
 }
Example #11
0
 public static GameObject CreateGameObject(MapAttr attrs)
 {
     return(null);
 }
Example #12
0
        internal ClientEntity CreateEntity(string typeName, string entityID, bool isClientOwner, float x, float y, float z, float yaw, MapAttr attrs)
        {
            if (typeName == SPACE_ENTITY_NAME)
            {
                typeName = "ClientSpace";
            }

            GoWorldLogger.Assert(this.entityTypes.ContainsKey(typeName), "Entity Type {0} Not Found", typeName);

            if (this.entities.ContainsKey(entityID))
            {
                ClientEntity old = this.entities[entityID];
                GoWorldLogger.Warn("EntityManager", "Entity {0} Already Exists, Destroying Old One: {1}", entityID, old);
                old.Destroy();
            }

            // create new Game Object of specified type
            Type entityType = this.entityTypes[typeName];

            System.Reflection.MethodInfo createGameObjectMethod = entityType.GetMethod("CreateGameObject");
            GoWorldLogger.Assert(createGameObjectMethod != null, "CreateGameObject Method Not Found For Entity Type {0}", typeName);

            GameObject gameObject = createGameObjectMethod.Invoke(null, new object[1] {
                attrs
            }) as GameObject;

            if (gameObject == null)
            {
                GoWorldLogger.Error("EntityManager", "Fail To Create GameObject For Entity Type {0}, Please Define New CreateGameObject Method Like: public static new GameObject CreateGameObject(MapAttr attrs) { ... }", typeName);
                return(null);
            }

            ClientEntity e = gameObject.GetComponent <ClientEntity>();

            if (e == null || e.GetType().Name != typeName)
            {
                // GameObject created, but wrong entity type
                GameObject.Destroy(gameObject);
                GoWorldLogger.Error("EntityManager", "Fail To Create GameObject For Entity Type {0}: wrong entity type {1}", typeName, e.GetType().Name);
                return(null);
            }

            GameObject.DontDestroyOnLoad(gameObject);
            e.init(entityID, isClientOwner, x, y, z, yaw, attrs);
            this.entities[entityID]       = e;
            gameObject.transform.position = e.Position;
            gameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, e.Yaw, 0f));
            e.onCreated();

            // new entity created
            if (e.IsSpace)
            {
                // enter new space
                if (this.Space != null)
                {
                    this.Space.Destroy();
                }

                this.Space = e as ClientSpace;
                this.onEnterSpace();
            }
            else
            {
                if (e.IsClientOwner)
                {
                    if (this.ClientOwner != null)
                    {
                        GoWorldLogger.Warn("EntityManager", "Replacing Existing Player: " + this.ClientOwner);
                        this.ClientOwner.Destroy();
                    }

                    this.ClientOwner = e;
                    e.becomeClientOwner();
                }

                if (this.Space != null)
                {
                    e.enterSpace();
                }
            }
            return(e);
        }