Ejemplo n.º 1
0
        public void Remove <T>() where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(typeof(T));

            this.componentsToAdd.Remove(idx);
            this.componentIdxToRemove.Add(idx);
        }
Ejemplo n.º 2
0
        public void Remove(Component component)
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentsToAdd.Remove(idx);
            this.componentIdxToRemove.Add(idx);
        }
Ejemplo n.º 3
0
        private void ComponentEnabled(Component component)
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentIdxToDisable.Remove(idx);
            this.componentIdxToEnable.Add(idx);
        }
Ejemplo n.º 4
0
        public T Add <T>(T component) where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentsToAdd.Add(idx, component);
            return(component);
        }
Ejemplo n.º 5
0
        public void SetState(Entity entity, string stateName)
        {
            List <string> newState = this.States[stateName];

            if (this.CurrentState == newState)
            {
                return;
            }

            foreach (string componentName in this.CurrentState)
            {
                if (newState.IndexOf(componentName) == -1)
                {
                    entity.RemoveComponent(ComponentTypeManager.GetTypeFor(this.Components[componentName].GetType()));
                }
            }

            foreach (string componentName in newState)
            {
                if (this.CurrentState.IndexOf(componentName) == -1)
                {
                    entity.AddComponent(this.Components[componentName]);
                }
            }

            this.CurrentState = newState;
        }
Ejemplo n.º 6
0
        public void RemoveComponent <T>()
            where T : UnityEngine.Component
        {
            var c = _gameObject.GetComponent <T>();

            GameObject.Destroy(c);
            this.ComponentBits.set(ComponentTypeManager.GetIndexFor(typeof(T)), false);
            _scene.EntityProcessors.onComponentRemoved(this);
        }
 /// <summary>Initializes a new instance of the <see cref="HybridQueueSystemProcessing" /> class.</summary>
 /// <param name="requiredType">Type of the required.</param>
 /// <param name="otherTypes">The other types.</param>
 protected HybridQueueSystemProcessing(Type requiredType, params Type[] otherTypes)
     : base(requiredType, otherTypes)
 {
     this.queue     = new Queue <Entity>();
     this.compTypes = new List <ComponentType>();
     foreach (Type type in this.Types)
     {
         this.compTypes.Add(ComponentTypeManager.GetTypeFor(type));
     }
 }
Ejemplo n.º 8
0
        public ComponentArray <T> GetComponentArray <T>()
        {
            var r = componentArrays[ComponentTypeManager.GetTypeIndex <T>()];

            if (r == null)
            {
                throw new ArgumentException($"Entity not contains component of type {typeof(T)}");
            }
            return((ComponentArray <T>)r);
        }
        /// <summary>
        /// Creates a new EntitySystem instance associated with the provided types.
        /// </summary>
        /// <param name="types">Types to associate with the EntitySystem.</param>
        public EntitySystem(params Type[] types)
            : this()
        {
            int typeCount = types.Length;

            for (int i = 0; i < typeCount; ++i)
            {
                Type          type          = types[i];
                ComponentType componentType = ComponentTypeManager.GetTypeFor(type);
                _typeFlags |= componentType.Bit;
            }
        }
Ejemplo n.º 10
0
        public void TestInitializeComponentTypes()
        {
            FieldInfo field = typeof(ComponentTypeManager).GetField("ComponentTypes", BindingFlags.Static | BindingFlags.NonPublic);

            Assert.IsNotNull(field, "ComponentTypeManager.ComponentTypes field has not been found");
            Assert.IsTrue(field.GetValue(null).GetType() == typeof(Dictionary <Type, ComponentType>), "ComponentTypes container is expected to be of type Dictionary<Type, ComponentType>");

            // Debug.WriteLine("Resetting ComponentTypeManager.ComponentTypes...");
            // field.SetValue(null, new Dictionary<Type, ComponentType>());

            var componentTypes = (Dictionary <Type, ComponentType>)field.GetValue(null);

            Assert.IsNotNull(componentTypes, "Component Types dictionary must not be null");
            // Assert.IsTrue(componentTypes.Count == 0, "Initial Component Types dictionary is expected to be empty.");
            // Debug.WriteLine("OK");

            Debug.WriteLine("Initializing specific Component types...");
            ComponentTypeManager.Initialize(new List <Type>
            {
                typeof(TestBaseComponent),
                typeof(TestDerivedComponent),
                typeof(TestHealthComponent),
                typeof(TestPowerComponent),
                typeof(TestPowerComponentPoolable),
                typeof(IComponent),        // should be filtered out
                typeof(ComponentPoolable), // should be filtered out
            });
            Debug.WriteLine("OK");

            Assert.IsNotNull(componentTypes, "Initialized Component Types dictionary must not be null");

            // NOTE: list of initialized types may change if you change existing Components

            var expectedTypes = new List <Type>
            {
                typeof(TestBaseComponent),
                typeof(TestDerivedComponent),
                typeof(TestHealthComponent),
                typeof(TestPowerComponent),
                typeof(TestPowerComponentPoolable)
            };

            Debug.WriteLine("Checking initialized Component types...");

            // Assert.AreEqual(expectedTypes.Count, componentTypes.Count, "Expected and actual Component Types count do not match.");

            foreach (var expectedType in expectedTypes)
            {
                Assert.IsTrue(componentTypes.ContainsKey(expectedType), "ComponentTypes is expected to contain {0}", expectedType);
            }

            Debug.WriteLine("OK");
        }
Ejemplo n.º 11
0
        public T AddComponent <T>()
            where T : UnityEngine.Component
        {
            var c = _gameObject.AddComponent <T>();

            if (c is BaseMonoBehaviour)
            {
                object b = (object)c;
                ((BaseMonoBehaviour)b).Entity = this;
            }
            this.ComponentBits.set(ComponentTypeManager.GetIndexFor(typeof(T)));
            _scene.EntityProcessors.onComponentAdded(this);
            return(c);
        }
Ejemplo n.º 12
0
 public static void Destroy(Entity entity)
 {
     foreach (var c in entity.GameObject.GetComponents(typeof(MonoBehaviour)))
     {
         //Debug.Log("Removed [" + entity.name + "] " + c.GetType().ToString());
         if (c is Entity)
         {
             continue;
         }
         entity.ComponentBits.set(ComponentTypeManager.GetIndexFor(c.GetType()), false);
         entity.Scene.EntityProcessors.onComponentRemoved(entity);
         GameObject.Destroy(c);
     }
     GameObject.Destroy(entity.GameObject);
 }
Ejemplo n.º 13
0
        public T Get <T>(bool withPending = true) where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(typeof(T));

            if (this.components.ContainsKey(idx))
            {
                return((T)this.components[idx]);
            }

            if (withPending && this.componentsToAdd.ContainsKey(idx))
            {
                return((T)this.componentsToAdd[idx]);
            }

            return(null);
        }
Ejemplo n.º 14
0
        public virtual void Start()
        {
            pixelCamera              = Camera.main.gameObject.AddComponent <PixelCamera>();
            pixelCamera.DesignWidth  = _designWidth;
            pixelCamera.DesignHeight = _designHeight;

            var guiCamera = GameObject.Find("GUI Camera");

            if (guiCamera != null)
            {
                PixelCamera pixelCameraGUI = guiCamera.AddComponent <PixelCamera>();
                pixelCameraGUI.DesignWidth  = _designWidth;
                pixelCameraGUI.DesignHeight = _designHeight;
            }

            ComponentTypeManager.Initialize();
        }
Ejemplo n.º 15
0
 private void Validate()
 {
     if (entityMap.Count == 0)
     {
         throw new InvalidOperationException("No entities registered to scene");
     }
     if (entityMap.CountEntities(e => e.ContainsComponent(ComponentTypeManager.GetKeyPart <CameraComponent>())) == 0)
     {
         LogEvent.Engine.Warning("No camera registered to scene");
     }
     if (entityMap.CountEntities(e => e.ContainsComponent(ComponentTypeManager.GetKeyPart <PointLightComponent>())) == 0)
     {
         LogEvent.Engine.Warning("No light registered to scene");
     }
     if (!entityMap.Validate())
     {
         throw new InvalidOperationException("Entities are missing required components");
     }
 }
Ejemplo n.º 16
0
 public TComponent GetComponent <TComponent>()
     where TComponent : Component
 {
     return(GetComponent <TComponent>(ComponentTypeManager.GetKeyPart <TComponent>()));
 }
Ejemplo n.º 17
0
        internal bool CommitChanges()
        {
            if (this.componentIdxToDisable.Count > 0)
            {
                foreach (var idx in this.componentIdxToDisable)
                {
                    this.Bits.Set(idx, false);
                }
            }

            if (this.componentIdxToEnable.Count > 0)
            {
                if (!this.entity.Enabled)
                {
                    this.componentIdxToEnable.Clear();
                }

                foreach (var idx in this.componentIdxToEnable)
                {
                    this.Bits.Set(idx, true);
                }
            }

            if (this.componentIdxToRemove.Count > 0)
            {
                foreach (var idx in this.componentIdxToRemove)
                {
                    this.Bits.Set(idx, false);

                    if (this.components.ContainsKey(idx))
                    {
                        var component = this.components[idx];
                        component.Entity             = null;
                        component.ComponentEnabled  -= this.ComponentEnabled;
                        component.ComponentDisabled -= this.ComponentDisabled;
                        this.components.Remove(idx);
                    }
                }
            }

            if (this.componentsToAdd.Count > 0)
            {
                foreach (var component in this.componentsToAdd)
                {
                    var idx = ComponentTypeManager.GetIndexFor(component.Value.GetType());
                    this.Bits.Set(idx, this.entity.Enabled && component.Value.Enabled);

                    component.Value.Entity             = this.entity;
                    component.Value.ComponentEnabled  += this.ComponentEnabled;
                    component.Value.ComponentDisabled += this.ComponentDisabled;
                    this.components.Add(idx, component.Value);
                }
            }

            var isSomethingChanged = this.componentIdxToRemove.Count > 0 || this.componentsToAdd.Count > 0 ||
                                     this.componentIdxToDisable.Count > 0 ||
                                     this.componentIdxToEnable.Count > 0;

            this.componentIdxToRemove.Clear();
            this.componentsToAdd.Clear();
            this.componentIdxToDisable.Clear();
            this.componentIdxToEnable.Clear();

            return(isSomethingChanged);
        }
Ejemplo n.º 18
0
 public OptimizationSystem() : base(Selector.All(typeof(ModelComponent), typeof(ShaderComponent)))
 {
     tShader      = ComponentTypeManager.GetType <ShaderComponent>();
     tModel       = ComponentTypeManager.GetType <ModelComponent>();
     renderMapper = new RenderMapper();
 }
Ejemplo n.º 19
0
        public bool ContainsComponent <TComponent>()
            where TComponent : IComponent

        {
            return(ContainsComponent(ComponentTypeManager.GetKeyPart <TComponent>()));
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Gets a component of type T associated with the Entity.
 /// </summary>
 /// <typeparam name="T"><see cref="IComponent"/> type to get for the Entity.</typeparam>
 /// <returns>>An <see cref="IComponent"/> instance of the specified type if one is associated with the Entity, or null if not.</returns>
 public T GetComponent <T>() where T : IComponent
 {
     return((T)GetComponent(ComponentTypeManager.GetTypeFor <T>()));
 }
Ejemplo n.º 21
0
 public ControllerComponent() : base(ComponentTypeManager.GetType <ControllerComponent>())
 {
 }
Ejemplo n.º 22
0
 public BloomComponent() : base(ComponentTypeManager.GetType <BloomComponent>())
 {
 }
Ejemplo n.º 23
0
 protected UpdateableSystemBase(Selector selector) : base(selector)
 {
     tUpdate = ComponentTypeManager.GetType <UpdateComponent>();
 }
Ejemplo n.º 24
0
 public UserInterfaceSystem()
     : base(Selector.All(typeof(UserInterfaceComponent)))
 {
     tUserInterface = ComponentTypeManager.GetType <UserInterfaceComponent>();
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Creates a new ComponentMapper instance associated with the specified entity world.
 /// </summary>
 /// <param name="world">Entity world instance to associate with.</param>
 public ComponentMapper(IEntityWorld world)
 {
     _world         = world;
     _componentType = ComponentTypeManager.GetTypeFor <T>();
 }
Ejemplo n.º 26
0
 public GlowComponent()
     : base(ComponentTypeManager.GetType <GlowComponent>())
 {
 }
Ejemplo n.º 27
0
 public bool TryGetComponent <TComponent>(out TComponent component)
     where TComponent : Component
 {
     return(Scene.EntityMap.TryGetEntityComponent(this, ComponentTypeManager.GetKeyPart <TComponent>(), out component));
 }
Ejemplo n.º 28
0
        public bool HasComponent <T>()
        {
            var typeId = ComponentTypeManager.GetTypeIndex <T>();

            return(componentArrays.Length > typeId && componentArrays[typeId] != null);
        }