/*--------------------------------------------------------------------
- Author Name: DXL
- Creation Time: 5/17/2019 10:56:13 AM
- File Describe: 背景云朵循环漂浮功能
- ------------------------------------------------------------------*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CloudsFloat : MonoBehaviour
{
#region
public CloudDir direction = CloudDir.Left; //移动方向
public float speed = 10; //移动速度
public CloudLoopType loopType = CloudLoopType.Restart; //循环方式
public float startPoint; //起始点
public float endPoint; //移动结束点
public bool startByMyself = true; //是否使用当前自身点作为起始点(仅仅首次)
public delegate void MovePosCallback(GameObject go); //再移动位置的时候调用一次,目前仅支持Uniform方式
MovePosCallback callback;
bool IsPause = false; //当前是否处于暂停状态
bool IsStope = false; //当前是否处于停止状态
public enum CloudDir
{
Left, //从右向左移动
Right,
Up, //从向向上移动
Down
}
public enum CloudLoopType
{
NoLoop, //不循环
Restart, //从头开始移动,先快后满,速度不均匀
Reverse, //移动结束后开始反向移动
Uniform, //匀速移动
}
#endregion
#region
void Start()
{
if (!startByMyself)
{
//先将自身位置放在起始位置
if (direction == CloudDir.Left || direction == CloudDir.Right)
{
transform.localPosition = new Vector3(startPoint, transform.localPosition.y, 0);
}
else
{
transform.localPosition = new Vector3(transform.localPosition.x, startPoint, 0);
}
}
if (direction == CloudDir.Left || direction == CloudDir.Right)
{
if (loopType == CloudLoopType.NoLoop)
{
transform.DOLocalMoveX(endPoint, Mathf.Abs(endPoint - transform.localPosition.x) / speed).SetEase(Ease.Linear);
}
else if (loopType == CloudLoopType.Restart)
{
transform.DOLocalMoveX(endPoint, Mathf.Abs(endPoint - transform.localPosition.x) / speed).OnComplete(() =>
{
transform.localPosition = new Vector3(startPoint, transform.localPosition.y, 0);
transform.DOLocalMoveX(endPoint, Mathf.Abs(endPoint - startPoint) / speed).SetLoops(-1, LoopType.Restart).SetEase(Ease.Linear);
});
}
else if (loopType == CloudLoopType.Reverse)
{
transform.DOLocalMoveX(endPoint, Mathf.Abs(endPoint - transform.localPosition.x) / speed).OnComplete(() =>
{
transform.DOLocalMoveX(startPoint, Mathf.Abs(endPoint - startPoint) / speed).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
});
}
}
else if (direction == CloudDir.Up || direction == CloudDir.Down)
{
if (loopType == CloudLoopType.NoLoop)
{
transform.DOLocalMoveY(endPoint, Mathf.Abs(endPoint - transform.localPosition.y) / speed).SetEase(Ease.Linear);
}
else if (loopType == CloudLoopType.Restart)
{
transform.DOLocalMoveY(endPoint, Mathf.Abs(endPoint - transform.localPosition.y) / speed).OnComplete(() =>
{
transform.localPosition = new Vector3(transform.localPosition.x, startPoint, 0);
transform.DOLocalMoveY(endPoint, Mathf.Abs(endPoint - transform.localPosition.x) / speed).SetLoops(-1, LoopType.Restart).SetEase(Ease.Linear);
});
}
else if (loopType == CloudLoopType.Reverse)
{
transform.DOLocalMoveY(endPoint, Mathf.Abs(endPoint - transform.localPosition.y) / speed).OnComplete(() =>
{
transform.DOLocalMoveY(startPoint, Mathf.Abs(endPoint - startPoint) / speed).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
});
}
}
}
private void Update()
{
if(IsPause||IsStope)
{
return;
}
if (loopType == CloudLoopType.Uniform)
{
float dis = speed * Time.deltaTime;
if (direction == CloudDir.Left)
{
transform.Translate(Vector3.left * dis);
if (transform.localPosition.x <= endPoint)
{
transform.localPosition = new Vector3(startPoint, transform.localPosition.y, transform.localPosition.z);
if(callback != null)
{
callback(this.gameObject);
}
}
}
else if (direction == CloudDir.Right)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
if (transform.localPosition.x > endPoint)
{
transform.localPosition = new Vector3(startPoint, transform.localPosition.y, transform.localPosition.z);
if (callback != null)
{
callback(this.gameObject);
}
}
}
else if (direction == CloudDir.Up)
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
if (transform.localPosition.y > endPoint)
{
transform.localPosition = new Vector3(transform.localPosition.x, startPoint, transform.localPosition.z);
if (callback != null)
{
callback(this.gameObject);
}
}
}
else if (direction == CloudDir.Down)
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
if (transform.localPosition.y < endPoint)
{
transform.localPosition = new Vector3(transform.localPosition.x, startPoint, transform.localPosition.z);
if (callback != null)
{
callback(this.gameObject);
}
}
}
}
}
#endregion
#region out
public void SetInfo(CloudDir dir, float spd, CloudLoopType loop, float start, float end, bool myself)
{
this.direction = dir;
this.speed = spd;
this.loopType = loop;
this.startPoint = start;
this.endPoint = end;
this.startByMyself = myself;
}
public void Toggle()
{
DOTween.TogglePause(transform);
IsPause = !IsPause;
}
public void Pause()
{
DOTween.Pause(transform);
IsPause = true;
}
public void Stop()
{
DOTween.Kill(transform);
IsStope = true;
}
public void CallMoveBack(MovePosCallback call)
{
this.callback = call;
}
#endregion
}