/// <summary>
        /// Update the element position acording to Parallax.
        /// </summary>
        /// <param name="elem"></param>
        void UpdateElementPos(ParallaxElement elem)
        {
            if (elem.OffsetSpeed == Vector2.zero)
            {
                return;
            }

            Vector3 mvmDist = transform.position - campPrevPos;

            if (!elem.reverseX)
            {
                mvmDist.x *= -1;
            }
            if (!elem.reverseY)
            {
                mvmDist.y *= -1;
            }

            Vector3 newElemPos = elem.Trans.position + (new Vector3(mvmDist.x * elem.OffsetSpeed.x, mvmDist.y * elem.OffsetSpeed.y, elem.Trans.position.z));

            float journeyLength   = Vector3.Distance(elem.Trans.position, newElemPos);
            float distanceCovered = elem.OffsetSpeed.x;             // speed
            float fracJourney     = distanceCovered / journeyLength;

            elem.Trans.position = Vector3.Lerp(elem.Trans.position, newElemPos, fracJourney);
        }
    private void SpawnIsles()
    {
        // OBJECT LAYER SPAWN
        for (int i = 0; i < objectsInLayer; i++)
        {
            int objectID = Random.Range(0, Controler.ParallaxIsles.Length);

            Object     paralaxObject = Controler.ParallaxIsles[objectID];
            GameObject go            = (GameObject)Instantiate(paralaxObject, transform) as GameObject;

            SpriteRenderer renderer         = go.GetComponent <SpriteRenderer>();
            SpriteRenderer childrenRenderer = go.transform.GetChild(0).GetComponent <SpriteRenderer>();

            // Position
            go.transform.position   = CalculatePosition(i);
            go.transform.localScale = CalculateScale(go.transform.localScale);
            // Layer
            SetSpriteRenderers(ref renderer, ref childrenRenderer, go.transform.position.z);
            // Add to the List
            ParallaxElement element = go.GetComponent <ParallaxElement>();
            element.SetLayer(this);

            parallaxElements.Add(go.GetComponent <ParallaxElement>());
        }
    }
Exemple #3
0
    /// <summary>
    /// Add a new element to the parallax
    /// </summary>
    /// <param name="e">Element to add</param>
    public void AddElement(ParallaxElement e)
    {
        if (Elements == null)
        {
            Elements = new List <ParallaxElement>();
        }
        int i = Elements.Count;

        Elements.Add(e);
        SetupElementAtIndex(i);
    }
Exemple #4
0
 protected override void Awake()
 {
     base.Awake();
     Target = GetComponentInChildren <ParallaxElement>();
     Context.CharacterManager.OnActiveCharacterSet.AddListener(async it =>
     {
         if (WillDelaySet)
         {
             await UniTask.Delay(TimeSpan.FromSeconds(0.4f));
         }
         Load(it.parallaxPrefab);
     });
 }
        /// <summary>
        /// Set the initial element position acording to Parallax.
        /// </summary>
        /// <param name="elem">The element to be set.</param>
        void SetStartingElementPos(ParallaxElement elem)
        {
            Vector3 startingPlayerPos = GetStartingPlayerPos();
            Vector3 mvmDist           = startingPlayerPos - GameObject.FindGameObjectWithTag("Player").transform.position;

            if (!elem.reverseX)
            {
                mvmDist.x *= -1;
            }
            if (!elem.reverseY)
            {
                mvmDist.y *= -1;
            }

            Vector3 newElemPos = elem.Trans.position + (new Vector3(mvmDist.x * elem.OffsetSpeed.x, mvmDist.y * elem.OffsetSpeed.y, elem.Trans.position.z));

            elem.Trans.position = newElemPos;
        }
    /// <summary>
    /// The main method that implemented parallax.
    /// </summary>
    /// <param name="direction"> Parallax movement direction </param>
    protected void Parallaxing(Vector3 direction)
    {
        if (!_isInitialize)
        {
            return;
        }

        if (ParallaxLayers.Length <= 0)
        {
            Debug.LogError("UIParallax! Parallax layers are not found");
            return;
        }

        for (int i = 0; i < ParallaxLayers.Length; i++)
        {
            ParallaxElement parallaxLayer = ParallaxLayers[i];

            Vector2 offset = direction * Time.deltaTime * parallaxLayer.Intensity;

            if (ParallaxLayers[i].Image == null)
            {
                Debug.LogError("ParallaxElement with id : " + i + " error! Not found Image!");
                continue;
            }

            float x = ParallaxLayers[i].Image.uvRect.x + offset.x;
            float y = ParallaxLayers[i].Image.uvRect.y + offset.y;

            if (x > 1 || x < -1)
            {
                x = 0;
            }

            if (y > 1 || y < -1)
            {
                y = 0;
            }

            ParallaxLayers[i].Image.uvRect = new Rect(x, y, ParallaxLayers[i].Image.uvRect.width, ParallaxLayers[i].Image.uvRect.height);
        }
    }
Exemple #7
0
    private void SetupElementAtIndex(int i)
    {
        ParallaxElement e = Elements[i];

        if (e.GameObjects == null || e.GameObjects.Count == 0)
        {
            Debug.LogError("No game objects found at element index " + i.ToString() + ", be sure to set at least one game object for each element in the parallax");
            return;
        }
        foreach (GameObject obj in e.GameObjects)
        {
            if (obj == null)
            {
                Debug.LogError("Null game object found at element index " + i.ToString());
                return;
            }
        }

        e.SetupState(this, parallaxCamera, i);
        e.SetupScale(this, parallaxCamera, i);
        e.SetupPosition(this, parallaxCamera, i);
    }
    private void SpawnClouds()
    {
        for (int i = 0; i < cloudsNumber; i++)
        {
            int        cloudID  = Random.Range(0, Controler.ParallaxClouds.Length);
            Object     cloudObj = Controler.ParallaxClouds[cloudID];
            GameObject cloudGo  = (GameObject)Instantiate(cloudObj, transform) as GameObject;

            cloudGo.transform.localScale = CalculateCloudScale(cloudGo.transform.localScale);

            int posXnb;
            int posYnb;

            posXnb = Mathf.CeilToInt(layerRect.width / minSpaceBetweenClouds.x);
            posYnb = Mathf.CeilToInt(layerRect.height / minSpaceBetweenClouds.y);

            int posXid;
            int posYid;

            posXid = Random.Range(0, posXnb);
            posYid = Random.Range(0, posYnb);

            Vector3 pos = cloudGo.transform.position;
            pos.x = layerRect.xMin + posXid * minSpaceBetweenClouds.x;
            pos.y = layerRect.yMin + posYid * minSpaceBetweenClouds.y + cloudsOffsetY;
            pos.z = (parallaxElements.Count > 0) ? (parallaxElements[parallaxElements.Count - 1].transform.position.z + 5) : (transform.position.z + 20);
            cloudGo.transform.position = pos;

            SpriteRenderer renderer = cloudGo.GetComponent <SpriteRenderer>();
            renderer.sortingOrder = (int)(-pos.z) + Controler.MinSortingOrder + i;

            ParallaxElement element = cloudGo.GetComponent <ParallaxElement>();
            element.SetLayer(this);
            parallaxElements.Add(cloudGo.GetComponent <ParallaxElement>());
        }
    }
Exemple #9
0
 public void Load(ParallaxElement parallax)
 {
     Unload();
     Target = Instantiate(parallax, transform);
 }
Exemple #10
0
        public void AddElement(LayerObject[] layerObjects, LayerType type)
        {
            foreach (LayerObject layerObject in layerObjects)
            {
                MapCollision collision  = null;
                MapElement   mapElement = null;

                switch (type)
                {
                case LayerType.Background:
                    mapElement = new MapElement(layerObject.Source.Value, layerObject.DestinationRectangle);
                    backgroundElements.Add(mapElement);
                    break;

                case LayerType.Parallax:
                    ParallaxElement.HorizontalDirection parallaxDirection = ParallaxElement.HorizontalDirection.Left;
                    if (layerObject.Direction == "right")
                    {
                        parallaxDirection = ParallaxElement.HorizontalDirection.Right;
                    }
                    mapElement = new ParallaxElement(layerObject.MoveSpeed.Value,
                                                     parallaxDirection,
                                                     layerObject.Source.Value, layerObject.DestinationRectangle);
                    backgroundElements.Add(mapElement);
                    break;

                case LayerType.PathBlock:
                    collision = new PathBlockCollision(layerObject.DestinationRectangle);
                    collisions.Add(collision);
                    break;

                case LayerType.SceneryElements:
                    if (layerObject.Collision != null)
                    {
                        collision = new PathBlockCollision(layerObject.Collision.Value);
                        collisions.Add(collision);
                        if (layerObject.AnimationSequence != null)
                        {
                            ElementAnimation animation = GetAnimation(layerObject);
                            mapElement = new MapElement(
                                layerObject.Source.Value,
                                layerObject.DestinationRectangle,
                                animation, collision
                                );
                        }
                        else
                        {
                            mapElement = new MapElement(layerObject.Source.Value,
                                                        layerObject.DestinationRectangle, collision);
                        }
                        middlegroundElements.Add(mapElement);
                    }
                    else
                    {
                        if (layerObject.AnimationSequence != null)
                        {
                            ElementAnimation animation = GetAnimation(layerObject);
                            mapElement = new MapElement(
                                layerObject.Source.Value,
                                layerObject.DestinationRectangle,
                                animation);
                        }
                        else
                        {
                            mapElement = new MapElement(layerObject.Source.Value,
                                                        layerObject.DestinationRectangle);
                        }
                        mapElement = new MapElement(layerObject.Source.Value,
                                                    layerObject.DestinationRectangle);
                        middlegroundElements.Add(mapElement);
                    }
                    break;

                case LayerType.Transition:
                    Rectangle rectangle = new Rectangle(layerObject.X, layerObject.Y,
                                                        layerObject.Width, layerObject.Height);
                    collision = new TransitionCollision(layerObject.ToId.Value, layerObject.ToMap, rectangle);
                    collisions.Add(collision);
                    break;
                }
            }
        }