Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System.Collections.Generic;
- namespace WindowsGame1
- {
- class Emitter
- {
- Texture2D Texture;
- public bool Active;
- public int Density;
- public Vector2 Position;
- public int Life;
- TimeSpan timeCreated;
- TimeSpan timeToLive;
- public float Gravity;
- public float yvel;
- public Particle particle;
- List<Particle> particles;
- Random Randomnum;
- float Direction;
- Vector2 InitialVel;
- public void Initialize(Texture2D texture, Vector2 position,int density,int life,GameTime gameTime, float gravity)
- {
- particles = new List<Particle>();
- Texture = texture;
- Active = true;
- Position = position;
- Density = density;
- Life = life;
- timeCreated = gameTime.TotalGameTime;
- timeToLive = TimeSpan.FromSeconds(Life);
- Gravity = gravity;
- yvel = 0;
- AddParticle();
- }
- public void AddParticle()
- {
- for (int i = 0; i < Density; i++)
- {
- particle = new Particle();
- Randomnum = new Random();
- Direction = Randomnum.Next(360);
- InitialVel = AngleToVector(Direction);
- particle.Initialize(Texture, Position, 10, 2, InitialVel);
- particles.Add(particle);
- }
- }
- Vector2 AngleToVector(float angle)
- {
- return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
- }
- public void Update(GameTime gameTime)
- {
- if (gameTime.TotalGameTime - timeCreated > timeToLive)
- {
- Active = false;
- Position = Vector2.Zero;
- }
- else
- {
- yvel += Gravity;
- Position.Y += yvel;
- }
- UpdateParticles(gameTime);
- }
- public void UpdateParticles(GameTime gameTime)
- {
- for (int i = 0; i < particles.Count; i++)
- {
- particles[i].Update(gameTime);
- }
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- for (int i = 0; i < particles.Count; i++)
- {
- particles[i].Draw(spriteBatch);
- }
- spriteBatch.Draw(Texture,Position,Color.White);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement