xxxxxxxxxx
var thestuff;
var s = 0; // which scene am i on?
var thediv;
function preload() {
thestuff = loadJSON("adventure2.json");
}
function setup() {
createCanvas(windowWidth, windowHeight);
thediv = createDiv('this is a test');
thediv.style('font-size', '48px');
thediv.style('font-family', 'Courier');
thediv.position(50, 50);
thediv.size(width-50, height-50);
noLoop(); // turn of auto drawing
redraw();
}
function draw() {
background(255);
fill(255);
// draw the main text:
var t = thestuff.scenes[s].text;
// draw the picture if it exists:
if(thestuff.scenes[s].image) {
t+= "<br><img src="+thestuff.scenes[s].image+" width=20%>";
}
// render the options:
for(let i = 0;i<thestuff.scenes[s].choices.length;i++)
{
t+="<br>"+"<b>"+(i+1)+"</b>" + ": " + thestuff.scenes[s].choices[i].text;
}
// play the sound:
if(thestuff.scenes[s].sound) {
let a="<audio autoplay><source src="+thestuff.scenes[s].sound+" type=\"audio/mpeg\"></audio>";
t+=a;
}
// show the html:
thediv.elt.innerHTML = t;
// do the fancy p5 code:
if(thestuff.scenes[s].code) {
let c = thestuff.scenes[s].code;
eval(c); // this is crazy insecure but go for it anyway
}
}
function keyPressed() {
for(let i = 0;i<thestuff.scenes[s].choices.length;i++)
{
if(key==i+1)
{
s = thestuff.scenes[s].choices[i].branch;
}
}
redraw();
}