UNPKG

@drincs/pixi-vn

Version:

Pixi'VN is a npm package that provides various features for creating visual novels.

2 lines 8.33 kB
import'./chunk-52EK6LYW.mjs';import {a}from'./chunk-EC6URLQW.mjs';import {d,k,j as j$1}from'./chunk-FIGGUFAM.mjs';import {GameUnifier}from'@drincs/pixi-vn/core';var _={Date:true,RegExp:true,String:true,Number:true};function x(m,e,r={cyclesFix:true},t=[]){let a=[],s=Array.isArray(m);for(let o in m){let n=m[o],u=s?+o:o;if(!(o in e)){a.push({type:"REMOVE",path:[u],oldValue:m[o]});continue}let p=e[o],d=typeof n=="object"&&typeof p=="object"&&Array.isArray(n)===Array.isArray(p);n&&p&&d&&!_[Object.getPrototypeOf(n)?.constructor?.name]&&(!r.cyclesFix||!t.includes(n))?a.push.apply(a,x(n,p,r,r.cyclesFix?t.concat([n]):[]).map(f=>(f.path.unshift(u),f))):n!==p&&!(Number.isNaN(n)&&Number.isNaN(p))&&!(d&&(isNaN(n)?n+""==p+"":+n==+p))&&a.push({path:[u],type:"CHANGE",value:p,oldValue:n});}let l=Array.isArray(e);for(let o in e)o in m||a.push({type:"CREATE",path:[l?+o:o],value:e[o]});return a}function I(m,e){let r=k(m);return e.length>1&&"type"in e[0]&&(e=e.slice().reverse()),e.forEach(t=>{v(r,t);}),r}function v(m,e){let r=m;e.path&&e.path.length>0?e.path.forEach((t,a)=>{if(e.path&&a===e.path.length-1)switch(e.type){case "CHANGE":case "REMOVE":r[t]=e.oldValue;break;case "CREATE":Array.isArray(r)&&typeof t=="number"?r.splice(t,1):delete r[t];break}else r=r[t];}):j$1.warn("No path found, skipping diff",e);}var g=class g{static get originalStepData(){return g._originalStepData?k(g._originalStepData):{path:"",storage:{},canvas:{elementAliasesOrder:[],elements:{},stage:{},tickers:{},tickersSteps:{},tickersOnPause:{},tickersToCompleteOnStepEnd:{tikersIds:[],stepAlias:[]}},sound:{soundAliasesOrder:[],soundsPlaying:{},playInStepIndex:{},filters:void 0},labelIndex:-1,openedLabels:[]}}static set originalStepData(e){g._originalStepData=k(e);}};d(g,"_diffHistory",new a({cacheSize:5})),d(g,"_stepsInfoHistory",new a({cacheSize:5})),d(g,"_narrationHistory",new a({cacheSize:50})),d(g,"stepLimitSaved",20),d(g,"_originalStepData");var i=g;var b=class{get stepsInfoMap(){return i._stepsInfoHistory}get diffMap(){return i._diffHistory}get narrationMap(){return i._narrationHistory}get size(){return i._stepsInfoHistory.size}get lastKey(){return this.size===0?null:Math.max(...Array.from(this.keys()))}keys(){return i._stepsInfoHistory.keys()}get(e){let r=i._narrationHistory.get(e);if(!(r&&Object.keys(r).length===1&&r.stepIndex!==void 0))return r}delete(e){i._stepsInfoHistory.delete(e),i._diffHistory.delete(e),i._narrationHistory.delete(e);}getOldGameState(e,r){if(e<=0||this.size==0)return r;let t=this.lastKey;if(typeof t!="number")return j$1.warn("You can't go back, there is no step to go back"),r;let a=i._diffHistory.get(t);if(a)try{let s=I(r,a);return GameUnifier.stepCounter=t,this.delete(t),this.getOldGameState(e-1,s)}catch(s){return j$1.error("Error applying diff",s),r}else return j$1.warn("No diff found for the last step, cannot go back"),r}async back(e,r={}){let{steps:t=1}=r;if(!Number.isFinite(t)){j$1.warn(`[back] The parameter steps must be a valid finite number, received: ${t}`);return}if(t<=0){j$1.warn(`[back] The parameter steps must be greater than 0, received: ${t}`);return}if(GameUnifier.runningStepsCount>0){GameUnifier.increaseBackRequest(t);return}if(i._diffHistory.size<1){j$1.warn("You can't go back, there is no step to go back");return}GameUnifier.runningStepsCount++;try{let a=k(this.getOldGameState(t,i.originalStepData));if(a){await GameUnifier.restoreGameStepState(a,GameUnifier.navigate);let s=GameUnifier.stepCounter-1,l=i._narrationHistory.get(s);if(l&&Object.keys(l).length===1&&l.stepIndex!==void 0){let o=i._stepsInfoHistory.get(s);if(o){let n=this.itemMapper({step:o});i._narrationHistory.set(o.index,n);}}}else j$1.error("Error going back");i.originalStepData=a;}catch(a){j$1.error("Error going back",a);}finally{if(GameUnifier.runningStepsCount--,GameUnifier.runningStepsCount===0&&GameUnifier.backRequestsCount!==0)return await GameUnifier.processNavigationRequests(e)}}add(e,r={}){let t=i.originalStepData,{ignoreSameStep:a}=r,s=GameUnifier.currentGameStepState;if(!a&&this.isSameStep(t,s))return;let l=this.lastKey;(async()=>{try{let n,u;if(typeof l=="number"&&(n=i._stepsInfoHistory.get(l),u=i._narrationHistory.get(l)),i._stepsInfoHistory.set(e.index,e),e.index!==0){let f=x(t,s);f?i._diffHistory.set(e.index,f):j$1.warn("It was not possible to create the difference between the two steps");}let p={},d=this.itemMapper({step:e},p);if(i._narrationHistory.set(e.index,d),n&&u&&typeof l=="number"){let f=this.itemMapper({...p,step:n},{});i._narrationHistory.set(l,f);}}catch(n){j$1.error("Error adding history step",n);}})(),i.originalStepData=s;}itemMapper(e,r){let{step:t,choiceIndexMade:a,inputValue:s,removeDialogue:l}=e,o=t.dialogue;r&&t.isGlued&&(r.removeDialogue=true),l&&(o=void 0);let n=t.choices;if(r&&t.currentLabel&&t.choiceIndexMade!==void 0&&(r.choiceIndexMade=t.choiceIndexMade),t.inputValue&&r&&(r.inputValue=t.inputValue),o||n||s){let u=n?.map((p,d)=>{let f=false;return p.oneTime&&t.alreadyMadeChoices&&t.alreadyMadeChoices.includes(d)&&(f=true),{text:p.text,type:p.type,isResponse:false,hidden:f}});if(u){if(u.every(p=>p.hidden)){let p=u.find(d=>d.hidden===false);p&&(p.hidden=false);}a!==void 0&&(u[a].isResponse=true);}return {dialogue:o?{...o,character:o.character?GameUnifier.getCharacter(o.character)||o.character:void 0}:void 0,playerMadeChoice:typeof a=="number",choices:u,stepIndex:t.index,inputValue:s}}return {stepIndex:t.index}}get narrativeHistory(){let e=[];return Array.from(this.keys()).sort((t,a)=>t-a).forEach(t=>{let a=this.get(t);a&&e.push(a);}),e}get currentLabelHistory(){let e=[],r=Array.from(this.keys()).sort((n,u)=>u-n);if(r.length===0)return e;let t=r[0];if(typeof t!="number")return e;let a=i._stepsInfoHistory.get(t);if(!a)return e;let s=a.openedLabels;if(!s||!Array.isArray(s)||s.length===0)return e;let l=s[0].currentStepIndex,o=s[0].label;return r.every((n,u)=>{let p=this.get(n);if(p){if(u===0)return e.push(p),true;let d=i._stepsInfoHistory.get(n);if(!d)return true;let f=d.openedLabels;if(!f||!Array.isArray(f)||f.length===0||f[0].label!==o||f[0].currentStepIndex>=l)return false;l=f[0].currentStepIndex,e.push(p);}return true}),e.reverse()}removeNarrativeHistory(e){if(e){let r=Array.from(this.keys()).sort((t,a)=>t-a);r=r.slice(0,e),r.forEach(t=>{i._narrationHistory.delete(t),i._stepsInfoHistory.delete(t),i._diffHistory.delete(t);});}else i._stepsInfoHistory.clear(),i._diffHistory.clear(),i._narrationHistory.clear();}get canGoBack(){let e=this.lastKey;return typeof e!="number"?false:i._diffHistory.has(e)}blockGoBack(){GameUnifier.runningStepsCount===0&&i._diffHistory.clear();}isSameStep(e,r){if(e.openedLabels.length===r.openedLabels.length)try{let t=JSON.stringify(e.openedLabels),a=JSON.stringify(r.openedLabels);if(t===a&&e.path===r.path&&e.labelIndex===r.labelIndex)return !0}catch(t){return j$1.error("Error comparing opened labels",t),true}return false}clear(){i._stepsInfoHistory.clear(),i._diffHistory.clear(),i._narrationHistory.clear(),i._originalStepData=void 0;}get stepLimitSaved(){return i.stepLimitSaved}set stepLimitSaved(e){i.stepLimitSaved=e;}export(){let e=Array.from(this.keys()).sort((t,a)=>t-a);e.length>this.stepLimitSaved&&(e=e.slice(e.length-this.stepLimitSaved));let r=[];return e.forEach(t=>{let a=i._stepsInfoHistory.get(t);if(a){let s=i._diffHistory.get(t);r.push({...a,diff:s});}}),{stepsHistory:k(r),originalStepData:k(i._originalStepData)}}restoreNarrativeHistory(){let e=Array.from(this.keys()).sort((t,a)=>t-a),r={};e.forEach(t=>{let a=i._stepsInfoHistory.get(t);if(a){let s={...r};r={};let l=this.itemMapper({step:a,choiceIndexMade:s.choiceIndexMade,inputValue:s.inputValue,removeDialogue:s.removeDialogue},r);l&&i._narrationHistory.set(t,l);}});}async restore(e){this.clear();try{e.hasOwnProperty("stepsHistory")?(e.stepsHistory.forEach(t=>{t.diff&&i._diffHistory.set(t.index,t.diff);let a={...t,diff:void 0};i._stepsInfoHistory.set(t.index,a);}),this.restoreNarrativeHistory()):j$1.warn("Could not import stepsHistory data, so will be ignored"),e.hasOwnProperty("originalStepData")?i._originalStepData=e.originalStepData:j$1.warn("Could not import originalStepData data, so will be ignored");}catch(r){j$1.error("Error importing data",r);}}};var j=new b;export{i as HistoryManagerStatic,j as stepHistory};//# sourceMappingURL=history.mjs.map //# sourceMappingURL=history.mjs.map