2
0

ShatterEffectGame.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //-----------------------------------------------------------------------------
  2. // ShatterEffectGame.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.Content;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. namespace ShatterEffect
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class ShatterEffectGame : Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. Vector3 lightPosition = Vector3.UnitY;
  21. Vector4 ambientColor = Color.DarkGray.ToVector4();
  22. Vector4 diffuseColor = Color.White.ToVector4();
  23. Vector4 specularColor = Color.White.ToVector4();
  24. float specularPower = 50;
  25. float time;
  26. const float translationRate = 50;
  27. const float rotationRate = MathHelper.Pi * 3;
  28. const float duration = 2.0f;
  29. Model model;
  30. SpriteFont font;
  31. SpriteBatch spriteBatch;
  32. Matrix view;
  33. Matrix projection;
  34. Vector3 cameraPosition = new Vector3(-696, 429, 835);
  35. Vector3 targetPosition = new Vector3(0, 60, 0);
  36. int shatterEffectIndex = 0; // 0 for default shatter, 1 for explosion
  37. bool autoShatter = false;
  38. bool autoShatterReversing = false;
  39. float autoShatterSpeed = 0.5f; // Slow-motion multiplier for auto-shatter
  40. float autoShatterPauseDuration = 1.0f; // Pause duration in seconds before changing direction
  41. float autoShatterPauseTimer = 0.0f; // Timer to track pause duration
  42. KeyboardState previousKeyboardState;
  43. public ShatterEffectGame()
  44. {
  45. graphics = new GraphicsDeviceManager(this);
  46. Content.RootDirectory = "Content";
  47. }
  48. /// <summary>
  49. /// Load your graphics content.
  50. /// </summary>
  51. protected override void LoadContent()
  52. {
  53. spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
  54. font = Content.Load<SpriteFont>("font");
  55. model = Content.Load<Model>("model/ship1");
  56. // Calculate View/Projection Matrices.
  57. view = Matrix.CreateLookAt(cameraPosition, targetPosition, Vector3.Up);
  58. projection = Matrix.CreatePerspectiveFieldOfView(
  59. MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio, 1.0f, 10000.0f);
  60. }
  61. /// <summary>
  62. /// Allows the game to run logic such as updating the world,
  63. /// checking for collisions, gathering input and playing audio.
  64. /// </summary>
  65. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  66. protected override void Update(GameTime gameTime)
  67. {
  68. // Handle input
  69. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  70. KeyboardState keyboardState = Keyboard.GetState();
  71. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  72. // Toggle between shatter effects with Tab (on key release)
  73. if (keyboardState.IsKeyUp(Keys.Tab) && previousKeyboardState.IsKeyDown(Keys.Tab))
  74. {
  75. shatterEffectIndex = (shatterEffectIndex + 1) % 2; // Toggle between 0 and 1
  76. }
  77. // Toggle auto-shatter with Enter (on key release)
  78. if (keyboardState.IsKeyUp(Keys.Enter) && previousKeyboardState.IsKeyDown(Keys.Enter))
  79. {
  80. autoShatter = !autoShatter;
  81. autoShatterReversing = false; // Reset reversing state
  82. }
  83. if (autoShatter)
  84. {
  85. float adjustedElapsedTime = elapsedTime * autoShatterSpeed; // Apply slow-motion multiplier
  86. if (autoShatterPauseTimer > 0.0f)
  87. {
  88. autoShatterPauseTimer -= elapsedTime; // Decrease pause timer
  89. }
  90. else if (!autoShatterReversing)
  91. {
  92. time += adjustedElapsedTime;
  93. if (time >= duration)
  94. {
  95. time = duration;
  96. autoShatterReversing = true;
  97. autoShatterPauseTimer = autoShatterPauseDuration; // Start pause timer
  98. }
  99. }
  100. else
  101. {
  102. time -= adjustedElapsedTime;
  103. if (time <= 0.0f)
  104. {
  105. time = 0.0f;
  106. autoShatterReversing = false;
  107. autoShatterPauseTimer = autoShatterPauseDuration; // Start pause timer
  108. }
  109. }
  110. }
  111. else
  112. {
  113. // Manual control for shatter effects
  114. if (keyboardState.IsKeyDown(Keys.Up) || gamePadState.Buttons.A == ButtonState.Pressed)
  115. {
  116. time = Math.Min(duration, time + elapsedTime);
  117. }
  118. if (keyboardState.IsKeyDown(Keys.Down) || gamePadState.Buttons.B == ButtonState.Pressed)
  119. {
  120. time = Math.Max(0.0f, time - elapsedTime);
  121. }
  122. }
  123. previousKeyboardState = keyboardState; // Update previous keyboard state
  124. base.Update(gameTime);
  125. }
  126. /// <summary>
  127. /// This is called when the game should draw itself.
  128. /// </summary>
  129. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  130. protected override void Draw(GameTime gameTime)
  131. {
  132. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  133. GraphicsDevice.BlendState = BlendState.Opaque;
  134. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  135. GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  136. Matrix[] transforms = new Matrix[model.Bones.Count];
  137. model.CopyAbsoluteBoneTransformsTo(transforms);
  138. foreach (ModelMesh mesh in model.Meshes)
  139. {
  140. foreach (ModelMeshPart part in mesh.MeshParts)
  141. {
  142. SetupEffect(transforms, mesh, part);
  143. }
  144. mesh.Draw();
  145. }
  146. // Draw the UI
  147. spriteBatch.Begin();
  148. spriteBatch.DrawString(font, "Shatter Effect Demo", new Vector2(10, 10), Color.White);
  149. spriteBatch.DrawString(font, "Press Tab to toggle shatter effects", new Vector2(10, 30), Color.White);
  150. spriteBatch.DrawString(font, "Press Enter to toggle auto-shatter", new Vector2(10, 50), Color.White);
  151. spriteBatch.DrawString(font, "Use Up/Down arrows or GamePad A/B to control shatter amount", new Vector2(10, 70), Color.White);
  152. spriteBatch.End();
  153. base.Draw(gameTime);
  154. }
  155. void SetupEffect(Matrix[] transforms, ModelMesh mesh, ModelMeshPart part)
  156. {
  157. Effect effect = part.Effect;
  158. if (shatterEffectIndex == 0) // Default shatter effect
  159. {
  160. effect.Parameters["TranslationAmount"].SetValue(translationRate * time);
  161. effect.Parameters["RotationAmount"].SetValue(rotationRate * time);
  162. }
  163. else if (shatterEffectIndex == 1) // Explosion effect
  164. {
  165. effect.Parameters["TranslationAmount"].SetValue(translationRate * time * 2); // Expand outward
  166. effect.Parameters["RotationAmount"].SetValue(0); // No rotation for explosion
  167. }
  168. effect.Parameters["time"].SetValue(time);
  169. effect.Parameters["WorldViewProjection"].SetValue(
  170. transforms[mesh.ParentBone.Index] * view * projection);
  171. effect.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index]);
  172. effect.Parameters["eyePosition"].SetValue(cameraPosition);
  173. effect.Parameters["lightPosition"].SetValue(lightPosition);
  174. effect.Parameters["ambientColor"].SetValue(ambientColor);
  175. effect.Parameters["diffuseColor"].SetValue(diffuseColor);
  176. effect.Parameters["specularColor"].SetValue(specularColor);
  177. effect.Parameters["specularPower"].SetValue(specularPower);
  178. }
  179. }
  180. }