-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
114 lines (98 loc) · 3.25 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var playerInventory = {
"sword": false,
"axe": false,
"staff": false,
"key": true,
"meat": false,
"dragonEye": false,
"coins": 5,
"knife": true
};
// Don't edit below here! :)
var displayQuestion = function (questionId) {
'use strict';
var question = null;
for (var i = 0; i<questionData.length;i++) {
if (questionData[i].id === questionId) {
question = questionData[i];
}
}
if (question !== null) {
// Change the picture if it's specified
if (question.picture && question.picture.length) {
var pictureEl = document.querySelector('#picture');
var newSrc = "images/" + question.picture;
pictureEl.src = newSrc;
}
// Update the inventory
if (question.inventory) {
if (question.inventory.coins) {
if (playerInventory.coins === 0) {
// If not enough coins, get the fudge out of dodge.
return;
}
playerInventory.coins += question.inventory.coins;
document.querySelector('.inventory div span').innerText = playerInventory.coins;
}
if (question.inventory.sword) {
playerInventory.sword = true;
document.querySelector('.inventory .sword').classList.add("show");
}
if (question.inventory.axe) {
playerInventory.axe = true;
document.querySelector('.inventory .axe').classList.add("show");
}
if (question.inventory.staff) {
playerInventory.staff = true;
document.querySelector('.inventory .staff').classList.add("show");
}
// console.log("Updated inventory", playerInventory);
}
// Can this question auto navigate?
if (question.auto_navigate) {
setTimeout(function () {
displayQuestion(question.auto_navigate);
}, 1500);
}
if (question.answers && Object.keys(question.answers).length) {
var theQuestion = question.question;
var questionArea = document.querySelector('.question');
questionArea.innerText = theQuestion;
var theAnswers = question.answers;
var buttonArea = document.querySelector('.buttons');
buttonArea.innerHTML = '';
var newButtonHTML = '';
var answerButton = document.createElement('button');
for (var answer in theAnswers) {
var thisButton = answerButton.cloneNode(false);
thisButton.innerText = answer;
var thisValue = theAnswers[answer];
thisButton.setAttribute("onclick","displayQuestion("+thisValue+")", false);
// Crappy inventory management....
var weapons = ["SWORD", "AXE", "STAFF"];
var show_button = true;
for (var i = 0; i<weapons.length;i++) {
var weapon = weapons[i];
if (
playerInventory[weapon.toLowerCase()] === true &&
answer === weapon
) {
show_button = false;
}
};
if (!show_button) {
continue;
}
buttonArea.appendChild(thisButton);
}
} else {
// No answers needed, just show the text!
document.querySelector('.question').innerText = question.question;
document.querySelector('.buttons').innerHTML = '';
}
} else {
// That's uh... not a question. Awkward.
alert("That's not an option Wary Traveller...");
}
};
displayQuestion(0);