FPS_Camera.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3. using OpenVIII.Encoding.Tags;
  4. using System;
  5. namespace OpenVIII
  6. {
  7. public class FPS_Camera
  8. {
  9. #region Fields
  10. public static readonly float camDistance = 10.0f;
  11. public static readonly float defaultmaxMoveSpeed = 1f;
  12. public static readonly float maxLookSpeedMouse = 0.25f;
  13. public static readonly float maxLookSpeedGamePad = 0.15f;
  14. public static readonly float MoveSpeedChange = 1f;
  15. //private static Vector3 camPosition, camTarget;
  16. //private float degrees = 90;
  17. private float Yshift;
  18. private Vector2 left;
  19. private static float maxMoveSpeed = defaultmaxMoveSpeed;
  20. private Vector2 shift;
  21. private Vector2 leftdist;
  22. public static float MaxMoveSpeed { get => maxMoveSpeed; set => maxMoveSpeed = value; }
  23. #endregion Fields
  24. #region Methods
  25. private void Inputs_Speed()
  26. {
  27. //speedcontrols
  28. //+ to increase
  29. //- to decrease
  30. //* to reset
  31. if (Input2.Button(Keys.OemPlus) || Input2.Button(Keys.Add))
  32. {
  33. MaxMoveSpeed += MoveSpeedChange;
  34. }
  35. if (Input2.Button(Keys.OemMinus) || Input2.Button(Keys.Subtract))
  36. {
  37. MaxMoveSpeed -= MoveSpeedChange;
  38. if (MaxMoveSpeed < defaultmaxMoveSpeed) MaxMoveSpeed = defaultmaxMoveSpeed;
  39. }
  40. if (Input2.Button(Keys.Multiply)) MaxMoveSpeed = defaultmaxMoveSpeed;
  41. //speed is effected by the milliseconds between frames. so alittle goes a long way. :P
  42. }
  43. private void Inputs_Sticks(ref float degrees)
  44. {
  45. //require mouselock to center of screen for mouse joystick mode.
  46. InputMouse.Mode = MouseLockMode.Center;
  47. Memory.IsMouseVisible = false;
  48. // check mouse to move camera
  49. shift = InputMouse.Distance(MouseButtons.MouseToStick, maxLookSpeedMouse);
  50. // check right stick to adjust camera
  51. shift += InputGamePad.Distance(GamePadButtons.ThumbSticks_Right, maxLookSpeedGamePad);
  52. //convert stick readings to degrees
  53. degrees = (degrees + shift.X);
  54. degrees %= 360f;
  55. if (degrees < 0)
  56. degrees += 360f;
  57. Yshift -= shift.Y;
  58. Yshift = MathHelper.Clamp(Yshift, -80f, 80f);
  59. // grab left stick reading for moving camera position
  60. // storing signed value to detect direction of movement for left stick.
  61. left = InputGamePad.Distance(GamePadButtons.ThumbSticks_Left, MaxMoveSpeed);
  62. // convert to positive value to get distance traveled
  63. leftdist = left.Abs();
  64. if (leftdist == Vector2.Zero)
  65. {
  66. leftdist.Y = leftdist.X = (float)Input2.Distance(MaxMoveSpeed);
  67. }
  68. }
  69. private void Inputs_D_Pad( ref Vector3 camPosition, ref float degrees)
  70. {
  71. // using the calcuated direction and distance to move camera position
  72. // also fall back to arrow keys to move when not using a left stick
  73. if (Input2.Button(FF8TextTagKey.Up) || left.Y > 0)
  74. {
  75. camPosition.X += (float)Math.Cos(MathHelper.ToRadians(degrees)) * leftdist.Y / 10;
  76. camPosition.Z += (float)Math.Sin(MathHelper.ToRadians(degrees)) * leftdist.Y / 10;
  77. camPosition.Y -= Yshift / 50;
  78. }
  79. if (Input2.Button(FF8TextTagKey.Down) || left.Y < 0)
  80. {
  81. camPosition.X -= (float)Math.Cos(MathHelper.ToRadians(degrees)) * leftdist.Y / 10;
  82. camPosition.Z -= (float)Math.Sin(MathHelper.ToRadians(degrees)) * leftdist.Y / 10;
  83. camPosition.Y += Yshift / 50;
  84. }
  85. if (Input2.Button(FF8TextTagKey.Left) || left.X < 0)
  86. {
  87. camPosition.X += (float)Math.Cos(MathHelper.ToRadians(degrees - 90)) * leftdist.X / 10;
  88. camPosition.Z += (float)Math.Sin(MathHelper.ToRadians(degrees - 90)) * leftdist.X / 10;
  89. }
  90. if (Input2.Button(FF8TextTagKey.Right) || left.X > 0)
  91. {
  92. camPosition.X += (float)Math.Cos(MathHelper.ToRadians(degrees + 90)) * leftdist.X / 10;
  93. camPosition.Z += (float)Math.Sin(MathHelper.ToRadians(degrees + 90)) * leftdist.X / 10;
  94. }
  95. }
  96. public Matrix Update(ref Vector3 camPosition,ref Vector3 camTarget, ref float degrees)
  97. {
  98. if (Memory.IsActive)
  99. {
  100. InputMouse.Mode = MouseLockMode.Center;
  101. Inputs_Speed();
  102. Inputs_Sticks(ref degrees);
  103. Inputs_D_Pad(ref camPosition, ref degrees);
  104. // adjust the camera target
  105. camTarget.X = camPosition.X + (float)Math.Cos(MathHelper.ToRadians(degrees)) * camDistance;
  106. camTarget.Z = camPosition.Z + (float)Math.Sin(MathHelper.ToRadians(degrees)) * camDistance;
  107. camTarget.Y = camPosition.Y - Yshift / 5;
  108. // return the matrix of camera posistion and camera target to adjust the camera.
  109. }
  110. return Matrix.CreateLookAt(camPosition, camTarget,
  111. Vector3.Up);
  112. }
  113. #endregion Methods
  114. }
  115. }