Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*:
- * @plugindesc v1.00 Extension, that comes with predefined behaviours to be used with the particle engine.
- * @author Iavra
- *
- * @help
- * To use one of the behaviours included in this plugin, provide it as an argument to the "addBehaviour" function of
- * your created emitter:
- *
- * emitter.addBehaviour(new IAVRA.PARTICLE.B.Life(200));
- *
- * This example will register a "Life" behaviour, that will cause particles to die after 200 frames.
- *
- * The following behaviours are contained in the IAVRA.PARTICLE.B / IAVRA.PARTICLE.BEHAVIOUR namespace:
- *
- * BindToMap(offset) Moves the emitter and all of its particles with the map.
- *
- * BindToPlayer(offset) Moves the emitter and all of its particles with the player.
- *
- * BindToEvent(id, offset) Moves the emitter and all of its particles with the given event.
- *
- * Position(zone) Sets the starting position of created particles to a random point inside
- * the given zone.
- *
- * Radius(radius) Sets the collision radius of created particles to the given value.
- * Behaviours using this property need to make sure to factor in the
- * particle's scale as well.
- *
- * Image(image, scaling) Sets the texture to be displayed by created particles. Can be overloaded
- * by specifying multiple images and will pick one of them at random.
- *
- * Velocity(x, y) Increases the starting velocity of created particles by the given amount.
- * Can be overloaded to randomize the actual values.
- *
- * PolarVelocity(radius, angle) Increases the starting velocity of created particles by a vector
- * calculated from the given radius and angle. Can be overloaded to randomize
- * the actual values.
- *
- * Blend(mode) Sets the blend mode of created particles. PIXI.blendModes features a full
- * list of all available modes. Support variies between renderers.
- *
- * Life(life, easing) Sets the particle's life to the given value. Can be overloaded to
- * randomize the actual values. Also, will set the particle's energy
- * according to its life and age.
- *
- * Scale(startScale, endScale) Transitions the particle's scale between the given start and end values.
- *
- * Alpha(startAlpha, endAlpha) Transitions the particle's alpha value between the given start and end
- * values.
- *
- * Rotation(startAngle, endAngle) Transitions the particle's rotation between the given start and end
- * values.
- *
- * Color(startColor, endColor) Transitions the particle's tinting color between the given start and end
- * values.
- *
- * Friction(friction) Applies linear friction to particles, causing them to slow down over time.
- *
- * Force(x, y) Applies a linear force to particles.
- *
- * PolarForce(radius, angle) Applies a linear force to particles, that gets calculated from a polar
- * vector.
- *
- * RandomDrift(x, y) Randomly accelerates particles.
- *
- * SpeedLimit(min, max) Limits the velocity of particles to a given range.
- *
- * Attraction(center, force, epsilon) Places a gravity well at a given point, that will attract particles
- * depending on their distance.
- *
- * Repulsion(center, force, epsilon) Places an anti-gravity well at a given point, that will repel particles
- * depending on their distance.
- *
- * RotateToDirection() Rotates particles to always face in the direction, they are moving to.
- *
- * Collision(bounce) Applies collision between all particles of the emitter, depending on
- * their radius and scale.
- *
- * BoundingBox(x, y, width, height, bounce) Restricts particles to a bounding box, causing them to bounce back, if
- * they try to leave it. Particles should spawn inside the box, to prevent
- * unpredictable side effects.
- *
- * WrapAroundBox(x, y, width, height) Restricts particles to a bounding box, causing them to wrap around to the
- * other side, if they try to leave it. Particles should spawn inside the
- * box, to prevent unpredictable side effects.
- *
- * CollisionZone(zone, bounce) Applies collision between particles and the given zone, both from the
- * inside and outside.
- *
- * DeathZone(zone, killOutside) Destroys all particles in- or outside the given zone.
- */
- (function($, undefined) {
- "use strict";
- if(!$.PARTICLE) { throw new Error("This plugin depends on 'Iavra Particle - Core'."); }
- /**
- * Extends a given object with all properties from another given object. Mainly used to simplify subclassing.
- * @param {Object} base - Base object to be extended.
- * @param {Object} extend - Object containing the properties to be appended to the given base object.
- * @returns {Object} The base object, after it has been extended.
- */
- var _extend = function(base, extend) {
- for(var key in extend) { base[key] = extend[key]; }
- return base;
- };
- /**
- * Returns a random floating point number between the given minimum (inclusive) and maximum (exclusive).
- * @param {number} min - Minimum number to be generated (inclusive).
- * @param {number} max - Maximum number to be generated (exclusive).
- * @returns {number} A random number between the given minimum and maximum numbers.
- */
- var _random = function(min, max) {
- return min + (max - min) * Math.random();
- };
- /**
- * Returns the given value or, if it's not defined, the default value.
- * @param {*} value - Value to test.
- * @param {*} defaultValue - Value to be returned, if the tested value is undefined.
- * @param {*} Either the given value or defaultValue, of the former one is undefined.
- */
- var _default = function(value, defaultValue) {
- return value === undefined ? defaultValue : value;
- };
- /**
- * Converts a given angle from its degree to radian representation.
- * @param {number} deg - An angle, in degree.
- * @returns {number} The given angle's radian representation.
- */
- var _degToRad = function(deg) {
- return deg * Math.PI / 180;
- };
- /**
- * Constructs a polar vector with the given radius and angle (in radians) and returns its cartesian representation.
- * @param {number} radius - Radius component of the polar vector, which defines the resulting vector's length.
- * @param {number} angle - Angle component of the polar vector, in radians.
- * @returns {number[]} An array containing the cartesian representation of the polar vector.
- */
- var _polarVector = function(radius, angle) {
- return [radius * Math.cos(angle), -radius * Math.sin(angle)]
- };
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR
- //=============================================================================
- $.PARTICLE.BEHAVIOUR = $.PARTICLE.B = {
- // activities only
- BindToMap: function(offset) { this._initialize(offset); },
- BindToPlayer: function(offset) { this._initialize(offset); },
- BindToEvent: function(id, offset) { this._initialize(id, offset); },
- // setup only
- Position: function(zone) { this._initialize(zone); },
- Radius: function(radius) { this._initialize(radius); },
- Image: function(image, scaling) { this._initialize(image, scaling); },
- Velocity: function(x, y) { this._initialize(x, y); },
- PolarVelocity: function(radius, angle) { this._initialize(radius, angle); },
- Blend: function(mode) { this._initialize(mode); },
- // age-dependent
- Life: function(life, easing) { this._initialize(life, easing); },
- Scale: function(startScale, endScale) { this._initialize(startScale, endScale); },
- Alpha: function(startAlpha, endAlpha) { this._initialize(startAlpha, endAlpha); },
- Rotation: function(startAngle, endAngle) { this._initialize(startAngle, endAngle); },
- Color: function(startColor, endColor) { this._initialize(startColor, endColor); },
- // permanent
- Friction: function(friction) { this._initialize(friction); },
- Force: function(x, y) { this._initialize(x, y); },
- PolarForce: function(radius, angle) { this._initialize(radius, angle); },
- RandomDrift: function(x, y) { this._initialize(x, y); },
- SpeedLimit: function(min, max) { this._initialize(min, max); },
- Attraction: function(center, force, epsilon) { this._initialize(center, force, epsilon); },
- Repulsion: function(center, force, epsilon) { this._initialize(center, force, epsilon); },
- RotateToDirection: function() {},
- Collision: function(bounce) { this._initialize(bounce); },
- BoundingBox: function(x, y, width, height, bounce) { this._initialize(x, y, width, height, bounce); },
- WrapAroundBox: function(x, y, width, height) { this._initialize(x, y, width, height); },
- CollisionZone: function(zone, bounce) { this._initialize(zone, bounce); },
- DeathZone: function(zone, outside) { this._initialize(zone, outside); }
- };
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.BindToMap
- //=============================================================================
- /**
- * Moves the emitter and all of its particles with the map.
- */
- $.PARTICLE.BEHAVIOUR.BindToMap.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. The given offset is added to the adjusted emitter position.
- * @param {PIXI.Point} [offset] - Positional offset relative to the map's upper left corner.
- */
- _initialize: function(offset) {
- this._offset = offset || new PIXI.Point();
- },
- /**
- * Gets called on every update cycle, before particles are updated. Sets the particle's position relative to
- * the current map position.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing this behaviour.
- */
- activity: function(emitter) {
- emitter.position.x = this._offset.x + $gameMap.adjustX(0) * $gameMap.tileWidth();
- emitter.position.y = this._offset.y + $gameMap.adjustY(0) * $gameMap.tileHeight();
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.BindToPlayer
- //=============================================================================
- /**
- * Moves the emitter and all of its particles with the player.
- */
- $.PARTICLE.BEHAVIOUR.BindToPlayer.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. The given offset is added to the adjusted emitter position.
- * @param {PIXI.Point} [offset] - Positional offset relative to the player position's center.
- */
- _initialize: function(offset) {
- this._offset = offset || new PIXI.Point();
- },
- /**
- * Gets called on every update cycle, before particles are updated. Sets the particle's position relative to
- * the player's position.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing this behaviour.
- */
- activity: function(emitter) {
- var player = $gamePlayer;
- emitter.position.x = this._offset.x + $gameMap.adjustX(player._realX + 0.5) * $gameMap.tileWidth();
- emitter.position.y = this._offset.y + $gameMap.adjustY(player._realY + 0.5) * $gameMap.tileHeight();
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.BindToEvent
- //=============================================================================
- /**
- * Moves the emitter and all of its particles with the given event
- */
- $.PARTICLE.BEHAVIOUR.BindToEvent.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. The given offset is added to the adjusted emitter position.
- * @param {number} id - Id of the event to be tracked.
- * @param {PIXI.Point} [offset] - Positional offset relative to the event position's center.
- */
- _initialize: function(id, offset) {
- this._id = id;
- this._offset = offset || new PIXI.Point();
- },
- /**
- * Gets called on every update cycle, before particles are updated. Sets the particle's position relative to
- * the given event's position.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing this behaviour.
- */
- activity: function(emitter) {
- var event = $gameMap.event(this._id);
- emitter.position.x = this._offset.x + $gameMap.adjustX(event._realX + 0.5) * $gameMap.tileWidth();
- emitter.position.y = this._offset.y + $gameMap.adjustY(event._realY + 0.5) * $gameMap.tileHeight();
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Position
- //=============================================================================
- /**
- * Sets the starting position of created particles to a random point inside the given zone.
- */
- $.PARTICLE.BEHAVIOUR.Position.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. The given zone will be used to pick a random point as a starting position.
- * @param {IAVRA.PARTICLE.Zone} zone - Zone instance to be used to pick a random point inside it.
- */
- _initialize: function(zone) {
- this._zone = zone;
- },
- /**
- * Gets called, whenever a new particle is created. Sets the particle's position (and oldPosition, to stay
- * compatible with other behaviours) to a random point inside the given zone.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- var point = this._zone.getPoint();
- particle.oldPosition.x = particle.position.x = point.x;
- particle.oldPosition.y = particle.position.y = point.y;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Radius
- //=============================================================================
- /**
- * Sets the collision radius of created particles to the given value. Behaviours using this property need to make
- * sure to factor in the particle's scale as well.
- */
- $.PARTICLE.BEHAVIOUR.Radius.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} radius - Base collision radius to be used by created particles.
- */
- _initialize: function(radius) {
- this._radius = radius;
- },
- /**
- * Gets called, whenever a new particle is created. Sets the particle's collision radius to the given value.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- particle.radius = this._radius;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Image
- //=============================================================================
- /**
- * Sets the texture to be displayed by created particles. Can be overloaded by specifying multiple images and will
- * pick one of them at random.
- */
- $.PARTICLE.BEHAVIOUR.Image.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. Can either be called with a single image or an array of images, in which case
- * particles will be assigned one of them at random.
- * @param {(string|string[])} image - Path to an image file or an array of images to be used.
- * @param {number} [scaling=PIXI.scaleModes.DEFAULT] scaling - Scale mode to be used. Possible values are 0
- * (linear) and 1 (nearest). Defaults to 0.
- */
- _initialize: function(image, scaling) {
- this._images = [];
- if(Array.isArray(image)) {
- for(var i = 0, max = image.length; i < max; ++i) {
- this._images.push(PIXI.Texture.fromImage(image[i], null, scaling || PIXI.scaleModes.DEFAULT));
- }
- } else {
- this._images.push(PIXI.Texture.fromImage(image, null, scaling || PIXI.scaleModes.DEFAULT));
- }
- },
- /**
- * Gets called, whenever a new particle is created. Sets the particle's texture according to the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- particle.texture = (this._images.length === 1)
- ? this._images[0]
- : this._images[(this._images.length * Math.random())|0];
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Velocity
- //=============================================================================
- /**
- * Increases the starting velocity of created particles by the given amount. Can be overloaded to randomize the
- * actual values.
- */
- $.PARTICLE.BEHAVIOUR.Velocity.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. Can either be called with static numbers or arrays, in which case their contents
- * will be used as the minimum and maximum bounds, when creating random numbers.
- * @param {(number|numbers[])} x - Horizontal velocity or bounds for random number creation.
- * @param {(number|numbers[])} y - Vertical velocity or bounds for random number creation.
- */
- _initialize: function(x, y) {
- if(Array.isArray(x)) {
- this._minX = x[0];
- this._maxX = x[1];
- } else {
- this._minX = this._maxX = x || 0;
- }
- if(Array.isArray(y)) {
- this._minY = y[0];
- this._maxY = y[1];
- } else {
- this._minY = this._maxY = y || 0;
- }
- },
- /**
- * Gets called, whenever a new particle is created. Increases its velocity according to the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- particle.velocity.x += (this._minX === this._maxX) ? this._minX : _random(this._minX, this._maxX);
- particle.velocity.y += (this._minY === this._maxY) ? this._minY : _random(this._minY, this._maxY);
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.PolarVelocity
- //=============================================================================
- /**
- * Increases the starting velocity of created particles by a vector calculated from the given radius and angle.
- * Can be overloaded to randomize the actual values.
- */
- $.PARTICLE.BEHAVIOUR.PolarVelocity.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. Can either be called with static numbers or arrays, in which case their contents
- * will be used as the minimum and maximum bounds, when creating random numbers.
- * @param {(number|numbers[])} radius - Radius of the polar vector or bounds for random number creation.
- * @param {(number|numbers[])} angle - Angle of the polar vector or bounds for random numbers, in degree.
- */
- _initialize: function(radius, angle) {
- if(Array.isArray(radius)) {
- this._minRadius = radius[0];
- this._maxRadius = radius[1];
- } else {
- this._minRadius = this._maxRadius = radius || 0;
- }
- if(Array.isArray(angle)) {
- this._minAngle = _degToRad(angle[0]);
- this._maxAngle = _degToRad(angle[1]);
- } else {
- this._minAngle = this._maxAngle = _degToRad(angle || 0);
- }
- },
- /**
- * Gets called, whenever a new particle is created. Increases its velocity according to the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- var vector = _polarVector(
- (this._minRadius === this._maxRadius) ? this._minRadius : _random(this._minRadius, this._maxRadius),
- (this._minAngle === this._maxAngle) ? this._minAngle : _random(this._minAngle, this._maxAngle)
- )
- particle.velocity.x += vector[0];
- particle.velocity.y += vector[1];
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Blend
- //=============================================================================
- /**
- * Sets the blend mode of created particles. PIXI.blendModes features a full list of all available modes. Support
- * variies between renderers.
- */
- $.PARTICLE.BEHAVIOUR.Blend.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} image - Blend mode to be used.
- */
- _initialize: function(mode) {
- this._mode = mode;
- },
- /**
- * Gets called, whenever a new particle is created. Sets its blend mode to the given value.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- particle.blendMode = this._mode;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Life
- //=============================================================================
- /**
- * Sets the particle's life to the given value. Can be overloaded to randomize the actual values. Also, will set
- * the particle's energy according to its life and age.
- */
- $.PARTICLE.BEHAVIOUR.Life.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour. Can either be called with static numbers or an array, in which case its contents
- * will be used as the minimum and maximum bound, when creating random numbers. An optional easing function
- * can be specified to modify the particle's energy curve.
- * @param {(number|numbers[])} life - Particle's life or minimum and maximum values to create random numbers.
- * @param {function} [easing] - Optional easing function to calculate the particle's energy.
- */
- _initialize: function(life, easing) {
- if(Array.isArray(life)) {
- this._min = life[0];
- this._max = life[1];
- } else {
- this._min = this._max = life || 0;
- }
- this._easing = easing || function(k) { return k; };
- },
- /**
- * Gets called, whenever a new particle is created. Sets its life according to the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance, that created the particle.
- * @param {IAVRA.PARTICLE.Particle} particle - A new particle, that has just been created by the emitter.
- */
- setup: function(emitter, particle) {
- particle.life = (this._min === this._max) ? this._min : _random(this._min, this._max);
- },
- /**
- * Gets called on every update cycle, for each particle. Calculates the particle's energy according to the
- * given easing function.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- particle.energy = particle.dead ? 0 : 1 - this._easing(particle.age / particle.life);
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Scale
- //=============================================================================
- /**
- * Transitions the particle's scale between the given start and end values.
- */
- $.PARTICLE.BEHAVIOUR.Scale.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [startScale=0] - Starting value to be used for the particle's scale.
- * @param {number} [endScale] - End value to be used for the particle's scale. If this is omitted, the particle
- * will have a fixed scale, instead.
- */
- _initialize: function(startScale, endScale) {
- this._start = startScale || 0;
- this._end = endScale;
- },
- /**
- * Gets called on every update cycle, for each particle. Sets the particle's scale according to the given
- * values and its energy.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle) {
- particle.scale.x = particle.scale.y = (this._end === undefined)
- ? this._start
- : this._end + (this._start - this._end) * particle.energy;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Alpha
- //=============================================================================
- /**
- * Transitions the particle's alpha value between the given start and end values.
- */
- $.PARTICLE.BEHAVIOUR.Alpha.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [startAlpha=0] - Starting value to be used for the particle's alpha.
- * @param {number} [endAlpha] - End value to be used for the particle's alpha. If this is omitted, the particle
- * will have a fixed alpha value, instead.
- */
- _initialize: function(startAlpha, endAlpha) {
- this._start = startAlpha || 0;
- this._end = endAlpha;
- },
- /**
- * Gets called on every update cycle, for each particle. Sets the particle's alpha according to the given
- * values and its energy.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle) {
- particle.alpha = (this._end === undefined)
- ? this._start
- : this._end + (this._start - this._end) * particle.energy;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Rotation
- //=============================================================================
- /**
- * Transitions the particle's rotation between the given start and end values.
- */
- $.PARTICLE.BEHAVIOUR.Rotation.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [startAngle=0] - Starting value to be used for the particle's rotation, in radians.
- * @param {number} [endAngle] - End value to be used for the particle's rotation, in radians. If this is
- * omitted, the particle will have a fixed rotation, instead.
- */
- _initialize: function(startAngle, endAngle) {
- this._start = _degToRad(startAngle || 0);
- if(endAngle !== undefined) { this._end = _degToRad(endAngle); }
- },
- /**
- * Gets called on every update cycle, for each particle. Sets the particle's rotation according to the given
- * values and its energy.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle) {
- particle.rotation = (this._end === undefined)
- ? this._start
- : this._end + (this._start - this._end) * particle.energy;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Color
- //=============================================================================
- /**
- * Transitions the particle's tinting color between the given start and end values.
- */
- $.PARTICLE.BEHAVIOUR.Color.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [startColor=0] - Starting value to be used for the particle's color, as hex string.
- * @param {number} [endColor] - End value to be used for the particle's color, as hex string. If this is
- * omitted, the particle will have a fixed tinting color, instead.
- */
- _initialize: function(startColor, endColor) {
- this._start = this._getRgbColor(startColor);
- this._end = this._getRgbColor(endColor);
- },
- /**
- * Gets called on every update cycle, for each particle. Sets the particle's tint according to the given
- * values and its energy.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- if(this._end === null) {
- particle.tint = this._getHexColor(this._start);
- } else {
- var start = this._start, end = this._end, ratio = particle.energy, inverse = 1 - ratio;
- particle.tint = this._getHexColor([
- start[0] * ratio + end[0] * inverse,
- start[1] * ratio + end[1] * inverse,
- start[2] * ratio + end[2] * inverse
- ]);
- }
- },
- /**
- * Internal utility function, that converts a given hex string to its rgb representation.
- * @param {string} [color] - Hex color string to be converted.
- * @returns {(number[]|null)} - An array containing the rgb values of the given color or null, if the color
- * parameter was omitted.
- */
- _getRgbColor: function(color) {
- if(color === undefined || color === null) { return null; }
- color = (color.charAt(0) == '#') ? color.substring(1, 7) : color;
- return [
- parseInt(color.substring(0, 2), 16),
- parseInt(color.substring(2, 4), 16),
- parseInt(color.substring(4, 6), 16)
- ];
- },
- /**
- * Internal utility function, that converts a given rgb array to its hex number representation.
- * @param {number[]} rgb - Color array to be converted.
- * @returns {number} - Hex number representation of the given color.
- */
- _getHexColor: function(rgb) {
- return ((rgb[0] << 16) + (rgb[1] << 8) + rgb[2]);
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Friction
- //=============================================================================
- /**
- * Applies linear friction to particles, causing them to slow down over time.
- */
- $.PARTICLE.BEHAVIOUR.Friction.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} friction - Damping value to be used. Since this is applies every frame, it's advised to use
- * very low values.
- */
- _initialize: function(friction) {
- this._friction = friction;
- },
- /**
- * Gets called on every update cycle, for each particle. Multiplies the particle's velocity vector with (1 -
- * friction), slowing it down.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var lengthSq = particle.velocity.x * particle.velocity.x + particle.velocity.y * particle.velocity.y;
- if(lengthSq === 0) { return; }
- var scale = 1 - this._friction / Math.sqrt(lengthSq);
- if(scale <= 0) {
- particle.velocity.x = particle.velocity.y = 0;
- } else {
- particle.velocity.x *= scale;
- particle.velocity.y *= scale;
- }
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Force
- //=============================================================================
- /**
- * Applies a linear force to particles.
- */
- $.PARTICLE.BEHAVIOUR.Force.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [x=0] - X component of the force vector to apply.
- * @param {number} [y=0] - Y component of the force vector to apply.
- */
- _initialize: function(x, y) {
- this._x = x || 0;
- this._y = y || 0;
- },
- /**
- * Gets called on every update cycle, for each particle. Accelerates the particle by the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- particle.velocity.x += this._x;
- particle.velocity.y += this._y;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.PolarForce
- //=============================================================================
- /**
- * Applies a linear force to particles, that gets calculated from a polar vector.
- */
- $.PARTICLE.BEHAVIOUR.PolarForce.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [radius=0] - Radius component of the polar vector to apply.
- * @param {number} [angle=0] - Angle component of the polar vector to apply, in degree.
- */
- _initialize: function(radius, angle) {
- this._radius = radius || 0;
- this._angle = _degToRad(angle || 0);
- },
- /**
- * Gets called on every update cycle, for each particle. Accelerates the particle by the given values.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var vector = _polarVector(this._radius, this._angle);
- particle.velocity.x += vector[0];
- particle.velocity.y += vector[1];
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.RandomDrift
- //=============================================================================
- /**
- * Randomly accelerates particles.
- */
- $.PARTICLE.BEHAVIOUR.RandomDrift.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [x=0] - X component of the force vector to apply.
- * @param {number} [y=0] - Y component of the force vector to apply.
- */
- _initialize: function(x, y) {
- this._x = (x || 0) * 2;
- this._y = (y || 0) * 2;
- },
- /**
- * Gets called on every update cycle, for each particle. Accelerates the particle by a random force in the
- * range of ([-x;x[, [-y;y[).
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- particle.velocity.x += (Math.random() - 0.5) * this._x;
- particle.velocity.y += (Math.random() - 0.5) * this._y;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.SpeedLimit
- //=============================================================================
- /**
- * Limits the velocity of particles to a given range.
- */
- $.PARTICLE.BEHAVIOUR.SpeedLimit.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [min=0] - Minimum velocity allowed for particles.
- * @param {number} [max=0] - Maximum velocity allowed for particles.
- */
- _initialize: function(min, max) {
- this._min = min || 0;
- this._max = max || 0;
- this._minSq = this._min * this._min;
- this._maxSq = this._max * this._max;
- },
- /**
- * Gets called on every update cycle, for each particle. If the particle's velocity vector is lower or higher,
- * than the given limits, it will be adjusted accordingly. Does not affect particles with 0 velocity.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var speedSq = particle.velocity.x * particle.velocity.x + particle.velocity.y * particle.velocity.y;
- if(speedSq < this._minSq) {
- var factor = this._min / Math.sqrt(speedSq);
- particle.velocity.x *= factor;
- particle.velocity.y *= factor;
- } else if(speedSq > this._maxSq) {
- var factor = this._max / Math.sqrt(speedSq);
- particle.velocity.x *= factor;
- particle.velocity.y *= factor;
- }
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Attraction
- //=============================================================================
- /**
- * Places a gravity well at a given point, that will attract particles depending on their distance.
- */
- $.PARTICLE.BEHAVIOUR.Attraction.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {PIXI.Point} center - Center point of the attraction.
- * @param {number} force - Force to be applied to particles. This value will get scaled according to their
- * distance from the center.
- * @param {number} [epsilon=100] - Particles nearer than the given distance will be treated, as though they are
- * that distance away. Prevents the gravity well from blowing up, when distances get low.
- */
- _initialize: function(center, force, epsilon) {
- this._center = center;
- this._force = force;
- this._epsilon = _default(epsilon, 100);
- this._epsilonSq = this._epsilon * this._epsilon;
- },
- /**
- * Gets called on every update cycle, for each particle. Attracts all particles to the center point, with an
- * increasing force for shorter distances. A force too high or epsilon too low might cause particles to be
- * catapulted away from the well.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var x = this._center.x - particle.position.x;
- var y = this._center.y - particle.position.y;
- var distanceSq = x * x + y * y;
- if(distanceSq === 0) { return; }
- var distance = Math.sqrt(distanceSq);
- if(distanceSq < this._epsilonSq) { distanceSq = this._epsilonSq; };
- var factor = this._force * 10000 / (distanceSq * distance);
- particle.velocity.x += x * factor;
- particle.velocity.y += y * factor;
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Repulsion
- //=============================================================================
- /**
- * Places an anti-gravity well at a given point, that will repel particles depending on their distance.
- */
- $.PARTICLE.BEHAVIOUR.Repulsion.prototype = _extend(Object.create($.PARTICLE.BEHAVIOUR.Attraction.prototype), {
- /**
- * Initializes the behaviour.
- * @param {PIXI.Point} center - Center point of the repulsion.
- * @param {number} force - Force to be applied to particles. This value will get scaled according to their
- * distance from the center.
- * @param {number} [epsilon=100] - Particles nearer than the given distance will be treated, as though they are
- * that distance away.
- */
- _initialize: function(center, force, epsilon) {
- $.PARTICLE.BEHAVIOUR.Attraction.prototype._initialize.call(this, center, -force, epsilon)
- },
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.RotateToDirection
- //=============================================================================
- /**
- * Rotates particles to always face in the direction, they are moving to.
- */
- $.PARTICLE.BEHAVIOUR.RotateToDirection.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Gets called on every update cycle, for each particle.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- particle.rotation = Math.atan2(-particle.velocity.y, particle.velocity.x);
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.Collision
- //=============================================================================
- /**
- * Applies collision between all particles of the emitter, depending on their radius and scale.
- */
- $.PARTICLE.BEHAVIOUR.Collision.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} [bounce=1] - Bounce coefficient to be applied. Values between 0 and 1 will cause particles
- * to lose velocity on collision, while values > 0 will cause them to gain velocity.
- */
- _initialize: function(bounce) {
- this._bounce = _default(bounce, 1);
- },
- /**
- * Gets called on every update cycle, for each particle. Particles will bounce from each other, depending on
- * their radius and scale, with smaller particles getting pushed farer, than bigger ones.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var pool = emitter.children, other, dx, dy, distance, lengthSq, factor, n1, n2, relN, m1, m2, f1, f2;
- for(var i = index + 1, max = pool.length; i < max; ++i) {
- other = pool[i];
- m1 = particle.radius * particle.scale.x;
- m2 = other.radius * other.scale.x;
- distance = m1 + m2;
- dx = other.position.x - particle.position.x;
- if(dx > distance || dx < -distance) { continue; }
- dy = other.position.y - particle.position.y;
- if(dy > distance || dy < -distance) { continue; }
- lengthSq = dx * dx + dy * dy;
- if(lengthSq <= distance * distance && lengthSq > 0) {
- factor = 1 / Math.sqrt(lengthSq);
- dx *= factor;
- dy *= factor;
- n1 = dx * particle.velocity.x + dy * particle.velocity.y;
- n2 = dx * other.velocity.x + dy * other.velocity.y;
- relN = n1 - n2;
- if(relN > 0) {
- factor = ((1 + this._bounce) * relN) / distance;
- f1 = factor * m2;
- f2 = -factor * m1;
- particle.velocity.x -= f1 * dx;
- particle.velocity.y -= f1 * dy;
- other.velocity.x -= f2 * dx;
- other.velocity.y -= f2 * dy;
- }
- }
- }
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.BoundingBox
- //=============================================================================
- /**
- * Restricts particles to a bounding box, causing them to bounce back, if they try to leave it. Particles should
- * spawn inside the box, to prevent unpredictable side effects.
- */
- $.PARTICLE.BEHAVIOUR.BoundingBox.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} x - X component of the box's upper left corner.
- * @param {number} y - Y component of the box's upper left corner.
- * @param {number} width - Width of the box.
- * @param {number} height - Height of the box.
- * @param {number} [bounce=1] - Bounce coefficient to be applied. Values between 0 and 1 will cause particles
- * to lose velocity on collision, while values > 0 will cause them to gain velocity.
- */
- _initialize: function(x, y, width, height, bounce) {
- this._left = x;
- this._top = y;
- this._right = x + width;
- this._bottom = y + height;
- this._bounce = _default(bounce, 1);
- },
- /**
- * Gets called on every update cycle, for each particle. Particles will bounce off the inside of the box, to
- * keep them inside.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- var radius = particle.radius * particle.scale.x, position;
- if(particle.velocity.x > 0 && (position = particle.position.x + radius) >= this._right) {
- particle.velocity.x *= -this._bounce;
- particle.position.x += 2 * (this._right - position);
- } else if(particle.velocity.x < 0 && (position = particle.position.x - radius) <= this._left) {
- particle.velocity.x *= -this._bounce;
- particle.position.x += 2 * (this._left - position);
- }
- if(particle.velocity.y > 0 && (position = particle.position.y + radius) >= this._bottom) {
- particle.velocity.y *= -this._bounce;
- particle.position.y += 2 * (this._bottom - position);
- } else if(particle.velocity.y < 0 && (position = particle.position.y - radius) <= this._top) {
- particle.velocity.y *= -this._bounce;
- particle.position.y += 2 * (this._top - position);
- }
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.WrapAroundBox
- //=============================================================================
- /**
- * Restricts particles to a bounding box, causing them to wrap around to the other side, if they try to leave it.
- * Particles should spawn inside the box, to prevent unpredictable side effects.
- */
- $.PARTICLE.BEHAVIOUR.WrapAroundBox.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {number} x - X component of the box's upper left corner.
- * @param {number} y - Y component of the box's upper left corner.
- * @param {number} width - Width of the box.
- * @param {number} height - Height of the box.
- */
- _initialize: function(x, y, width, height) {
- this._left = x;
- this._top = y;
- this._right = x + width;
- this._bottom = y + height;
- this._width = width;
- this._height = height;
- },
- /**
- * Gets called on every update cycle, for each particle. Particles leaving the box will appear on the other
- * side, to keep them inside.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- if (particle.velocityx > 0 && particle.position.x >= this._right) {
- particle.position.x -= this._width;
- } else if (particle.velocity.x < 0 && particle.position.x <= this._left) {
- particle.position.x += this._width;
- }
- if(particle.velocity.y > 0 && particle.position.y >= this._bottom) {
- particle.position.y -= this._height;
- } else if (particle.velocity.y < 0 && particle.position.y <= this._top) {
- particle.position.y += this._height;
- }
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.CollisionZone
- //=============================================================================
- /**
- * Applies collision between particles and the given zone, both from the inside and outside.
- */
- $.PARTICLE.BEHAVIOUR.CollisionZone.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {IAVAR.PARTICLE.Zone} zone - Zone used to apply collision.
- * @param {number} [bounce=1] - Bounce coefficient to be applied. Values between 0 and 1 will cause particles
- * to lose velocity on collision, while values > 0 will cause them to gain velocity.
- */
- _initialize: function(zone, bounce) {
- this._zone = zone;
- this._bounce = _default(bounce, 1);
- },
- /**
- * Gets called on every update cycle, for each particle. Causes particles to bounce off the given zone.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- this._zone.collide(particle, this._bounce);
- }
- });
- //=============================================================================
- // IAVRA.PARTICLE.BEHAVIOUR.DeathZone
- //=============================================================================
- /**
- * Destroys all particles in- or outside the given zone.
- */
- $.PARTICLE.BEHAVIOUR.DeathZone.prototype = _extend(Object.create($.PARTICLE.Behaviour.prototype), {
- /**
- * Initializes the behaviour.
- * @param {IAVAR.PARTICLE.Zone} zone - Zone to be used.
- * @param {boolean} [killOutside=false] - If set to true, will destroy all particles outside the zone, instead.
- */
- _initialize: function(zone, killOutside) {
- this._zone = zone;
- this._killOutside = !!killOutside;
- },
- /**
- * Gets called on every update cycle, for each particle. Destroys all particles in- or outside the given zone,
- * depending on the "killOutside" parameter.
- * @param {IAVRA.PARTICLE.Emitter} emitter - Emitter instance containing the given particle.
- * @param {IAVRA.PARTICLE.Particle} particle - Particle to be updated.
- * @param {number} index - The given particle's position inside the given emitter's children array.
- */
- update: function(emitter, particle, index) {
- if(this._zone.contains(particle.position.x, particle.position.y) !== this._killOutside) {
- particle.dead = true;
- }
- }
- });
- })(this.IAVRA || (this.IAVRA = {}));
Add Comment
Please, Sign In to add comment