public ComponentType GetTypeFor(Type component)
        {
            ComponentType result;
            if (!componentTypes.TryGetValue(component, out result))
            {
                int index = componentTypeCount++;
                result = new ComponentType(component, index);
                componentTypes.Add(component, result);
                Types[index] = result;
            }

            return result;
        }
Ejemplo n.º 2
0
        public void TestComponentTypeBit()
        {
            bool int32Used = typeof(ComponentType).GetProperty("Bit").PropertyType == typeof(global::System.Int32);

            // Ugly resetting of private static fields in case the type has already been initialized and used
            typeof(ComponentType).GetField("nextId", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, 0);

            if (int32Used)
                typeof(ComponentType).GetField("nextBit", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, 1);
            else
                typeof(ComponentType).GetField("nextBit", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, (BigInteger)1);

            if (int32Used)
            {
                for (int i = 0; i < 32; i++)
                {
                    Assert.AreEqual(1 << i, new ComponentType().Bit);
                }
            }
            else
            {
                for (int i = 0; i < 32; i++)
                {
#if UNITY5
                    Assert.AreEqual(new BigInteger(1) << i, new ComponentType().Bit);
#else
                    Assert.AreEqual(BigInteger.One << i, new ComponentType().Bit);
#endif
                }
            }

            // If ComponentType.Bit property is compiled as Int32,
            // next (33rd) ComponentType instance will get invalid (overflown) Bit value.
            // Should get exception instead of silent overflow.

            bool thrown = false;

            try
            {
                var badCtype = new ComponentType();
            }
            catch (InvalidOperationException)
            {
                thrown = true;
            }

            if (int32Used)
            {
                Assert.IsTrue(thrown, "33rd ComponentType constructor should fail when Int32 bit type is used");
            }
            else
            {
                Assert.IsFalse(thrown, "33rd ComponentType constructor should not fail when BigInteger bit type is used");
            }
        }
Ejemplo n.º 3
0
        /// <summary><para>Removes the component.</para>
        ///   <para>Faster removal of components from a entity.</para></summary>
        /// <param name="componentType">The type.</param>
        public void RemoveComponent(ComponentType componentType)
        {
            Debug.Assert(componentType != null, "Component type must not be null.");

            this.entityManager.RemoveComponent(this, componentType);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// <para>Gets the component.</para>
        /// <para>Slower retrieval of components from this entity.</para>
        /// <para>Minimize usage of this, but is fine to use e.g. when</para>
        /// <para>creating new entities and setting data in components.</para>
        /// </summary>
        /// <param name="componentType">Type of the component.</param>
        /// <returns>component that matches, or null if none is found.</returns>
        public IComponent GetComponent(ComponentType componentType)
        {
            Debug.Assert(componentType != null, "Component type must not be null.");

            return(this.entityManager.GetComponent(this, componentType));
        }
        public void RemoveComponent <T>(Entity e, Component component) where T : Component
        {
            ComponentType type = ComponentTypeManager.GetTypeFor <T>();

            RemoveComponent(e, type);
        }
Ejemplo n.º 6
0
 /**
  * This is the preferred method to use when retrieving a component from a entity. It will provide good performance.
  *
  * @param type in order to retrieve the component fast you must provide a ComponentType instance for the expected component.
  * @return
  */
 public Component GetComponent(ComponentType type)
 {
     return(entityManager.GetComponent(this, type));
 }
Ejemplo n.º 7
0
 /**
  * Faster removal of components from a entity.
  * @param component to remove from this entity.
  */
 public void RemoveComponent(ComponentType type)
 {
     entityManager.RemoveComponent(this, type);
 }