Repository navigation
Expand file tree
/
Copy pathboxing.js
More file actions
executable file
·393 lines (335 loc) · 12.9 KB
/
Copy pathboxing.js
File metadata and controls
executable file
·393 lines (335 loc) · 12.9 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
$(document).ready(function() {
//########################################################### GLOBALS ######################################################
var headBad = $("#headBad");
var headBadHit = $("#headBadHit");
var gloveRightBad = $("#gloveRightBad");
var gloveLeftBad = $("#gloveLeftBad");
var gloveLeftMine = $("#gloveLeftMine");
var inputCanvas = $("#inCanvas");
var gloveRightMine = $("#gloveRightMine");
var gloveLeftBadHit = $("#gloveLeftBadHit");
var myPunchs = $("#myPunchs");
var pow = $("#pow");
var wtf = $("#wtf");
var smash = $("#smash");
var badPunchs = $("#badPunchs");
var punch = $("#punch");
var win = $("#uwin");
var lost = $("#ulost");
var timerDiv = $("#timer");
var timer = 60;
var gloveRightBadHit = $("#gloveRightBadHit");
var red = $("#red");
var red2 = $("#red2");
var badPoint = 0;
var myPoint = 0;
var myHead = $("#myHead");
var X, Y, Z;
var htracker = new headtrackr.Tracker({calcAngles: true});
htracker.init(inputVideo, inputCanvas[0]);
htracker.start();
var topGloveLeftMine = posActTop(gloveLeftMine);
var leftGloveLeftMine = posActLeft(gloveLeftMine);
var topGloveRightMine = posActTop(gloveRightMine);
var leftGloveRightMine = posActLeft(gloveRightMine);
//##########################################################################################################################
//##########################################################################################################################
//##########################################################################################################################
//########################################################### EVENTS #######################################################
$("#starty").click(function() {
$("#instructions").hide(300);
startGame();
});
$(window).resize(function() {
topGloveLeftMine = posActTop(gloveLeftMine);
leftGloveLeftMine = posActLeft(gloveLeftMine);
topGloveRightMine = posActTop(gloveRightMine);
leftGloveRightMine = posActLeft(gloveRightMine);
});
document.addEventListener("headtrackingEvent", function(event) {
X = event.x;
Y = event.y;
Z = event.z;
myHeadPosition(myHead);
});
$(document).keyup(function(event) {
event.stopImmediatePropagation();
if (event.keyCode === 67) {
punchMine(gloveLeftMine, "left", topGloveLeftMine, leftGloveLeftMine);
}
else if (event.keyCode === 77) {
punchMine(gloveRightMine, "right", topGloveRightMine, leftGloveRightMine);
}
});
//##########################################################################################################################
//##########################################################################################################################
//##########################################################################################################################
//########################################################### FUNCTIONS #######################################################
function verifyMyGloves(Object, side) {
if (side === "left") {
if (posActLeft(Object) > posActLeft(headBad) - 5 && posActTop(Object) < 22) {
myPoint++;
changeHead();
letter();
}
} else {
if (posActLeft(Object) < posActLeft(headBad) + 5 && posActTop(Object) < 22) {
myPoint++;
changeHead();
letter();
}
}
myPunchs.text(myPoint);
}
function changeHead() {
headBad.css("visibility", "hidden");
headBadHit.css("visibility", "visible");
setTimeout(function() {
headBad.css("visibility", "visible");
headBadHit.css("visibility", "hidden");
}, 1000);
}
function verifyBadGloves(side) {
if (side === "left") {
if (posActLeft(myHead) > 5 && posActLeft(myHead) < 30) {
badPoint++;
letter();
}
} else {
if (posActLeft(myHead) > 25 && posActLeft(myHead) < 90) {
badPoint++;
letter();
}
}
badPunchs.text(badPoint);
}
function getRandomSide() {
num = Math.random() * 100;
if (num > 50)
return 'left';
return 'right';
}
function letter() {
num = Math.round(Math.random() * 3);
if (num === 1) {
smash.show(100);
smash.hide(1000);
} else if (num === 2) {
pow.show(100);
pow.hide(1000);
} else {
wtf.show(100);
wtf.hide(1000);
}
}
function posActLeft(Object) {
return Math.round((Object.css("left").substring(0, Object.css("left").length - 2) / window.innerWidth) * 100);
}
function posActTop(Object) {
return Math.round((Object.css("top").substring(0, Object.css("top").length - 2) / window.innerHeight) * 100);
}
function moveSide(Object, posAct, lengthPercent, time) {
setTimeout(function() {
back = (posAct + lengthPercent);
front = (posAct - lengthPercent);
side = getRandomSide();
if (side === "left") {
Object.animate({
left: back.toString() + '%'
}, time);
Object.animate({
left: posAct.toString() + '%'
}, time);
}
else {
Object.animate({
left: front.toString() + '%'
}, time);
Object.animate({
left: posAct.toString() + '%'
}, time);
}
}, Math.random() * 1000);
}
function moveUpDown(Object, posAct, lengthPercent, time) {
setTimeout(function() {
back = (posAct + lengthPercent);
front = (posAct - lengthPercent);
Object.animate({
top: back.toString() + '%'
}, time);
Object.animate({
top: posAct.toString() + '%'
}, time);
}, Math.random() * 1000);
}
function punchBad(Object, Object2, alert) {
alert.css("visibility", "visible")
setTimeout(function() {
Object.css("visibility", "hidden");
Object2.css("visibility", "visible");
alert.css("visibility", "hidden");
}, 800);
setTimeout(function() {
Object.css("visibility", "visible");
Object2.css("visibility", "hidden");
}, 1200);
}
function punchMine(Object, side, top, left) {
moveTop2 = "20%";
if (side === "left") {
moveLeft2 = "35%";
} else {
moveLeft2 = "55%";
}
if (posActTop(Object) == 70) {
Object.stop(true, true).animate({"top": moveTop2, "left": moveLeft2}, 400);
Object.stop(true, true).animate({"top": top.toString() + "%", "left": left.toString() + "%"}, 400);
verifyMyGloves(Object, side);
}
}
function myHeadPosition(Object) {
Object.css("left", Math.round((window.innerWidth / 2) - (Object.width() / 2) + (X * (15))));
}
function cargaImg() {
var img1 = new Image();
img1.src = 'img/gloveRightBad.png';
img1.onload = function() {
cargaContextoCanvas(gloveRightBad[0]).drawImage(img1, 0, 0);
}
var img2 = new Image();
img2.src = 'img/gloveLeftBad.png';
img2.onload = function() {
cargaContextoCanvas(gloveLeftBad[0]).drawImage(img2, 0, 0);
}
var img3 = new Image();
img3.src = 'img/gloveLeftMine.png';
img3.onload = function() {
cargaContextoCanvas(gloveLeftMine[0]).drawImage(img3, 0, 0);
}
var img4 = new Image();
img4.src = 'img/gloveRightMine.png';
img4.onload = function() {
cargaContextoCanvas(gloveRightMine[0]).drawImage(img4, 0, 0);
}
imga = new Image();
imga.src = 'img/gloveRightBadHit.png';
imga.onload = function() {
cargaContextoCanvas(gloveRightBadHit[0]).drawImage(imga, 0, 0);
}
imgb = new Image();
imgb.src = 'img/gloveLeftBadHit.png';
imgb.onload = function() {
cargaContextoCanvas(gloveLeftBadHit[0]).drawImage(imgb, 0, 0);
}
var img6 = new Image();
img6.src = 'img/youwin.png';
img6.onload = function() {
cargaContextoCanvas(win[0]).drawImage(img6, 0, 0);
}
var img7 = new Image();
img7.src = 'img/youlost.png';
img7.onload = function() {
cargaContextoCanvas(lost[0]).drawImage(img7, 0, 0);
}
var img8 = new Image();
img8.src = 'img/boxeador.png';
img8.onload = function() {
cargaContextoCanvas(myHead[0]).drawImage(img8, 0, 0);
}
var img9 = new Image();
img9.src = 'img/opponent.png';
img9.onload = function() {
cargaContextoCanvas(headBad[0]).drawImage(img9, 0, 0);
}
var img10 = new Image();
img10.src = 'img/opponentHit.png';
img10.onload = function() {
cargaContextoCanvas(headBadHit[0]).drawImage(img10, 0, 0);
}
var img11 = new Image();
img11.src = 'img/pow.png';
img11.onload = function() {
cargaContextoCanvas(pow[0]).drawImage(img11, 0, 0);
}
var img12 = new Image();
img12.src = 'img/smash.png';
img12.onload = function() {
cargaContextoCanvas(smash[0]).drawImage(img12, 0, 0);
}
var img13 = new Image();
img13.src = 'img/wtf.png';
img13.onload = function() {
cargaContextoCanvas(wtf[0]).drawImage(img13, 0, 0);
}
}
function follow(Object, Follower) {
Follower.css("left", Object.css("left"));
}
function cargaContextoCanvas(elemento) {
if (elemento && elemento.getContext) {
var contexto = elemento.getContext('2d');
if (contexto) {
return contexto;
}
}
return false;
}
function init() {
punch.hide();
win.hide();
lost.hide();
myHead.css("left", (window.innerWidth / 2) - (myHead.width() / 2));
gradient = cargaContextoCanvas(red[0]).createRadialGradient(red[0].width / 2, red[0].height / 2, 0, red[0].width / 2, red[0].height / 2, 100);
gradient.addColorStop(0, '#F00300');
gradient.addColorStop(1, '#FFF');
cargaContextoCanvas(red[0]).fillStyle = gradient;
cargaContextoCanvas(red[0]).fillRect(0, 0, 600, 800);
cargaContextoCanvas(red2[0]).fillStyle = gradient;
cargaContextoCanvas(red2[0]).fillRect(0, 0, 600, 800);
cargaImg();
}
function startGame() {
var Idtimer = setInterval(function() {
timerDiv.text(timer);
if (timer > 0) {
timer--;
}
else {
if (badPoint <= myPoint) {
win.show(500);
headBad.css("visibility", "hidden");
headBadHit.css("visibility", "visible");
}
else {
lost.show(500);
}
clearInterval(Idtimer);
clearInterval(IdHead);
clearInterval(IdHeadHit);
}
}, 1000);
time = 0;
while (time < 56000) {
time += Math.round(1000 + Math.random() * 4000);
setTimeout(punchBad, time, gloveLeftBad, gloveLeftBadHit, red);
setTimeout(verifyBadGloves, time + 800, "left");
}
time = 0;
while (time < 57000) {
time += Math.round(1000 + Math.random() * 4000);
setTimeout(punchBad, time, gloveRightBad, gloveRightBadHit, red2);
setTimeout(verifyBadGloves, time + 800, "right");
}
var IdHead = setInterval(moveSide, 1000, headBad, posActLeft(headBad), 10, 200);
var IdHeadHit = setInterval(follow, 1, headBad, headBadHit);
setInterval(moveUpDown, 500, gloveLeftBad, posActTop(gloveLeftBad), 5, 200);
setInterval(moveUpDown, 500, gloveRightBad, posActTop(gloveRightBad), 5, 200);
}
//##########################################################################################################################
//##########################################################################################################################
//##########################################################################################################################
init();
//startGame();
//@TODO Poner las instrucciones, y boton comenzar, al terminar debe salir un cartel de que perdistes o ganastes.
});