Human.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Human.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region File Information
  10. //-----------------------------------------------------------------------------
  11. // Human.cs
  12. //
  13. // Microsoft XNA Community Game Platform
  14. // Copyright (C) Microsoft Corporation. All rights reserved.
  15. //-----------------------------------------------------------------------------
  16. #endregion
  17. #region Using Statements
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using Microsoft.Xna.Framework;
  23. using Microsoft.Xna.Framework.Graphics;
  24. using Microsoft.Xna.Framework.Input.Touch;
  25. using GameStateManagement;
  26. #endregion
  27. namespace CatapultGame
  28. {
  29. enum PlayerSide
  30. {
  31. Left,
  32. Right
  33. }
  34. class Human : Player
  35. {
  36. #region Fields/Constants
  37. // Drag variables to hold first and last gesture samples
  38. GestureSample? prevSample;
  39. GestureSample? firstSample;
  40. public bool IsAI { get; protected set; }
  41. public bool isDragging { get; set; }
  42. // Constant for longest distance possible between drag points
  43. readonly float maxDragDelta = (new Vector2 (480, 800)).Length ();
  44. // Textures & position & spriteEffects used for Catapult
  45. Texture2D arrow;
  46. float arrowScale;
  47. Vector2 catapultPosition;
  48. PlayerSide playerSide;
  49. SpriteEffects spriteEffect = SpriteEffects.None;
  50. #endregion
  51. #region Initialization
  52. public Human (Game game) : base(game)
  53. {
  54. }
  55. public Human (Game game, SpriteBatch screenSpriteBatch, PlayerSide playerSide) : base(game, screenSpriteBatch)
  56. {
  57. string idleTextureName = "";
  58. this.playerSide = playerSide;
  59. if (playerSide == PlayerSide.Left) {
  60. catapultPosition = new Vector2 (140, 332);
  61. idleTextureName = "Textures/Catapults/Blue/blueIdle/blueIdle";
  62. } else {
  63. catapultPosition = new Vector2 (600, 332);
  64. spriteEffect = SpriteEffects.FlipHorizontally;
  65. idleTextureName = "Textures/Catapults/Red/redIdle/redIdle";
  66. }
  67. Catapult = new Catapult (game, screenSpriteBatch,
  68. idleTextureName, catapultPosition, spriteEffect,
  69. playerSide == PlayerSide.Left ? false : true);
  70. }
  71. public override void Initialize ()
  72. {
  73. arrow = curGame.Content.Load<Texture2D> ("Textures/HUD/Arrow");
  74. Catapult.Initialize ();
  75. base.Initialize ();
  76. }
  77. #endregion
  78. #region Handle Input
  79. /// <summary>
  80. /// Function processes the user input
  81. /// </summary>
  82. /// <param name="gestureSample"></param>
  83. public void HandleInput (GestureSample gestureSample)
  84. {
  85. // Process input only if in Human's turn
  86. if (IsActive) {
  87. // Process any Drag gesture
  88. if (gestureSample.GestureType == GestureType.FreeDrag) {
  89. // If drag just began save the sample for future
  90. // calculations and start Aim "animation"
  91. if (null == firstSample) {
  92. firstSample = gestureSample;
  93. Catapult.CurrentState = CatapultState.Aiming;
  94. }
  95. // save the current gesture sample
  96. prevSample = gestureSample;
  97. // calculate the delta between first sample and current
  98. // sample to present visual sound on screen
  99. Vector2 delta = prevSample.Value.Position - firstSample.Value.Position;
  100. Catapult.ShotStrength = delta.Length () / maxDragDelta;
  101. float baseScale = 0.001f;
  102. arrowScale = baseScale * delta.Length ();
  103. isDragging = true;
  104. } else if (gestureSample.GestureType == GestureType.DragComplete) {
  105. // calc velocity based on delta between first and last
  106. // gesture samples
  107. if (null != firstSample) {
  108. Vector2 delta = prevSample.Value.Position - firstSample.Value.Position;
  109. Catapult.ShotVelocity = MinShotStrength + Catapult.ShotStrength *
  110. (MaxShotStrength - MinShotStrength);
  111. Catapult.Fire (Catapult.ShotVelocity);
  112. Catapult.CurrentState = CatapultState.Firing;
  113. }
  114. // turn off dragging state
  115. ResetDragState ();
  116. }
  117. }
  118. }
  119. Vector2? firstMouseSample = null;
  120. Vector2? prevMouseSample = null;
  121. public void HandleInput (InputState input)
  122. {
  123. // Process input only if in Human's turn
  124. if (IsActive && !IsAI) {
  125. if (input.MouseGesture.HasFlag (MouseGestureType.FreeDrag)) {
  126. // If drag just began save the sample for future
  127. // calculations and start Aim "animation"
  128. if (null == firstMouseSample) {
  129. firstMouseSample = input.MouseDragStartPosition;
  130. Catapult.CurrentState = CatapultState.Aiming;
  131. }
  132. // save the current gesture sample
  133. prevMouseSample = input.CurrentMousePosition;
  134. // calculate the delta between first sample and current
  135. // sample to present visual sound on screen
  136. Vector2 delta = (Vector2)prevMouseSample - (Vector2)firstMouseSample;
  137. Catapult.ShotStrength = delta.Length () / maxDragDelta;
  138. float baseScale = 0.001f;
  139. arrowScale = baseScale * delta.Length ();
  140. isDragging = true;
  141. } else if (input.MouseGesture.HasFlag (MouseGestureType.DragComplete)) {
  142. // calc velocity based on delta between first and last
  143. // gesture samples
  144. if (null != firstMouseSample) {
  145. Vector2 delta = (Vector2)prevMouseSample - (Vector2)firstMouseSample;
  146. Catapult.ShotVelocity = MinShotStrength + Catapult.ShotStrength *
  147. (MaxShotStrength - MinShotStrength);
  148. Catapult.Fire (Catapult.ShotVelocity);
  149. Catapult.CurrentState = CatapultState.Firing;
  150. }
  151. ResetDragState ();
  152. }
  153. }
  154. }
  155. #endregion
  156. #region Draw
  157. public override void Draw (GameTime gameTime)
  158. {
  159. if (isDragging)
  160. DrawDragArrow (arrowScale);
  161. base.Draw (gameTime);
  162. }
  163. public void DrawDragArrow (float arrowScale)
  164. {
  165. if (playerSide == PlayerSide.Left) {
  166. spriteBatch.Draw (arrow, catapultPosition + new Vector2 (0, -40),
  167. null, Color.Blue, 0,
  168. Vector2.Zero, new Vector2 (arrowScale, 0.1f), spriteEffect, 0);
  169. } else {
  170. spriteBatch.Draw (arrow, catapultPosition + new Vector2 (-arrow.Width * arrowScale + 40, -40),
  171. null, Color.Red, 0,
  172. Vector2.Zero, new Vector2 (arrowScale, 0.1f), spriteEffect, 0);
  173. }
  174. }
  175. #endregion
  176. // used to set the arrow scale from networking
  177. internal float ArrowScale
  178. {
  179. get { return arrowScale; }
  180. set { arrowScale = value; }
  181. }
  182. /// <summary>
  183. /// /// /// Turn off dragging state and reset drag related variables
  184. /// </summary>
  185. public void ResetDragState ()
  186. {
  187. firstSample = null;
  188. prevSample = null;
  189. firstMouseSample = null;
  190. prevMouseSample = null;
  191. isDragging = false;
  192. arrowScale = 0;
  193. Catapult.ShotStrength = 0;
  194. }
  195. }
  196. }