Esempio n. 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);
        }