Esempio n. 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;
        }
Esempio n. 2
0
 /// <summary>
 /// Gets a component from the entity.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>The component instance.</returns>
 internal Component GetComponent(Type type)
 {
     var info = ComponentHelper.GetInfo(type);
     if (!info.HasValue)
     {
         throw new Exception("Internal error - trying to get a component that is not registered.");
     }
     return this.components[info.Value.Id];
 }
Esempio n. 3
0
        /// <summary>
        /// Constructs a new rule instance.
        /// </summary>
        /// <param name="types">Type list</param>
        /// <exception cref="System.Exception">Invalid component type in filter.</exception>
        public HasNotRule(Type[] types)
        {
            for (int i = 0; i < types.Length; i++)
            {
                var info = ComponentHelper.GetInfo(types[i]);
                if (!info.HasValue)
                {
                    throw new Exception("Invalid component type in filter.");
                }

                this.mask |= info.Value.Mask;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes the entity manager.
        /// </summary>
        internal static void Initialize()
        {
            if (initialized)
            {
                return;
            }

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                try
                {
                    var types = assembly.GetExportedTypes();
                    foreach (var type in types)
                    {
                        if (type.IsClass && type.IsAbstract)
                        {
                            continue;
                        }

                        if (typeof(Component).IsAssignableFrom(type))
                        {
                            ComponentHelper.GetInfo(type);
                        }
                        else if (typeof(EntitySystem).IsAssignableFrom(type))
                        {
                            SystemHelper.GetInfo(type);
                        }
                    }
                }
                catch (NotSupportedException)
                {
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            initialized = true;
        }