UNPKG

@drincs/pixi-vn-json

Version:

Pixi'VN can be integrated with JSON files to create a visual novel.

3 lines (2 loc) 8.89 kB
import {createExportableElement,storage,SYSTEM_RESERVED_STORAGE_KEYS,narration,NarrationManagerStatic}from'@drincs/pixi-vn';var b="___param___";var V=class e{static _beforeToTranslate=void 0;static _translate=n=>n;static _afterToTranslate=void 0;static t(n){return Array.isArray(n)?n.map(t=>e.translate(`${t}`)):e.translate(`${n}`)}static set beforeToTranslate(n){e._beforeToTranslate=n;}static set translate(n){e._translate=n;}static get translate(){return n=>{let t=n;return e._beforeToTranslate&&(t=e._beforeToTranslate(t)),t=e._translate(t),e._afterToTranslate&&(t=e._afterToTranslate(t)),t}}static set afterToTranslate(n){e._afterToTranslate=n;}static addKey(n,t,a){let i=a.defaultValue||"empty_string";typeof t=="string"&&(t=[t]),Array.isArray(t)&&t.forEach(r=>{n[r]===void 0&&(i==="empty_string"?n[r]="":i==="copy_key"&&(e._beforeToTranslate?n[r]=e._beforeToTranslate(r):n[r]=r));});}static getConditionalsThenElse(n,t=[]){if(typeof n=="object"&&n&&"type"in n)if(n.type==="ifelse")n.then&&e.getConditionalsThenElse(n.then,t),n.else&&e.getConditionalsThenElse(n.else,t);else if(n.type==="stepswitch"){if(n.elements)if(Array.isArray(n.elements))n.elements.forEach(a=>{e.getConditionalsThenElse(a,t);});else if(n.elements.type==="ifelse"){let a=[];e.getConditionalsThenElse(n.elements,a),a.forEach(i=>{t.push(...i);});}else if(n.elements.type==="stepswitch"){let a=[];e.getConditionalsThenElse(n.elements,a),a.forEach(i=>{t.push(...i);});}else e.getConditionalsThenElse(n.elements,t);}else n.type==="resulttocombine"?(n.firstItem&&e.getConditionalsThenElse(n.firstItem,t),n.secondConditionalItem&&n.secondConditionalItem.forEach(a=>{e.getConditionalsThenElse(a,t);})):t.push(n);else n&&t.push(n);return t}static generateJsonTranslation(n,t={},a={}){return n.forEach(i=>{if(i.choices){let r=[];Array.isArray(i.choices)?r=[i.choices]:r=e.getConditionalsThenElse(i.choices),r.forEach(l=>l.forEach(f=>{if("type"in f){let c=[];f.type==="ifelse"||f.type==="stepswitch"?e.getConditionalsThenElse(f,c):c=[f],c.map(g=>e.getConditionalsThenElse(g.text)).forEach(g=>{Array.isArray(g)&&g.forEach(p=>{Array.isArray(p)?p.forEach(m=>{typeof m=="string"?e.addKey(t,m,a):e.getConditionalsThenElse(m).forEach(h=>{Array.isArray(h)?h.forEach(N=>{typeof N=="string"&&e.addKey(t,N,a);}):typeof h=="string"&&e.addKey(t,h,a);});}):typeof p=="string"&&e.addKey(t,p,a);});});}}));}if(i.dialogue){let r=[];Array.isArray(i.dialogue)?r=[i.dialogue]:r=e.getConditionalsThenElse(i.dialogue),r.forEach(l=>{typeof l=="string"?e.addKey(t,l,a):"text"in l&&Array.isArray(l.text)&&l.text.forEach(f=>{typeof f=="string"?e.addKey(t,f,a):e.getConditionalsThenElse(f).forEach(d=>{typeof d=="string"?e.addKey(t,d,a):Array.isArray(d)&&d.forEach(g=>{typeof g=="string"?e.addKey(t,g,a):e.getConditionalsThenElse(g).forEach(p=>{typeof p=="string"&&e.addKey(t,p,a);});});});});});}i.conditionalStep&&e.getConditionalsThenElse(i.conditionalStep).forEach(l=>{Array.isArray(l)?e.generateJsonTranslation(l,t,a):e.generateJsonTranslation([l],t,a);});}),t}};var E=V;function D(e,n){return Math.floor(Math.random()*(n-e+1)+e)}function R(e){if(Array.isArray(e)||!e)return e;if(e&&typeof e=="object"&&"type"in e)switch(e.type){case "resulttocombine":return L(e);case "ifelse":return s(e.condition)?s(e.then):s(e.else);case "stepswitch":let t=s(e.elements)||[];if(t.length===0){console.error("[Pixi\u2019VN Json] getValueFromConditionalStatements elements.length === 0");return}switch(e.choiceType){case "random":let a=D(0,t.length-1);return s(t[a]);case "loop":let i=NarrationManagerStatic.getCurrentStepTimesCounter(e.nestedId)-1;return i>t.length-1&&(i=i%t.length),s(t[i]);case "sequential":let r,l=NarrationManagerStatic.getCurrentStepTimesCounter(e.nestedId)-1;return e.end=="lastItem"&&(r=s(t[t.length-1])),l>t.length-1?r:s(t[l]);case "sequentialrandom":let f=NarrationManagerStatic.getRandomNumber(0,t.length-1,{nestedId:e.nestedId,onceOnly:true});if(f==null&&e.end=="lastItem"){let c=NarrationManagerStatic.getCurrentStepTimesCounterData(e.nestedId);if(!c||!c?.usedRandomNumbers){console.warn("[Pixi\u2019VN Json] getValueFromConditionalStatements randomIndexWhitExclude == undefined");return}let d=c.usedRandomNumbers[`0-${t.length-1}`];return s(t[d[d.length-1]])}if(f==null){console.warn("[Pixi\u2019VN Json] getValueFromConditionalStatements randomIndexWhitExclude == undefined");return}return s(t[f])}}return e}function S(e){if(e.conditionalStep){let n=createExportableElement(s(e.conditionalStep));if(n?.glueEnabled===void 0&&delete n?.glueEnabled,n?.goNextStep===void 0&&delete n?.goNextStep,n?.end===void 0&&delete n?.end,n?.choices===void 0&&delete n?.choices,n?.dialogue===void 0&&delete n?.dialogue,n?.labelToOpen===void 0&&delete n?.labelToOpen,n?.operations===void 0&&delete n?.operations,n){let t={...e,conditionalStep:void 0,...n};return S(t)}else storage.getFlag(SYSTEM_RESERVED_STORAGE_KEYS.ADD_NEXT_DIALOG_TEXT_INTO_THE_CURRENT_DIALOG_FLAG_KEY)&&storage.setFlag(SYSTEM_RESERVED_STORAGE_KEYS.ADD_NEXT_DIALOG_TEXT_INTO_THE_CURRENT_DIALOG_FLAG_KEY,false);}return e}function L(e){let n=e.firstItem,t=[];e.secondConditionalItem?.forEach(i=>{if(Array.isArray(i))i.forEach(r=>{let l=s(r);t.push(l);});else {let r=s(i);t.push(r);}});let a=n?[n,...t]:t;if(a.length===0){console.warn("[Pixi\u2019VN Json] combinateResult toCheck.length === 0");return}if(typeof a[0]=="string")return E.t(a).join("");if(typeof a[0]=="object"){let i=a,r,l=i.map((o,y)=>{o=S(o);let u=s(o.dialogue)||"";return y===0?(r=s(o.glueEnabled)||false,u):(typeof u=="object"&&"text"in u&&(u=u.character+": "+u.text),r===false&&(u=` `+u),r=s(o.glueEnabled)||false,u)}),f=s(l[0]),c=typeof f=="object"&&"character"in f?f.character:void 0,d=l.map(o=>{let y;o&&typeof o=="object"&&"text"in o?y=o.text:y=o;let u;return Array.isArray(y)?u=y.map(A=>{let I=s(A);return E.t(`${I}`)}).join(""):u=s(y)||"",E.t(u)}).join(""),g=i.find(o=>o.end),p=i.find(o=>o.choices),m=false,h=false;i.length>0&&(i[0].glueEnabled&&i[0].goNextStep&&i[0].dialogue===void 0&&storage.setFlag(SYSTEM_RESERVED_STORAGE_KEYS.ADD_NEXT_DIALOG_TEXT_INTO_THE_CURRENT_DIALOG_FLAG_KEY,true),m=i[i.length-1].glueEnabled,h=i.reverse().find(o=>!(o.operations&&(!o.dialogue||!o.choices)))?.goNextStep);let N=i.find(o=>o.labelToOpen),P=[];return i.forEach(o=>{o.operations&&P.forEach(y=>{P.push(y);});}),{dialogue:c?{character:c,text:d}:d,end:g?.end,choices:p?.choices,glueEnabled:m,goNextStep:h,labelToOpen:N?.labelToOpen,operations:P}}}function w(e){if(!e)return false;if(typeof e!="object"||!("type"in e))return !!e;switch(e.type){case "compare":let n=s(e.leftValue),t=s(e.rightValue);switch(e.operator){case "==":return n===t;case "!=":return n!==t;case "<":return n<t;case "<=":return n<=t;case ">":return n>t;case ">=":return n>=t;case "CONTAINS":return n.toString().includes(t.toString())}break;case "valueCondition":return !!s(e.value);case "union":return k(e)}return !!e}function G(e){if(e&&typeof e=="object"&&"type"in e&&e.type==="value"&&e.storageOperationType==="get")switch(e.storageType){case "storage":case "tempstorage":return storage.getVariable(e.key);case "flagStorage":return storage.getFlag(e.key);case "label":return narration.getTimesLabelOpened(e.label);case "choice":return narration.getTimesChoiceMade(e.index);case "logic":return s(e.operation);case "params":let n=storage.getVariable(`${b}${narration.openedLabels.length-1}`)||[];if(n&&n.length>e.key)return n[e.key];console.warn("[Pixi\u2019VN Json] getValue params not found");return}return e}function k(e){if(e.unionType==="not")return !s(e.condition);let n=e.unionType==="and";for(let t=0;t<e.conditions.length;t++)if(n=s(e.conditions[t])||false,e.unionType==="and"){if(!n)return false}else if(n)return true;return n}function H(e){let n=s(e.value),t;switch(n&&typeof n=="object"&&"type"in n?t=s(n):t=n,e.storageType){case "flagStorage":storage.setFlag(e.key,e.value);break;case "storage":storage.setVariable(e.key,t);break;case "tempstorage":storage.setTempVariable(e.key,t);break;case "params":let a=storage.getVariable(`${b}${narration.openedLabels.length-1}`)||[];a&&a.length-1>=e.key&&(a[e.key]=t),storage.setTempVariable(`${b}${narration.openedLabels.length-1}`,a);break}}function s(e){let n=R(e);if(n&&typeof n=="object"&&"type"in n)switch(n.type){case "value":return G(n);case "arithmetic":case "arithmeticsingle":return _(n);case "compare":case "valueCondition":case "union":return w(n)}return n}function _(e){let n=s(e.leftValue);switch(e.type){case "arithmetic":let t=s(e.rightValue);switch(e.operator){case "*":return n*t;case "/":return n/t;case "+":return n+t;case "-":return n-t;case "%":return n%t;case "POW":return Math.pow(n,t);case "RANDOM":return narration.getRandomNumber(n,t)}case "arithmeticsingle":switch(e.operator){case "INT":return parseInt(n);case "FLOOR":return Math.floor(n);case "FLOAT":return parseFloat(n)}break}}export{S as getConditionalStep,s as getLogichValue,H as setStorageJson};