@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
1 lines • 5.67 kB
JavaScript
!function(){"use strict";var t=function(t){this.game=t,this.list=[]};(t.prototype={add:function(t){return t.system=this,this.list.push(t),t},remove:function(t){var i=this.list.indexOf(t);if(i>-1)return(t=this.list.splice(i,1)).system=null,t},update:function(t){for(var i=this.list.length;i--;)this.list[i].update(t)},destroy:function(){this.list.length=0}}).constructor=t,Tiny.registerSystem('particles',t);var i=function(t){Tiny.BaseObject2D.call(this),this.parent=t,this.anchor=new Tiny.Vec2,this.texture={valid:!1},this._frame=0,this.lifespan=0};(i.prototype=Object.create(Tiny.BaseObject2D.prototype)).constructor=i,i.prototype.setTexture=function(t,i){if('string'==typeof t){var e=t;null!=i&&(e=e+'.'+i),(t=Tiny.Cache.texture[e])||(t=new Tiny.Texture(e))}this.texture=t},Object.defineProperty(i.prototype,'frameName',{get:function(){return this.texture.frame.name},set:function(t){this.texture.frame.name&&this.setTexture(Tiny.Cache.texture[this.texture.key+'.'+t])}}),i.prototype.drawTexture=function(t){var i=this.anchor.x*-this.texture.frame.width,e=this.anchor.y*-this.texture.frame.height,s=this.texture.base.resolution/t.resolution;t.context.drawImage(this.texture.base.source,this.texture.crop.x,this.texture.crop.y,this.texture.crop.width,this.texture.crop.height,i/s,e/s,this.texture.crop.width/s,this.texture.crop.height/s)},i.prototype.reset=function(t,i){this.x=t||0,this.y=i||0,this.alpha=1,this.scale.set(1)},i.prototype.update=function(t,i){},i.prototype.onEmit=function(){},i.prototype.draw=function(t){},i.prototype._update=function(t){return!1!==this.visible&&(this.lifespan-=t,this.lifespan<=0?(this.visible=!1,!1):void this.update(this.lifespan,t))},i.prototype.renderCanvas=function(t){!1!==this.visible&&0!==this.alpha&&(this.updateTransform(),t.context.globalAlpha=this.worldAlpha,t.context.setTransform(this.worldTransform.a,this.worldTransform.b,this.worldTransform.c,this.worldTransform.d,this.worldTransform.tx*t.resolution,this.worldTransform.ty*t.resolution),this.texture.valid?this.drawTexture(t):this.draw(t.context,t.resolution))};var e=function(t){Tiny.Object2D.call(this),this.anchor=new Tiny.Vec2,this.maxParticles=t,this.particles=[],this.pattern=i,this.fillStyle='#f54545',this.particleAnchor=new Tiny.Vec2(.5,.5),this.on=!1,this._timer=0,this._quantity=0,this._counter=0,this._flowQuantity=0,this._flowTotal=0,this.blendMode='source-over'};(e.prototype=Object.create(Tiny.Object2D.prototype)).constructor=e,Object.defineProperty(e.prototype,'width',{get:function(){return this._width},set:function(t){this._width=t}}),Object.defineProperty(e.prototype,'height',{get:function(){return this._height},set:function(t){this._height=t}}),e.prototype.makeParticles=function(t,i){var e;void 0===i&&(i=this.maxParticles);var s=this.particles.length;for(i>this.maxParticles&&(this.maxParticles=i);s<i;)e=new this.pattern(this),t&&e.setTexture(t),e.visible=!1,e.anchor.set(this.particleAnchor.x,this.particleAnchor.y),this.particles.push(e),s++},e.prototype.flow=function(t,i,e,s,r){return void 0!==e&&0!==e||(e=1),void 0===s&&(s=-1),void 0===r&&(r=!0),e>this.maxParticles&&(e=this.maxParticles),this._counter=0,this._flowQuantity=e,this._flowTotal=s,r?(this.start(!0,t,i,e),this._counter+=e,this.on=!0,this._timer=i):this.start(!1,t,i,e),this},e.prototype.explode=function(t,i){return this._flowTotal=0,void 0===i&&(i=this.particles.length),this.start(!0,t,0,i,!1),this},e.prototype.start=function(t,i,e,s){if(void 0===t&&(t=!0),void 0===i&&(i=0),null==e&&(e=250),void 0===s&&(s=0),s>this.maxParticles&&(s=this.maxParticles),this.visible=!0,this.lifespan=i,this.frequency=e,t)for(var r=0;r<s;r++)this.emitParticle();else this.on=!0,this._quantity=s,this._counter=0,this._timer=e;return this},e.prototype.emitParticle=function(t,i){void 0===t&&(t=null),void 0===i&&(i=null);for(var e,s=null,r=this.particles.length;r--;)if(!this.particles[r].visible){s=this.particles[r];break}if(null===s)return!1;var o=0,h=0;return null!==t?o=t:this._width>1&&(e=this._width/2,o=Tiny.rnd(-e,e)),null!==i?h=i:this._height>1&&(e=this._height/2,h=Tiny.rnd(-e,e)),s.reset(o,h),s.rotation=0,s.lifespan=this.lifespan,s.onEmit(),s.visible=!0,!0},e.prototype.destroy=function(){this.system&&this.system.remove(this),Tiny.Object2D.prototype.destroy.call(this)},e.prototype.update=function(t){if(!1!==this.visible){if(this.on&&(this._timer-=t,this._timer<=0))if(this._timer=this.frequency,0!==this._flowTotal)if(this._flowQuantity>0){for(var i=0;i<this._flowQuantity;i++)if(this.emitParticle()&&(this._counter++,-1!==this._flowTotal&&this._counter>=this._flowTotal)){this.on=!1;break}}else this.emitParticle()&&(this._counter++,-1!==this._flowTotal&&this._counter>=this._flowTotal&&(this.on=!1));else this.emitParticle()&&(this._counter++,this._quantity>0&&this._counter>=this._quantity&&(this.on=!1));for(i=this.particles.length;i--;)this.particles[i]._update(t)}},e.prototype.renderCanvas=function(t){if(!1!==this.visible&&0!==this.alpha)if(this.blendMode!==t.currentBlendMode&&(t.currentBlendMode=this.blendMode,t.context.globalCompositeOperation=t.blendModes[t.currentBlendMode]),this._cacheAsBitmap)this._renderCachedSprite(t);else{this._mask&&t.maskManager.pushMask(this._mask,t),t.context.fillStyle=this.fillStyle,t.context.globalAlpha=this.worldAlpha,t.context.setTransform(this.worldTransform.a,this.worldTransform.b,this.worldTransform.c,this.worldTransform.d,this.worldTransform.tx*t.resolution,this.worldTransform.ty*t.resolution);var i=0;for(i=0;i<this.particles.length;i++)this.particles[i].renderCanvas(t);for(i=0;i<this.children.length;i++)this.children[i].renderCanvas(t);this._mask&&t.maskManager.popMask(t)}},Tiny.Particle=i,Tiny.Emitter=e}();