MoveAction.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace CommonUnity3D.UGUIAction
  6. {
  7. /// <summary>
  8. /// 移动动作.
  9. /// eg:
  10. /// MoveAction move = new MoveAction();
  11. /// move.Duration = 1;
  12. /// move.TargetX = 960;
  13. /// move.TargetY = 640;
  14. /// move.ActionEaseType = CommonUnity3D.UGUIAction.EaseType.easeInOutBack;
  15. /// move.ActionFinishCallBack = MoveActionCallBack;
  16. /// cvs.AddAction(move);
  17. /// </summary>
  18. public class MoveAction : ActionBase
  19. {
  20. public const string ACTIONTYPE = "MoveAction";
  21. private float mTargetX = 0.0f;
  22. private float mTargetY = 0.0f;
  23. private float mStartX = 0.0f;
  24. private float mStartY = 0.0f;
  25. private float mTotalTime = 0.0f;
  26. private float mCurrentTime = 0.0f;
  27. private int mCurrentCycle = 0;
  28. private bool mReverse = false;
  29. private Vector2 mV = Vector2.zero;
  30. public float TargetX
  31. {
  32. get { return mTargetX; }
  33. set { mTargetX = value; }
  34. }
  35. public float TargetY
  36. {
  37. get { return mTargetY; }
  38. set { mTargetY = value; }
  39. }
  40. public float Duration
  41. {
  42. get { return mTotalTime; }
  43. set { mTotalTime = value; }
  44. }
  45. public override void onUpdate(IActionCompment unit, float deltaTime)
  46. {
  47. if (deltaTime == 0 || (mCurrentTime == mTotalTime)) return;
  48. float previousTime = mCurrentTime;
  49. float restTime = mTotalTime - mCurrentTime;
  50. float carryOverTime = deltaTime > restTime ? deltaTime - restTime : 0.0f;
  51. mCurrentTime = Math.Min(mTotalTime, mCurrentTime + deltaTime);
  52. if (mCurrentCycle < 0 && previousTime <= 0 && mCurrentTime > 0)
  53. {
  54. mCurrentCycle++;
  55. }
  56. float ratio = mCurrentTime / mTotalTime;
  57. bool reversed = mReverse && (mCurrentCycle % 2 == 1);
  58. float deltaX = mTargetX - mStartX;
  59. float deltaY = mTargetY - mStartY;
  60. float transitionValue = 0.0f;
  61. if (reversed == true)
  62. {
  63. //transitionValue = Transitions.GetTransitionValue(mTransitions, (float)(1.0 - ratio));
  64. transitionValue = EaseManager.EasingFromType(0, 1, (float)(1.0 - ratio), mEaseType);
  65. }
  66. else
  67. {
  68. //transitionValue = Transitions.GetTransitionValue(mTransitions, ratio);
  69. transitionValue = EaseManager.EasingFromType(0, 1, ratio, mEaseType);
  70. }
  71. float currentValueX = mStartX + transitionValue * deltaX;
  72. float currentValueY = mStartY + transitionValue * deltaY;
  73. mV.x = currentValueX;
  74. mV.y = currentValueY;
  75. unit.Position2D = mV;
  76. if (previousTime < mTotalTime && mCurrentTime >= mTotalTime) { mIsEnd = true; }
  77. }
  78. public override void onStart(IActionCompment unit)
  79. {
  80. Vector2 v = unit.Position2D;
  81. mStartX = v.x;
  82. mStartY = v.y;
  83. }
  84. public override bool IsEnd()
  85. {
  86. return mIsEnd;
  87. }
  88. public override string GetActionType()
  89. {
  90. return ACTIONTYPE;
  91. }
  92. }
  93. }