Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* I'm trying to get my particle manager working in my game.
- * Everything works fine, EXCEPT for a particle appearing in the top left corner during initialization.
- * It stems from the line shortParticle = new ShortParticle(textures, new Vector2());
- * under my Bat constructor. if I leave it blank, or use 0,0 for the Vector2 then the particle remains in *the top left corner until the ball and bat make contact. At that point the new particle continues to *display point of contact until the ball hits the bat on the other side.
- */
- // 1) How do I NOT have the particle display in the top left corner when the game starts?
- // 2) How do I kill the particle after 2 seconds? I tried adjusting the TTL in my ShortParticle class, but
- // it doesn't seem to change much.
- Public Class Ball
- {
- // Ball constructor
- public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
- {
- ......
- shortParticle = new ShortParticle(textures, new Vector2());
- ......
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- // Draws the short particle
- shortParticle.Draw(spriteBatch);
- }
- Update
- }
- shortParticle.Update(gameTime);
- }
- public void BatHit(int block)
- {
- if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
- {
- /// Generates particle when bat and ball collide
- RightBatParticles();
- ......
- }
- }
- }
- #region File Description
- //----------------------------------------------------------------------------------------------
- // Engine is designed by RB Whitaker http://rbwhitaker.wikidot.com/2d-particle-engine-1
- //----------------------------------------------------------------------------------------------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace Pong
- {
- public class ParticleEngine
- {
- private Random random;
- public Vector2 EmitterLocation { get; set; }
- private List<Particle> particles;
- private List<Texture2D> textures;
- public ParticleEngine(List<Texture2D> textures, Vector2 location)
- {
- EmitterLocation = location;
- this.textures = textures;
- this.particles = new List<Particle>();
- random = new Random();
- }
- public void Update()
- {
- int total = 10;
- for (int i = 0; i < total; i++)
- {
- particles.Add(GenerateNewParticle());
- }
- for (int particle = 0; particle < particles.Count; particle++)
- {
- particles[particle].Update();
- if (particles[particle].TTL <= 0)
- {
- particles.RemoveAt(particle);
- particle--;
- }
- }
- }
- /// <summary>
- /// Generates a normal particle
- /// </summary>
- public Particle GenerateNewParticle()
- {
- Texture2D texture = textures[random.Next(textures.Count)];
- Vector2 position = EmitterLocation;
- Vector2 velocity = new Vector2(
- 1f * (float)(random.NextDouble() * 2 - 1),
- 1f * (float)(random.NextDouble() * 2 - 1));
- float angle = 0;
- float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
- Color color = new Color(
- (float)random.NextDouble(),
- (float)random.NextDouble(),
- (float)random.NextDouble());
- float size = (float)random.NextDouble();
- int ttl = 20 + random.Next(40);
- return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
- }
- /// <summary>
- /// Draws particles. I commented out spriteBatch for now, as I've adjusted it to be drawn during the bat's spriteBatch
- /// </summary>
- public void Draw(SpriteBatch spriteBatch)
- {
- // spriteBatch.Begin();
- for (int index = 0; index < particles.Count; index++)
- {
- particles[index].Draw(spriteBatch);
- }
- // spriteBatch.End();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////
- #region File Description
- //----------------------------------------------------------------------------------------------
- // Engine is designed by RB Whitaker http://rbwhitaker.wikidot.com/2d-particle-engine-1
- //----------------------------------------------------------------------------------------------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace Pong
- {
- public class ShortParticle
- {
- private Random random;
- public Vector2 EmitterLocation { get; set; }
- private List<Particle> particles;
- private List<Texture2D> textures;
- private int threeSecondTime = 3;
- private int elapsedTime;
- public ShortParticle(List<Texture2D> textures, Vector2 location)
- {
- EmitterLocation = location;
- this.textures = textures;
- this.particles = new List<Particle>();
- random = new Random();
- }
- public void Update(GameTime gameTime)
- {
- // Keps track of time
- elapsedTime = gameTime.ElapsedGameTime.Milliseconds;
- // How many particles appear on screen at once
- int total = 7;
- for (int i = 0; i < total; i++)
- {
- particles.Add(GenerateNewParticle());
- }
- for (int particle = 0; particle < particles.Count; particle++)
- {
- particles[particle].Update();
- if (particles[particle].TTL <= 0)
- {
- particles.RemoveAt(particle);
- particle--;
- }
- }
- for (int particle = 0; particle < particles.Count; particle++)
- {
- particles[particle].Update();
- if (threeSecondTime <= elapsedTime)
- {
- particles.RemoveAt(particle);
- }
- }
- }
- /// <summary>
- /// Generates a normal particle
- /// </summary>
- public Particle GenerateNewParticle()
- {
- Texture2D texture = textures[random.Next(textures.Count)];
- Vector2 position = EmitterLocation;
- Vector2 velocity = new Vector2(
- 1f * (float)(random.NextDouble() * 2 - 1),
- 1f * (float)(random.NextDouble() * 2 - 1));
- float angle = 0;
- float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
- Color color = new Color(
- (float)random.NextDouble(),
- (float)random.NextDouble(),
- (float)random.NextDouble());
- float size = (float)random.NextDouble();
- int ttl = 20 + random.Next(40);
- return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
- }
- /// <summary>
- /// Draws particles. I commented out spriteBatch for now, as I've adjusted it to be drawn during the bat's spriteBatch
- /// </summary>
- public void Draw(SpriteBatch spriteBatch)
- {
- // spriteBatch.Begin();
- for (int index = 0; index < particles.Count; index++)
- {
- particles[index].Draw(spriteBatch);
- }
- // spriteBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement