/// <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)); }
private void RaiseComponentAdded(ComponentTypeKey key, Component component) { Task.Run(() => { if (ComponentAdded != null) { ComponentAdded(this, key, component); } }); }
/// <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); } }
/// <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); }
/// /// <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>()); }
/// <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(); } } }
/// <summary> /// Handler called when a Component is removed from the Entity. /// </summary> protected virtual void OnEntityComponentRemoved(Entity entity, ComponentTypeKey key, Component component) { }
private void OnComponentAdded(Entity entity, ComponentTypeKey key, Component component) { RaiseEntityUpdated(entity); }