Ejemplo n.º 1
0
        public void ConventionCatalog_should_support_type_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .Export();

            var catalog =
                new ConventionCatalog(registry);

            var instance =
                new ConventionPart <SampleExport>();

            var batch =
                new CompositionBatch();

            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports.Count().ShouldEqual(1);
        }
Ejemplo n.º 2
0
        public void ContractService_should_be_instance_of_defaultconventioncontractservice_on_new_instance()
        {
            var registry =
                new PartRegistry();

            registry.ContractService.ShouldBeOfType<DefaultConventionContractService>();
        }
Ejemplo n.º 3
0
        protected override void OnUpdate()
        {
            Entities.WithNone <State>().ForEach((Entity entity, ref RegistryAsset registryAsset) =>
            {
                GameDebug.Log(World, Part.ShowLifetime, "Initializing PartOwner:{0}", entity);

                var registry      = PartRegistry.GetPartRegistry(World, registryAsset.Value);
                var categoryCount = registry.Value.GetCategoryCount();

                var buffer = PostUpdateCommands.AddBuffer <PartElement>(entity);
                for (int i = 0; i < categoryCount; i++)
                {
                    buffer.Add(PartElement.Default);
                }

                PostUpdateCommands.AddComponent(entity, State.Default);
            });

            Entities.WithNone <InputState>().ForEach((Entity entity, ref State state) =>
            {
                GameDebug.Log(World, Part.ShowLifetime, "Deinitializing PartOwner:{0}", entity);

                var partBuffer = EntityManager.GetBuffer <PartElement>(entity);
                for (int i = 0; i < partBuffer.Length; i++)
                {
                    if (partBuffer[i].PartEntity != Entity.Null)
                    {
                        PostUpdateCommands.DestroyEntity(partBuffer[i].PartEntity);
                        GameDebug.Log(Part.ShowLifetime, "   destroying part:{0}", partBuffer[i].PartEntity);
                    }
                }
                PostUpdateCommands.RemoveComponent <State>(entity);
                PostUpdateCommands.RemoveComponent <PartElement>(entity);
            });
        }
Ejemplo n.º 4
0
        public void ConventionCatalog_should_support_type_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .Export();

            var catalog =
               new ConventionCatalog(registry);

            var instance =
                new ConventionPart<SampleExport>();

            var batch =
                new CompositionBatch();
            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports.Count().ShouldEqual(1);
        }
Ejemplo n.º 5
0
        public void TypeScanner_should_be_null_on_new_instance()
        {
            var registry =
                new PartRegistry();

            registry.TypeScanner.ShouldBeNull();
        }
Ejemplo n.º 6
0
        public void ContractService_should_be_instance_of_defaultconventioncontractservice_on_new_instance()
        {
            var registry =
                new PartRegistry();

            registry.ContractService.ShouldBeOfType <DefaultConventionContractService>();
        }
Ejemplo n.º 7
0
        public void TypeScanner_should_be_null_on_new_instance()
        {
            var registry =
                new PartRegistry();

            registry.TypeScanner.ShouldBeNull();
        }
Ejemplo n.º 8
0
        public void ConventionCatalog_should_support_field_imports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportField("IntValue", "V1");

            registry
            .Part()
            .ForType <SampleImport>()
            .Export()
            .ImportField("IntValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <SampleImport>();

            exportedValue.ShouldNotBeNull();
            exportedValue.IntValue.ShouldEqual(1234);
        }
Ejemplo n.º 9
0
        public void ConventionCatalog_should_support_property_imports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportProperty("TextValue", "V1");

            registry
            .Part()
            .ForType <SampleImport>()
            .Export()
            .ImportProperty("TextValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <SampleImport>();

            exportedValue.ShouldNotBeNull();
            exportedValue.TextValue.ShouldEqual("this is some text");
        }
Ejemplo n.º 10
0
        public void Part_of_tconvention_should_return_instance_of_partconventionbuilder_for_tconvention_type()
        {
            var registry =
                new PartRegistry();

            var result =
                registry.PartWithConvention<PartConvention>();

            result.ShouldBeOfType<PartConventionBuilder<PartConvention>>();
        }
Ejemplo n.º 11
0
        public void Scan_should_throw_argumentnullexception_when_called_with_null()
        {
            var registry =
                new PartRegistry();

            var exception =
                Catch.Exception(() => registry.Scan(null));

            exception.ShouldBeOfType <ArgumentNullException>();
        }
Ejemplo n.º 12
0
        public void Scan_should_throw_argumentnullexception_when_called_with_null()
        {
            var registry =
                new PartRegistry();

            var exception =
                Catch.Exception(() => registry.Scan(null));

            exception.ShouldBeOfType<ArgumentNullException>();
        }
Ejemplo n.º 13
0
        public void Part_of_tconvention_should_return_instance_of_partconventionbuilder_for_tconvention_type()
        {
            var registry =
                new PartRegistry();

            var result =
                registry.PartWithConvention <PartConvention>();

            result.ShouldBeOfType <PartConventionBuilder <PartConvention> >();
        }
Ejemplo n.º 14
0
    public Registry()
    {
        ScriptLoader.LoadScript("Entity", "Entity.lua");
        ScriptLoader.AddCategory("Generator");

        partRegistry   = new PartRegistry();
        entityRegistry = new EntityRegistry();

        namesRegistry = new NameRegistry();

        generator = new Generator();
    }
Ejemplo n.º 15
0
        public void Scan_should_set_typescanner_to_scanner_created_by_closure()
        {
            var registry =
                new PartRegistry();

            registry.Scan(x => {
                x.Types(new[] { typeof(object) });
                x.Types(new[] { typeof(string) });
            });

            var results =
                registry.TypeScanner.GetTypes(x => true);

            results.Count().ShouldEqual(2);
        }
Ejemplo n.º 16
0
        public void Scan_should_set_typescanner_to_scanner_created_by_closure()
        {
            var registry =
                new PartRegistry();

            registry.Scan(x => {
                x.Types(new[] { typeof(object) });
                x.Types(new[] { typeof(string) });
            });

            var results =
                registry.TypeScanner.GetTypes(x => true);

            results.Count().ShouldEqual(2);
        }
Ejemplo n.º 17
0
        public void ConventionCatalog_should_support_property_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportProperty("TextValue", "V1");
            
            var catalog =
               new ConventionCatalog(registry);
            
            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<string>("V1");
            Assert.That(exportedValue, Is.EqualTo("this is some text"));
        }
Ejemplo n.º 18
0
        public void ConventionCatalog_should_support_property_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportProperty("TextValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <string>("V1");

            Assert.That(exportedValue, Is.EqualTo("this is some text"));
        }
Ejemplo n.º 19
0
        public void ConventionCatalog_should_support_field_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportField("IntValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <int>("V1");

            Assert.That(exportedValue, Is.EqualTo(1234));
        }
Ejemplo n.º 20
0
        protected override void OnUpdate()
        {
            // Camera.main may not be available when the system is created, so need this to set it for the first time
            if (MainCamera == null)
            {
                if (Camera.main != null)
                {
                    MainCamera = Camera.main;
                }
                else
                {
                    GameDebug.LogWarning("PartOwner update: No camera.main");
                    return;
                }
            }

            var camPos = (float3)MainCamera.transform.position;

            // TODO: Jobified ForEach blocked by PrefabAssetRegistry.CreateEntity
            Entities.ForEach((Entity partOwnerEntity, ref Translation translation, ref RegistryAsset registryAsset, ref InputState inputState,
                              ref State state) =>
            {
                var registry = PartRegistry.GetPartRegistry(World, registryAsset.Value);

                // Calc lod
                var charPos = translation.Value;
                var dist    = math.distance(camPos, charPos);
                var newLod  = -1;
                // TODO (mogensh) add threshold that needs to be passed before change (so it does not flicker)
                for (int lod = 0; lod < registry.Value.LODLevels.Length; lod++)
                {
                    if (dist <= registry.Value.LODLevels[lod].EndDist)
                    {
                        newLod = lod;
                        break;
                    }
                }


                // TODO (mogensh) hack: force LOD 0
                newLod = 0;


                // Handle out of lod distance specifically
                if (newLod == -1)
                {
                    if (state.currentLOD != newLod)
                    {
                        state.currentLOD = newLod;

                        GameDebug.Log(Part.ShowLifetime, "Out of LOD distance");

                        var partBuf = EntityManager.GetBuffer <PartElement>(partOwnerEntity)
                                      .ToNativeArray(Allocator.Temp);
                        var partOutBuf = PostUpdateCommands.SetBuffer <PartElement>(partOwnerEntity);
                        partOutBuf.ResizeUninitialized(partBuf.Length);
                        for (int j = 0; j < partBuf.Length; j++)
                        {
                            var partElement = partBuf[j];

                            // Destroy old part
                            if (partElement.PartId != 0)
                            {
                                if (partElement.PartEntity != Entity.Null)
                                {
                                    GameDebug.Log(Part.ShowLifetime, "Destroying part. Category:{0} partId:{1}", j,
                                                  partElement.PartId);
                                    PostUpdateCommands.DestroyEntity(partElement.PartEntity);
                                }

                                partElement.PartEntity = Entity.Null;
                                partElement.PartId     = 0;
                                partElement.Asset      = WeakAssetReference.Default;
                            }

                            partOutBuf[j] = partElement;
                        }

                        PostUpdateCommands.SetComponent(partOwnerEntity, state);
                    }


                    return;
                }



                var newRig = BlobAssetReference <RigDefinition> .Null;
                if (EntityManager.HasComponent <SharedRigDefinition>(partOwnerEntity))
                {
                    newRig = EntityManager.GetSharedComponentData <SharedRigDefinition>(partOwnerEntity).Value;
                }


                // Change bodypart if LOD or rig changed
                var packedPartIds = inputState.PackedPartIds;
                if (packedPartIds != state.CurrentPackedPartIds ||
                    newLod != state.currentLOD ||
                    (newRig != BlobAssetReference <RigDefinition> .Null && newRig != state.currentRig))
                {
                    var partBuf = EntityManager.GetBuffer <PartElement>(partOwnerEntity).ToNativeArray(Allocator.Temp);


                    var partIds = new NativeArray <int>(partBuf.Length, Allocator.Temp);
                    registry.Value.UnpackPartsList(inputState.PackedPartIds, partIds);

                    GameDebug.Log(World, Part.ShowLifetime, "Property changed. Lod:{0}", newLod);

                    var partOutBuf = PostUpdateCommands.SetBuffer <PartElement>(partOwnerEntity);
                    partOutBuf.ResizeUninitialized(partBuf.Length);
                    for (int j = 0; j < partBuf.Length; j++)
                    {
                        var partId      = partIds[j];
                        var partElement = partBuf[j];

                        // Find new asset given the new properties
                        var asset = new WeakAssetReference();
                        if (partId > 0)
                        {
                            var skeletonHash = newRig.IsCreated ? newRig.Value.GetHashCode() : 0;
                            var found        = registry.Value.FindAsset(j, partId, skeletonHash, newLod, ref asset);
                            if (!found)
                            {
                                GameDebug.Log(World, Part.ShowLifetime,
                                              "Failed to find valid part. Category:{0} PartId:{1}", j, partId);
                            }
                        }


                        // No change if asset has not changed
                        if (partElement.Asset == asset)
                        {
                            partOutBuf[j] = partElement;
                            continue;
                        }


                        // Destroy old part
                        if (partElement.PartId != 0)
                        {
                            if (partElement.PartEntity != Entity.Null)
                            {
                                GameDebug.Log(World, Part.ShowLifetime, "Destroying part. Category:{0} partId:", j,
                                              partElement.PartId);
                                PostUpdateCommands.DestroyEntity(partElement.PartEntity);
                            }

                            partElement.PartEntity = Entity.Null;
                            partElement.PartId     = 0;
                            partElement.Asset      = WeakAssetReference.Default;
                        }

                        // Create new part
                        if (partId != 0 && asset.IsSet())
                        {
                            partElement.PartEntity = PrefabAssetManager.CreateEntity(EntityManager, asset);
                            partElement.PartId     = partId;
                            partElement.Asset      = asset;

                            if (partElement.PartEntity != Entity.Null)
                            {
                                GameDebug.Log(World, Part.ShowLifetime,
                                              "Creating part. Owner:{0} Cat:{1} PartId:{2} Asset:{3} part:{4}", partOwnerEntity,
                                              j, partId, asset.ToGuidStr(), partElement.PartEntity);
                                var part   = Part.Owner.Default;
                                part.Value = partOwnerEntity;
                                PostUpdateCommands.SetComponent(partElement.PartEntity, part);

                                // TODO (mogensh) add "static" property on owner (or get somehow). If static just set world transform
                                PostUpdateCommands.AddComponent(partElement.PartEntity,
                                                                new Parent {
                                    Value = partOwnerEntity
                                });
                                PostUpdateCommands.AddComponent(partElement.PartEntity, new LocalToParent());
                            }
                            else
                            {
                                GameDebug.LogError("Failed to create part. Asset:" + asset.ToGuidStr());
                            }
                        }

                        partOutBuf[j] = partElement;
                    }

                    state.CurrentPackedPartIds = packedPartIds;
                    state.currentRig           = newRig;
                    state.currentLOD           = newLod;
                    PostUpdateCommands.SetComponent(partOwnerEntity, state);
                }
            });
        }
Ejemplo n.º 21
0
        public void ConventionCatalog_should_support_property_imports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportProperty("TextValue", "V1");

            registry
                .Part()
                .ForType<SampleImport>()
                .Export()
                .ImportProperty("TextValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<SampleImport>();
            exportedValue.ShouldNotBeNull();
            exportedValue.TextValue.ShouldEqual("this is some text");
        }
Ejemplo n.º 22
0
        public void ConventionCatalog_should_support_field_imports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportField("IntValue", "V1");

            registry
                .Part()
                .ForType<SampleImport>()
                .Export()
                .ImportField("IntValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<SampleImport>();
            exportedValue.ShouldNotBeNull();
            exportedValue.IntValue.ShouldEqual(1234);
        }
Ejemplo n.º 23
0
        public void ConventionCatalog_should_support_field_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportField("IntValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<int>("V1");
            Assert.That(exportedValue, Is.EqualTo(1234));
        }