Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        public ComponentReference Copy(GameEntityHandle from, GameEntityHandle to, ComponentType componentType)
        {
            ThrowOnInvalidHandle(from);
            ThrowOnInvalidHandle(to);

            if (!HasComponent(from, componentType))
            {
                throw new InvalidOperationException();
            }

            var fromMetadata   = GetComponentMetadata(from, componentType);
            var toRef          = AddComponent(to, componentType);
            var componentBoard = GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType);

            switch (componentBoard)
            {
            case SingleComponentBoard singleComponentBoard:
                singleComponentBoard.ReadRaw(fromMetadata.Id)
                .CopyTo(singleComponentBoard.ReadRaw(toRef.Id));
                break;

            case BufferComponentBoard bufferComponentBoard:
            {
                var toBuffer = bufferComponentBoard.AsSpan()[(int)toRef.Id];
                toBuffer.Clear();
                toBuffer.AddRange(bufferComponentBoard.AsSpan()[(int)fromMetadata.Id]);
                break;
            }
            }

            return(toRef);
        }
Esempio n. 3
0
        /// <summary>
        /// Create a new component
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public ComponentReference CreateComponent <T>()
            where T : struct, IEntityComponent
        {
            var componentType = AsComponentType <T>();

            return(new ComponentReference(componentType, GameWorldLL.CreateComponent(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType))));
        }
Esempio n. 4
0
        /// <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);
            }
        }
Esempio n. 5
0
        public GameEntityHandle CreateEntity()
        {
            var handle = new GameEntityHandle(Boards.Entity.CreateRow());

            GameWorldLL.UpdateArchetype(Boards.Archetype, Boards.ComponentType, Boards.Entity, handle);
            return(handle);
        }
Esempio n. 6
0
 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);
     }
 }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
        /// <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);
        }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        /// <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);
        }
Esempio n. 11
0
        /// <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);
        }
Esempio n. 12
0
 public Span <GameEntityHandle> GetReferencedEntities(ComponentReference component)
 {
     return(GameWorldLL.GetReferences(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, component.Type), component));
 }
Esempio n. 13
0
 public GameEntityHandle GetComponentOwner(ComponentReference component)
 {
     return(GameWorldLL.GetOwner(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, component.Type), component));
 }
Esempio n. 14
0
 /// <summary>
 /// Create a new component
 /// </summary>
 /// <param name="componentType"></param>
 /// <returns></returns>
 public ComponentReference CreateComponent(ComponentType componentType)
 {
     return(new ComponentReference(componentType, GameWorldLL.CreateComponent(GameWorldLL.GetComponentBoardBase(Boards.ComponentType, componentType))));
 }