/// <summary> /// Assure that an entity has the components. If an entity already possess one of them, it will not get replaced. /// </summary> /// <param name="entityHandle"></param> /// <param name="componentTypeSpan"></param> public void AssureComponents(GameEntityHandle entityHandle, Span <ComponentType> componentTypeSpan) { var updateArchetype = false; var entityBoard = Boards.Entity; foreach (ref readonly var componentType in componentTypeSpan) { // TODO: support for shared component if (entityBoard.GetComponentColumn(componentType.Id)[(int)entityHandle.Id].Valid) { continue; } var componentBoard = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType); var cRef = new ComponentReference(componentType, GameWorldLL.CreateComponent(componentBoard)); GameWorldLL.AssignComponent(componentBoard, cRef, Boards.Entity, entityHandle); GameWorldLL.SetOwner(componentBoard, cRef, entityHandle); updateArchetype = true; } if (updateArchetype) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); } }
/// <summary> /// Add and remove multiple component to an entity /// </summary> /// <param name="entityHandle"></param> /// <param name="componentType"></param> /// <returns></returns> public void AddRemoveMultipleComponent(GameEntityHandle entityHandle, Span <ComponentType> addSpan, Span <ComponentType> removeSpan) { ThrowOnInvalidHandle(entityHandle); var updateArch = false; foreach (ref readonly var componentType in addSpan) { var componentBoard = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType); var cRef = new ComponentReference(componentType, GameWorldLL.CreateComponent(componentBoard)); updateArch |= GameWorldLL.AssignComponent(componentBoard, cRef, Boards.Entity, entityHandle); GameWorldLL.SetOwner(componentBoard, cRef, entityHandle); } foreach (ref readonly var componentType in removeSpan) { updateArch |= GameWorldLL.RemoveComponentReference(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType), componentType, Boards.Entity, entityHandle); } if (updateArch) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); } }
/// <summary> /// Assign and set as a owner a component to an entity /// </summary> /// <param name="entityHandle"></param> /// <param name="component"></param> public void AssignComponentAsOwner(GameEntityHandle entityHandle, ComponentReference component) { ThrowOnInvalidHandle(entityHandle); var board = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, component.Type); GameWorldLL.AssignComponent(board, component, Boards.Entity, entityHandle); GameWorldLL.SetOwner(board, component, entityHandle); GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); }
/// <summary> /// Add multiple component to an entity /// </summary> /// <param name="entityHandle"></param> /// <param name="componentType"></param> /// <remarks> /// If you wish to not replace existing components, use <see cref="AssureComponents"/> (a bit slower than this method) /// </remarks> public void AddMultipleComponent(GameEntityHandle entityHandle, Span <ComponentType> componentTypeSpan) { ThrowOnInvalidHandle(entityHandle); foreach (ref readonly var componentType in componentTypeSpan) { var componentBoard = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType); var cRef = new ComponentReference(componentType, GameWorldLL.CreateComponent(componentBoard)); GameWorldLL.AssignComponent(componentBoard, cRef, Boards.Entity, entityHandle); GameWorldLL.SetOwner(componentBoard, cRef, entityHandle); } GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); }
/// <summary> /// Add a component to an entity /// </summary> /// <param name="entityHandle"></param> /// <param name="componentType"></param> /// <returns></returns> public ComponentReference AddComponent(GameEntityHandle entityHandle, ComponentType componentType) { ThrowOnInvalidHandle(entityHandle); var componentBoard = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType); var cRef = new ComponentReference(componentType, GameWorldLL.CreateComponent(componentBoard)); GameWorldLL.SetOwner(componentBoard, cRef, entityHandle); // Only update archetype if this is a new component to the entity, and not just an update if (GameWorldLL.AssignComponent(componentBoard, cRef, Boards.Entity, entityHandle)) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); } return(cRef); }