Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Windows.Forms;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
  7. using Keys = Microsoft.Xna.Framework.Input.Keys;
  8.  
  9. namespace ParticleSystem
  10. {
  11.     /// <summary>
  12.     /// This is the main type for your game.
  13.     /// </summary>
  14.     public class Game1 : Game
  15.     {
  16.         private readonly GraphicsDeviceManager graphics;
  17.         private SpriteBatch spriteBatch;
  18.  
  19.         private const int particleCount = 10000;
  20.         private const float particleSpeed = 1f;
  21.  
  22.         private Texture2D particleTexture;
  23.         private readonly List<Particle> particles = new List<Particle>();
  24.  
  25.         public Game1()
  26.         {
  27.             graphics = new GraphicsDeviceManager( this )
  28.             {
  29.                 IsFullScreen = true,
  30.                 PreferredBackBufferWidth = Screen.PrimaryScreen.Bounds.Width,
  31.                 PreferredBackBufferHeight = Screen.PrimaryScreen.Bounds.Height
  32.             };
  33.             graphics.ApplyChanges();
  34.             Content.RootDirectory = "Content";
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Allows the game to perform any initialization it needs to before starting to run.
  39.         /// This is where it can query for any required services and load any non-graphic
  40.         /// related content.  Calling base.Initialize will enumerate through any components
  41.         /// and initialize them as well.
  42.         /// </summary>
  43.         protected override void Initialize()
  44.         {
  45.             // TODO: Add your initialization logic here
  46.             IsMouseVisible = true;
  47.  
  48.             base.Initialize();
  49.         }
  50.  
  51.         /// <summary>
  52.         /// LoadContent will be called once per game and is the place to load
  53.         /// all of your content.
  54.         /// </summary>
  55.         protected override void LoadContent()
  56.         {
  57.             // Create a new SpriteBatch, which can be used to draw textures.
  58.             spriteBatch = new SpriteBatch( GraphicsDevice );
  59.  
  60.             // TODO: use this.Content to load your game content here
  61.             particleTexture = Content.Load<Texture2D>( "Particle" );
  62.  
  63.             for ( int i = 0; i < particleCount; i++ )
  64.             {
  65.                 particles.Add( new Particle( graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height,
  66.                                              particleTexture, Color.White ) );
  67.             }
  68.         }
  69.  
  70.         /// <summary>
  71.         /// UnloadContent will be called once per game and is the place to unload
  72.         /// game-specific content.
  73.         /// </summary>
  74.         protected override void UnloadContent()
  75.         {
  76.             // TODO: Unload any non ContentManager content here
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Allows the game to run logic such as updating the world,
  81.         /// checking for collisions, gathering input, and playing audio.
  82.         /// </summary>
  83.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  84.         protected override void Update( GameTime gameTime )
  85.         {
  86.             if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown( Keys.Escape ) )
  87.             {
  88.                 Exit();
  89.             }
  90.  
  91.             // TODO: Add your update logic here
  92.             if ( Mouse.GetState().LeftButton == ButtonState.Pressed )
  93.             {
  94.                 UpdateParticlesPosition( gameTime, Mouse.GetState().Position.ToVector2() );
  95.             }
  96.  
  97.             base.Update( gameTime );
  98.         }
  99.  
  100.         /// <summary>
  101.         /// This is called when the game should draw itself.
  102.         /// </summary>
  103.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  104.         protected override void Draw( GameTime gameTime )
  105.         {
  106.             GraphicsDevice.Clear( Color.CornflowerBlue );
  107.  
  108.             // TODO: Add your drawing code here
  109.             spriteBatch.Begin();
  110.  
  111.             DrawParticles();
  112.  
  113.             spriteBatch.End();
  114.  
  115.             base.Draw( gameTime );
  116.         }
  117.  
  118.         private void DrawParticles()
  119.         {
  120.             for ( int i = 0; i < particleCount; i++ )
  121.             {
  122.                 Particle particle = particles[ i ];
  123.  
  124.                 spriteBatch.Draw( particle.Texture, particle.Position, scale: particle.Scale, color: particle.Color );
  125.             }
  126.         }
  127.  
  128.         private void UpdateParticlesPosition( GameTime gameTime, Vector2 target )
  129.         {
  130.             for ( int i = 0; i < particleCount; i++ )
  131.             {
  132.                 Particle particle = particles[ i ];
  133.  
  134.                 Vector2 direction = target - particle.Position;
  135.                 Vector2 normalizedDirection = new Vector2( direction.X, direction.Y );
  136.                 normalizedDirection.Normalize();
  137.                 Vector2 movement = Vector2.Multiply( normalizedDirection, particleSpeed * ( float ) gameTime.ElapsedGameTime.TotalMilliseconds );
  138.  
  139.                 if ( movement.Length() > direction.Length() )
  140.                 {
  141.                     particle.Position = target;
  142.                 }
  143.                 else
  144.                 {
  145.                     particle.Position += movement;
  146.                 }
  147.             }
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement