﻿//==================================================================================
using UnityEngine;
using DG.Tweening;
using System.Collections;
//==================================================================================
public class StagePlanet : MonoBehaviour
{
    //---------------------------------
    public Transform _center;
    //---------------------------------
    Transform _myTransf;    
    Vector3 _originScale, _targetScale;
    float _dist, _prevDist;
    bool _isOriginScaleSwitchOn = false;
    //---------------------------------
    void Start()
    {
        _myTransf = transform;
        _originScale = _myTransf.lossyScale;
        _targetScale = _originScale + (_originScale * 0.5f);
        _dist = Vector3.Distance(_myTransf.localPosition, _center.localPosition);
        _prevDist = _dist;
        _isOriginScaleSwitchOn = false;

        if (Mathf.Abs(_dist) <= 0.1f)
        {
            SetScaleTarget();
            _isOriginScaleSwitchOn = true;
        }
        else
            SetScaleOriginal();

        InvokeRepeating("GCCollect", 2f, 30f);

    }// void Start()
    //---------------------------------
    void GCCollect()    { System.GC.Collect(); }
    //---------------------------------    
    void Update()
    {
        _dist = Vector3.Distance(_myTransf.position, _center.localPosition);
        
        if( _prevDist != _dist )
        {
            if (Mathf.Abs(_dist) <= 0.000005f)
            {
                SetScaleTarget();
                _isOriginScaleSwitchOn = true;

            }// if (Mathf.Abs(_dist) <= 0.000005f)
            else
            {
                if (_isOriginScaleSwitchOn == true)
                {
                    SetScaleOriginal();
                    _isOriginScaleSwitchOn = false;

                }// if (_isOriginScaleSwitchOn == true)

            }// ~if (Mathf.Abs(_dist) <= 0.000005f)

        }// if( _prevDist != _dist )

        _prevDist = _dist;

    }// void Update()
    //---------------------------------
    void SetScaleTarget()       { _myTransf.DOScale(_targetScale, 0.3f); }
    //---------------------------------
    void SetScaleOriginal()     { _myTransf.DOScale(_originScale, 0.3f); }
    //---------------------------------

}// public class StagePlanet : MonoBehaviour
//==================================================================================