Player.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. namespace RockRain
  6. {
  7. /// <summary>
  8. /// This is a game component that implements the player ship.
  9. /// </summary>
  10. public class Player : DrawableGameComponent
  11. {
  12. protected Texture2D texture;
  13. protected Rectangle spriteRectangle;
  14. protected Vector2 position;
  15. protected TimeSpan elapsedTime = TimeSpan.Zero;
  16. // Screen Area
  17. protected Rectangle screenBounds;
  18. // Game Stuff
  19. protected int score;
  20. protected int power;
  21. private const int INITIALPOWER = 100;
  22. public Player(Game game, ref Texture2D theTexture) : base(game)
  23. {
  24. texture = theTexture;
  25. position = new Vector2();
  26. // Create the source rectangle.
  27. // This represents where is the sprite picture in surface
  28. spriteRectangle = new Rectangle(86,11,24,22);
  29. screenBounds = new Rectangle(0, 0, Game.Window.ClientBounds.Width,
  30. Game.Window.ClientBounds.Height);
  31. }
  32. /// <summary>
  33. /// Put the ship in your start position in screen
  34. /// </summary>
  35. public void Reset()
  36. {
  37. position.X = screenBounds.Width/3;
  38. position.Y = screenBounds.Height - spriteRectangle.Height;
  39. score = 0;
  40. power = INITIALPOWER;
  41. }
  42. /// <summary>
  43. /// Total Points of the Player
  44. /// </summary>
  45. public int Score
  46. {
  47. get { return score; }
  48. set
  49. {
  50. if (value < 0)
  51. {
  52. score = 0;
  53. }
  54. else
  55. {
  56. score = value;
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Remaining Power
  62. /// </summary>
  63. public int Power
  64. {
  65. get { return power; }
  66. set { power = value; }
  67. }
  68. /// <summary>
  69. /// Update the ship position, points and power
  70. /// </summary>
  71. public override void Update(GameTime gameTime)
  72. {
  73. HandleInput();
  74. UpdateShip(gameTime);
  75. base.Update(gameTime);
  76. }
  77. /// <summary>
  78. /// Get the ship position
  79. /// </summary>
  80. protected void HandleInput()
  81. {
  82. GamePadState gamepadstatus = GamePad.GetState(PlayerIndex.One);
  83. // Check the thumbstick
  84. position.Y += (int)(gamepadstatus.ThumbSticks.Left.Y * -4);
  85. position.X += (int)(gamepadstatus.ThumbSticks.Left.X * 4);
  86. // Check the accelerometer - not available in MonoGame 3.8.4
  87. // position.Y += (int)(Accelerometer.GetState().Acceleration.Y * -4);
  88. // position.X += (int)(Accelerometer.GetState().Acceleration.X * 4);
  89. }
  90. /// <summary>
  91. /// Update ship status
  92. /// </summary>
  93. private void UpdateShip(GameTime gameTime)
  94. {
  95. // Keep the player inside the screen
  96. KeepInBound();
  97. // Update score
  98. elapsedTime += gameTime.ElapsedGameTime;
  99. if (elapsedTime > TimeSpan.FromSeconds(1))
  100. {
  101. elapsedTime -= TimeSpan.FromSeconds(1);
  102. score++;
  103. power--;
  104. }
  105. }
  106. /// <summary>
  107. /// Keep the ship inside the screen
  108. /// </summary>
  109. private void KeepInBound()
  110. {
  111. if (position.X < screenBounds.Left)
  112. {
  113. position.X = screenBounds.Left;
  114. }
  115. if (position.X > screenBounds.Width - spriteRectangle.Width)
  116. {
  117. position.X = screenBounds.Width - spriteRectangle.Width;
  118. }
  119. if (position.Y < screenBounds.Top)
  120. {
  121. position.Y = screenBounds.Top;
  122. }
  123. if (position.Y > screenBounds.Height - spriteRectangle.Height)
  124. {
  125. position.Y = screenBounds.Height - spriteRectangle.Height;
  126. }
  127. }
  128. /// <summary>
  129. /// Draw the ship sprite
  130. /// </summary>
  131. public override void Draw(GameTime gameTime)
  132. {
  133. // Get the current spritebatch
  134. SpriteBatch sBatch = (SpriteBatch)
  135. Game.Services.GetService(typeof (SpriteBatch));
  136. // Draw the ship
  137. sBatch.Draw(texture, position, spriteRectangle, Color.White);
  138. base.Draw(gameTime);
  139. }
  140. /// <summary>
  141. /// Get the bound rectangle of ship position in screen
  142. /// </summary>
  143. public Rectangle GetBounds()
  144. {
  145. return new Rectangle((int) position.X, (int) position.Y,
  146. spriteRectangle.Width, spriteRectangle.Height);
  147. }
  148. public Vector2 Position
  149. {
  150. get
  151. {
  152. return position;
  153. }
  154. }
  155. }
  156. }