Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Windows.Forms;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
- using Keys = Microsoft.Xna.Framework.Input.Keys;
- namespace ParticleSystem
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- private readonly GraphicsDeviceManager graphics;
- private SpriteBatch spriteBatch;
- private const int particleCount = 10000;
- private const float particleSpeed = 1f;
- private Texture2D particleTexture;
- private readonly List<Particle> particles = new List<Particle>();
- public Game1()
- {
- graphics = new GraphicsDeviceManager( this )
- {
- IsFullScreen = true,
- PreferredBackBufferWidth = Screen.PrimaryScreen.Bounds.Width,
- PreferredBackBufferHeight = Screen.PrimaryScreen.Bounds.Height
- };
- graphics.ApplyChanges();
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- IsMouseVisible = true;
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch( GraphicsDevice );
- // TODO: use this.Content to load your game content here
- particleTexture = Content.Load<Texture2D>( "Particle" );
- for ( int i = 0; i < particleCount; i++ )
- {
- particles.Add( new Particle( graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height,
- particleTexture, Color.White ) );
- }
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update( GameTime gameTime )
- {
- if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown( Keys.Escape ) )
- {
- Exit();
- }
- // TODO: Add your update logic here
- if ( Mouse.GetState().LeftButton == ButtonState.Pressed )
- {
- UpdateParticlesPosition( gameTime, Mouse.GetState().Position.ToVector2() );
- }
- base.Update( gameTime );
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw( GameTime gameTime )
- {
- GraphicsDevice.Clear( Color.CornflowerBlue );
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- DrawParticles();
- spriteBatch.End();
- base.Draw( gameTime );
- }
- private void DrawParticles()
- {
- for ( int i = 0; i < particleCount; i++ )
- {
- Particle particle = particles[ i ];
- spriteBatch.Draw( particle.Texture, particle.Position, scale: particle.Scale, color: particle.Color );
- }
- }
- private void UpdateParticlesPosition( GameTime gameTime, Vector2 target )
- {
- for ( int i = 0; i < particleCount; i++ )
- {
- Particle particle = particles[ i ];
- Vector2 direction = target - particle.Position;
- Vector2 normalizedDirection = new Vector2( direction.X, direction.Y );
- normalizedDirection.Normalize();
- Vector2 movement = Vector2.Multiply( normalizedDirection, particleSpeed * ( float ) gameTime.ElapsedGameTime.TotalMilliseconds );
- if ( movement.Length() > direction.Length() )
- {
- particle.Position = target;
- }
- else
- {
- particle.Position += movement;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement