UNPKG

@drincs/pixi-vn

Version:

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

1 lines 8.16 kB
import'./chunk-JMOSOAGB.mjs';import {d,e,g,f}from'./chunk-Z4GZCINS.mjs';import {GameUnifier}from'@drincs/pixi-vn/core';var h=class h{static get originalStepData(){return h._originalStepData?g(h._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){h._originalStepData=g(e);}};d(h,"_diffHistory",new e({cacheSize:5})),d(h,"_stepsInfoHistory",new e({cacheSize:5})),d(h,"_narrationHistory",new e({cacheSize:50})),d(h,"stepLimitSaved",20),d(h,"_originalStepData");var i=h;function I(m,e){let r=g(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];}):f.warn("No path found, skipping diff",e);}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 c=Array.isArray(e);for(let o in e)o in m||a.push({type:"CREATE",path:[c?+o:o],value:e[o]});return a}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 f.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 f.error("Error applying diff",s),r}else return f.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)){f.warn(`[back] The parameter steps must be a valid finite number, received: ${t}`);return}if(t<=0){f.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){f.warn("You can't go back, there is no step to go back");return}GameUnifier.runningStepsCount++;try{let a=g(this.getOldGameState(t,i.originalStepData));if(a){await GameUnifier.restoreGameStepState(a,GameUnifier.navigate);let s=GameUnifier.stepCounter-1,c=i._narrationHistory.get(s);if(c&&Object.keys(c).length===1&&c.stepIndex!==void 0){let o=i._stepsInfoHistory.get(s);if(o){let n=this.itemMapper({step:o});i._narrationHistory.set(o.index,n);}}}else f.error("Error going back");i.originalStepData=a;}catch(a){f.error("Error going back",a);}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 c=this.lastKey;(async()=>{try{let n,u;if(typeof c=="number"&&(n=i._stepsInfoHistory.get(c),u=i._narrationHistory.get(c)),i._stepsInfoHistory.set(e.index,e),e.index!==0){let f$1=x(t,s);f$1?i._diffHistory.set(e.index,f$1):f.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 c=="number"){let f=this.itemMapper({...p,step:n},{});i._narrationHistory.set(c,f);}}catch(n){f.error("Error adding history step",n);}})(),i.originalStepData=s;}itemMapper(e,r){let{step:t,choiceIndexMade:a,inputValue:s,removeDialogue:c}=e,o=t.dialogue;r&&t.isGlued&&(r.removeDialogue=true),c&&(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?.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 c=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>=c)return false;c=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 f.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:g(r),originalStepData:g(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 c=this.itemMapper({step:a,choiceIndexMade:s.choiceIndexMade,inputValue:s.inputValue,removeDialogue:s.removeDialogue},r);c&&i._narrationHistory.set(t,c);}});}async restore(e){this.clear();try{Object.hasOwn(e,"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()):f.warn("Could not import stepsHistory data, so will be ignored"),Object.hasOwn(e,"originalStepData")?i._originalStepData=e.originalStepData:f.warn("Could not import originalStepData data, so will be ignored");}catch(r){f.error("Error importing data",r);}}};var P=new b;export{i as HistoryManagerStatic,P as stepHistory};