Guest User

Untitled

a guest
Aug 2nd, 2012
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace Pong
  10. {
  11.     public class EmitterBase : DrawableGameComponent, IParticleEmitter
  12.     {
  13.         public SpriteBatch SpriteBatch
  14.         {
  15.             get
  16.             {
  17.                 return (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
  18.             }
  19.         }
  20.  
  21.         public ParticleEmitterService ParticleEmitter
  22.         {
  23.             get { return (ParticleEmitterService)Game.Services.GetService(typeof(ParticleEmitterService)); }
  24.         }
  25.  
  26.         public BlendState ParticleBlendState { get; set; }
  27.  
  28.         public IGameEntity GameEntity { get; set; }
  29.         public Vector2 EnityOffset { get; set; }
  30.  
  31.  
  32.         protected Matrix transform { get; set; }
  33.  
  34.         public Vector2 Position { get; set; }
  35.  
  36.         public Vector2 Origin { get; set; }
  37.  
  38.         public float Scale { get; set; }
  39.  
  40.         public float Rotation { get; set; }
  41.  
  42.         public Texture2D texture { get; set; }
  43.  
  44.         public Vector2[] particles { get; set; }
  45.         public float[] scaleParticle { get; set; }
  46.         public float[] rotationParticle { get; set; }
  47.         public bool[] activeParticle { get; set; }
  48.  
  49.         public Color Color { get; set; }
  50.  
  51.         public bool Alive { get; set; }
  52.  
  53.         protected int particleCount = 0;
  54.         public int ParticleCount { get { return particleCount; } }
  55.  
  56.         protected Random rnd = new Random(DateTime.Now.Millisecond);
  57.  
  58.         protected string textureAsset;
  59.  
  60.         public Rectangle bounds { get { return new Rectangle(); } }
  61.  
  62.         public EmitterBase(Game game, string TextureAsset)
  63.             : base(game)
  64.         {
  65.             textureAsset = TextureAsset;
  66.  
  67.             Position = Vector2.Zero;
  68.             Origin = Vector2.Zero;
  69.             Scale = 1f;
  70.             Rotation = 0;
  71.  
  72.             ParticleBlendState = BlendState.Additive;
  73.  
  74.             Color = Color.White;
  75.         }
  76.  
  77.         public override void Update(GameTime gameTime)
  78.         {
  79.             if (GameEntity != null)
  80.             {
  81.                 Rotation = GameEntity.Rotation;
  82.                 Position = GameEntity.Position + EnityOffset;
  83.             }
  84.         }
  85.  
  86.         public override void Initialize()
  87.         {
  88.             particles = new Vector2[ParticleCount];
  89.             scaleParticle = new float[ParticleCount];
  90.             rotationParticle = new float[ParticleCount];
  91.             activeParticle = new bool[ParticleCount];
  92.  
  93.             texture = Game.Content.Load<Texture2D>(textureAsset);
  94.             Origin = new Vector2(texture.Width / 2, texture.Height / 2);
  95.  
  96.             for (int p = 0; p < ParticleCount; p++)
  97.             {
  98.                 rotationParticle[p] = 0;
  99.                 particles[p] = Vector2.Zero;
  100.                 scaleParticle[p] = Scale;
  101.             }
  102.         }
  103.  
  104.         public override void Draw(GameTime gameTime)
  105.         {
  106.             SpriteBatch.Begin(SpriteSortMode.Deferred, ParticleBlendState);
  107.  
  108.             for (int p = 0; p < ParticleCount; p++)
  109.             {
  110.                 if (activeParticle[p])
  111.                     SpriteBatch.Draw(texture, particles[p], null, Color, rotationParticle[p], Origin, scaleParticle[p], SpriteEffects.None, 0);
  112.             }
  113.  
  114.             SpriteBatch.End();
  115.         }
  116.  
  117.         public virtual bool DoCollision(IGameEntity entity)
  118.         {
  119.             return false;
  120.         }
  121.  
  122.         public virtual void Die()
  123.         {
  124.  
  125.         }
  126.     }
  127. }
RAW Paste Data