Beispiel #1
0
        public void JsonDeserializeEntityInstance()
        {
            TextAsset jsonProject = TestJsonLoader.LoadJson(TestJsonLoader.MOCK_ENTITY_INSTANCE);

            //try deserializing entity
            LDtkDataEntity entity = JsonConvert.DeserializeObject <LDtkDataEntity>(jsonProject.text);
        }
Beispiel #2
0
        private static void InstantiateEntity(LDtkDataEntity entityData, GameObject assetPrefab, Vector2 spawnPos,
                                              GameObject layerObj, int layerSortingOrder)
        {
            GameObject instance = Object.Instantiate(assetPrefab, spawnPos, Quaternion.identity, layerObj.transform);

            LDtkFieldInjector.InjectInstanceFields(entityData, instance);

            SetEntitySortingOrder(instance, layerSortingOrder);
        }
Beispiel #3
0
        private static void BuildEntityInstance(LDtkDataLayer layerData, LDtkDataEntity entityData,
                                                LDtkEntityAssetCollection entityAssets, GameObject layerObj, int layerSortingOrder)
        {
            LDtkEntityAsset entityAsset = entityAssets.GetAssetByIdentifier(entityData.__identifier);

            if (entityAsset == null)
            {
                return;
            }

            int        pixelsPerUnit = layerData.__gridSize;
            Vector2Int pixelPos      = entityData.px.ToVector2Int();
            Vector2    spawnPos      = (LDtkToolOriginCoordConverter.ConvertPosition(pixelPos, layerData.__cHei * pixelsPerUnit, pixelsPerUnit) / pixelsPerUnit) + Vector2.up;

            InstantiateEntity(entityData, entityAsset.ReferencedAsset, spawnPos, layerObj, layerSortingOrder);
        }
        public static void InjectInstanceFields(LDtkDataEntity entity, GameObject instance)
        {
            if (entity.fieldInstances.NullOrEmpty())
            {
                return;
            }

            MonoBehaviour[] behaviors = instance.GetComponents <MonoBehaviour>();
            List <LDtkFieldInjectorData> injectableFields = GatherInjectableFields(behaviors);

            CheckFieldDefinitionsExistence(entity.__identifier,
                                           entity.fieldInstances.Select(p => p.__identifier).ToList(),
                                           injectableFields.Select(p => p.FieldIdentifier).ToList());

            //run though all of the LEd variables as the main proprietor.
            InjectAllFieldsIntoInstance(injectableFields, entity);
        }
        private static void InjectFieldIntoInstance(LDtkDataEntity entity, LDtkDataField instanceField,
                                                    List <LDtkFieldInjectorData> injectableFields)
        {
            string dataFieldIdentifier = instanceField.__identifier;

            LDtkFieldInjectorData fieldToInjectInto = GetInjectableFieldMatchingIdentifier(injectableFields, dataFieldIdentifier);

            if (fieldToInjectInto == null)
            {
                Debug.LogError($"LDtk: '{entity.__identifier}'s LDtk {instanceField.__type} field \"{dataFieldIdentifier}\" could not find a matching Game Code field to inject into. Is the field not public?");
                return;
            }

            if (fieldToInjectInto.Info.FieldType.IsArray)
            {
                InjectArray(instanceField, fieldToInjectInto);
            }
            else
            {
                InjectSingle(instanceField, fieldToInjectInto);
            }
        }
Beispiel #6
0
        private static void BuildEntityInstance(LDtkDataLayer layerData, LDtkDataEntity entityData,
                                                LDtkEntityAssetCollection entityAssets, GameObject layerObj, int layerSortingOrder)
        {
            LDtkEntityAsset entityAsset = entityAssets.GetAssetByIdentifier(entityData.__identifier);

            if (entityAsset == null)
            {
                return;
            }

            int        pixelsPerUnit = layerData.__gridSize;
            Vector2Int pixelPos      = entityData.px.ToVector2Int();
            Vector2    spawnPos      = (LDtkToolOriginCoordConverter.ConvertPosition(pixelPos, layerData.__cHei * pixelsPerUnit, pixelsPerUnit) / pixelsPerUnit) + Vector2.up;

            GameObject entityObj = InstantiateEntity(entityAsset.ReferencedAsset, spawnPos, layerObj);

            LDtkFieldInjector.InjectInstanceFields(entityData, entityObj);

            MonoBehaviour[] behaviors = entityObj.GetComponents <MonoBehaviour>();

            PostEntityInterfaceEvent <ILDtkFieldInjectedEvent>(behaviors, e => e.OnLDtkFieldsInjected());
            PostEntityInterfaceEvent <ILDtkSettableSortingOrder>(behaviors, e => e.OnLDtkSetSortingOrder(layerSortingOrder));
            PostEntityInterfaceEvent <ILDtkSettableOpacity>(behaviors, e => e.OnLDtkSetOpacity(layerData.__opacity));
        }
 private static void InjectAllFieldsIntoInstance(List <LDtkFieldInjectorData> injectableFields, LDtkDataEntity entity)
 {
     foreach (LDtkDataField instanceField in entity.fieldInstances)
     {
         InjectFieldIntoInstance(entity, instanceField, injectableFields);
     }
 }