Beispiel #1
0
        public Entity(EntityDec template, params object[] insertions)
        {
            dec        = template;
            components = new object[Dec.Database <ComponentDec> .Count];

            if (insertions != null)
            {
                foreach (var element in insertions)
                {
                    // we could certainly cache the result of this if we wanted it to be faster
                    Type matching = element.GetType();
                    int  idx      = dec.componentIndexDict.TryGetValue(matching, Environment.COMPONENTINDEX_MISSING);
                    while (idx == Environment.COMPONENTINDEX_MISSING && matching.BaseType != null)
                    {
                        matching = matching.BaseType;
                        idx      = dec.componentIndexDict.TryGetValue(matching, Environment.COMPONENTINDEX_MISSING);
                    }

                    if (idx == Environment.COMPONENTINDEX_MISSING || idx == Environment.COMPONENTINDEX_AMBIGUOUS || !Dec.Database <ComponentDec> .List[idx].type.IsAssignableFrom(element.GetType()))
                    {
                        Dbg.Err($"Attempted construction with non-component type {element.GetType()} when initializing {dec}");
                        continue;
                    }

                    if (!dec.components.Any(c => c.index == idx))
                    {
                        Dbg.Err($"Received invalid entity component parameter type {element.GetType()} when initializing {dec}");
                        continue;
                    }

                    if (components[idx] != null)
                    {
                        Dbg.Err($"Received duplicate entity component parameters {components[idx]} and {element} when initializing {dec}");
                    }

                    components[idx] = element;
                }
            }

            foreach (var component in dec.components)
            {
                if (components[component.index] == null)
                {
                    components[component.index] = Activator.CreateInstance(component.type);
                }
            }
        }
Beispiel #2
0
 public Entity(EntityDec template) : this(template, null)
 {
 }