public void ConvertToNewType(int typeIndex, Action <BubbleScript> callback)
 {
     if (!HandleAnimation(BubbleAnimations.ConvertType))
     {
         return;
     }
     _newTypeIndex    = typeIndex;
     _newTypeCallback = callback;
     if (_parentGrid.mergeFadeOut)
     {
         Sprite.DOFade(0.2f, _parentGrid.mergeDuration);
     }
     StopCoroutine("DOScale");
     animation = BubbleAnimations.ConvertType;
     StartCoroutine(DOScale(new Vector3(0.6f, 0.6f), _parentGrid.mergeDuration, () =>
     {
         transform.localScale = this._startScale;
         if (_parentGrid.mergeFadeOut)
         {
             Sprite.DOFade(1, 0);
         }
         SetType(typeIndex, true);
         callback?.Invoke(this);
         _newTypeCallback = null;
     }, _parentGrid.mergeExplodeDelay));
 }
    public void ExplodeWithNeighbors()
    {
        foreach (var n in neighbors.GetNeighborsList())
        {
            if (n.bubble == null)
            {
                continue;
            }
            n.bubble.MoveToAndExplode(transform.position, _parentGrid.mergeExplodeDelay);
        }

        if (!HandleAnimation(BubbleAnimations.ScaleAndExplode))
        {
            return;
        }
        RemoveFromParentGrid();
        Sprite.DOFade(0.2f, _parentGrid.mergeDuration).SetDelay((_parentGrid.mergeExplodeDelay * 2));
        StopCoroutine("DOScale");
        animation = BubbleAnimations.ScaleAndExplode;
        StartCoroutine(DOScale(new Vector3(0.6f, 0.6f), _parentGrid.mergeDuration, () =>
        {
            transform.localScale = this._startScale;
            Explode(true);
        }));
    }
    public void SetNewLinePosition(BubbleGrid parentGrid, Dimension position)
    {
        this._parentGrid  = parentGrid;
        this.gridPosition = position;
        if (gridPosition.rows == 0)
        {
            isConnected = true;
        }
        isShifted          = this._lastOffset > 0;
        this.scenePosition = new Vector3(
            ((this.gridPosition.columns * _parentGrid.bubbleSize) - _parentGrid.firstBubblePos.x) + this._lastOffset + _parentGrid.leftPadding,
            (((-this.gridPosition.rows * _parentGrid.bubbleSize) + _parentGrid.firstBubblePos.y) + _parentGrid.topPadding),
            0f);

        if (!HandleAnimation(BubbleAnimations.NewLine))
        {
            return;
        }
        StopCoroutine("DOMove");
        animation = BubbleAnimations.NewLine;
        StartCoroutine(DOMove(scenePosition, _parentGrid.newLineSpeed, () =>
        {
            transform.localScale = this._startScale;
        }));
    }
 public override void Initialize()
 {
     base.Initialize();
     neighborsColor     = Color.red;
     isVisited          = false;
     animation          = BubbleAnimations.None;
     _isRemovedFromGrid = false;
     if (GameController.Instance.aimSetting == AimSetting.AimOnAvailableNeighbor)
     {
         collider.radius = this.neighborColliderSize;
     }
     else
     {
         collider.radius = this.bubbleColliderSize;
     }
 }
    public void MoveToAndExplode(Vector2 pos, float delay = 0f, Action callback = null)
    {
        RemoveFromParentGrid();

        if (!HandleAnimation(BubbleAnimations.MoveAndExplode))
        {
            return;
        }
        _moveAndExplodeCallback = callback;
        StopCoroutine("DOMove");
        animation = BubbleAnimations.MoveAndExplode;
        StartCoroutine(DOMove(pos, _parentGrid.mergeDuration, () =>
        {
            Explode(true);
            callback?.Invoke();
            _moveAndExplodeCallback = null;
        }, delay));
    }
    private IEnumerator DOScale(Vector3 scale, float timeToMove, Action callback = null, float delay = 0f)
    {
        if (delay > 0)
        {
            yield return(new WaitForSeconds(delay));
        }
        var currentScale = transform.localScale;
        var t            = 0f;

        while (t < 1)
        {
            if (!gameObject.activeInHierarchy)
            {
                yield break;
            }
            t += Time.deltaTime / timeToMove;
            transform.localScale = Vector3.Lerp(currentScale, scale, t);
            yield return(null);
        }
        animation            = BubbleAnimations.None;
        transform.localScale = scale;
        callback?.Invoke();
    }
    public void SpawnNewBubble(BubbleGrid parentGrid, Dimension position, float offset)
    {
        this._parentGrid  = parentGrid;
        this.gridPosition = position;
        if (gridPosition.rows == 0)
        {
            isConnected = true;
        }
        if (offset <= -100)
        {
            this._lastOffset = position.rows % 2 == 0
                                ? 0f
                                : _parentGrid.rowsOffset;
        }
        else
        {
            this._lastOffset = offset;
        }

        isShifted = _lastOffset > 0;

        this.scenePosition = new Vector3(
            ((this.gridPosition.columns * _parentGrid.bubbleSize) - _parentGrid.firstBubblePos.x) + _lastOffset + _parentGrid.leftPadding,
            (((-this.gridPosition.rows * _parentGrid.bubbleSize) + _parentGrid.firstBubblePos.y) + _parentGrid.topPadding),
            0f);

        transform.localPosition = this.scenePosition;
        transform.localScale    = Vector3.zero;
        if (!HandleAnimation(BubbleAnimations.Spawn))
        {
            return;
        }
        StopCoroutine("DOScale");
        animation = BubbleAnimations.Spawn;
        StartCoroutine(DOScale(this._startScale, _parentGrid.spawnSpeed));
    }
    //- Set Animation Priority
    private bool HandleAnimation(BubbleAnimations newAnimation)
    {
        if (!gameObject.activeInHierarchy)
        {
            return(false);
        }
        switch (newAnimation)
        {
        case BubbleAnimations.Spawn:
            if (this.animation == BubbleAnimations.None)
            {
                return(true);
            }
            else
            {
                return(false);
            }

        case BubbleAnimations.NewLine:
            if (this.animation == BubbleAnimations.None)
            {
                return(true);
            }
            else if (this.animation == BubbleAnimations.Spawn)
            {
                transform.localScale = _startScale;
                return(true);
            }
            else if (this.animation == BubbleAnimations.ConvertType)
            {
                transform.localScale = _startScale;
                Sprite.DOFade(1, 0);
                SetType(_newTypeIndex, true);
                _newTypeCallback?.Invoke(this);
                return(true);
            }
            else if (this.animation == BubbleAnimations.MoveAndExplode)
            {
                _moveAndExplodeCallback?.Invoke();
                Explode(true);
                return(false);
            }
            else if (this.animation == BubbleAnimations.ScaleAndExplode)
            {
                transform.localScale = this._startScale;
                Explode(true);
                return(false);
            }
            break;

        case BubbleAnimations.ConvertType:
            if (this.animation == BubbleAnimations.None)
            {
                return(true);
            }
            else if (this.animation == BubbleAnimations.Spawn)
            {
                transform.localScale = _startScale;
                return(true);
            }
            else if (this.animation == BubbleAnimations.NewLine)
            {
                return(true);
            }
            else if (this.animation == BubbleAnimations.MoveAndExplode)
            {
                _moveAndExplodeCallback?.Invoke();
                Explode(true);
                return(false);
            }
            else if (this.animation == BubbleAnimations.ScaleAndExplode)
            {
                transform.localScale = this._startScale;
                Explode(true);
                return(false);
            }
            break;

        case BubbleAnimations.MoveAndExplode: return(true);

        case BubbleAnimations.ScaleAndExplode: return(true);

        default: return(true);
        }

        return(true);
    }