@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.34 kB
JavaScript
import{extends as t}from"../../../../_virtual/_rollupPluginBabelHelpers.js";import{AnimSnapshot as e}from"./anim-snapshot.js";class s{constructor(t,s,i,r,n,h){this._name=t.name,this._track=t,this._snapshot=new e(t),this._playing=r,this._time=s,this._speed=i,this._loop=n,this._blendWeight=1,this._blendOrder=0,this._eventHandler=h,this.alignCursorToCurrentTime()}set name(t){this._name=t}get name(){return this._name}set track(t){this._track=t,this._snapshot=new e(t)}get track(){return this._track}get snapshot(){return this._snapshot}set time(t){this._time=t,this.alignCursorToCurrentTime()}get time(){return this._time}set speed(t){const e=Math.sign(t)!==Math.sign(this._speed);this._speed=t,e&&this.alignCursorToCurrentTime()}get speed(){return this._speed}set loop(t){this._loop=t}get loop(){return this._loop}set blendWeight(t){this._blendWeight=t}get blendWeight(){return this._blendWeight}set blendOrder(t){this._blendOrder=t}get blendOrder(){return this._blendOrder}set eventCursor(t){this._eventCursor=t}get eventCursor(){return this._eventCursor}get eventCursorEnd(){return this.isReverse?0:this._track.events.length-1}get nextEvent(){return this._track.events[this._eventCursor]}get isReverse(){return this._speed<0}nextEventAheadOfTime(t){return!!this.nextEvent&&(this.isReverse?this.nextEvent.time<=t:this.nextEvent.time>=t)}nextEventBehindTime(t){return!!this.nextEvent&&(t===this.track.duration?this.isReverse?this.nextEvent.time>=t:this.nextEvent.time<=t:this.isReverse?this.nextEvent.time>t:this.nextEvent.time<t)}resetEventCursor(){this._eventCursor=this.isReverse?this._track.events.length-1:0}moveEventCursor(){this._eventCursor+=this.isReverse?-1:1,this._eventCursor>=this.track.events.length?this._eventCursor=0:this._eventCursor<0&&(this._eventCursor=this.track.events.length-1)}clipFrameTime(t){const e=s.eventFrame;e.start=0,e.end=t,e.residual=0,this.isReverse?t<0&&(e.start=this.track.duration,e.end=0,e.residual=t+this.track.duration):t>this.track.duration&&(e.start=0,e.end=this.track.duration,e.residual=t-this.track.duration)}alignCursorToCurrentTime(){for(this.resetEventCursor();this.nextEventBehindTime(this._time)&&this._eventCursor!==this.eventCursorEnd;)this.moveEventCursor()}fireNextEvent(){this._eventHandler.fire(this.nextEvent.name,t({track:this.track},this.nextEvent)),this.moveEventCursor()}fireNextEventInFrame(t,e){return!(!this.nextEventAheadOfTime(t)||!this.nextEventBehindTime(e))&&(this.fireNextEvent(),!0)}activeEventsForFrame(t,e){const i=s.eventFrame;this.clipFrameTime(e);const r=this.eventCursor;for(;this.fireNextEventInFrame(t,i.end)&&r!==this.eventCursor;);this.loop&&Math.abs(i.residual)>0&&this.activeEventsForFrame(i.start,i.residual)}progressForTime(t){return t*this._speed/this._track.duration}_update(t){if(this._playing){let e=this._time;const s=this._track.duration,i=this._speed,r=this._loop;this._track.events.length>0&&s>0&&this.activeEventsForFrame(e,e+i*t),e+=i*t,i>=0?e>s&&(r?e=e%s||0:(e=this._track.duration,this.pause())):e<0&&(r?e=s+(e%s||0):(e=0,this.pause())),this._time=e}this._time!==this._snapshot._time&&this._track.eval(this._time,this._snapshot)}play(){this._playing=!0,this._time=0}stop(){this._playing=!1,this._time=0}pause(){this._playing=!1}resume(){this._playing=!0}reset(){this._time=0}}s.eventFrame={start:0,end:0,residual:0};export{s as AnimClip};