added comments

This commit is contained in:
Yannik
2023-06-23 22:35:38 +02:00
parent 6e7217fb59
commit be1b6f59f9

View File

@@ -1,25 +1,25 @@
// BITTE VORHER README LESEN // BITTE VORHER README LESEN
var speed = 50; // some values get saved to local storage for the checkpoint system
var typeindex = 0; var speed = 50; //typewriting effect speed
var dlgPointer = parseInt(localStorage.getItem("dlgPointer")); var typeindex = 0;//wich character is already typed out
var dlgFile; var dlgPointer = parseInt(localStorage.getItem("dlgPointer")); //which line the user has loaded from the dialogue.json dictionary
var dlgFile; //the current loaded dictionary (Gets replaced after answer branches)
var dlgKeyMain = localStorage.getItem("dlgKeyMain"); var dlgKeyMain = localStorage.getItem("dlgKeyMain");
let dlgLines; let dlgLines; //string array with all current user readable dialogue lines
var skipDlg = false; var skipDlg = false;
var answers; var answers;
var keys; var keys; //answer box answers
var allowNextDlg = true; var allowNextDlg = true;
var voice; var voice; //sound that plays every few characters (feature not used in the current game)
var playerName;
var enemyHealth = 0, enemyDamage = 1, enemyStartsHit = false; var enemyHealth = 0, enemyDamage = 1, enemyStartsHit = false;
var health = 25; var health = 25;
var weaponName = localStorage.getItem("weaponName"), continueCount = true; var weaponName = localStorage.getItem("weaponName"), continueCount = true;
var weaponDamage = parseInt(localStorage.getItem("weaponDamage")); var weaponDamage = parseInt(localStorage.getItem("weaponDamage"));
var counttx = 0, countup = true; var counttx = 0, countup = true; //counter values for counting weapon damage up and down in the fight screen
window.addEventListener('contextmenu', (event) => { window.addEventListener('contextmenu', (event) => {
event.preventDefault(); event.preventDefault(); //prevents normal right click (shift rightclick works)
speed = 0; speed = 0; //makes dialogue faster
}) })
function load(isJump = false) { function load(isJump = false) {
@@ -40,7 +40,7 @@ function load(isJump = false) {
nextDlg(false); //Skip to first nextDlg(false); //Skip to first
}); });
} }
//change current dialogue lines with dictionary keys
function changeDlg(dlgKey) { function changeDlg(dlgKey) {
if (typeof dlgFile[dlgKey] === "string") { if (typeof dlgFile[dlgKey] === "string") {
var oneline = [dlgFile[dlgKey]]; var oneline = [dlgFile[dlgKey]];
@@ -49,14 +49,14 @@ function changeDlg(dlgKey) {
else if (Array.isArray(dlgFile[dlgKey])) else if (Array.isArray(dlgFile[dlgKey]))
dlgLines = dlgFile[dlgKey]; dlgLines = dlgFile[dlgKey];
} }
//force typeout the dialogue
function updateDlg() { function updateDlg() {
typeindex = 0; typeindex = 0;
document.getElementById("dlg-text").innerHTML = ""; document.getElementById("dlg-text").innerHTML = "";
document.getElementById("triangle").hidden = true; document.getElementById("triangle").hidden = true;
typeWriter(); typeWriter();
} }
//typewriter effect
function typeWriter() { function typeWriter() {
if (typeindex < dlgLines[dlgPointer].length) { if (typeindex < dlgLines[dlgPointer].length) {
document.getElementById("dlg-text").innerHTML += dlgLines[dlgPointer].charAt(typeindex); document.getElementById("dlg-text").innerHTML += dlgLines[dlgPointer].charAt(typeindex);
@@ -69,13 +69,13 @@ function typeWriter() {
} else if (allowNextDlg) } else if (allowNextDlg)
document.getElementById("triangle").hidden = false; document.getElementById("triangle").hidden = false;
} }
//go to next dialogue line and interpretate it
function nextDlg(dlgPointerIncrease = true) { function nextDlg(dlgPointerIncrease = true) {
speed = 50; speed = 50;
if (document.getElementById("dlg-text").innerHTML.length == dlgLines[dlgPointer].length && allowNextDlg || skipDlg) { if (document.getElementById("dlg-text").innerHTML.length == dlgLines[dlgPointer].length && allowNextDlg || skipDlg) {
skipDlg = false; skipDlg = false;
if (dlgPointerIncrease) if (dlgPointerIncrease)
dlgPointer++; dlgPointer++; //actually go to the next line in the string array
if (dlgPointer < dlgLines.length) { if (dlgPointer < dlgLines.length) {
if (typeof dlgLines[dlgPointer] === 'number') { if (typeof dlgLines[dlgPointer] === 'number') {
speed = 50 / dlgLines[dlgPointer]; speed = 50 / dlgLines[dlgPointer];
@@ -83,20 +83,20 @@ function nextDlg(dlgPointerIncrease = true) {
nextDlg(); nextDlg();
} }
else if (typeof dlgLines[dlgPointer] === 'string') { else if (typeof dlgLines[dlgPointer] === 'string') {
if (dlgLines[dlgPointer].startsWith("_")) { if (dlgLines[dlgPointer].startsWith("_")) { //checks if its a string command wich doesn't get typed out
skipDlg = true; skipDlg = true;
if (dlgLines[dlgPointer].split(":")[0] === "_title") { if (dlgLines[dlgPointer].split(":")[0] === "_title") { //topleft NPC name
document.getElementById("title").innerHTML = dlgLines[dlgPointer].split(':')[1]; document.getElementById("title").innerHTML = dlgLines[dlgPointer].split(':')[1];
nextDlg(); nextDlg();
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_voice") { else if (dlgLines[dlgPointer].split(":")[0] === "_voice") { //voice wich is a sound wich plays every few text characters
if (dlgLines[dlgPointer].split(':')[1].length != 0) if (dlgLines[dlgPointer].split(':')[1].length != 0)
voice = new Audio("text_horror/assets/voices/" + dlgLines[dlgPointer].split(':')[1] + ".wav"); voice = new Audio("text_horror/assets/voices/" + dlgLines[dlgPointer].split(':')[1] + ".wav");
else else
voice = null; voice = null;
nextDlg(); nextDlg();
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_playsound") { else if (dlgLines[dlgPointer].split(":")[0] === "_playsound") { //play sounds
var sound; var sound;
if (dlgLines[dlgPointer].split(':')[1].length != 0) { if (dlgLines[dlgPointer].split(':')[1].length != 0) {
sound = new Audio("text_horror/assets/sounds/" + dlgLines[dlgPointer].split(':')[1] + ".wav"); sound = new Audio("text_horror/assets/sounds/" + dlgLines[dlgPointer].split(':')[1] + ".wav");
@@ -107,21 +107,18 @@ function nextDlg(dlgPointerIncrease = true) {
sound = null; sound = null;
nextDlg(); nextDlg();
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_jump") { else if (dlgLines[dlgPointer].split(":")[0] === "_jump") { // jump to keys wich are not in nested dictionaries (answer boxes)
dlgKeyMain = dlgLines[dlgPointer].split(':')[1]; dlgKeyMain = dlgLines[dlgPointer].split(':')[1];
load(true); load(true);
dlgPointer = 0; dlgPointer = 0;
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_lock") { else if (dlgLines[dlgPointer].split(":")[0] === "_weapon") { //set the current player weapon
nextDlg();
}
else if (dlgLines[dlgPointer].split(":")[0] === "_weapon") {
weaponName = dlgLines[dlgPointer].split(':')[1]; weaponName = dlgLines[dlgPointer].split(':')[1];
weaponDamage = parseInt(dlgLines[dlgPointer].split(':')[2]); weaponDamage = parseInt(dlgLines[dlgPointer].split(':')[2]);
console.log(weaponDamage); console.log(weaponDamage);
nextDlg(); nextDlg();
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_enemy") { else if (dlgLines[dlgPointer].split(":")[0] === "_enemy") { //begin a fight against an enemy
document.getElementById("enemy-name").innerHTML = dlgLines[dlgPointer].split(":")[1]; document.getElementById("enemy-name").innerHTML = dlgLines[dlgPointer].split(":")[1];
document.getElementById("enemy-texture").src = "text_horror/assets/textures/" + dlgLines[dlgPointer].split(":")[2] + ".png"; document.getElementById("enemy-texture").src = "text_horror/assets/textures/" + dlgLines[dlgPointer].split(":")[2] + ".png";
enemyHealth = dlgLines[dlgPointer].split(":")[3]; enemyHealth = dlgLines[dlgPointer].split(":")[3];
@@ -137,6 +134,7 @@ function nextDlg(dlgPointerIncrease = true) {
document.getElementById("triangle").hidden = true; document.getElementById("triangle").hidden = true;
} }
else if (dlgLines[dlgPointer].split(":")[0] === "_checkpoint") { else if (dlgLines[dlgPointer].split(":")[0] === "_checkpoint") {
ß //set a checkpoint with local storage
localStorage.setItem("dlgPointer", (dlgPointer + 1).toString()); localStorage.setItem("dlgPointer", (dlgPointer + 1).toString());
localStorage.setItem("dlgKeyMain", dlgKeyMain); localStorage.setItem("dlgKeyMain", dlgKeyMain);
localStorage.setItem("weaponDamage", weaponDamage.toString()); localStorage.setItem("weaponDamage", weaponDamage.toString());
@@ -145,7 +143,7 @@ function nextDlg(dlgPointerIncrease = true) {
} else } else
updateDlg(); updateDlg();
} }
else if (typeof dlgLines[dlgPointer] === 'object') { else if (typeof dlgLines[dlgPointer] === 'object') { //if inside a dictionary is a dictionary it gets built as answer box
allowNextDlg = false; allowNextDlg = false;
document.getElementById("triangle").hidden = true; document.getElementById("triangle").hidden = true;
document.getElementById("answer-box").hidden = false; document.getElementById("answer-box").hidden = false;
@@ -156,7 +154,7 @@ function nextDlg(dlgPointerIncrease = true) {
} }
} }
} }
else { else { //if dlgLines array is at the end the game ends
document.getElementById("bubble").hidden = true; document.getElementById("bubble").hidden = true;
document.getElementById("answer-box").hidden = true; document.getElementById("answer-box").hidden = true;
document.getElementById("triangle").hidden = true; document.getElementById("triangle").hidden = true;
@@ -164,7 +162,7 @@ function nextDlg(dlgPointerIncrease = true) {
} }
} }
function answered(answerId) { function answered(answerId) { //handles answerbox answer clicks
dlgFile = dlgLines[dlgPointer]; dlgFile = dlgLines[dlgPointer];
document.getElementById("answer-box").hidden = true; document.getElementById("answer-box").hidden = true;
changeDlg(document.getElementById(answerId).innerHTML); changeDlg(document.getElementById(answerId).innerHTML);
@@ -177,7 +175,7 @@ function answered(answerId) {
} }
} }
function fight() { function fight() { //handles the click on the fight button
continueCount = false; continueCount = false;
if (!enemyStartsHit) { if (!enemyStartsHit) {
if (!continueCount) { if (!continueCount) {
@@ -212,7 +210,7 @@ function fight() {
} }
} }
function countWeaponDamage() { function countWeaponDamage() { //counts up and down
if (countup) { if (countup) {
++counttx; ++counttx;
if (counttx >= weaponDamage) if (counttx >= weaponDamage)