Esempio n. 1
0
 internal void onCreated()
 {
     try
     {
         this.OnCreated();
     }
     catch (Exception e)
     {
         GoWorldLogger.Error(this.ToString(), e.ToString());
     }
 }
Esempio n. 2
0
 internal void tick()
 {
     try
     {
         this.Tick();
     }
     catch (Exception e)
     {
         GoWorldLogger.Error(this.ToString(), e.ToString());
     }
 }
Esempio n. 3
0
 internal void becomeClientOwner()
 {
     try
     {
         this.OnBecomeClientOwner();
     }
     catch (Exception e)
     {
         GoWorldLogger.Error(this.ToString(), e.ToString());
     }
 }
Esempio n. 4
0
 internal void enterSpace()
 {
     try
     {
         this.OnEnterSpace();
     }
     catch (Exception e)
     {
         GoWorldLogger.Error(this.ToString(), e.ToString());
     }
 }
Esempio n. 5
0
        public Int64 GetInt(string key)
        {
            object val = this.get(key);

            try
            {
                return((Int64)val);
            } catch (InvalidCastException)
            {
                GoWorldLogger.Error("MapAttr", "Can Not Convert From Type {0} To Int64", val.GetType());
                return(0);
            }
        }
Esempio n. 6
0
        internal void DestroyEntity(string entityID)
        {
            GoWorldLogger.Debug("EntityManager", "Destroy Entity {0}", entityID);
            ClientEntity entity;

            if (this.entities.TryGetValue(entityID, out entity))
            {
                entity.Destroy();
            }
            else
            {
                GoWorldLogger.Error("EntityManager", "Destroy Entity {0} Failed: Entity Not Found", entityID);
            }
        }
Esempio n. 7
0
        internal Packet RecvPacket()
        {
            if (this.tcpClient.Available == 0)
            {
                return(null);
            }
            int nr;

            if (this.recvState == RecvState.receivingPayloadLen)
            {
                if (tcpClient.Available < Proto.SIZE_FIELD_SIZE)
                {
                    return(null);
                }

                nr = tcpClient.Client.Receive(this.recvPayloadLenBuff);
                Debug.Assert(nr == Proto.SIZE_FIELD_SIZE);

                this.recvPayloadLen = BitConverter.ToUInt32(recvPayloadLenBuff, 0);
                if (this.recvPayloadLen < 2 || this.recvPayloadLen > Proto.MAX_PAYLOAD_LEN)
                {
                    GoWorldLogger.Error("PacketReceiver", "Invalid Packet Payload Length: " + this.recvPayloadLen);
                    this.tcpClient.Close();
                    return(null);
                }
                this.recvState       = RecvState.receivingPayload;
                this.recvPayloadBuff = new byte[this.recvPayloadLen];
            }

            if (tcpClient.Available < this.recvPayloadLen)
            {
                return(null);
            }

            nr = tcpClient.Client.Receive(this.recvPayloadBuff);
            Debug.Assert(nr == this.recvPayloadLen);
            this.recvState = RecvState.receivingPayloadLen;
            byte[] payload = this.recvPayloadBuff;
            this.recvPayloadBuff = null;
            return(new Packet(payload));
        }
Esempio n. 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);
        }
Esempio n. 9
0
        internal void CallEntity(string entityID, string method, object[] args)
        {
            ClientEntity entity;

            if (this.entities.TryGetValue(entityID, out entity))
            {
                System.Reflection.MethodInfo methodInfo = entity.GetType().GetMethod(method);
                if (methodInfo == null)
                {
                    GoWorldLogger.Error("EntityManager", "Call Entity {0}.{1}({2} Args) Failed: Public Method Not Found", entity, method, args.Length);
                    return;
                }

                GoWorldLogger.Debug("EntityManager", "Call Entity {0}: {1}({2} Args)", entity, method, args.Length);
                methodInfo.Invoke(entity, args);
            }
            else
            {
                // entity not found
                GoWorldLogger.Error("EntityManager", "Call Entity {0}.{1}({2} Args) Failed: Entity Not Found", entityID, method, args.Length);
            }
        }
Esempio n. 10
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);
        }