-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
100 lines (80 loc) · 3.04 KB
/
init.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
'use strict';
var resultsKey = 'results'
var node = document.getElementById('app-container')
var randomNumber = Math.floor(Math.random()*0xFFFFFFFF)
var results = JSON.parse(localStorage.getItem(resultsKey)) || []
var app = Elm.Main.init({
node: node,
flags: {
randomNumber: randomNumber,
rawGameResults: results,
},
})
app.ports.saveGameResult.subscribe(function(gameResult) {
var results = JSON.parse(localStorage.getItem(resultsKey)) || []
results.push(gameResult)
localStorage.setItem(resultsKey, JSON.stringify(results))
app.ports.receiveGameResults.send(results)
})
app.ports.updateNameInGameResult.subscribe(function([gameResult, newName]) {
var results = JSON.parse(localStorage.getItem(resultsKey)) || []
var resultToUpdate = results.find((result) => result.startedAt == gameResult.startedAt)
resultToUpdate.name = newName
localStorage.setItem(resultsKey, JSON.stringify(results))
app.ports.receiveGameResults.send(results)
})
var hitByMonsterTimeoutId, levelUpTimeoutId;
app.ports.emitGameEvents.subscribe(function(gameEvents) {
gameEvents.forEach(function(gameEvent) {
switch (gameEvent) {
case 'HitByMonster':
// If already happening, just make the animation last longer.
if (hitByMonsterTimeoutId) {
clearTimeout(hitByMonsterTimeoutId)
} else {
document.body.classList.add('shaking')
}
hitByMonsterTimeoutId = window.setTimeout(function() {
document.body.classList.remove('shaking')
hitByMonsterTimeoutId = undefined
}, 600)
break
case 'LevelUp':
window.requestAnimationFrame(function() {
// If already happening, interrupt the animation.
if (levelUpTimeoutId) {
clearTimeout(levelUpTimeoutId)
// Without those two calls requestAnimationFrame, removing a class and then just
// adding it again wouldn't really trigger the animation. So first we issue one
// requestAnimationFrame to do the class removal if needed. Inside another animation
// frame we add the class again, guaranteeing that the animation will be interrupted.
document.getElementById('grid').classList.remove('green-flash')
}
window.requestAnimationFrame(function() {
document.getElementById('grid').classList.add('green-flash')
levelUpTimeoutId = window.setTimeout(function() {
document.getElementById('grid').classList.remove('green-flash')
levelUpTimeoutId = undefined
}, 1500)
})
})
break
}
})
})
document.onkeydown = function(event) {
var cell = document.querySelectorAll('.grid-cell:hover')[0]
if (!cell) {
return
}
var index = parseInt(cell.dataset.index, 10)
app.ports.keyDown.send([event.code, index])
}
document.onkeyup = function(event) {
var cell = document.querySelectorAll('.grid-cell:hover')[0]
if (!cell) {
return
}
var index = parseInt(cell.dataset.index, 10)
app.ports.keyUp.send([event.code, index])
}