예제 #1
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_lifeTime <= 0) {
                if(Children.Count == 0)
                    Destroy();
                return;
            } else {
                _lifeTime -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            _spawnTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (_spawnTimer > _spawnDelay) {
                _spawnTimer = 0f;

                float randomLifeTime = _particleLifeTime * NumberFactory.RandomFloat();
                float randomAngle = _angle + (0.5f * NumberFactory.RandomFloat() - 0.25f);
                Vector2 randomVelocity = new Vector2(_radius * (float)Math.Cos(randomAngle), _radius * (float)Math.Sin(randomAngle));

                Particle particle = new Particle(this, "blood", randomLifeTime, _color);
                particle.Initialize();
                particle.Acceleration = new Vector2(0, 9.81f);
                particle.Velocity = randomVelocity;

                particle.Position = _stuckTo.Center;

                Add(particle, true);

            }
        }
예제 #2
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_lifeTime <= 0) {
                if (Children.Count == 0)
                    Destroy();
                return;
            } else {
                _lifeTime -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            _spawnTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (_spawnTimer > _spawnDelay) {
                _spawnTimer = NumberFactory.RandomBetween(0, _spawnDelay);

                for (int i = 0; i < _howMany; i++) {
                    float randomLifeTime = _particleLifeTime * NumberFactory.RandomFloat();
                    float randomAngle = NumberFactory.RandomBetween(0f, NumberFactory.TwoPi);

                    Particle particle = new Particle(this, "blood", randomLifeTime, _color, (World)Parent, _position, true);
                    particle.Acceleration = new Vector2(0, 9.81f);
                    particle.Size = new Vector2(NumberFactory.RandomBetween(3, 10), NumberFactory.RandomBetween(3, 10));
                    particle.Velocity = new Vector2(_radius * (float)Math.Cos(randomAngle), _radius * (float)Math.Sin(randomAngle));

                    particle.Initialize();
                    Add(particle);
                }

            }
        }
        protected override void InitializeParticle(Particle p, Vector2 where)
        {
            base.InitializeParticle(p, where);

            // vt = v0 + (a0 * t).
            p.Acceleration = -p.Velocity / p.Lifetime;
        }
예제 #4
0
        public ParticleCloud(IParent parent, Vector2 position, int howMany, float radius, float lifeTime, Color color)
            : base(parent)
        {
            Vector2 randomPosition = Vector2.Zero;
            Position = position;

            for (int i = 0; i < howMany; i++) {
                double randomRadius = Math.Sqrt(NumberFactory.RandomDouble()) * radius;
                double randomAngle = NumberFactory.RandomDouble() * NumberFactory.TwoPi;
                float randomLifeTime = (float)(lifeTime * NumberFactory.RandomDouble());

                randomPosition.X = (float)(randomRadius * Math.Cos(randomAngle));
                randomPosition.Y = (float)(randomRadius * Math.Sin(randomAngle));

                Particle particle = new Particle(this, "cloud", randomLifeTime, color, (World)parent);
                particle.Initialize();
                particle.Position = position + randomPosition - particle.Size / 2;

                Add(particle);
            }
        }
예제 #5
0
        public void AddParticle()
        {
            float randomLifeTime = _particleLifeTime * NumberFactory.RandomFloat();
            float randomAngle = _angle + (NumberFactory.HalfPi * NumberFactory.RandomFloat() - NumberFactory.QuarterPi);

            Particle particle = new Particle(this, "blood", randomLifeTime, Color, (World)Parent, _stuckTo.Position, true, _layerDepth + 0.01f);
            particle.Acceleration = new Vector2(0, 9.81f);
            particle.Size = new Vector2(NumberFactory.RandomBetween(3, 5), NumberFactory.RandomBetween(3, 5));

            if (_stuckTo is Sprite) {
                float spriteRotationAngle = ((Sprite)_stuckTo).RotationAngle;
                randomAngle += spriteRotationAngle;

                //Vector2 offset = new Vector2(_offset.X * (float)Math.Sin(-spriteRotationAngle), _offset.Y * (float)Math.Cos(-spriteRotationAngle));

            }
            particle.Position = particle.Position + _offset;

            particle.Velocity = new Vector2(_radius * (float)Math.Cos(randomAngle), _radius * (float)Math.Sin(randomAngle));

            particle.Initialize();
            Add(particle);
        }
예제 #6
0
        /// <summary>
        /// InitializeParticle randomizes some properties for a particle, then
        /// calls initialize on it. It can be overriden by subclasses if they 
        /// want to modify the way particles are created. For example, 
        /// SmokePlumeParticleSystem overrides this function make all particles
        /// accelerate to the right, simulating wind.
        /// </summary>
        /// <param name="p">the particle to initialize</param>
        /// <param name="where">the position on the screen that the particle should be
        /// </param>
        protected virtual void InitializeParticle(Particle p, Vector2 where)
        {
            // first, call PickRandomDirection to figure out which way the particle
            // will be moving. velocity and acceleration's values will come from this.
            Vector2 direction = PickRandomDirection();

            // pick some random values for our particle
            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);

            // then initialize it with those random values. initialize will save those,
            // and make sure it is marked as active.
            p.Initialize(
                where, velocity * direction, acceleration * direction,
                lifetime, scale, rotationSpeed);
        }
예제 #7
0
        /// <summary>
        /// override the base class's Initialize to do some additional work; we want to
        /// call InitializeConstants to let subclasses set the constants that we'll use.
        /// 
        /// also, the particle array and freeParticles queue are set up here.
        /// </summary>
        public override void Initialize()
        {
            InitializeConstants();

            // calculate the total number of particles we will ever need, using the
            // max number of effects and the max number of particles per effect.
            // once these particles are allocated, they will be reused, so that
            // we don't put any pressure on the garbage collector.
            particles = new Particle[howManyEffects * maxNumParticles];
            freeParticles = new Queue<Particle>(howManyEffects * maxNumParticles);
            for (int i = 0; i < particles.Length; i++)
            {
                particles[i] = new Particle();
                freeParticles.Enqueue(particles[i]);
            }
            base.Initialize();
        }
예제 #8
0
        private void AddParticle()
        {
            double randomRadius = Math.Sqrt(NumberFactory.RandomDouble()) * _radius;
            double randomAngle = NumberFactory.RandomDouble() * NumberFactory.TwoPi;
            float randomLifeTime = (float)(_lifeTime * NumberFactory.RandomDouble());

            Vector2 _randomPosition = Vector2.Zero;
            _randomPosition.X = (float)(randomRadius * Math.Cos(randomAngle));
            _randomPosition.Y = (float)(randomRadius * Math.Sin(randomAngle));

            Color _newColor;
            if (_randomColor) {
                _newColor = RandomColorList[NumberFactory.RandomBetween(0, RandomColorList.Count)];
            } else {
                _newColor = _color;
            }

            if(_parent is World) {
                Particle particle = new Particle(this, "sparkle", randomLifeTime, _newColor, (World)_parent);
                particle.Initialize();
                particle.Size *= _scale;
                particle.Position = _position + _randomPosition - particle.Size / 2;

                Add(particle);
            }
        }