|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 2:12 pm Post subject: [C#]XNA Particle system Template |
|
|
I coded this with the help of a tutorial but I find it quite useful to just copy and paste on any kind of C# game
its all about a principal class file and make separated class files ccalling to the principal class file then just use the functions in the main file
here it it
------------------
to use it you should call this function
particle variable.AddParticles(position value here);
and add this in the main public game() of the XNA game studio template
particle variable = new particle system class(this, 1, "particle image file");
Components.Add(particle variable);
also declare the variable at the top
ParticleSystem particle class variable;
after doing this all what you need is to add the following classes::
Particles.cs
Code: | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNa_game_2D
{
namespace particles
{
public class Particles
{
public Vector2 position;
public Vector2 velocity;
public Vector2 acceleration;
public float lifetime;
public float timesincestart;
public float scale;
public float rotation;
public float rotationspeed;
public bool Active{
get { return timesincestart < lifetime;}
}
public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration, float lifetime, float scale, float rotationspeed)
{
this.position = position;
this.velocity = velocity;
this.acceleration = acceleration;
this.lifetime = lifetime;
this.scale = scale;
this.rotationspeed = rotationspeed;
this.timesincestart = 0.0f;
this.rotation = 0.0f;
}
public void Update(float dt)
{
velocity += acceleration * dt;
position += velocity * dt;
rotation += rotationspeed * dt;
timesincestart += dt;
}
}
}
}
|
ParticleSystem.cs
Code: | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNa_game_2D
{
namespace particles
{
public abstract class ParticleSystem : DrawableGameComponent
{
private Game1 game;
private Texture2D texture;
private Vector2 origin;
private static Random random = new Random();
public static Random Random { get { return random; } }
private int howmanyeffects;
Particles[] particles;
Queue<Particles> freeparticles;
public int freeparticlecount
{
get { return freeparticles.Count; }
}
public const int AlphaBlendDrawOrder = 100;
public const int AdditiveDrawOrder = 200;
protected SpriteBlendMode spriteBlendMode;
protected int MinNumParticles;
protected int MaxNumParticles;
protected float MinLifeTime;
protected float MaxLifeTime;
protected string textureFileName;
protected float MinInitialSpeed;
protected float MaxInitialSpeed;
protected float MinAcceleration;
protected float MaxAcceleration;
protected float MinRotationSpeed;
protected float MaxRotationSpeed;
protected float MinScale;
protected float MaxScale;
protected ParticleSystem(Game1 game, int howmanyeffects, string textureFileName)
: base(game)
{
this.game = game;
this.howmanyeffects = howmanyeffects;
this.textureFileName = textureFileName;
}
public override void Initialize()
{
InitializeConstants();
particles = new Particles[howmanyeffects * MaxNumParticles];
freeparticles = new Queue<Particles>(howmanyeffects * MaxNumParticles);
for (int i = 0; i < particles.Length; i++)
{
particles[i] = new Particles();
freeparticles.Enqueue(particles[i]);
}
base.Initialize();
}
protected abstract void InitializeConstants();
protected override void LoadContent()
{
texture = game.Content.Load<Texture2D>(textureFileName);
origin.X = texture.Width / 2;
origin.Y = texture.Height / 2;
base.LoadContent();
}
public void AddParticles(Vector2 where)
{
int numParticles = random.Next(MinNumParticles, MaxNumParticles);
for (int i = 0; i < numParticles && freeparticles.Count > 0; i++)
{
Particles p = freeparticles.Dequeue();
InitializeParticles(p, where);
}
}
protected virtual Vector2 pickrandomDirection()
{
float angle = RandomBetween(0, MathHelper.TwoPi);
return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
}
public static float RandomBetween(float min, float max)
{
return min + (float)random.NextDouble() * (max - min);
}
protected virtual void InitializeParticles(Particles p, Vector2 where)
{
Vector2 direction = pickrandomDirection();
float velocity = RandomBetween(MinInitialSpeed, MaxInitialSpeed);
float acceleration = RandomBetween(MinAcceleration, MaxAcceleration);
float lifetime = RandomBetween(MinLifeTime, MaxLifeTime);
float scale = RandomBetween(MinScale, MaxScale);
float rotationspeed = RandomBetween(MinRotationSpeed, MaxRotationSpeed);
p.Initialize(where, velocity * direction, acceleration * direction, lifetime, scale, rotationspeed);
}
public override void Update(GameTime gameTime)
{
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (Particles p in particles)
{
if (p.Active)
{
p.Update(dt);
}
if (!p.Active)
{
freeparticles.Enqueue(p);
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
game.SpriteBatch.Begin(spriteBlendMode);
foreach (Particles p in particles)
{
if (!p.Active)
continue;
float normalizedLifetime = p.timesincestart / p.lifetime;
float alpha = 4 * normalizedLifetime * (1 - normalizedLifetime);
float scale = p.scale * (float)(.75f + .25f * normalizedLifetime);
Color color = new Color(new Vector4(1, 1, 1, alpha));
game.SpriteBatch.Draw(texture, p.position, null, color, p.rotation, origin, scale, SpriteEffects.None, 0.0f);
}
game.SpriteBatch.End();
base.Draw(gameTime);
}
}
}
} |
and add your particle system class with the name you wish
Code: | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNa_game_2D
{
namespace particles
{
class ExplosionParticleSystem: ParticleSystem
{
public ExplosionParticleSystem(Game1 game, int howmanyeffects, string textureFileName)
: base(game, howmanyeffects, textureFileName)
{
}
protected override void InitializeConstants()
{
MinInitialSpeed = 40;
MaxInitialSpeed = 500;
MinAcceleration = 0;
MaxAcceleration = 0;
MinLifeTime = 0.5f;
MaxLifeTime = 1.0f;
MinScale = 0.1f;
MaxScale = 0.6f;
MinNumParticles = 20;
MaxNumParticles = 25;
MinRotationSpeed = -MathHelper.PiOver4;
MaxRotationSpeed = MathHelper.PiOver4;
spriteBlendMode = SpriteBlendMode.Additive;
DrawOrder = AdditiveDrawOrder;
}
protected override void InitializeParticles(Particles p, Vector2 where)
{
base.InitializeParticles(p, where);
p.acceleration = -p.velocity / p.lifetime;
}
}
}
}
|
this last one is what will control the behavior of each particle system you create, you can create multiple particle system with the same 2 root class files I posted above Particles.cs and ParticleSystem.cs
-Gogodr ;3
-edit uhmm maybe I should have posted this in general programming... but its just for games.. so I posted it here... xP
|
|
Back to top |
|
|
iGod Grandmaster Cheater Reputation: -1
Joined: 31 Jan 2009 Posts: 621 Location: Manchester, uk.
|
Posted: Tue Jul 21, 2009 2:20 pm Post subject: |
|
|
Seems foolproof, can i see it in action?
|
|
Back to top |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 2:46 pm Post subject: |
|
|
its in the game I posted before
it launches each time you hit an UFO
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|