Пример #1
0
 public void AddComponent(Entity entity, ComponentType componentType)
 {
     m_Manager.AddComponent(entity, componentType);
 }
        /// <summary>
        /// Play back all recorded operations against an entity manager.
        /// </summary>
        /// <param name="mgr">The entity manager that will receive the operations</param>
        public void Playback(EntityManager mgr)
        {
            if (mgr == null)
            {
                throw new NullReferenceException($"{nameof(mgr)} cannot be null");
            }

            EnforceSingleThreadOwnership();

            var head       = m_Data->m_Head;
            var lastEntity = new Entity();

            while (head != null)
            {
                var off = 0;
                var buf = ((byte *)head) + sizeof(Chunk);

                while (off < head->Used)
                {
                    var header = (BasicCommand *)(buf + off);

                    switch ((Command)header->CommandType)
                    {
                    case Command.DestroyEntityExplicit:
                        mgr.DestroyEntity(((EntityCommand *)header)->Entity);
                        break;

                    case Command.AddComponentExplicit:
                    {
                        var cmd           = (EntityComponentCommand *)header;
                        var componentType = (ComponentType)TypeManager.GetType(cmd->ComponentTypeIndex);
                        mgr.AddComponent(cmd->Header.Entity, componentType);
                        mgr.SetComponentRaw(cmd->Header.Entity, cmd->ComponentTypeIndex, (cmd + 1), cmd->ComponentSize);
                    }
                    break;

                    case Command.RemoveComponentExplicit:
                    {
                        var cmd = (EntityComponentCommand *)header;
                        mgr.RemoveComponent(cmd->Header.Entity, TypeManager.GetType(cmd->ComponentTypeIndex));
                    }
                    break;

                    case Command.SetComponentExplicit:
                    {
                        var cmd = (EntityComponentCommand *)header;
                        mgr.SetComponentRaw(cmd->Header.Entity, cmd->ComponentTypeIndex, (cmd + 1), cmd->ComponentSize);
                    }
                    break;

                    case Command.CreateEntityImplicit:
                    {
                        var cmd = (CreateCommand *)header;
                        if (cmd->Archetype.Valid)
                        {
                            lastEntity = mgr.CreateEntity(cmd->Archetype);
                        }
                        else
                        {
                            lastEntity = mgr.CreateEntity();
                        }
                        break;
                    }

                    case Command.AddComponentImplicit:
                    {
                        var cmd           = (EntityComponentCommand *)header;
                        var componentType = (ComponentType)TypeManager.GetType(cmd->ComponentTypeIndex);
                        mgr.AddComponent(lastEntity, componentType);
                        mgr.SetComponentRaw(lastEntity, cmd->ComponentTypeIndex, (cmd + 1), cmd->ComponentSize);
                    }
                    break;

                    case Command.SetComponentImplicit:
                    {
                        var cmd = (EntityComponentCommand *)header;
                        //var componentType = (ComponentType)TypeManager.GetType(cmd->ComponentTypeIndex);
                        mgr.SetComponentRaw(lastEntity, cmd->ComponentTypeIndex, (cmd + 1), cmd->ComponentSize);
                    }
                    break;
                    }

                    off += header->TotalSize;
                }

                head = head->Next;
            }
        }