public ParticleEngine(Vector2 position, Vector2 scale, Color tint, int particlesPerSpawn, TimeSpan particleLifeTime, TimeSpan spawnRate, params Texture2D[] particleImages) { _position = position; _particles = new List<Particle> (); _particleImages = particleImages; _lifeTime = particleLifeTime; _scale = scale; _random = new Random(); _autoSpawn = true; _spawnCount = particlesPerSpawn; _spawnRate = spawnRate; _speed = 3; _useGravity = false; _gravityScale = 1; _particleTint = tint; _targetAlpha = tint.A; _scale = scale; _targetScale = scale; _autoSpawn = true; _targetRotation = 0; _rotation = 0; _randomizeSpeed = false; //the pool of particles will be the 1.5 times the maximum number of particles will ever be on screen //with the current spawnrate, spawncount, and lifetime _particlePool = new Particle[MaxParticles]; for (int i = 0; i < _particlePool.Length; i++) { _particlePool[i] = new Particle(_particleImages[_random.Next(0, _particleImages.Length)], _position, _particleTint, _particleSpeed, _scale, _rotation, _lifeTime); } }
private void ExpandParticlePool() { //if this ever gets called then either SpawnCount, LifeTime, or SpawnRate were changed, //recalculate the new max size of the array Particle[] newPool = new Particle[((MaxParticles * 1.5f).ToInt())]; _particlePool.CopyTo(newPool, 0); int newStartIndex = _particlePool.Length; _particlePool = newPool; for (int i = newStartIndex; i < _particlePool.Length; i++) { _particlePool[i] = new Particle(_particleImages[_random.Next(0, _particleImages.Length)], _position, _particleTint, _particleSpeed, _scale, _rotation, _lifeTime); } }