-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
309 lines (230 loc) · 7.44 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"use strict";
Pablo(document.body).load('images/all.svg', function (sprite) {
sprite.css('display', 'none');
displayIngredients();
});
var START_INGREDIENTS = ['air', 'earth', 'fire', 'water'];
// Copy the starting ingredients
var discoveredIngredients = START_INGREDIENTS.slice();
var potion = [];
function chooseIngredient (ingredient) {
var message;
potion.push(ingredient);
if (potion.length === 1) {
//message = `<strong>${ingredient}</strong> + ?`;
//message = '<strong>' + ingredient + '</strong> + ?';
message = ingredient + ' + ?';
}
if (potion.length === 2) {
var ingredient1 = potion[0];
var ingredient2 = potion[1];
var child = lookupResult(ingredient1, ingredient2);
if (child) {
updateFoundElements(child);
//message = `<strong>${ingredient1}</strong> + <strong>${ingredient2}</strong> = <strong>${child}</strong>`;
//message = '<strong>' + ingredient1 + '</strong> + <strong>' + ingredient2 + '</strong> = <strong>' + child + '</strong>';
message = ingredient1 + ' + ' + ingredient2 + ' = ' + child;
}
else {
//message = `<strong>${ingredient1}</strong> + <strong>${ingredient2}</strong> = nothing`;
//message = '<strong>' + ingredient1 + '</strong> + <strong>' + ingredient2 + '</strong> = nothing';
message = ingredient1 + ' + ' + ingredient2 + ' = nothing';
}
// Empty the `potion` array
potion.splice(0, 2);
// Refresh the displayed ingredients
displayIngredients();
}
displayPotion(message);
}
function lookupResult (ingredient1, ingredient2) {
var result;
POTIONS.some(function (combination) {
var test1 = combination[0];
var test2 = combination[1];
if (
(ingredient1 === test1 && ingredient2 === test2) ||
(ingredient2 === test1 && ingredient1 === test2)
) {
result = combination[2];
return true;
}
});
return result || false;
}
// Write out found ingredients
function displayIngredients () {
var list = document.getElementById('discovered');
var listItems = '';
discoveredIngredients.forEach(function (ingredient) {
var hasPotential = ingredientHasPotential(ingredient);
var className = hasPotential ? 'potential' : '';
//listItems += `<li class="${className}">${ingredient}</li>`;
listItems += '<li data-ingredient="' + ingredient + '" class="' + className + '">' + ingredient + '</li>';
});
list.innerHTML = listItems;
Pablo(list).children().forEach(function (item) {
var ingredient = item.getAttribute('data-ingredient');
var image = Pablo('#' + ingredient);
if (!image) {
return;
}
var imageRoot = Pablo.svg().append(image.clone()).crop();
imageRoot.prependTo(item);
});
displayCount();
}
function addListeners () {
var discoveredList = document.getElementById('discovered');
discoveredList.addEventListener('click', function (event) {
var target = event.target;
if (target && target.nodeName === 'LI') {
var ingredient = event.target.getAttribute('data-ingredient');
chooseIngredient(ingredient);
target.classList.add('chosen');
}
});
// When header off-screen, enable scrollable ingredients
var headerElem = document.querySelector('header');
var headerHeight = headerElem.clientHeight;
var boardElem = document.getElementById('board');
var scrollingEnabled = false;
// window.addEventListener('scroll', function (event) {
// var scrollY = document.body.scrollTop;
// var headerIsVisible;
// if (!scrollingEnabled) {
// headerIsVisible = scrollY < headerHeight;
// }
// else {
// headerIsVisible = scrollY === 0;
// }
// console.log('scroll', {scrollY, headerHeight, headerIsVisible});
// if (headerIsVisible && scrollingEnabled) {
// boardElem.classList.remove('scrollable');
// headerElem.classList.add('shiftY');
// scrollingEnabled = false;
// }
// else if (!headerIsVisible && !scrollingEnabled) {
// boardElem.classList.add('scrollable');
// headerElem.classList.remove('shiftY');
// scrollingEnabled = true;
// }
// });
}
function displayPotion (message) {
var el = document.getElementById('potion');
el.innerHTML = message;
}
function displayMessage (message) {
var el = document.getElementById('message');
el.innerHTML = message;
}
function displayStartMessage () {
//displayMessage(`You have ${discoveredIngredients.length} ingredients. Mix two together to make some more.`);
displayMessage('You have ' + discoveredIngredients.length + ' ingredients. Mix two together to make some more.');
}
function uniqueIngredients (potions) {
// ['air', 'earth', 'fire', 'water'];
var ingredients = START_INGREDIENTS.slice();
// e.g. `potion = ['fire', 'water', 'steam']`
potions.forEach(function (potion) {
var child = potion[2];
if (ingredients.indexOf(child) === -1) {
ingredients.push(child);
}
});
return ingredients.sort();
}
var allIngredients = uniqueIngredients(POTIONS);
function displayCount () {
var foundCount = discoveredIngredients.length;
var totalCount = allIngredients.length;
var el = document.getElementById('count');
//el.textContent = `${foundCount} of ${totalCount}`;
el.textContent = foundCount + ' of ' + totalCount;
}
function updateFoundElements (child) {
if (discoveredIngredients.indexOf(child) === -1) {
discoveredIngredients.push(child);
discoveredIngredients.sort();
save();
return true;
}
return false;
}
function displayAll () {
discoveredIngredients = allIngredients.slice()
displayIngredients();
}
function ingredientHasPotential (ingredient) {
return POTIONS.some(function (potion) {
if (potion[0] === ingredient || potion[1] === ingredient) {
var child = potion[2];
if (discoveredIngredients.indexOf(child) === -1) {
return true;
}
}
});
}
var STORAGE_KEY = 'alchemy';
function save () {
localStorage[STORAGE_KEY] = JSON.stringify(discoveredIngredients);
}
function restore () {
if (STORAGE_KEY in localStorage) {
discoveredIngredients = JSON.parse(localStorage[STORAGE_KEY]);
}
displayIngredients();
displayStartMessage();
}
// Activate!
addListeners();
restore();
////////////////////
// ORIGINAL PROGRAM
// function attemptChild () {
// var ingredient1 = prompt('What is the first ingredient?');
// var ingredient2 = prompt('What is the second ingredient?');
// var message;
// if (!ingredient1 || !ingredient2) {
// return;
// }
// var child = lookupResult(ingredient1, ingredient2);
// if (child) {
// var result = updateFoundElements(child);
// if (result) {
// displayIngredients();
// }
// message = `You have created ${child} with ${ingredient1} and ${ingredient2}`;
// }
// else {
// message = `${ingredient1} and ${ingredient2} does not make anything`;
// }
// // Write out created child
// displayMessage(message);
// attemptChild();
// }
////////////////////
// NOTES
/*
Display all known elements
Allow adding 2 elements:
- typing ingredient text
When 2 elements are added:
- look up the result
- show/alert the result or no result
If result:
- add to `elements` array
- update display of all known elements
*/
// start: fire water air earth
// fire water steam
// air water bubbles
// air fire campfire
// air earth dustcloud
// fire earth lava
// earth water bog
// water water flood
// fire fire fireball
// air air pressure
// earth earth mountain