예제 #1
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);
        }