コード例 #1
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
        /// <summary>
        /// <para>Checks whether this Entity has a Component of the given Type.</para>
        /// <para>Due to the highly parallel nature of Jacinth, this method should NOT be used before gettig a Component - use TryGetComponent instead</para>
        /// </summary>
        /// <typeparam name="T">The type of Component to add to this Entity, used to determine the ComponentTypeKey</typeparam>
        /// <returns>True if the Component was found, False otherwise</returns>
        public bool HasComponent <T>()
            where T : Component
        {
            var key = ComponentTypeKey.GetKey <T>();

            return(_components.ContainsKey(key));
        }
コード例 #2
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
 private void RaiseComponentAdded(ComponentTypeKey key, Component component)
 {
     Task.Run(() =>
     {
         if (ComponentAdded != null)
         {
             ComponentAdded(this, key, component);
         }
     });
 }
コード例 #3
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
        /// <summary>
        /// Adds a new Component to this Entity
        /// </summary>
        /// <typeparam name="T">The type of Component to add to this Entity, used to determine the ComponentTypeKey</typeparam>
        /// <param name="component">The Component to be added</param>
        public void AddComponent <T>(T component)
            where T : Component
        {
            component.Entity = this;
            var key = ComponentTypeKey.GetKey <T>();

            if (_components.TryAdd(key, component))
            {
                RaiseComponentAdded(key, component);
            }
        }
コード例 #4
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
        /// <summary>
        /// Attempts to get the Component of the specified type on this Entity
        /// </summary>
        /// <typeparam name="T">The type of Component to add to this Entity, used to determine the ComponentTypeKey</typeparam>
        /// <param name="component">The Component, if found</param>
        /// <returns>True if the Component was found, False otherwise</returns>
        public bool TryGetComponent <T>(out T component)
            where T : Component
        {
            var       key = ComponentTypeKey.GetKey <T>();
            Component rawComponent;

            var result = _components.TryGetValue(key, out rawComponent);

            component = rawComponent as T;
            return(result &&
                   component != null);
        }
コード例 #5
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
        /// /// <summary>
        /// Gets the specified type of Component from this Entity, or creates and adds it if not found.
        /// </summary>
        /// <typeparam name="T">The type of Component to add to this Entity, used to determine the ComponentTypeKey</typeparam>
        /// <param name="created">True if the Component was created, False otherwise</param>
        /// <returns>The found or created Component</returns>
        public T GetOrCreateComponent <T>(out bool created)
            where T : Component, new()
        {
            var       key = ComponentTypeKey.GetKey <T>();
            Component result;

            created = !_components.TryGetValue(key, out result);
            if (!created)
            {
                return((T)result); // Hard cast to propogate an InvalidCastException if used incorrectly
            }
            return(AddComponent <T>());
        }
コード例 #6
0
ファイル: Entity.cs プロジェクト: russellpierce/Jacinth
        /// <summary>
        /// Removes the Component of the specified type from this Entity.
        /// Destroys this Entity if there are no Components left on this Entity.
        /// </summary>
        /// <typeparam name="T">The type of Component to add to this Entity, used to determine the ComponentTypeKey</typeparam>
        public void RemoveComponent <T>()
            where T : Component
        {
            var       key = ComponentTypeKey.GetKey <T>();
            Component component;

            if (_components.TryRemove(key, out component))
            {
                if (_components.Any())
                {
                    RaiseComponentRemoved(key, component);
                }
                else
                {
                    Destroy();
                }
            }
        }
コード例 #7
0
 /// <summary>
 /// Handler called when a Component is removed from the Entity.
 /// </summary>
 protected virtual void OnEntityComponentRemoved(Entity entity, ComponentTypeKey key, Component component)
 {
 }
コード例 #8
0
ファイル: JacinthWorld.cs プロジェクト: drknexus/Jacinth
 private void OnComponentAdded(Entity entity, ComponentTypeKey key, Component component)
 {
     RaiseEntityUpdated(entity);
 }
コード例 #9
0
 private void OnComponentAdded(Entity entity, ComponentTypeKey key, Component component)
 {
     RaiseEntityUpdated(entity);
 }
コード例 #10
0
ファイル: SubEntity.cs プロジェクト: drknexus/Jacinth
 /// <summary>
 /// Handler called when a Component is removed from the Entity.
 /// </summary>
 protected virtual void OnEntityComponentRemoved(Entity entity, ComponentTypeKey key, Component component)
 {
 }