ShipInput.cs 5.9 KB

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