@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
1 lines • 4.87 kB
JavaScript
!function(){"use strict";var t,e,s=function(){},i=0;class r{constructor(t,e){this.uuid=i++,this.key=e.key,this.system=e.system,this.parent=t,this.valid=!1,this.duration=e.duration||1e3,this._time=0,this.running=!1,this.repeat=e.repeat||0,this._reverse=e.reverse||!1,this.yoyo=e.yoyo||!1,this.repeatDelay=e.repeatDelay||0,this._delay=0,this.onStart=e.onStart||s,this.onStop=e.onStop||s,this.onComplete=e.onComplete||s,this.onRepeat=e.onRepeat||s,e.delay&&(this._delay=-e.delay),this._onStartFired=!1}setValue(){}reverse(){this._reverse=!this._reverse}start(){this.running||(this.running=!0,this.system&&this.system.addAnim(this))}pause(){this.running&&(this.running=!1,this.system&&this.system.removeAnim(this))}stop(){this.running=!1,this._time=0,this.setValue(0),this.system&&this.system.removeAnim(this),this.onStop(this.parent)}update(t){if(!this.parent.worldTransform||!this.valid)return!1;if(this.running){if(this._delay<0)return this._delay+=t,!0;if(this._onStartFired||(this._onStartFired=!0,this.onStart(this.parent)),this._time+=t,this._time>this.duration){if(!(this.repeat>0||-1==this.repeat))return this.onComplete(this.parent),this._time=this.duration,this.running=!1,!1;do{this._time-=this.duration}while(this._time>this.duration);this.repeat>0&&this.repeat--,this.repeatDelay&&(this._delay=-this.repeatDelay),this.yoyo&&this.reverse(),this.onRepeat(this.parent)}var e=this._time/this.duration;this._reverse&&(e=1-e),this.setValue(e)}return!0}}class a extends r{constructor(t,e){super(t,e),this.frames=[],this.currentIndex=0;var s=e.data;if('string'==typeof s&&(s={key:s}),Array.isArray(s))this.frames=s;else if(s.key){var i=Tiny.Cache.texture[s.key+'.0'];if(i)for(var r=s.from||0,a=s.to||i.lastFrame,n=r;n<=a;n++)this.frames.push(i.key+'.'+n)}this.frames.length>0&&(e.fps&&(this.duration=1e3/e.fps*this.frames.length),this.valid=!0)}reverse(){super.reverse(),this.repeatDelay||(this._time+=this.duration/this.frames.length)}setValue(t){var e=this.frames.length*t|0;if(e>this.frames.length-1&&(e=this.frames.length-1),e!=this.currentIndex){this.currentIndex=e;var s=this.frames[e],i=Tiny.Cache.texture[s];this.parent.setTexture(i)}}}r.spritesheet=a;class n extends r{constructor(t,e){super(t,e),this.setFrames(e.data),e.duration||(this.duration=this._duration)}setFrames(t){var e=this.parent;this.setPose();var s=e.anim.data;e.anim.pose=e.anim.pose||{};var i=e.anim.pose;if(s&&t){var r=t.values,a=t.times,n=a[a.length-1];if(n){if(this._duration=1e3*n,e.anim.cache[this.key])return this.frames=e.anim.cache[this.key],void(this.valid=!0);this.frames=[];for(var h={},m=0;m<a.length;m++){var o,u=a[m],l=r[m],p=u/n,f=[];if(o=0===m?p:p-this.frames[m-1].trigger,this.frames.push({trigger:p,paths:f,prevDelta:o}),l&&0!==l.length)for(let t=0;t<l.length;t++){var d,v,y=l[t],c=y.name,g=e;if(c?g=s[c]:c='__default__',g)for(var _ in y)if('name'!=_)if('object'==typeof y[_])for(var k in y[_])null==(v=h[c+_+k])&&(i[d=c+'.'+_]=i[d]||{},null==(v=i[d][k])&&(v=g[_][k],i[d][k]=v)),f.push({obj:g[_],name:k,val:y[_][k],prev:v}),h[c+_+k]=y[_][k];else null==(v=h[c+_])&&null==(v=i[d=c+'.'+_])&&(v=g[_],i[d]=v),f.push({obj:g,name:_,val:y[_],prev:v}),h[c+_]=y[_]}}0!==Object.keys(h).length&&(this.lastIndex=this._reverse?this.frames.length-1:0,this.valid=!0,e.anim.cache[this.key]=this.frames)}}}setPose(t){var e=this.parent;t&&(e.anim.pose=t),t=e.anim.pose;var s=e.anim.data;for(var i in t){var r=i.split('.'),a=s[r[0]];a||(a=e);var n=r[1];if('object'==typeof t[i])for(var h in t[i])a[n][h]=t[i][h];else a[n]=t[i]}}_setValue(s,i){if(0!=(e=s.paths.length))for(;e--;)(t=s.paths[e]).obj[t.name]=t.prev+(t.val-t.prev)*i}setValue(t){for(var e=0,s=this.frames[e];s.trigger<t;)s=this.frames[++e];e!=this.lastIndex&&(this.lastIndex=e,this._reverse?this._setValue(this.frames[this.lastIndex],0):this._setValue(this.frames[this.lastIndex],1)),this._setValue(s,1-(s.trigger-t)/s.prevDelta)}}r.keyframes=n;Tiny.registerSystem('anim',class{constructor(t){this.game=t,this.anims={},this.cache={}}create(t){t.system=this,t.type=t.type||'spritesheet',this.cache[t.key]=t}removeAnim(t){var e=t.uuid;delete this.anims[e]}addAnim(t){var e=t.uuid;this.anims[e]=t}add(t,e){let s=this;t.anim={cache:{},data:e||{},current:null,system:this},t.play=function(e){t.anim.current&&s.removeAnim(t.anim.current),'string'==typeof e&&(e={key:e});var i=s.cache[e.key],a=r[i.type];for(var n in i)void 0===e[n]&&(e[n]=i[n]);var h=new a(t,e);return t.anim.current=h,h.start(),h},t.resume=function(){t.anim.current&&t.anim.current.start()},t.pause=function(){t.anim.current&&t.anim.current.pause()},t.stop=function(){t.anim.current&&t.anim.current.stop()}}update(t){var e=Object.keys(this.anims);if(0!==e.length){for(var s=0;s<e.length;s++){var i=this.anims[e[s]];i&&!1===i.update(t)&&delete this.anims[e[s]]}return!0}}destroy(t){this.cache={},this.anims={}}}),Tiny.Anim=r,Tiny.SpritesheetAnim=a,Tiny.KeyframesAnim=n}();