now uses json files for dialogue

This commit is contained in:
Yannik
2023-05-26 17:19:55 +02:00
parent 2bb288b827
commit e97816759d
3 changed files with 24 additions and 6 deletions

View File

@@ -10,7 +10,7 @@
<title>TextHorror</title> <title>TextHorror</title>
</head> </head>
<body onpageshow="typeWriter()" onclick="mouseClick()"> <body onpageshow="load()" onclick="mouseClick()">
<div id="bubble"></div> <div id="bubble"></div>
<img id="triangle" src="text_horror/assets/triangle.png"> <img id="triangle" src="text_horror/assets/triangle.png">
<p class="thx-for-playing">Thanks for playing</p> <p class="thx-for-playing">Thanks for playing</p>

View File

@@ -1,12 +1,23 @@
var speed = 50; var speed = 50;
var dlgLines = ["Hello welcome to TextHorror", "This is the second line"]; var typeindex = 0;
var dlgFile = {};
var dlgLines = [""];
var dlgPointer = 0; var dlgPointer = 0;
var i = 0; function load() {
fetch("text_horror/dialogue.json")
.then(Response => Response.json())
.then(data => {
dlgFile = data;
dlgLines = dlgFile["<start>"];
typeWriter();
})
}
function typeWriter() { function typeWriter() {
if (document.getElementById("bubble").innerHTML.length - 1 < dlgLines[dlgPointer].length) { if (document.getElementById("bubble").innerHTML.length - 1 < dlgLines[dlgPointer].length) {
document.getElementById("bubble").innerHTML += dlgLines[dlgPointer].charAt(i); document.getElementById("bubble").innerHTML += dlgLines[dlgPointer].charAt(typeindex);
i++; typeindex++;
setTimeout(typeWriter, speed);//loops because of running "typeWriter" after waiting setTimeout(typeWriter, speed);//loops because of running "typeWriter" after waiting
} }
} }
@@ -15,7 +26,7 @@ function mouseClick() {
if (document.getElementById("bubble").innerHTML.length == dlgLines[dlgPointer].length) { if (document.getElementById("bubble").innerHTML.length == dlgLines[dlgPointer].length) {
if (dlgPointer < dlgLines.length - 1) { if (dlgPointer < dlgLines.length - 1) {
dlgPointer++; dlgPointer++;
i = 0; typeindex = 0;
document.getElementById("bubble").innerHTML = ""; document.getElementById("bubble").innerHTML = "";
typeWriter(); typeWriter();
} else { } else {

View File

@@ -0,0 +1,7 @@
{
"<start>": [
"Hello welcome to TextHorror!",
"This is the second line",
"Aand a third line"
]
}