Пример #1
0
        private void RegisterGameComponents()
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(Path.Combine(CaravelApp.Instance.GetGameWorkingDirectory(), CaravelApp.Instance.ComponentDescriptionLocation));
            var root = doc.DocumentElement;

            if (root != null)
            {
                foreach (XmlElement elem in root.ChildNodes)
                {
                    string name      = elem.Attributes["name"].Value;
                    string nameSpace = elem.Attributes["namespace"].Value;

                    if (name != null && name != "" && nameSpace != null)
                    {
                        var assembly      = Assembly.GetEntryAssembly();
                        var componentType = assembly.GetType(nameSpace + "." + name);
                        var method        = ComponentFactory.GetType().GetMethods().Single(m => m.Name == "Register" && m.IsGenericMethodDefinition);
                        method = method.MakeGenericMethod(componentType);
                        object[] arguments = { Cv_EntityComponent.GetID(name) };
                        method.Invoke(ComponentFactory, arguments);

                        m_GameComponentInfo[Cv_EntityComponent.GetID(name)] = elem;
                    }
                }
            }
        }
Пример #2
0
        virtual protected internal Cv_EntityComponent[] ModifyEntity(Cv_Entity entity, XmlNodeList overrides)
        {
            var modifiedComponents = new List <Cv_EntityComponent>();

            foreach (XmlElement componentNode in overrides)
            {
                var componentID = Cv_EntityComponent.GetID(componentNode.Name);
                var component   = entity.GetComponent(componentID);

                if (component != null)
                {
                    component.VInitialize(componentNode);
                    component.VOnChanged();
                    modifiedComponents.Add(component);
                }
                else
                {
                    component = CreateComponent(componentNode);
                    if (component != null)
                    {
                        entity.AddComponent(component);
                        modifiedComponents.Add(component);
                    }
                }
            }

            return(modifiedComponents.ToArray());
        }
Пример #3
0
        internal void AddComponent(Cv_EntityComponent component)
        {
            RemoveComponent(component.ID);

            lock (m_ComponentMap)
            {
                m_ComponentMap.Add(component.ID, component);
                m_ComponentsToAdd.Add(component);
                component.Owner = this;
            }
        }
Пример #4
0
        //Note(JM): Used for editor
        #if EDITOR
        public void AddComponent(string componentTypeName, Cv_EntityComponent component)
        {
            RemoveComponent(componentTypeName);

            lock (m_ComponentMap)
            {
                m_ComponentMap.Add(Cv_EntityComponent.GetID(componentTypeName), component);
                m_ComponentsToAdd.Add(component);
                component.Owner = this;
            }
        }
Пример #5
0
        virtual protected internal Component CreateComponent <Component>() where Component : Cv_EntityComponent
        {
            var component = (Component)ComponentFactory.Create(Cv_EntityComponent.GetID(typeof(Component)));

            if (component == null)
            {
                Cv_Debug.Error("Couldn't find component " + typeof(Component).Name + ". All components must be registered before use.");
            }

            return(component);
        }
Пример #6
0
        virtual protected internal Cv_EntityComponent CreateComponent(string componentName)
        {
            var component = ComponentFactory.Create(Cv_EntityComponent.GetID(componentName));

            if (component == null)
            {
                Cv_Debug.Error("Couldn't find component " + componentName + ". All components must be registered before use.");
            }

            return(component);
        }
Пример #7
0
        private Cv_EntityComponent GetComponent(Type componentType)
        {
            lock (m_ComponentMap)
            {
                Cv_EntityComponent component;
                if (m_ComponentMap.TryGetValue(Cv_EntityComponent.GetID(componentType), out component))
                {
                    return(component);
                }
            }

            return(null);
        }
Пример #8
0
        private void RemoveComponent(Type componentType)
        {
            var component = GetComponent(componentType);

            if (component != null)
            {
                lock (m_ComponentMap)
                {
                    m_ComponentsToRemove.Add(component);
                    m_ComponentMap.Remove(Cv_EntityComponent.GetID(componentType));
                    component.OnDestroy();
                    component.Owner = null;
                }
            }
        }
Пример #9
0
        internal void RemoveComponent <Component>() where Component : Cv_EntityComponent
        {
            var component = GetComponent <Component>();

            if (component != null)
            {
                lock (m_ComponentMap)
                {
                    m_ComponentsToRemove.Add(component);
                    m_ComponentMap.Remove(Cv_EntityComponent.GetID <Component>());
                    component.OnDestroy();
                    component.Owner = null;
                }
            }
        }
Пример #10
0
        public Cv_EntityComponent GetComponent(string componentName)
        {
            Cv_EntityComponent component;
            Cv_ComponentID     componentID = Cv_EntityComponent.GetID(componentName);

            lock (m_ComponentMap)
            {
                if (m_ComponentMap.TryGetValue(componentID, out component))
                {
                    return(component);
                }
            }

            return(null);
        }
Пример #11
0
        public Component GetComponent <Component>() where Component : Cv_EntityComponent
        {
            Cv_EntityComponent component;
            Cv_ComponentID     componentID = Cv_EntityComponent.GetID <Component>();

            lock (m_ComponentMap)
            {
                if (m_ComponentMap.TryGetValue(componentID, out component))
                {
                    return((Component)component);
                }
            }

            return(null);
        }
Пример #12
0
        virtual protected Cv_EntityComponent CreateComponent(XmlElement componentData)
        {
            var component = ComponentFactory.Create(Cv_EntityComponent.GetID(componentData.Name));

            if (component != null)
            {
                if (!component.VInitialize(componentData))
                {
                    Cv_Debug.Error("Failed to initialize component: " + componentData.Name);
                    return(null);
                }
            }
            else
            {
                Cv_Debug.Error("Couldn't find component " + componentData.Name + ". All components must be registered before use.");
            }

            return(component);
        }
Пример #13
0
        protected internal Cv_EntityFactory()
        {
            m_Mutex             = new object();
            m_GameComponentInfo = new Dictionary <Cv_ComponentID, XmlElement>();
            ComponentFactory    = new GenericObjectFactory <Cv_EntityComponent, Cv_ComponentID>();

            ComponentFactory.Register <Cv_TransformComponent>(Cv_EntityComponent.GetID <Cv_TransformComponent>());
            ComponentFactory.Register <Cv_SpriteComponent>(Cv_EntityComponent.GetID <Cv_SpriteComponent>());
            ComponentFactory.Register <Cv_CameraComponent>(Cv_EntityComponent.GetID <Cv_CameraComponent>());
            ComponentFactory.Register <Cv_RigidBodyComponent>(Cv_EntityComponent.GetID <Cv_RigidBodyComponent>());
            ComponentFactory.Register <Cv_SoundEmitterComponent>(Cv_EntityComponent.GetID <Cv_SoundEmitterComponent>());
            ComponentFactory.Register <Cv_SoundListenerComponent>(Cv_EntityComponent.GetID <Cv_SoundListenerComponent>());
            ComponentFactory.Register <Cv_ScriptComponent>(Cv_EntityComponent.GetID <Cv_ScriptComponent>());
            ComponentFactory.Register <Cv_ClickableComponent>(Cv_EntityComponent.GetID <Cv_ClickableComponent>());
            ComponentFactory.Register <Cv_TextComponent>(Cv_EntityComponent.GetID <Cv_TextComponent>());
            ComponentFactory.Register <Cv_TransformAnimationComponent>(Cv_EntityComponent.GetID <Cv_TransformAnimationComponent>());
            ComponentFactory.Register <Cv_ParticleEmitterComponent>(Cv_EntityComponent.GetID <Cv_ParticleEmitterComponent>());

            if (!CaravelApp.Instance.EditorRunning)
            {
                RegisterGameComponents();
            }
        }
Пример #14
0
 public static string GetComponentName(Cv_EntityComponent component)
 {
     return(component.GetType().Name);
 }