UNPKG

starling-framework

Version:

A fast, productive library for 2D cross-platform development.

493 lines (477 loc) 24.1 kB
// Class: starling.extensions.PDParticleSystem var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $import = require("./../../import_stub").default; var $extend = require("./../../extend_stub").default; function starling_extensions_ParticleSystem() {return require("./../../starling/extensions/ParticleSystem");} function starling_extensions_PDParticle() {return require("./../../starling/extensions/PDParticle");} function haxe_xml_Fast() {return require("./../../haxe/xml/Fast");} function Xml() {return require("./../../Xml");} function Std() {return require("./../../Std");} function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");} function starling_extensions_ColorArgb() {return require("./../../starling/extensions/ColorArgb");} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));} // Constructor var PDParticleSystem = function(config,texture) { this._lifespan = 0; (starling_extensions_ParticleSystem().default).call(this,texture); this.parseConfig(config); } // Meta PDParticleSystem.__name__ = ["starling","extensions","PDParticleSystem"]; PDParticleSystem.__super__ = (starling_extensions_ParticleSystem().default); PDParticleSystem.prototype = $extend((starling_extensions_ParticleSystem().default).prototype, { createParticle: function() { return new (starling_extensions_PDParticle().default)(); }, initParticle: function(aParticle) { var particle = aParticle; var lifespan = this._lifespan + this._lifespanVariance * (Math.random() * 2.0 - 1.0); var textureWidth = this.get_texture() != null ? this.get_texture().get_width() : 16; particle.currentTime = 0.0; particle.totalTime = lifespan > 0.0 ? lifespan : 0.0; if(lifespan <= 0.0) { return; } var emitterX = this.get_emitterX(); var emitterY = this.get_emitterY(); particle.x = emitterX + this._emitterXVariance * (Math.random() * 2.0 - 1.0); particle.y = emitterY + this._emitterYVariance * (Math.random() * 2.0 - 1.0); particle.startX = emitterX; particle.startY = emitterY; var angle = this._emitAngle + this._emitAngleVariance * (Math.random() * 2.0 - 1.0); var speed = this._speed + this._speedVariance * (Math.random() * 2.0 - 1.0); particle.velocityX = speed * Math.cos(angle); particle.velocityY = speed * Math.sin(angle); var startRadius = this._maxRadius + this._maxRadiusVariance * (Math.random() * 2.0 - 1.0); var endRadius = this._minRadius + this._minRadiusVariance * (Math.random() * 2.0 - 1.0); particle.emitRadius = startRadius; particle.emitRadiusDelta = (endRadius - startRadius) / lifespan; particle.emitRotation = this._emitAngle + this._emitAngleVariance * (Math.random() * 2.0 - 1.0); particle.emitRotationDelta = this._rotatePerSecond + this._rotatePerSecondVariance * (Math.random() * 2.0 - 1.0); particle.radialAcceleration = this._radialAcceleration + this._radialAccelerationVariance * (Math.random() * 2.0 - 1.0); particle.tangentialAcceleration = this._tangentialAcceleration + this._tangentialAccelerationVariance * (Math.random() * 2.0 - 1.0); var startSize = this._startSize + this._startSizeVariance * (Math.random() * 2.0 - 1.0); var endSize = this._endSize + this._endSizeVariance * (Math.random() * 2.0 - 1.0); if(startSize < 0.1) { startSize = 0.1; } if(endSize < 0.1) { endSize = 0.1; } particle.scale = startSize / textureWidth; particle.scaleDelta = (endSize - startSize) / lifespan / textureWidth; var startColor = particle.colorArgb; var colorDelta = particle.colorArgbDelta; startColor.red = this._startColor.red; startColor.green = this._startColor.green; startColor.blue = this._startColor.blue; startColor.alpha = this._startColor.alpha; if(this._startColorVariance.red != 0) { startColor.red += this._startColorVariance.red * (Math.random() * 2.0 - 1.0); } if(this._startColorVariance.green != 0) { startColor.green += this._startColorVariance.green * (Math.random() * 2.0 - 1.0); } if(this._startColorVariance.blue != 0) { startColor.blue += this._startColorVariance.blue * (Math.random() * 2.0 - 1.0); } if(this._startColorVariance.alpha != 0) { startColor.alpha += this._startColorVariance.alpha * (Math.random() * 2.0 - 1.0); } var endColorRed = this._endColor.red; var endColorGreen = this._endColor.green; var endColorBlue = this._endColor.blue; var endColorAlpha = this._endColor.alpha; if(this._endColorVariance.red != 0) { endColorRed += this._endColorVariance.red * (Math.random() * 2.0 - 1.0); } if(this._endColorVariance.green != 0) { endColorGreen += this._endColorVariance.green * (Math.random() * 2.0 - 1.0); } if(this._endColorVariance.blue != 0) { endColorBlue += this._endColorVariance.blue * (Math.random() * 2.0 - 1.0); } if(this._endColorVariance.alpha != 0) { endColorAlpha += this._endColorVariance.alpha * (Math.random() * 2.0 - 1.0); } colorDelta.red = (endColorRed - startColor.red) / lifespan; colorDelta.green = (endColorGreen - startColor.green) / lifespan; colorDelta.blue = (endColorBlue - startColor.blue) / lifespan; colorDelta.alpha = (endColorAlpha - startColor.alpha) / lifespan; var startRotation = this._startRotation + this._startRotationVariance * (Math.random() * 2.0 - 1.0); var endRotation = this._endRotation + this._endRotationVariance * (Math.random() * 2.0 - 1.0); particle.rotation = startRotation; particle.rotationDelta = (endRotation - startRotation) / lifespan; }, advanceParticle: function(aParticle,passedTime) { var particle = aParticle; var restTime = particle.totalTime - particle.currentTime; if(restTime > passedTime) { passedTime = passedTime; } else { passedTime = restTime; } particle.currentTime += passedTime; if(this._emitterType == 1) { particle.emitRotation += particle.emitRotationDelta * passedTime; particle.emitRadius += particle.emitRadiusDelta * passedTime; particle.x = this._emitterX - Math.cos(particle.emitRotation) * particle.emitRadius; particle.y = this._emitterY - Math.sin(particle.emitRotation) * particle.emitRadius; } else { var distanceX = particle.x - particle.startX; var distanceY = particle.y - particle.startY; var distanceScalar = Math.sqrt(distanceX * distanceX + distanceY * distanceY); if(distanceScalar < 0.01) { distanceScalar = 0.01; } var radialX = distanceX / distanceScalar; var radialY = distanceY / distanceScalar; var tangentialX = radialX; var tangentialY = radialY; radialX *= particle.radialAcceleration; radialY *= particle.radialAcceleration; var newY = tangentialX; tangentialX = -tangentialY * particle.tangentialAcceleration; tangentialY = newY * particle.tangentialAcceleration; particle.velocityX += passedTime * (this._gravityX + radialX + tangentialX); particle.velocityY += passedTime * (this._gravityY + radialY + tangentialY); particle.x += particle.velocityX * passedTime; particle.y += particle.velocityY * passedTime; } particle.scale += particle.scaleDelta * passedTime; particle.rotation += particle.rotationDelta * passedTime; particle.colorArgb.red += particle.colorArgbDelta.red * passedTime; particle.colorArgb.green += particle.colorArgbDelta.green * passedTime; particle.colorArgb.blue += particle.colorArgbDelta.blue * passedTime; particle.colorArgb.alpha += particle.colorArgbDelta.alpha * passedTime; particle.color = particle.colorArgb.toRgb(); particle.alpha = particle.colorArgb.alpha; }, updateEmissionRate: function() { this.set_emissionRate(this.get_capacity() / this._lifespan); }, parseConfig: function(config) { var xml = new (haxe_xml_Fast().default)((Xml().default).parse(config).firstElement()); var config1 = xml.node; this._emitterXVariance = (Std().default).parseFloat(config1.resolve("sourcePositionVariance").att.resolve("x")); this._emitterYVariance = (Std().default).parseFloat(config1.resolve("sourcePositionVariance").att.resolve("y")); this._gravityX = (Std().default).parseFloat(config1.resolve("gravity").att.resolve("x")); this._gravityY = (Std().default).parseFloat(config1.resolve("gravity").att.resolve("y")); this._emitterType = this.getIntValue(config1.resolve("emitterType")); this._startSize = this.getFloatValue(config1.resolve("startParticleSize")); this._startSizeVariance = this.getFloatValue(config1.resolve("startParticleSizeVariance")); this._endSize = this.getFloatValue(config1.resolve("finishParticleSize")); this._emitAngle = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("angle"))); this._emitAngleVariance = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("angleVariance"))); this._startRotation = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotationStart"))); this._startRotationVariance = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotationStartVariance"))); this._endRotation = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotationEnd"))); this._endRotationVariance = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotationEndVariance"))); this._speed = this.getFloatValue(config1.resolve("speed")); this._speedVariance = this.getFloatValue(config1.resolve("speedVariance")); this._radialAcceleration = this.getFloatValue(config1.resolve("radialAcceleration")); this._radialAccelerationVariance = this.getFloatValue(config1.resolve("radialAccelVariance")); this._tangentialAcceleration = this.getFloatValue(config1.resolve("tangentialAcceleration")); this._tangentialAccelerationVariance = this.getFloatValue(config1.resolve("tangentialAccelVariance")); this._maxRadius = this.getFloatValue(config1.resolve("maxRadius")); this._maxRadiusVariance = this.getFloatValue(config1.resolve("maxRadiusVariance")); this._minRadius = this.getFloatValue(config1.resolve("minRadius")); this._rotatePerSecond = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotatePerSecond"))); this._rotatePerSecondVariance = (starling_utils_MathUtil().default).deg2rad(this.getFloatValue(config1.resolve("rotatePerSecondVariance"))); this._startColor = this.getColor(config1.resolve("startColor")); this._startColorVariance = this.getColor(config1.resolve("startColorVariance")); this._endColor = this.getColor(config1.resolve("finishColor")); this._endColorVariance = this.getColor(config1.resolve("finishColorVariance")); this._blendFactorSource = this.getBlendFunc(config1.resolve("blendFuncSource")); this._blendFactorDestination = this.getBlendFunc(config1.resolve("blendFuncDestination")); this.set_defaultDuration(this.getFloatValue(config1.resolve("duration"))); this.set_capacity(this.getIntValue(config1.resolve("maxParticles"))); if(xml.hasNode.resolve("FinishParticleSizeVariance")) { this._endSizeVariance = this.getFloatValue(config1.resolve("FinishParticleSizeVariance")); } else { this._endSizeVariance = this.getFloatValue(config1.resolve("finishParticleSizeVariance")); } if(xml.hasNode.resolve("particleLifeSpan")) { this._lifespan = Math.max(0.01,this.getFloatValue(config1.resolve("particleLifeSpan"))); } else { this._lifespan = Math.max(0.01,this.getFloatValue(config1.resolve("particleLifespan"))); } if(xml.hasNode.resolve("particleLifespanVariance")) { this._lifespanVariance = this.getFloatValue(config1.resolve("particleLifespanVariance")); } else { this._lifespanVariance = this.getFloatValue(config1.resolve("particleLifeSpanVariance")); } if(xml.hasNode.resolve("minRadiusVariance")) { this._minRadiusVariance = this.getFloatValue(config1.resolve("minRadiusVariance")); } else { this._minRadiusVariance = 0.0; } this.updateEmissionRate(); this.updateBlendMode(); }, getIntValue: function(element) { return (Std().default).parseInt(element.att.resolve("value")); }, getFloatValue: function(element) { return (Std().default).parseFloat(element.att.resolve("value")); }, getColor: function(element) { var color = new (starling_extensions_ColorArgb().default)(); color.red = (Std().default).parseFloat(element.att.resolve("red")); color.green = (Std().default).parseFloat(element.att.resolve("green")); color.blue = (Std().default).parseFloat(element.att.resolve("blue")); color.alpha = (Std().default).parseFloat(element.att.resolve("alpha")); return color; }, getBlendFunc: function(element) { var value = this.getIntValue(element); switch(value) { case 0: return "zero"; case 1: return "one"; case 768: return "sourceColor"; case 769: return "oneMinusSourceColor"; case 770: return "sourceAlpha"; case 771: return "oneMinusSourceAlpha"; case 772: return "destinationAlpha"; case 773: return "oneMinusDestinationAlpha"; case 774: return "destinationColor"; case 775: return "oneMinusDestinationColor"; default: throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("unsupported blending function: " + value)); } }, get_emitterType: function() { return this._emitterType; }, set_emitterType: function(value) { return this._emitterType = value; }, get_emitterXVariance: function() { return this._emitterXVariance; }, set_emitterXVariance: function(value) { return this._emitterXVariance = value; }, get_emitterYVariance: function() { return this._emitterYVariance; }, set_emitterYVariance: function(value) { return this._emitterYVariance = value; }, get_defaultDuration: function() { return this._defaultDuration; }, set_defaultDuration: function(value) { return this._defaultDuration = value < 0 ? 1.79e+308 : value; }, set_capacity: function(value) { (starling_extensions_ParticleSystem().default).prototype.set_capacity.call(this,value); this.updateEmissionRate(); return value; }, get_lifespan: function() { return this._lifespan; }, set_lifespan: function(value) { this._lifespan = Math.max(0.01,value); this.updateEmissionRate(); return value; }, get_lifespanVariance: function() { return this._lifespanVariance; }, set_lifespanVariance: function(value) { return this._lifespanVariance = value; }, get_startSize: function() { return this._startSize; }, set_startSize: function(value) { return this._startSize = value; }, get_startSizeVariance: function() { return this._startSizeVariance; }, set_startSizeVariance: function(value) { return this._startSizeVariance = value; }, get_endSize: function() { return this._endSize; }, set_endSize: function(value) { return this._endSize = value; }, get_endSizeVariance: function() { return this._endSizeVariance; }, set_endSizeVariance: function(value) { return this._endSizeVariance = value; }, get_emitAngle: function() { return this._emitAngle; }, set_emitAngle: function(value) { return this._emitAngle = value; }, get_emitAngleVariance: function() { return this._emitAngleVariance; }, set_emitAngleVariance: function(value) { return this._emitAngleVariance = value; }, get_startRotation: function() { return this._startRotation; }, set_startRotation: function(value) { return this._startRotation = value; }, get_startRotationVariance: function() { return this._startRotationVariance; }, set_startRotationVariance: function(value) { return this._startRotationVariance = value; }, get_endRotation: function() { return this._endRotation; }, set_endRotation: function(value) { return this._endRotation = value; }, get_endRotationVariance: function() { return this._endRotationVariance; }, set_endRotationVariance: function(value) { return this._endRotationVariance = value; }, get_speed: function() { return this._speed; }, set_speed: function(value) { return this._speed = value; }, get_speedVariance: function() { return this._speedVariance; }, set_speedVariance: function(value) { return this._speedVariance = value; }, get_gravityX: function() { return this._gravityX; }, set_gravityX: function(value) { return this._gravityX = value; }, get_gravityY: function() { return this._gravityY; }, set_gravityY: function(value) { return this._gravityY = value; }, get_radialAcceleration: function() { return this._radialAcceleration; }, set_radialAcceleration: function(value) { return this._radialAcceleration = value; }, get_radialAccelerationVariance: function() { return this._radialAccelerationVariance; }, set_radialAccelerationVariance: function(value) { return this._radialAccelerationVariance = value; }, get_tangentialAcceleration: function() { return this._tangentialAcceleration; }, set_tangentialAcceleration: function(value) { return this._tangentialAcceleration = value; }, get_tangentialAccelerationVariance: function() { return this._tangentialAccelerationVariance; }, set_tangentialAccelerationVariance: function(value) { return this._tangentialAccelerationVariance = value; }, get_maxRadius: function() { return this._maxRadius; }, set_maxRadius: function(value) { return this._maxRadius = value; }, get_maxRadiusVariance: function() { return this._maxRadiusVariance; }, set_maxRadiusVariance: function(value) { return this._maxRadiusVariance = value; }, get_minRadius: function() { return this._minRadius; }, set_minRadius: function(value) { return this._minRadius = value; }, get_minRadiusVariance: function() { return this._minRadiusVariance; }, set_minRadiusVariance: function(value) { return this._minRadiusVariance = value; }, get_rotatePerSecond: function() { return this._rotatePerSecond; }, set_rotatePerSecond: function(value) { return this._rotatePerSecond = value; }, get_rotatePerSecondVariance: function() { return this._rotatePerSecondVariance; }, set_rotatePerSecondVariance: function(value) { return this._rotatePerSecondVariance = value; }, get_startColor: function() { return this._startColor; }, set_startColor: function(value) { return this._startColor = value; }, get_startColorVariance: function() { return this._startColorVariance; }, set_startColorVariance: function(value) { return this._startColorVariance = value; }, get_endColor: function() { return this._endColor; }, set_endColor: function(value) { return this._endColor = value; }, get_endColorVariance: function() { return this._endColorVariance; }, set_endColorVariance: function(value) { return this._endColorVariance = value; } }); PDParticleSystem.prototype.__class__ = PDParticleSystem.prototype.constructor = $hxClasses["starling.extensions.PDParticleSystem"] = PDParticleSystem; // Init Object.defineProperties(PDParticleSystem.prototype,{ "defaultDuration" : { get : function () { return this.get_defaultDuration (); }, set : function (v) { return this.set_defaultDuration (v); }}, "emitterType" : { get : function () { return this.get_emitterType (); }, set : function (v) { return this.set_emitterType (v); }}, "emitterXVariance" : { get : function () { return this.get_emitterXVariance (); }, set : function (v) { return this.set_emitterXVariance (v); }}, "emitterYVariance" : { get : function () { return this.get_emitterYVariance (); }, set : function (v) { return this.set_emitterYVariance (v); }}, "lifespan" : { get : function () { return this.get_lifespan (); }, set : function (v) { return this.set_lifespan (v); }}, "lifespanVariance" : { get : function () { return this.get_lifespanVariance (); }, set : function (v) { return this.set_lifespanVariance (v); }}, "startSize" : { get : function () { return this.get_startSize (); }, set : function (v) { return this.set_startSize (v); }}, "startSizeVariance" : { get : function () { return this.get_startSizeVariance (); }, set : function (v) { return this.set_startSizeVariance (v); }}, "endSize" : { get : function () { return this.get_endSize (); }, set : function (v) { return this.set_endSize (v); }}, "endSizeVariance" : { get : function () { return this.get_endSizeVariance (); }, set : function (v) { return this.set_endSizeVariance (v); }}, "emitAngle" : { get : function () { return this.get_emitAngle (); }, set : function (v) { return this.set_emitAngle (v); }}, "emitAngleVariance" : { get : function () { return this.get_emitAngleVariance (); }, set : function (v) { return this.set_emitAngleVariance (v); }}, "startRotation" : { get : function () { return this.get_startRotation (); }, set : function (v) { return this.set_startRotation (v); }}, "startRotationVariance" : { get : function () { return this.get_startRotationVariance (); }, set : function (v) { return this.set_startRotationVariance (v); }}, "endRotation" : { get : function () { return this.get_endRotation (); }, set : function (v) { return this.set_endRotation (v); }}, "endRotationVariance" : { get : function () { return this.get_endRotationVariance (); }, set : function (v) { return this.set_endRotationVariance (v); }}, "speed" : { get : function () { return this.get_speed (); }, set : function (v) { return this.set_speed (v); }}, "speedVariance" : { get : function () { return this.get_speedVariance (); }, set : function (v) { return this.set_speedVariance (v); }}, "gravityX" : { get : function () { return this.get_gravityX (); }, set : function (v) { return this.set_gravityX (v); }}, "gravityY" : { get : function () { return this.get_gravityY (); }, set : function (v) { return this.set_gravityY (v); }}, "radialAcceleration" : { get : function () { return this.get_radialAcceleration (); }, set : function (v) { return this.set_radialAcceleration (v); }}, "radialAccelerationVariance" : { get : function () { return this.get_radialAccelerationVariance (); }, set : function (v) { return this.set_radialAccelerationVariance (v); }}, "tangentialAcceleration" : { get : function () { return this.get_tangentialAcceleration (); }, set : function (v) { return this.set_tangentialAcceleration (v); }}, "tangentialAccelerationVariance" : { get : function () { return this.get_tangentialAccelerationVariance (); }, set : function (v) { return this.set_tangentialAccelerationVariance (v); }}, "maxRadius" : { get : function () { return this.get_maxRadius (); }, set : function (v) { return this.set_maxRadius (v); }}, "maxRadiusVariance" : { get : function () { return this.get_maxRadiusVariance (); }, set : function (v) { return this.set_maxRadiusVariance (v); }}, "minRadius" : { get : function () { return this.get_minRadius (); }, set : function (v) { return this.set_minRadius (v); }}, "minRadiusVariance" : { get : function () { return this.get_minRadiusVariance (); }, set : function (v) { return this.set_minRadiusVariance (v); }}, "rotatePerSecond" : { get : function () { return this.get_rotatePerSecond (); }, set : function (v) { return this.set_rotatePerSecond (v); }}, "rotatePerSecondVariance" : { get : function () { return this.get_rotatePerSecondVariance (); }, set : function (v) { return this.set_rotatePerSecondVariance (v); }}, "startColor" : { get : function () { return this.get_startColor (); }, set : function (v) { return this.set_startColor (v); }}, "startColorVariance" : { get : function () { return this.get_startColorVariance (); }, set : function (v) { return this.set_startColorVariance (v); }}, "endColor" : { get : function () { return this.get_endColor (); }, set : function (v) { return this.set_endColor (v); }}, "endColorVariance" : { get : function () { return this.get_endColorVariance (); }, set : function (v) { return this.set_endColorVariance (v); }}}); // Statics PDParticleSystem.EMITTER_TYPE_GRAVITY = 0 PDParticleSystem.EMITTER_TYPE_RADIAL = 1 // Export exports.default = PDParticleSystem;