示例#1
0
        /// <summary>
        /// Adds a component to the entity.
        /// </summary>
        /// <param name="type">The type of the component.</param>
        /// <returns>
        /// The component instance.
        /// </returns>
        /// <exception cref="System.Exception">Component already exists.</exception>
        internal Component AddComponent(Type type)
        {
            var info = ComponentHelper.GetInfo(type);
            if (!info.HasValue)
            {
                throw new Exception("Internal error: component is not registered.");
            }

            ulong id = info.Value.Id;

            if (this.components[id] != null)
            {
                throw new Exception("Component already exists.");
            }

            var component = ComponentHelper.CreateInstance(type);
            component.Owner = this;

            this.components[id] = component;

            var drawable = component as DrawableComponent;
            if (drawable != null)
            {
                this.drawableComponents[id] = drawable;
            }

            this.ComponentMask |= info.Value.Mask;

            return component;
        }