1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using DG.Tweening;
- using System;
- using UnityEngine;
- using XGame.Framework.Components;
- using XGame.Framework.Map;
- namespace FL.Battle
- {
- public interface IMoveContext
- {
- Transform OwnerTr { get; }
- }
- /// <summary>
- /// 移动参数
- /// </summary>
- public struct MoveArgs
- {
- public Vector3 target;
- public float speed;
- public Ease ease;
- /// <summary>
- /// 延迟,单位: 毫秒
- /// </summary>
- public int delay;
- public Action callback;
- }
- public class MoveComponent : Component<IMoveContext>, IParallelTarget
- {
- public override int Order => 0;
- private Tween _tween;
- public Vector3 MoveDir { get; private set; }
- public bool IsMoving => _tween != null;
- Vector3 IParallelTarget.Position => Context.OwnerTr.position;
- public void MoveTo(MoveArgs args)
- {
- var position = Context.OwnerTr.localPosition;
- var duration = Mathf.Abs(args.target.y - position.y) / args.speed;
- MoveDir = Vector3.Normalize(args.target - position);
- var tween = Context.OwnerTr.DOLocalMoveY(args.target.y, duration);
- if (args.delay > 0)
- {
- tween.SetDelay(args.delay * 0.001f);
- }
- _tween = tween;
- var ease = args.ease == Ease.Unset ? Ease.Linear : args.ease;
- tween.SetEase(ease);
- tween.onComplete += () =>
- {
- _tween = null;
- MoveDir = Vector3.zero;
- args.callback?.Invoke();
- };
- }
- public void StopMove()
- {
- _tween?.Kill();
- _tween = null;
- MoveDir = Vector3.zero;
- }
- protected override void OnDispose()
- {
- StopMove();
- }
- }
- }
|