Exemple #1
0
        public static SceneData LoadData(IEnumerable <List <EntityData> > enititySets)
        {
            var components          = new Dictionary <Type, Dictionary <EntityID, IComponent> >();
            var singletonComponents = new Dictionary <Type, Tuple <EntityID, ISingletonComponent> >();
            var tags = new Dictionary <string, EntityID>();



            foreach (var entityList in enititySets)
            {
                foreach (var entity in entityList)
                {
                    EntityID id = EntityID.GenerateNew();
                    foreach (var component in entity)
                    {
                        if (component.Item1 == typeof(TagComponent))
                        {
                            string tag = ((TagComponent)component.Item2).Tag;
                            if (!tags.ContainsKey(tag))
                            {
                                tags.Add(tag, id);
                            }
                        }

                        if (!components.ContainsKey(component.Item1))
                        {
                            var newArchetype = new Dictionary <EntityID, IComponent>();
                            newArchetype.Add(id, component.Item2);

                            components.Add(component.Item1, newArchetype);
                        }
                        else if (components.TryGetValue(component.Item1, out Dictionary <EntityID, IComponent> archetype))
                        {
                            archetype.Add(id, component.Item2);
                        }
                    }
                }
            }

            return(new SceneData(components, singletonComponents, tags));
        }