public virtual ProvenceComponent Clone()
        {
            ProvenceComponent clone = this.MemberwiseClone() as ProvenceComponent;

            clone.id = System.Guid.NewGuid().ToString();
            return(clone);
        }
Exemple #2
0
        public static HashSet <ProvenceComponent> Clone(this HashSet <ProvenceComponent> set)
        {
            Dictionary <ProvenceComponent, ProvenceComponent> componentCloneCache = new Dictionary <ProvenceComponent, ProvenceComponent>();

            foreach (dynamic component in set)
            {
                ProvenceComponent newComponent = component.Clone();
                componentCloneCache[component] = newComponent;
            }

            foreach (ProvenceComponent component in componentCloneCache.Values)
            {
                FieldInfo[] fields = component.GetType().GetFields();
                foreach (FieldInfo field in fields)
                {
                    object fieldValue = field.GetValue(component);

                    if (fieldValue is ProvenceComponent fieldComponent)
                    {
                        if (componentCloneCache.ContainsKey(fieldComponent))
                        {
                            field.SetValue(component, componentCloneCache[fieldComponent]);
                        }
                        else
                        {
                            field.SetValue(component, fieldComponent.Clone());
                        }
                        continue;
                    }

                    // if(fieldValue is System.Collections.IEnumerable collection){
                    //     System.Type[] genericTypes = collection.GetType().GenericTypeArguments;
                    //     if(genericTypes.Length == 1 && collection.GetType().GenericTypeArguments[0].IsSubclassOf(typeof(ProvenceComponent))){

                    //     }
                    //     /* System.Type[] genericTypes = collection.GetType().GenericTypeArguments;
                    //     for(int i = 0; i < genericTypes.Length; i++){
                    //         if(genericTypes[i] == typeof(ProvenceComponent) || genericTypes[i].IsSubclassOf(typeof(ProvenceComponent))){
                    //             if(collection is System.Collections.IDictionary dict){

                    //             }else{
                    //                 if(i == 0){

                    //                 }
                    //             }
                    //         }
                    //     }  */
                    // }
                }
            }

            return(new HashSet <ProvenceComponent>(componentCloneCache.Values));
        }
Exemple #3
0
        public static Dictionary <Entity, HashSet <ProvenceComponent> > Clone(this Dictionary <Entity, HashSet <ProvenceComponent> > dict)
        {
            Dictionary <Entity, Entity> entityCloneCache = new Dictionary <Entity, Entity>();
            Dictionary <ProvenceComponent, ProvenceComponent> componentCloneCache = new Dictionary <ProvenceComponent, ProvenceComponent>();
            Dictionary <Entity, HashSet <ProvenceComponent> > ecCloneCache        = new Dictionary <Entity, HashSet <ProvenceComponent> >();

            foreach (KeyValuePair <Entity, HashSet <ProvenceComponent> > kvp in dict)
            {
                Entity newEntity = new Entity();
                entityCloneCache[kvp.Key] = new Entity();
                ecCloneCache[newEntity]   = new HashSet <ProvenceComponent>();
                foreach (dynamic component in kvp.Value)
                {
                    ProvenceComponent newComponent = component.Clone();
                    componentCloneCache[component] = newComponent;
                    ecCloneCache[newEntity].Add(newComponent);
                }
            }

            foreach (HashSet <ProvenceComponent> set in ecCloneCache.Values)
            {
                foreach (ProvenceComponent component in set)
                {
                    FieldInfo[] fields = component.GetType().GetFields();
                    foreach (FieldInfo field in fields)
                    {
                        object fieldValue = field.GetValue(component);

                        if (fieldValue is Entity entity)
                        {
                            if (entityCloneCache.ContainsKey(entity))
                            {
                                field.SetValue(component, entityCloneCache[entity]);
                            }
                            continue;
                        }

                        if (fieldValue is ProvenceComponent fieldComponent)
                        {
                            if (componentCloneCache.ContainsKey(fieldComponent))
                            {
                                field.SetValue(component, componentCloneCache[fieldComponent]);
                            }
                        }
                    }
                }
            }

            return(ecCloneCache);
        }
Exemple #4
0
 public ComponentHandle <ProvenceComponent> GetComponent(Entity entity, Type type)
 {
     if (entity == null)
     {
         return(null);
     }
     if (componentDictionary.ContainsKey(type))
     {
         if (!componentDictionary[type].ContainsKey(entity))
         {
             return(null);
         }
         ProvenceComponent component = componentDictionary[type][entity];
         return(new ComponentHandle <ProvenceComponent>(entity, component, world));
     }
     return(null);
 }