PassiveMove.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PassiveMove
  4. {
  5. Fighter mFighter;
  6. int mMoveType = 0;
  7. Fix64 mAcceleration;
  8. Fix64 mVelocity = Fix64.Zero;
  9. Fix64 mInitVelocity = Fix64.Zero;
  10. FixVector3 mDestPos = FixVector3.Zero;
  11. Fix64 mMoveTime = Fix64.Zero; //浮空时长
  12. Fix64 mCurrentTime = Fix64.Zero;
  13. bool mMoveEnd = true;
  14. int mFrame = 0;
  15. int mTotalFrame = 0;
  16. FixVector3 SelfStartPos;
  17. FixVector3 SelfForward;
  18. Fix64 fStartSpeed;
  19. Fix64 fAcceleration;
  20. FixVector3 mTargetPosition;
  21. public PassiveMove (Fighter fighter)
  22. {
  23. mFighter = fighter;
  24. }
  25. public BattleField Field{ get { return mFighter.CurrentBattleField; } }
  26. public bool IsGround { get { return Field == null || PositionHeight <= (Fix64)mFighter.CurrentBattleField.FloorY; } }
  27. public Fix64 PositionHeight { get { return (Fix64)mFighter.Position.y; } }
  28. public Fix64 MoveTime { get { return mMoveTime; } }
  29. public void BeHitToMove(int moveType,Fix64 initSpeed, Fix64 acceleration,FixVector3 destPos)
  30. {
  31. Stop();
  32. mMoveType = moveType;
  33. SelfStartPos = new FixVector3(mFighter.Position);
  34. mTargetPosition = destPos;
  35. SelfForward = mTargetPosition - SelfStartPos;
  36. SelfForward.Normalize();
  37. fStartSpeed = initSpeed;
  38. fAcceleration = acceleration;
  39. mMoveEnd = false;
  40. //Debug.Log("Start destPos" + destPos.ToString());
  41. Fix64 dist = FixVector3.Distance(destPos, SelfStartPos);
  42. if (Fix64.Abs(acceleration) > 0)
  43. {
  44. Fix64 fvalue = (Fix64)2 * acceleration * dist + initSpeed * initSpeed;
  45. if(fvalue > Fix64.Zero)
  46. {
  47. Fix64 vt = Fix64.Sqrt(fvalue);
  48. mMoveTime = Fix64.Abs(vt - initSpeed) / acceleration;
  49. }
  50. else
  51. {
  52. Fix64 timeV = initSpeed / Fix64.Abs(acceleration);
  53. mMoveTime = mMoveTime < timeV ? mMoveTime : timeV;
  54. }
  55. }
  56. else
  57. {
  58. if (initSpeed > 0)
  59. mMoveTime = dist / initSpeed;
  60. else
  61. mMoveTime = Fix64.Zero;
  62. }
  63. mMoveEnd = false;
  64. mFrame = 0;
  65. mTotalFrame = (int)(mMoveTime * (Fix64)Constants.frame_to_time) + 2;
  66. //string strinfo = string.Format("Start Hit PassiveMove FieldFrame = {0},stateFrame = {1}, mTotalFrame= {2},mMoveTime = {3},dist = {4},mAcceleration = {5},mDestPos = {6}",
  67. // Field.BattleFrame.ToString(), Field.CurrentStateFrame.ToString(), mTotalFrame.ToString(), mMoveTime.ToString(), dist.ToString(), mAcceleration.ToString(), mDestPos.ToString());
  68. //Debug.Log(strinfo);
  69. //DebugHelper.LogError(mFighter.Name+"----BeHitToMove :" + mMoveType + " movetime:" + mMoveTime + " acc:" + mAcceleration + " dist:" + dist + " initSpeed:" + initSpeed + " destPos:" + destPos.ToString() + " pos:" + mFighter.Position + " mLastHeight:" + mLastHeight);
  70. }
  71. public void Update (float deltaTime)
  72. {
  73. if (!mMoveEnd)
  74. {
  75. mFrame += 1;
  76. //帧数差值
  77. //帧数差值
  78. Fix64 fLerpTime = (Fix64)mFrame * (Fix64)0.03f;
  79. Fix64 fDis = fStartSpeed * fLerpTime + (Fix64)0.5f * fAcceleration * fLerpTime * fLerpTime;
  80. FixVector3 pos = SelfStartPos + SelfForward * fDis;
  81. FixVector3 v3Forward = mTargetPosition - pos;
  82. v3Forward.Normalize();
  83. if (FixVector3.Dot(SelfForward, v3Forward) <= Fix64.Zero)
  84. {
  85. mFighter.SyncPosition(mTargetPosition.ToVector3());
  86. Stop();
  87. if (!IsGround)
  88. {
  89. DownToGround();
  90. }
  91. return;
  92. }
  93. mFighter.Ctrl.MoveTo(pos);
  94. // Debug.Log("fAcceleration" + fAcceleration.ToString() + "dist =" + fDis.ToString() + "mFrame =" + (mFrame / mTotalFrame).ToString() + "pos" + pos.ToString());
  95. if (mFrame >= mTotalFrame)
  96. {
  97. mFighter.SyncPosition(pos.ToVector3());
  98. Stop();
  99. if (!IsGround)
  100. {
  101. DownToGround();
  102. }
  103. return;
  104. }
  105. }
  106. }
  107. void DownToGround()
  108. {
  109. //Debug.Log("Start Down!!!!");
  110. FixVector3 pos = new FixVector3( mFighter.Position);
  111. pos.y = (Fix64)mFighter.CurrentBattleField.FloorY;
  112. BeHitToMove((int)FighterMoveType.Beat_Down, Fix64.Zero, (Fix64)Constants.gravity, pos);
  113. mFighter.StateData.SetStarkTime(mMoveTime);
  114. }
  115. void Stop()
  116. {
  117. //Debug.Log("End PassiveMove!!");
  118. mCurrentTime = Fix64.Zero;
  119. mMoveEnd = true;
  120. mFighter.StateData.SetStarkTime((Fix64)Constants.s_starkStopTime);
  121. }
  122. }