From 6d5a5ac81f5f1bce9cd77064d448e5070f39dcb8 Mon Sep 17 00:00:00 2001 From: Mihir Jichkar Date: Sun, 9 Aug 2020 23:19:03 +0530 Subject: [PATCH] evened downloadable and editor terps --- downloadable/if_r-terp.js | 57 ++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/downloadable/if_r-terp.js b/downloadable/if_r-terp.js index 402153a..ce46209 100644 --- a/downloadable/if_r-terp.js +++ b/downloadable/if_r-terp.js @@ -164,12 +164,33 @@ data-if_r-variables="${variables.join(", ")}">${choiceText} if (vars) { vars.forEach(val => { let varName = val.replace(/\$\{/, "").replace(/\}/, "").trim(); + let randomRegex = /random\([0-9]*,[0-9]*\)/; if (IF.story.variables[varName] || IF.story.variables[varName] === 0) { - if(IF.DEBUG) console.log("Replacing:", varName, " with", IF.story.variables[varName]); + + if(IF.DEBUG) + console.log("Replacing:", varName, " with", IF.story.variables[varName]); + + let valReplace = IF.story.variables[varName]; + + let arr = null; + if (typeof valReplace === 'string') + arr = (IF.story.variables[varName]).match(randomRegex); + if (arr && arr.length > 0) { + let [num1, num2] = arr[0].match(/[0-9]*/g); + num1 = parseInt(num1); + num2 = parseInt(num2); + + valReplace = + Math.floor( + Math.random() + * (Math.max(num2, num1) - Math.min(num2, num1))) + + Math.min(num2, num1); + } + text = text.replace( new RegExp("\\$\\{\\s*" + varName + "\\s*\\}"), - IF.story.variables[varName] + valReplace ); } else { if(IF.DEBUG) @@ -411,7 +432,25 @@ data-if_r-variables="${variables.join(", ")}">${choiceText} }, doSceneActions: function(scene) { - console.log("Doing relevant scene actions..."); + if (IF.DEBUG) console.log("Doing relevant scene actions..."); + + IF.state.scene = scene; + + let { music } = IF.state.scene; + + if (music) { + try { + let url = new URL(music); + document.querySelector("#if_r-audio-source").src = music; + let player = document.querySelector("#if_r-audio-player"); + player.load(); + player.play(); + // .then(d => console.log("Playing audio now.")) + // .catch(e => console.log(e)); + } catch(e) { + if(IF.DEBUG) console.log("Invalid URL."); + } + } }, resetStory: function () { @@ -470,10 +509,14 @@ data-if_r-variables="${variables.join(", ")}">${choiceText} $main.innerHTML = `
- × - Restart - Undo - ${`Stats` /* does nothing */} + × + Restart + Undo + ${`Stats` /* does nothing */} +