2
0

Player.cs 5.1 KB

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