-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.js
155 lines (123 loc) · 3.88 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
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
// declare global variables
var score;
var whingeTally = 0;
function init() {
score = 9500;
}
var quotes = [
'Hey! My mascara has run out. I\'m so depressed!',
'Why does no one love me?',
'You just don\'t care about real problems like mine.',
'My dress is not as dark as my soul...',
'Why? Whhhyyyyyy?',
'I\'m hungry! Why is there nothing to eat around here?',
'I hate it when people say I look like a goth. Emos are so different.',
'My floor-length leather trenchcoat makes me feel too hot...',
'My thoughts give way to nothing but dark sollioquies.',
'My toast is slightly burnt. Please end my suffering.'
]
var welcomePanel = document.querySelector('.welcome');
var welcomePara = document.querySelector('.welcome p');
var curLevel = document.querySelector('.current-level');
var curLevelPara = document.querySelector('.misery p');
var mainImage = document.querySelector('.game img');
var curImage = 'emo';
var updateImage = 'emo';
var btnStart = document.querySelector('.start');
var btnScore = document.querySelectorAll('.button-bar button');
// wire up buttons
btnStart.onclick = function() {
welcomePanel.style.zIndex = '-1';
init();
mainLoop();
}
for(i = 0; i < btnScore.length; i++) {
btnScore[i].onclick = function(e) {
var scoreMod = e.target.getAttribute('data-score');
var scoreNum = Number(scoreMod);
score += scoreNum;
};
}
// get permission to run notifications
Notification.requestPermission().then(function(result) {
console.log(result);
});
// update misery progress bar
function updateProgress() {
var percent = Math.floor((score/10000) * 100);
curLevel.style.width = percent + '%';
curLevelPara.textContent = 'Emo-level: ' + percent + '%';
}
// update current image display
var heads = ['well-adjusted','happy','sad','emo'];
function updateDisplay() {
if(score > 10000) {
score = 10000;
}
if(score <= 2000) {
curImage = 'well-adjusted';
} else if(score > 2000 && score <= 5000) {
curImage = 'happy';
} else if(score > 5000 && score <= 8000) {
curImage = 'sad';
} else if(score > 8000) {
curImage = 'emo';
}
if(updateImage !== curImage) {
if(heads.indexOf(updateImage) > heads.indexOf(curImage)) {
spawnNotification('Whaaa, I\'m becoming well-adjusted, pay attention to me!', 'img/' + curImage + '_head.png', 'Emogotchi says');
} else {
spawnNotification('That\'s better — I can feel the darkening!', 'img/' + curImage + '_head.png', 'Emogotchi says');
}
mainImage.src = 'img/' + curImage + '.png';
updateImage = curImage;
}
updateProgress();
}
// end the game, allow it to be restarted
function endGame() {
welcomePanel.style.zIndex = '1';
welcomePara.textContent = 'OH NO!! Your emo became too well-adjusted; now she\'ll be off to hang out at the mall, listen to Taylor Swift and bake cookies.'
btnStart.textContent = 'Restart Emogotchi?';
spawnNotification('YOU TOTAL LOSER. How could you do this to me?', 'img/' + curImage + '_head.png', 'Emogotchi says');
}
// spawn a permission
function spawnNotification(theBody, theIcon, theTitle) {
var options = {
body: theBody,
icon: theIcon
}
var n = new Notification(theTitle, options);
setTimeout(n.close.bind(n), 4000);
}
function randomNotification() {
var randomQuote = quoteChooser();
var options = {
body: randomQuote,
icon: 'img/sad_head.png',
}
var n = new Notification('Emogotchi says', options);
setTimeout(n.close.bind(n), 4000);
}
function quoteChooser() {
var randomNumber = Math.floor(Math.random() * 10);
quote = quotes[randomNumber];
return quote;
}
// run main loop
function mainLoop() {
score -= 3;
whingeTally += 1;
updateDisplay();
if(score <= 0) {
score = 0;
endGame();
} else {
if(whingeTally % 750 === 0) {
randomNotification();
}
window.requestAnimationFrame(mainLoop);
}
}
// handle the sound
var emoSound = document.querySelector('audio');