AimingGame.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-----------------------------------------------------------------------------
  2. // AimingGame.cs (moved to Core)
  3. // Microsoft XNA Community Game Platform
  4. // Copyright (C) Microsoft Corporation. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Content;
  12. namespace Aiming
  13. {
  14. /// <summary>
  15. /// This sample shows how to aim one object towards another. In this sample, a
  16. /// spotlight turns to aim towards a cat that the player controls.
  17. /// </summary>
  18. public class AimingGame : Game
  19. {
  20. // how fast can the cat move? this is in terms of pixels per frame.
  21. const float CatSpeed = 10.0f;
  22. // how fast can the spot light turn? this is in terms of radians per frame.
  23. const float SpotlightTurnSpeed = 0.025f;
  24. GraphicsDeviceManager graphics;
  25. SpriteBatch spriteBatch;
  26. Texture2D spotlightTexture;
  27. Vector2 spotlightPosition = new Vector2();
  28. Vector2 spotlightOrigin = new Vector2();
  29. float spotlightAngle = 0.0f;
  30. Texture2D catTexture;
  31. Vector2 catPosition = new Vector2();
  32. Vector2 catOrigin = new Vector2();
  33. public AimingGame()
  34. {
  35. graphics = new GraphicsDeviceManager(this);
  36. Content.RootDirectory = "Content";
  37. graphics.PreferredBackBufferWidth = 480;
  38. graphics.PreferredBackBufferHeight = 640;
  39. #if MOBILE
  40. graphics.IsFullScreen = true;
  41. #endif
  42. }
  43. protected override void Initialize()
  44. {
  45. base.Initialize();
  46. Viewport vp = graphics.GraphicsDevice.Viewport;
  47. spotlightPosition.X = vp.X + vp.Width / 2;
  48. spotlightPosition.Y = vp.Y + vp.Height / 2;
  49. catPosition.X = vp.X + vp.Width / 4;
  50. catPosition.Y = vp.Y + vp.Height / 2;
  51. }
  52. protected override void LoadContent()
  53. {
  54. spriteBatch = new SpriteBatch(GraphicsDevice);
  55. spotlightTexture = Content.Load<Texture2D>("spotlight");
  56. spotlightOrigin.X = spotlightTexture.Width / 2;
  57. spotlightOrigin.Y = spotlightTexture.Height / 2;
  58. catTexture = Content.Load<Texture2D>("cat");
  59. catOrigin.X = catTexture.Width / 2;
  60. catOrigin.Y = catTexture.Height / 2;
  61. }
  62. protected override void Update(GameTime gameTime)
  63. {
  64. // Allows the game to exit
  65. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
  66. Keyboard.GetState().IsKeyDown(Keys.Escape))
  67. Exit();
  68. // Move the cat with arrow keys or touch
  69. Vector2 move = Vector2.Zero;
  70. KeyboardState keyState = Keyboard.GetState();
  71. if (keyState.IsKeyDown(Keys.Left)) move.X -= 1;
  72. if (keyState.IsKeyDown(Keys.Right)) move.X += 1;
  73. if (keyState.IsKeyDown(Keys.Up)) move.Y -= 1;
  74. if (keyState.IsKeyDown(Keys.Down)) move.Y += 1;
  75. if (move.Length() > 1)
  76. move.Normalize();
  77. catPosition += move * CatSpeed;
  78. // Touch input (for mobile)
  79. TouchCollection touches = TouchPanel.GetState();
  80. if (touches.Count > 0)
  81. {
  82. Vector2 touch = touches[0].Position;
  83. Vector2 diff = touch - catPosition;
  84. if (diff.Length() > CatSpeed)
  85. {
  86. diff.Normalize();
  87. catPosition += diff * CatSpeed;
  88. }
  89. else
  90. {
  91. catPosition = touch;
  92. }
  93. }
  94. // Aim the spotlight at the cat
  95. Vector2 toCat = catPosition - spotlightPosition;
  96. float desiredAngle = (float)Math.Atan2(toCat.Y, toCat.X);
  97. float delta = MathHelper.WrapAngle(desiredAngle - spotlightAngle);
  98. if (Math.Abs(delta) < SpotlightTurnSpeed)
  99. spotlightAngle = desiredAngle;
  100. else
  101. spotlightAngle += Math.Sign(delta) * SpotlightTurnSpeed;
  102. base.Update(gameTime);
  103. }
  104. protected override void Draw(GameTime gameTime)
  105. {
  106. GraphicsDevice.Clear(Color.CornflowerBlue);
  107. spriteBatch.Begin();
  108. spriteBatch.Draw(spotlightTexture, spotlightPosition, null, Color.White, spotlightAngle, spotlightOrigin, 1.0f, SpriteEffects.None, 0f);
  109. spriteBatch.Draw(catTexture, catPosition, null, Color.White, 0f, catOrigin, 1.0f, SpriteEffects.None, 0f);
  110. spriteBatch.End();
  111. base.Draw(gameTime);
  112. }
  113. }
  114. }