BulletMoveProcessor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using System.Collections;
  3. public class BulletMoveProcessor
  4. {
  5. float bullet_move_speed_perFrame = 10f;
  6. const float bullet_throw_speed = 10f;
  7. const float bullet_throw_gravity = 13f;
  8. Bullet mBullet;
  9. bool mEnabled = false;
  10. Vector3 mVelocity = Vector3.zero;
  11. Vector3 mAcceleration = Vector3.zero;
  12. bool mToward = false;
  13. bool mTraceTarget = false;
  14. //float mLeftAccelerationTime = 0;
  15. int mLeftAccelerationFrame = 0;
  16. public BulletMoveProcessor(Bullet bullet,float moveSpeed)
  17. {
  18. mBullet = bullet;
  19. bullet_move_speed_perFrame = moveSpeed;
  20. InitMoveData ();
  21. }
  22. private void InitMoveData()
  23. {
  24. BulletMoveType moveType = mBullet.BulletData.moveType;
  25. mVelocity = mBullet.BulletData.initSpeedVec / Constants.frame_to_time;
  26. mAcceleration = mBullet.BulletData.accelationSpeedVec / Constants.frame_to_time;
  27. mLeftAccelerationFrame = (int)(mBullet.BulletData.accelationDuration * Constants.frame_to_time);
  28. mBullet.LookAt(mBullet.TargetPosition);
  29. if(mLeftAccelerationFrame <= 0)
  30. {
  31. mTraceTarget = true;
  32. }
  33. mEnabled = true;
  34. }
  35. public void Update(float deltaTime)
  36. {
  37. if (!mEnabled)
  38. return;
  39. //mBullet.SetPosition (mBullet.Position + mVelocity * deltaTime);
  40. mBullet.SetPosition(mBullet.Position + mVelocity);
  41. if (mBullet.Ctrl!=null)
  42. {
  43. mBullet.ForceSync(mBullet.Ctrl.transform);
  44. }
  45. if (mLeftAccelerationFrame > 0)
  46. {
  47. mVelocity += mAcceleration;
  48. mLeftAccelerationFrame--;
  49. if(mLeftAccelerationFrame <= 0)
  50. {
  51. mBullet.LookAt(mBullet.TargetPosition);
  52. float leftTime = (mBullet.TargetPosition.z - mBullet.Position.z) / mVelocity.z;
  53. mVelocity.x = (mBullet.TargetPosition.x - mBullet.Position.x) / leftTime;
  54. mVelocity.y = (mBullet.TargetPosition.y - mBullet.Position.y) / leftTime;
  55. //mToward = true;
  56. mTraceTarget = true;
  57. //DebugHelper.LogError("deltaZ:" + (mBullet.TargetPosition.z - mBullet.Position.z) + " mVelocity.z"+ mVelocity.z + "leftTime:" + leftTime+" mVelocity:" + mVelocity);
  58. }
  59. }
  60. if (mToward)
  61. mBullet.LookAt(mBullet.TargetPosition);
  62. if (mTraceTarget && !mBullet.TargetPosition.FEqual(mBullet.Position, 1e-2f))
  63. mVelocity = (mBullet.TargetPosition - mBullet.Position).normalized * mVelocity.magnitude;
  64. }
  65. Vector3 GetThrowVelocity(Vector3 startPostion, Vector3 targetPosition)
  66. {
  67. Vector3 offset = targetPosition - startPostion;
  68. Vector3 dirX = offset.SetY (0).normalized;
  69. float distanceX = offset.SetY (0).magnitude;
  70. float distanceY = offset.y;
  71. float flyTime = Mathf.Max(Mathf.Max(distanceX / bullet_throw_speed, Mathf.Sqrt (Mathf.Abs (2 * distanceY) / bullet_throw_gravity)), 1e-2f);
  72. return Vector3.up * (distanceY / flyTime + 0.5f * bullet_throw_gravity * flyTime) + dirX * bullet_throw_speed;
  73. }
  74. }