-
Notifications
You must be signed in to change notification settings - Fork 0
/
pet.js
365 lines (323 loc) · 13.4 KB
/
pet.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
var curPetName;
var posLeft;
var posTop;
var standRight;
var standLeft;
var dragLeft;
var dragRight;
var walkLeft1;
var walkLeft2;
var walkRight1;
var walkRight2;
var jumpLeft;
var jumpRight;
var eatRight;
var eatLeft;
//set and get positionLeft
chrome.storage.local.get('positionLeft', function(result) {
console.log(result)
if (result.positionLeft == undefined) {
posLeft = '100px'
console.log('null, but set to 100, 400' + posLeft)
} else {
posLeft = result.positionLeft;
console.log('LeftValue currently is ' + result.positionLeft);
}
});
//set and get positionTop
chrome.storage.local.get('positionTop', function(result) {
console.log(result)
if (result.positionTop == undefined) {
posTop = '400px'
console.log('null, but set to 100, 400')
} else {
posTop = result.positionTop;
console.log('TopValue currently is ' + result.positionTop);
}
});
$(document).ready(function readyHandler() {
//set and get name
chrome.storage.local.get('thoname', function(result) {
console.log(result)
if (result.thoname == undefined) {
curPetName = 'Jiejie'
console.log('null, but set to Jiejie')
} else {
console.log('Value currently is ' + result.thoname);
curPetName = result.thoname;
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
curPetName=request.getName;
chrome.storage.local.set({'thoname': curPetName}, function() {
console.log('Name is set to ' + curPetName);
});
sendResponse('GET message:'+JSON.stringify("request"));
$("body").parent().children("div").remove();
$(document).off();
readyHandler()
});
petImgConfigJSON_URL = chrome.runtime.getURL("pet-img-config.json");
$.getJSON(petImgConfigJSON_URL, function (data) {
standRight = chrome.runtime.getURL(data[curPetName].stand.right);
standLeft = chrome.runtime.getURL(data[curPetName].stand.left);
dragRight = chrome.runtime.getURL(data[curPetName].drag.right);
dragLeft = chrome.runtime.getURL(data[curPetName].drag.left);
walkRight1 = chrome.runtime.getURL(data[curPetName].walk.right1);
walkRight2 = chrome.runtime.getURL(data[curPetName].walk.right2);
walkLeft1 = chrome.runtime.getURL(data[curPetName].walk.left1);
walkLeft2 = chrome.runtime.getURL(data[curPetName].walk.left2);
jumpRight = chrome.runtime.getURL(data[curPetName].jump.right);
jumpLeft = chrome.runtime.getURL(data[curPetName].jump.left);
eatRight = chrome.runtime.getURL(data[curPetName].eat.right);
eatLeft = chrome.runtime.getURL(data[curPetName].eat.left);
})
var pet = $("<div class='pet'></div>");
var animating = false;
// initialize pet
$("body").parent().append(pet);
$.getJSON(petImgConfigJSON_URL, function (data) {
petImgURL = standRight;
$('.pet img').remove();
$('.pet').prepend($('<img>', { id: "pet-img", src: petImgURL }));
idleActivate = false;
document.body.style.cursor = 'url('+chrome.runtime.getURL(data[curPetName].point)+'), default'
$('.pet').css('cursor', 'url('+chrome.runtime.getURL(data[curPetName].move)+'), auto');
})
$(".pet").css({
//"left": "100px",
//"top": "400px",
"z-index": "9999",
"position": "fixed",
"touch-action": "none",
"will-change": "transform",
"image-rendering": "-webkit-optimize-contrast"
});
$(".pet").css("left", posLeft);
$(".pet").css("top", posTop);
var containx1 = window.scrollX
var containx2 = window.scrollX + window.screen.availWidth - 128
var containy1 = window.scrollY
var containy2 = window.scrollY + window.screen.availHeight- 220
var inputTarget = null;
var finishEating = false;
var idleActivate = false;
Idle()
function Idle() {
if(idleActivate == false) {
return;
}
if(animating == false) {
animating = true;
if(inputTarget != null && inputTarget.value != "") {
setTimeout(function(){
petImgURL_beforeEat = petImgURL
if (petImgURL_beforeEat == standRight) {
petImgURL = eatRight;
} else if (petImgURL_beforeEat == standLeft) {
petImgURL = eatLeft;
}
$("#pet-img").attr("src", petImgURL);
eatInputValue(inputTarget)
petImgURL = petImgURL_beforeEat;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
}, 200)
if(inputTarget.value == "") {
finishEating = true;
}
Idle();
}, 1000 + Math.random()*1000);
} else {
if(finishEating == true) {
setTimeout(function(){
finishEating = false;
petImgURL_beforeJump = petImgURL;
if (petImgURL_beforeJump == standRight) {
petImgURL = jumpRight;
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({top: "-=50px"}, 300);
$(".pet").animate({top: "+=50px"}, 200);
} else if (petImgURL_beforeJump == standLeft) {
petImgURL = jumpLeft;
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({top: "-=50px"}, 300);
$(".pet").animate({top: "+=50px"}, 200);
}
petImgURL = petImgURL_beforeJump;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
animating = false;
}, 500)
Idle();
}, 100);
} else {
setTimeout(function(){
petImgURL_beforeWink = petImgURL;
if (petImgURL_beforeWink == standRight) {
petImgURL = dragRight;
} else if (petImgURL_beforeWink == standLeft) {
petImgURL = dragLeft;
}
$("#pet-img").attr("src", petImgURL);
petImgURL = petImgURL_beforeWink;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
}, 170)
Idle();
}, 3000 + Math.random()*5000);
}
}
animating = false;
}
}
//eat
document.body.addEventListener('input', async event => {
inputTarget = event.target
});
function eatInputValue(elem) {
// remember selection position
const start = elem.selectionStart, end = elem.selectionEnd;
elem.value = elem.value.slice(1);
// restore selection position
elem.setSelectionRange(Math.max(start - 1, 0), Math.max(end - 1, 0));
}
$(window).scroll(function(){
containx1 = window.scrollX
containx2 = window.scrollX + window.screen.availWidth - 128
containy1 = window.scrollY
containy2 = window.scrollY + window.screen.availHeight- 220
var set1 = [containx1, containy1 , containx2, containy2];
$(".pet").draggable( "option", "containment", set1 );
});
// Drag pet around
$(".pet").draggable({
start: function () {
$.getJSON(petImgConfigJSON_URL, function (data) {
petImgURL_beforeDrag = petImgURL;
if (petImgURL_beforeDrag == standRight) {
petImgURL = dragRight;
} else if (petImgURL_beforeDrag == standLeft) {
petImgURL = dragLeft;
}
$("#pet-img").attr("src", petImgURL);
}, () => chrome.runtime.lastError)
},
stop: function () {
$.getJSON(petImgConfigJSON_URL, function (data) {
petImgURL = petImgURL_beforeDrag;
$("#pet-img").attr("src", petImgURL);
}, () => chrome.runtime.lastError)
containx1 = window.scrollX
containx2 = window.scrollX + window.screen.availWidth - 128
containy1 = window.scrollY
containy2 = window.scrollY + window.screen.availHeight- 220
var set2 = [containx1, containy1 , containx2, containy2];
$(".pet").draggable( "option", "containment", set2 );
setThePosition()
},
containment:[containx1, containy1 , containx2, containy2]
}, () => chrome.runtime.lastError);
var rightCount = true;
var leftCount = true;
var specialAnimation = false;
function setThePosition() {
posLeft = $(".pet").offset().left
posTop = $(".pet").offset().top
if (posTop > window.screen.availHeight) {
posTop = window.screen.availHeight- 220
}
console.log(window.screen.availWidth)
if (posLeft > window.screen.availWidth) {
posLeft = window.screen.availWidth - 128
}
chrome.storage.local.set({'positionLeft': posLeft}, function() {
console.log('posLeft is set to ' + posLeft);
});
chrome.storage.local.set({'positionTop': posTop}, function() {
console.log('posTop is set to ' + posTop);
});
}
//Jump and Walk
$(document).keydown(function(e){
if(animating == false) {
if(e.which == 38) { //Up arrow
animating = true;
petImgURL_beforeJump = petImgURL;
if (petImgURL_beforeJump == standRight) {
petImgURL = jumpRight;
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({top: "-=50px"}, 300);
$(".pet").animate({top: "+=50px"}, 200);
} else if (petImgURL_beforeJump == standLeft) {
petImgURL = jumpLeft;
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({top: "-=50px"}, 300);
$(".pet").animate({top: "+=50px"}, 200);
}
petImgURL = petImgURL_beforeJump;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
animating = false;
}, 500)
} else if (e.which == 39 && $(".pet").offset().left < window.screen.availWidth - 148) { //right arrow
animating = true;
petImgURL_afterWalk = standRight;
if (rightCount == true) {
petImgURL = walkRight1;
rightCount = false;
} else if (rightCount == false) {
petImgURL = walkRight2;
rightCount = true;
}
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({left: "+=10px", top: "-=10px"}, 130);
$(".pet").animate({left: "+=10px", top: "+=10px"}, 120);
petImgURL = petImgURL_afterWalk;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
animating = false;
}, 250)
} else if (e.which == 37 && $(".pet").offset().left > 20) { //left arrow
animating = true;
petImgURL_afterWalk = standLeft;
if (leftCount == true) {
petImgURL = walkLeft1;
leftCount = false;
} else if (leftCount == false) {
petImgURL = walkLeft2;
leftCount = true;
}
$("#pet-img").attr("src", petImgURL);
$(".pet").animate({left: "-=10px", top: "-=10px"}, 130);
$(".pet").animate({left: "-=10px", top: "+=10px"}, 120);
petImgURL = petImgURL_afterWalk;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
animating = false;
}, 250)
} else if (e.which == 40 && specialAnimation == false) { //down arrow
specialAnimation = true;
petImgURL_beforeDrag = petImgURL;
if (petImgURL_beforeDrag == standRight) {
petImgURL = dragRight;
} else if (petImgURL_beforeDrag == standLeft) {
petImgURL = dragLeft;
}
$("#pet-img").attr("src", petImgURL);
petImgURL = petImgURL_beforeDrag;
setTimeout(function () {
$("#pet-img").attr("src", petImgURL);
}, 170)
if(idleActivate == false) {
idleActivate = true;
Idle()
} else {
idleActivate = false;
}
specialAnimation = false;
}
}
setThePosition()
});
}, () => chrome.runtime.lastError);