Пример #1
0
    private static SpriteRenderer CreateSpriteInstance(ImageDescriptor imageDescriptor, SpritesCache cache)
    {
        Sprite sprite = cache.Get(imageDescriptor.assetPath, imageDescriptor.name);

        if (sprite == null)
        {
            return(null);
        }

        var obj = new GameObject(imageDescriptor.name);

        return(AttachSpriteToObject(obj.transform, imageDescriptor, sprite));
    }
    public static List <Transform> CreateInstances(List <ItemDescriptor> itemDescriptors, Transform parent, SpritesCache cache)
    {
        var objectsList = new List <Transform>();

        foreach (ItemDescriptor itemDescriptor in itemDescriptors)
        {
            Transform item = new GameObject(itemDescriptor.name).transform;
            item.SetParent(parent);
            item.position = new Vector3(0, 0, 0);
            objectsList.Add(item);

            SceneItem itemSceneComponent = item.gameObject.AddComponent <SceneItem>();

            Sprite silhouette = cache.Get(itemDescriptor.displayImage.assetPath, itemDescriptor.displayImage.name);
            if (silhouette != null)
            {
                itemSceneComponent.Silhouette = silhouette;
            }
            itemSceneComponent.DisplayName = itemDescriptor.displayName;

            foreach (ItemPlaceHolderDescriptor placeHolder in itemDescriptor.placeHolders)
            {
                var placeHolderObject = CreateSpriteInstance(placeHolder.image, item, cache);

                var shadows = CreateInstances(placeHolder.shadows, placeHolderObject.transform, cache);
                foreach (var shadow in shadows)
                {
                    var component = shadow.gameObject.AddComponent <SceneItemChildLayer>();
                    component.Type = SceneItemChildLayer.LayerType.Shadow;

                    itemSceneComponent.ChildLayers.Add(component);
                }

                var patches = CreateInstances(placeHolder.patches, placeHolderObject.transform, cache);
                foreach (var pacth in patches)
                {
                    var component = pacth.gameObject.AddComponent <SceneItemChildLayer>();
                    component.Type = SceneItemChildLayer.LayerType.Patch;

                    itemSceneComponent.ChildLayers.Add(component);
                }
            }
        }

        return(objectsList);
    }
    public static SpriteRenderer CreateSpriteInstance(ImageDescriptor imageDescriptor, Transform parent, SpritesCache cache)
    {
        Sprite sprite = cache.Get(imageDescriptor.assetPath, imageDescriptor.name);

        if (sprite == null)
        {
            return(null);
        }

        Transform image = new GameObject(imageDescriptor.name).transform;

        image.SetParent(parent);
        image.position = imageDescriptor.position;

        SpriteRenderer imageRenderer = image.gameObject.AddComponent <SpriteRenderer>();

        imageRenderer.sprite       = sprite;
        imageRenderer.sortingOrder = imageDescriptor.sortingOrder;

        return(imageRenderer);
    }
Пример #4
0
    /// <summary>
    /// Create constrained count of visible SceneItems
    ///
    /// BuildParams defines constraint on visible SceneItems count
    /// </summary>
    /// <returns></returns>
    private List <SceneItem> CreateVisibleItems(List <ItemDescriptor> itemDescriptors, Transform sceneFolder, SpritesCache cache, BuildParams buildParams)
    {
        List <SceneItem> visibleItems = new List <SceneItem>();

        if (buildParams.ItemPrefab == null)
        {
#if DEBIG_PICKABLE_ITEMS_BUILDER
            Debug.LogError("Item Prefab does not exist");
#endif
            return(visibleItems);
        }

        CollisionController collisionController = new CollisionController(sceneFolder.gameObject.GetComponent <SceneComponent>().SceneSize);

        foreach (ItemDescriptor itemDescriptor in itemDescriptors)
        {
            if (itemDescriptor.placeHolders.Count == 0)
            {
#if DEBIG_PICKABLE_ITEMS_BUILDER
                Debug.LogWarningFormat("Item Descriptor {0} does not have placeholders", itemDescriptor.name);
#endif
                continue;
            }

            var shuffledPlaceHolders = shuffle(itemDescriptor.placeHolders).ToList();

            foreach (ItemPlaceHolderDescriptor placeHolder in shuffledPlaceHolders)
            {
                if (collisionController.CheckCollision(placeHolder.image))
                {
                    continue;
                }

                Sprite sprite = cache.Get(placeHolder.image.assetPath, placeHolder.image.name);
                if (sprite == null)
                {
                    continue;
                }

                GameObject sceneItem = Instantiate(buildParams.ItemPrefab, sceneFolder);

                sceneItem.name = placeHolder.image.name;
                AttachSpriteToObject(sceneItem.transform, placeHolder.image, sprite);

                SceneItem sceneItemComponent = sceneItem.GetComponent <SceneItem>();
                sceneItemComponent.DisplayName = itemDescriptor.displayName;

                Sprite silhouette = cache.Get(itemDescriptor.displayImage.assetPath, itemDescriptor.displayImage.name);
                if (silhouette != null)
                {
                    sceneItemComponent.Silhouette = silhouette;
                }

                foreach (var shadow in placeHolder.shadows)
                {
                    var obj = CreateSpriteInstance(shadow, cache);
                    // NOTE in game mode shadows attached to scene, not to scene item
                    obj.transform.SetParent(sceneFolder);
                    var childComponent = obj.gameObject.AddComponent <SceneItemChildLayer>();
                    childComponent.Type = SceneItemChildLayer.LayerType.Shadow;
                    sceneItemComponent.ChildLayers.Add(childComponent);
                }

                foreach (var patch in placeHolder.patches)
                {
                    var obj = CreateSpriteInstance(patch, cache);
                    // NOTE in game mode patches attached to scene, not to scene item
                    obj.transform.SetParent(sceneFolder);
                    var childComponent = obj.gameObject.AddComponent <SceneItemChildLayer>();
                    childComponent.Type = SceneItemChildLayer.LayerType.Patch;
                    sceneItemComponent.ChildLayers.Add(childComponent);
                }

                visibleItems.Add(sceneItemComponent);

                break;
            }

            if (visibleItems.Count >= buildParams.VisibleItemCount)
            {
                break;
            }
        }

        return(visibleItems);
    }