ShipInput.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ShipInput.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Input;
  13. using Microsoft.Xna.Framework.Net;
  14. #endregion
  15. namespace NetRumble
  16. {
  17. /// <summary>
  18. /// Player input abstraction for ships.
  19. /// </summary>
  20. public struct ShipInput
  21. {
  22. #region Static Constants
  23. /// <summary>
  24. /// The empty version of this structure.
  25. /// </summary>
  26. private static ShipInput empty =
  27. new ShipInput(Vector2.Zero, Vector2.Zero, false);
  28. public static ShipInput Empty
  29. {
  30. get { return empty; }
  31. }
  32. #endregion
  33. #region Input Data
  34. /// <summary>
  35. /// The left-stick value of the ship input, used for movement.
  36. /// </summary>
  37. public Vector2 LeftStick;
  38. /// <summary>
  39. /// The right-stick value of the ship input, used for firing.
  40. /// </summary>
  41. public Vector2 RightStick;
  42. /// <summary>
  43. /// If true, the player is trying to fire a mine.
  44. /// </summary>
  45. public bool MineFired;
  46. #endregion
  47. #region Initialization Methods
  48. /// <summary>
  49. /// Constructs a new ShipInput object.
  50. /// </summary>
  51. /// <param name="leftStick">The left-stick value of the ship input.</param>
  52. /// <param name="rightStick">The right-stick value of the ship input.</param>
  53. /// <param name="mineFired">If true, the player is firing a mine.</param>
  54. public ShipInput(Vector2 leftStick, Vector2 rightStick, bool mineFired)
  55. {
  56. this.LeftStick = leftStick;
  57. this.RightStick = rightStick;
  58. this.MineFired = mineFired;
  59. }
  60. /// <summary>
  61. /// Create a new ShipInput object based on the data in the packet.
  62. /// </summary>
  63. /// <param name="packetReader">The packet with the data.</param>
  64. public ShipInput(PacketReader packetReader)
  65. {
  66. // safety-check the parameters, as they must be valid
  67. if (packetReader == null)
  68. {
  69. throw new ArgumentNullException("packetReader");
  70. }
  71. // read the data
  72. LeftStick = packetReader.ReadVector2();
  73. RightStick = packetReader.ReadVector2();
  74. MineFired = packetReader.ReadBoolean();
  75. }
  76. /// <summary>
  77. /// Create a new ShipInput object based on local input state.
  78. /// </summary>
  79. /// <param name="gamePadState">The local gamepad state.</param>
  80. /// <param name="keyboardState">The local keyboard state</param>
  81. public ShipInput(GamePadState gamePadState, KeyboardState keyboardState)
  82. {
  83. // check for movement action
  84. bool keyHit = false;
  85. LeftStick = Vector2.Zero;
  86. if (keyboardState.IsKeyDown(Keys.W))
  87. {
  88. LeftStick += Vector2.UnitY;
  89. keyHit = true;
  90. }
  91. if (keyboardState.IsKeyDown(Keys.A))
  92. {
  93. LeftStick += Vector2.Multiply(Vector2.UnitX, -1f);
  94. keyHit = true;
  95. }
  96. if (keyboardState.IsKeyDown(Keys.S))
  97. {
  98. LeftStick += Vector2.Multiply(Vector2.UnitY, -1f);
  99. keyHit = true;
  100. }
  101. if (keyboardState.IsKeyDown(Keys.D))
  102. {
  103. LeftStick += Vector2.UnitX;
  104. keyHit = true;
  105. }
  106. if (keyHit)
  107. {
  108. if (LeftStick.LengthSquared() > 0)
  109. {
  110. LeftStick.Normalize();
  111. }
  112. }
  113. else
  114. {
  115. LeftStick = gamePadState.ThumbSticks.Left;
  116. }
  117. // check for firing action
  118. keyHit = false;
  119. RightStick = Vector2.Zero;
  120. if (keyboardState.IsKeyDown(Keys.Up))
  121. {
  122. RightStick += Vector2.UnitY;
  123. keyHit = true;
  124. }
  125. if (keyboardState.IsKeyDown(Keys.Left))
  126. {
  127. RightStick += Vector2.Multiply(Vector2.UnitX, -1f);
  128. keyHit = true;
  129. }
  130. if (keyboardState.IsKeyDown(Keys.Down))
  131. {
  132. RightStick += Vector2.Multiply(Vector2.UnitY, -1f);
  133. keyHit = true;
  134. }
  135. if (keyboardState.IsKeyDown(Keys.Right))
  136. {
  137. RightStick += Vector2.UnitX;
  138. keyHit = true;
  139. }
  140. if (keyHit)
  141. {
  142. if (RightStick.LengthSquared() > 0)
  143. {
  144. RightStick.Normalize();
  145. }
  146. }
  147. else
  148. {
  149. RightStick = gamePadState.ThumbSticks.Right;
  150. }
  151. // check for firing a mine
  152. keyHit = false;
  153. MineFired = false;
  154. if (keyboardState.IsKeyDown(Keys.Space))
  155. {
  156. MineFired = true;
  157. keyHit = true;
  158. }
  159. if (!keyHit)
  160. {
  161. MineFired = gamePadState.Triggers.Right >= 0.9f;
  162. }
  163. }
  164. #endregion
  165. #region Networking Methods
  166. /// <summary>
  167. /// Serialize the object out to a packet.
  168. /// </summary>
  169. /// <param name="packetWriter">The packet to write to.</param>
  170. public void Serialize(PacketWriter packetWriter)
  171. {
  172. // safety-check the parameters, as they must be valid
  173. if (packetWriter == null)
  174. {
  175. throw new ArgumentNullException("packetWriter");
  176. }
  177. packetWriter.Write((int)World.PacketTypes.ShipInput);
  178. packetWriter.Write(LeftStick);
  179. packetWriter.Write(RightStick);
  180. packetWriter.Write(MineFired);
  181. }
  182. #endregion
  183. }
  184. }