Пример #1
0
        /// <summary>
        ///   Initializes the specified entity, adding components matching the
        ///   passed blueprint and initializing these with the data stored in
        ///   the blueprint and the specified configuration. Configuration
        ///   data is preferred over blueprint data.
        /// </summary>
        /// <param name="entityId">Id of the entity to initialize.</param>
        /// <param name="blueprint"> Blueprint describing the entity to create. </param>
        /// <param name="configuration"> Data for initializing the entity. </param>
        /// <param name="additionalComponents">Components to add to the entity, in addition to the ones specified by the blueprint.</param>
        public void InitEntity(
            int entityId,
            Blueprint blueprint,
            IAttributeTable configuration,
            IEnumerable <Type> additionalComponents)
        {
            if (blueprint == null)
            {
                throw new ArgumentNullException("blueprint", "Blueprint is null.");
            }

            // Setup attribute table.
            HierarchicalAttributeTable attributeTable = new HierarchicalAttributeTable();

            if (configuration != null)
            {
                attributeTable.AddParent(configuration);
            }

            // Add attribute tables of all ancestors.
            IAttributeTable blueprintAttributeTable = blueprint.GetAttributeTable();

            if (blueprintAttributeTable != null)
            {
                attributeTable.AddParent(blueprintAttributeTable);
            }

            // Build list of components to add.
            IEnumerable <Type> blueprintComponentTypes = blueprint.GetAllComponentTypes();
            IEnumerable <Type> componentTypes          = additionalComponents != null
                ? blueprintComponentTypes.Union(additionalComponents)
                : blueprintComponentTypes;

            // Add components.
            foreach (Type type in componentTypes)
            {
                this.AddComponent(type, entityId, attributeTable);
            }

            // Raise event.
            this.OnEntityInitialized(entityId);
        }
Пример #2
0
        /// <summary>
        ///   Returns the final attribute table to use for entity creation.
        ///   Considers the attribute tables of the ancestors of this blueprint if there are any.
        /// </summary>
        /// <returns>Final attribute table to use for entity creation.</returns>
        public IAttributeTable GetAttributeTable()
        {
            if (this.Parent == null)
            {
                return(this.AttributeTable);
            }

            HierarchicalAttributeTable attributeTable = new HierarchicalAttributeTable(this.AttributeTable);
            Blueprint ancestor = this.Parent;

            while (ancestor != null)
            {
                if (ancestor.AttributeTable != null)
                {
                    attributeTable.AddParent(ancestor.AttributeTable);
                }
                ancestor = ancestor.Parent;
            }
            return(attributeTable);
        }