コード例 #1
0
        // Will replace component if there is a match, if not it will add it as a new component
        public Entity ReplaceComponent(IComponent replaceComponent, bool notifySystems = true)
        {
            if (replaceComponent == null)
            {
                Console.WriteLine("Component that you intented to replace is null, method will return void");
                return(this);
            }

            for (int i = 0; i < components.Count; i++)
            {
                if (components[i].GetType().Equals(replaceComponent.GetType()))
                {
                    components[i].entity = null;
                    components[i]        = null;
                    components[i]        = replaceComponent;

                    // Notifies systems so they can perfom operations, like manipulating componet data
                    if (notifySystems)
                    {
                        SystemObserver.NotifySystems(this);
                    }
                    return(this);
                }
            }

            Console.WriteLine("No match for the component, will be added as a new component to the entity");
            return(AddComponent(replaceComponent, notifySystems));
        }
コード例 #2
0
        // Add new component to the entity, can support doubles of components
        public Entity AddComponent(IComponent newComponent, bool notifySystems = true)
        {
            if (newComponent == null)
            {
                Console.WriteLine("Component that you intented to add is null, method will return void");
                return(this);
            }

            components.Add(newComponent);
            newComponent.entity = this;

            // Notifies systems so they can perfom operations, like manipulating componet data
            if (notifySystems)
            {
                SystemObserver.NotifySystems(this);
            }
            return(this);
        }