-
Notifications
You must be signed in to change notification settings - Fork 1
/
move.js
271 lines (218 loc) · 6.54 KB
/
move.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
//global variables
var mImg = '';
var mImages = [];
var currentScore = 0;
var state = 0;
var scoreFile = null;
var scores = null;
var nickname = prompt("Enter your name");
// game functions
function newGame() {
console.log("newGame, state "+ state);
//check if game was running (removes listeners)
if (state == 1){
stopMonsters();
}
currentScore = 0;
postScore();
state = 1;
//put monsters to the left border
putMonstersToStart();
//start animation
animateMonsters();
}
function postScore(){
// outputs current score
document.getElementById('currentScore').innerHTML = '<ul>' + currentScore + '</ul>';
}
function pauseResumeGame(){
//console.log("pauseResume state "+ state);
if (state == 1){
stopMonsters();
state = 2;
}
else if (state == 2){
animateMonsters();
state = 1;
}
}
function putMonstersToStart(){
var dots = document.getElementsByClassName("dot");
for (i = 0; i < dots.length; i++) {
dots[i].style.marginLeft = "0px";
}
}
function animateMonsters(){
var dots = document.getElementsByClassName("dot");
for (i = 0; i < dots.length; i++) {
animateMonster(dots[i]);
}
}
function gameover(){
console.log("GAME OVER!");
stopMonsters();
state = 0;
document.getElementById('currentScore').innerHTML = '<ul> GAME OVER!</ul> <ul> Your score: '+currentScore + '</ul>';
//save it to highscores
saveScore(currentScore);
}
function stopMonsters(){
//stop all monsters and detach listeners
$(".dot").stop();
$(".dot").off();
}
function getDuration(){
//Get some random animation duration for monster
return Math.round(400000*Math.random()/(currentScore+1));
}
function animateMonster(monster){
console.log('animateMonster ', monster);
//attach listener
$(monster).click(function(){
respawn(this.id);
});
$(monster).animate({
marginLeft: "90%"
}, {
duration: getDuration(),
easing: "linear",
complete: function () {
gameover();
}
});
}
function respawn(monsterId){
console.log("respawn " + monsterId);
//console.log(document.getElementById(monsterId));
var monster = document.getElementById(monsterId);
//stop animation, make monster invisible
$(monster).stop();
$(monster).off();
currentScore += 1;
document.getElementById('currentScore').innerHTML = '<ul>' + currentScore + '</ul>';
monster.style.marginLeft = "0px";
animateMonster(monster);
}
function readScore(input){
console.log("readScore " + input);
if (input.files && input.files[0]) {
scoreFile = input.files[0];
console.log('scoreFile '+ scoreFile);
var reader = new FileReader();
reader.onload = function(evt) {
console.log('reader onload');
scores = evt.target.result;
console.log("scores " + scores);
};
reader.readAsText(input.files[0]);
// if you want to put the highscores somewhere on the screen
// the text is in variable scores
}}
function saveScore(score){
console.log("writeScore " + score);
if(scoreFile){
console.log('score file ' + scoreFile);
var string = nickname + ' ' + score + '\n';
var text = scores + string;
console.log(text);
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
window.URL.revokeObjectURL(scoreFile);
scoreFile = window.URL.createObjectURL(data);
var link = document.getElementById('link');
link.href = scoreFile;
link.style.display = 'block';
}
else {
console.log("no score file is selected");
}
}
function readURL(input){
console.log("readUrl");
stopMonsters();
var files = input.files;
var numMonsters = files.length;
console.log("num pictures "+numMonsters);
if (files) {
console.log(files);
var reader = new FileReader();
reader.onload = function(e) {
console.log('reader onload');
$('#dot').attr('src', e.target.result);
};
var imageInd = Math.floor(Math.random()*numMonsters);
reader.readAsDataURL(input.files[imageInd]);
console.log("image " + input.files[imageInd]);
}
else {
console.log('Default monster image');
//return default monster;
//if no files are selected, choose default monster img
//mImg = readImage('default_monster.png');
}
// var image = document.getElementById("monster");
// image.id = "box";
// if (input.files && input.files[0]) {
// console.log("reader");
// var reader = new FileReader();
// reader.onload = function(e) {
// $('#box').attr('src', e.target.result);
// };
// reader.readAsDataURL(input.files[0]); // convert to base64 string
// }
}
// Not used, should be calle in new game or animate
// depending on if monster is chosen once for the whole game
// or at each respawn
function chooseMonsterImg(){
//filter images
//var files = document.getElementById("files");
console.log(files);
var numMonsters = mImages.length;
console.log("num pictures "+numMonsters);
if (files.length == 0){
console.log('Default monster image');
//return default monster;
//if no files are selected, choose default monster img
//mImg = readImage('default_monster.png');
}
var monsterId = Math.floor(Math.random()*numMonsters);
console.log('selected monster id '+monsterId);
var monster = mImages[monsterId];
console.log(monster);
return monster
}
//hide box when clicked
//function hide(){
//$("#box").stop();
//document.getElementById("box").break();
//document.getElementById("box").className="invisible";
//}
//always working (starts directly w/o button)
//$(function animate() {
// console.log("in function");
// $("#box").click(function(){
// respawn();
// });
// $("#box").animate({
// marginLeft: "90%"
// }, {
// duration: 10000,
// easing: "linear",
// complete: function () {
// gameover();
// }
// });
//});
// var dots = document.getElementsByClassName("dot");
// console.log("num dots: " + dots.length);
// for (i = 0; i < dots.length; i++) {
// console.log(dots[i]);
// dots[i].style.marginLeft = "0px";
// dots[i].className ="alive";
// }
//function that respawns monster at start location when it's clicked
//make it visible at initial location
// document.getElementById("dot").style.marginLeft = "0px"
// document.getElementById("dot").className="alive";