/// <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> /// 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); } }
public GameEntityHandle CreateEntity() { var handle = new GameEntityHandle(Boards.Entity.CreateRow()); GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, handle); return(handle); }
public void CreateEntityBulk(Span <GameEntityHandle> entities) { Boards.Entity.CreateRowBulk(MemoryMarshal.Cast <GameEntityHandle, uint>(entities)); foreach (var ent in entities) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, ent); } }
/// <summary> /// Assign an existing component to an entity /// </summary> /// <param name="entityHandle"></param> /// <param name="component"></param> public void AssignComponent(GameEntityHandle entityHandle, ComponentReference component) { ThrowOnInvalidHandle(entityHandle); var board = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, component.Type); GameWorldLL.AssignComponent(board, component, Boards.Entity, entityHandle); GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); }
/// <summary> /// Remove a component from an entity. /// </summary> /// <param name="entityHandle">The entity</param> /// <param name="componentType">The component type</param> /// <returns>True if the component was removed, false if it did not exist.</returns> public bool RemoveComponent(GameEntityHandle entityHandle, ComponentType componentType) { ThrowOnInvalidHandle(entityHandle); if (GameWorldLL.RemoveComponentReference(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType), componentType, Boards.Entity, entityHandle)) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); return(true); } return(false); }
/// <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); }
/// <summary> /// Remove a component from an entity. /// </summary> /// <param name="entityHandle">The entity</param> /// <param name="componentType">The component type</param> /// <returns>True if the component was removed, false if it did not exist.</returns> public bool RemoveMultipleComponent(GameEntityHandle entityHandle, Span <ComponentType> componentTypeSpan) { ThrowOnInvalidHandle(entityHandle); var b = true; foreach (ref readonly var componentType in componentTypeSpan) { b &= GameWorldLL.RemoveComponentReference(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType), componentType, Boards.Entity, entityHandle); } if (b) { GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, entityHandle); } return(b); }