-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper_interaction.pjs
446 lines (345 loc) · 10.6 KB
/
paper_interaction.pjs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var hitOptions = {
fill: true,
tolerance: 1
};
project.currentStyle = {
strokeColor: '#000000',
strokeWidth: 3
};
//var explodeSound = new Audio("sonidos/mp3/Bow_Fire_Arrow-Stephan_Schutze-2133929391.mp3");
var explodeSound = "sonidos/mp3/bowfire.mp3";
var initSound = "sonidos/mp3/ray.mp3";
var endSound = "sonidos/mp3/buzzer.mp3";
var shakeSound = "sonidos/mp3/metroid.mp3";
var level = 2;
var leveltime = 15*60;
var bonusShape = 4;
var bonusTam = 40;
var bonusColor = 'white';
var bonusProb = 1;
var bonusTime = 180;
var bonusAumentaTiempo = 1;
var colorSelected = undefined;
var shapeSelected = undefined;
var sizeSelected = undefined;
var punto = 0;
var playtime = 3600;
var restartTime = 300;
var addtime = 120;
var segtime = 60;
var shake = 2;
var items = new Array();
var itemsBonus = new Array();
var figuresleft;
var cuadrosArray = [];
var colors = new Array('green', 'red', 'yellow','blueviolet','orange', 'darkcyan', 'fuchsia');
var sizes = new Array(40,60,80);
var sizesLabels = new Array("small","medium","large");
var shapes = new Array(1,2,3);
var shapesLabels = new Array("squares","circles","stars");
var colorCount = new Array(0,0,0,0,0);
var sizeCount = new Array(0,0,0);
var shapesCount = new Array(0,0,0);
function playSound(sound){
// alert(sound);
audio = new Audio(sound);
//audio.currentTime = 0;
audio.load();
audio.play();
return audio;
}
function randomXToY(minVal,maxVal,floatVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
function buscarCondicion(){
var colorTemp = randomXToY(0, level + 2);
var sizeTemp = randomXToY(0, sizes.length + 1);
var shapeTemp = randomXToY(0, shapes.length + 1);
sizeSelected = sizeTemp >= sizes.length? undefined: sizes[sizeTemp];
if(sizeSelected!=undefined && sizeCount[sizeTemp]==0){
sizeSelected = undefined;
}
colorSelected = colorTemp >= level ? undefined: colors[colorTemp];
if(colorSelected!=undefined && colorCount[colorTemp]==0){
colorSelected = undefined;
}
shapeSelected = shapeTemp >= shapes.length? undefined: shapes[shapeTemp];
if(shapeSelected!=undefined && shapesCount[shapeTemp]==0){
shapeSelected = undefined;
}
var mensaje = sizeSelected==undefined?'':sizesLabels[sizeTemp];
mensaje += colorSelected==undefined?'':' '+colorSelected;
mensaje += shapeSelected==undefined?'':' '+shapesLabels[shapeTemp];
var tmp = mensaje;
if(tmp.replace(/^\s+/g,'').replace(/\s+$/g,'').length == 0){
mensaje = 'get ALL the points!!! ';
}else{
mensaje ="Search for -> "+mensaje;
}
document.getElementById("search").innerHTML = mensaje;
document.getElementById("search").style.color = "greenyellow";
document.getElementById("search").style.fontSize = "18pt";
}
function create(shape, x, y, tam, color){
//alert("Creando "+shape+" --" + x+" "+y+" "+tam+" "+color);
switch(shape){
case 1:
var point = new Point(x, y);
//var size = new Size(tam, tam);
//var rectangle = new Rectangle(point, size);
//r = new Path.Rectangle(rectangle);
r = Path.RegularPolygon(point, 4, tam);
r.shape = 1;
break;
case 2:
var point = new Point(x, y);
r = new Path.Circle(point, tam);
r.shape = 2;
break;
case 3:
var point = new Point(x, y);
r = new Path.Star(point, 5, tam, tam - (tam/2));
r.shape = 3;
break;
case 4:
var point = new Point(x, y);
r = new Path.Star(point, 7, tam, tam - (tam/2));
r.shape = 4;
break;
}
r.fillColor = color;
r.selectedColor = color;
r.tam = tam;
items[items.length] = r;
if(esBonus(r)){
itemsBonus[itemsBonus.length] = new Array(r,bonusAumentaTiempo);
}
}
function populate(cant){
var canvas = document.getElementById('paperInteraction');
for(var i=0; i<cant; i++){
var xr = randomXToY(0, canvas.width-10);
var xy = randomXToY(0, canvas.height-10);
var indColor = randomXToY(0, level-1);
var color = colors[indColor];
var indTam = randomXToY(0, sizes.length-1);
var tam = sizes[indTam];
var indShape = randomXToY(0, shapes.length-1);
var shape = shapes[indShape];
colorCount[indColor]++;
sizeCount[indTam]++;
shapesCount[indShape]++;
create(shape,xr,xy,tam,color);
}
}
function refresh(cant, nuevo){
var canvas = document.getElementById('paperInteraction');
var context = canvas.getContext('2d');
clearCanvas(context, canvas);
if(nuevo){
rellenar(cant);
}else{
populate(cant);
}
}
function init(cant) {
var temp = playSound(initSound);
punto = 0;
level = 2;
leveltime = 15*60;
playtime = 5400;
figuresleft = randomXToY(90, 160);
document.getElementById("puntos").innerHTML = figuresleft;
document.getElementById("timer").innerHTML = ( playtime / segtime ) + " segs";
document.getElementById("timer").style.color = 'white';
document.getElementById("timer").style.fontSize = '16pt';
refresh(cant, true);
temp.pause();
}
function rellenar(cant){
populate(cant);
buscarCondicion();
}
function sumarPunto(){
figuresleft--;
document.getElementById("puntos").innerHTML = figuresleft;
};
function esBonus(hitItem){
if(hitItem.shape==bonusShape){
return true;
}
}
function accionBonus(item){
for (var i=0; i<itemsBonus.length; í++){
if(itemBonus[i][0]==item){
//veo que tipo de bonus es
switch(itemBonus[i][1]){
case bonusAumentaTiempo:
playtime += 300;
break;
}
itemBonus.splice(i,1); //elimino la entrada
}
}
}
function apareceBonus(tipo){
var x = randomXToY(0, canvas.width-10);
var y = randomXToY(0, canvas.height-10);
create(bonusShape, x, y, bonusTam, bonusColor)
}
function desapareceBonus(){
if(itemsBonus.length > 0){
itemsBonus.shift()
}
}
function onMouseUp(event) {
var hitResult = project.hitTest(event.point, hitOptions);
var remove = true;
if (hitResult && hitResult.item ) {
if (esBonus(hitResult.item)){
hitResult.item.remove();
accionBonus(item);
}
else{
remove = remove && ((colorSelected!=undefined && hitResult.item.selectedColor==colorSelected) || colorSelected==undefined);
remove = remove && ((sizeSelected!=undefined && hitResult.item.tam==sizeSelected) || sizeSelected==undefined);
remove = remove && ((shapeSelected!=undefined && hitResult.item.shape==shapeSelected) || shapeSelected==undefined);
if(remove){
var temp = playSound(explodeSound);
hitResult.item.remove();
sumarPunto();
temp.pause();
}
}
}
}
function onMouseDrag(event) {
// Move the last segment point of the path to the
// current position of the mouse:
var hitResult = project.hitTest(event.point, { fill: true, tolerance: -1});
if (hitResult && hitResult.item ) {
create(hitResult.item.shape,event.point.x,event.point.y,hitResult.item.tam,hitResult.item.selectedColor);
hitResult.item.remove();
}
}
function clearCanvas(context, canvas) {
for(var k=items.length; k>=0;k--){
if(items[k]!=undefined){items[k].remove();}
}
items = new Array();
itemsBonus = new Array();
context.clearRect(0, 0, canvas.width, canvas.height);
var w = canvas.width;
canvas.width = 1;
canvas.width = w;
colorCount = new Array(0,0,0,0,0);
sizeCount = new Array(0,0,0);
shapesCount = new Array(0,0,0);
}
function quitarEfecto(){
document.getElementById("search").style.color = "white";
document.getElementById("search").style.fontSize = "16pt";
document.getElementById("search").style.fontWeight = 'normal';
}
function customDialog() {
try {
var buttons = ["Yes", "Get me out of here!!"];
var res = blackberry.ui.dialog.customAsk("Do you wanna play again?", buttons, 0, true);
if(res){
blackberry.app.exit();
}else{
init(20);
}
} catch(e) {
alert("Exception in customDialog: " + e);
}
}
function onFrame(event) {
if(playtime==0 || figuresleft <= 0){
var temp = playSound(endSound);
var msg = "";
//Si gano
if(figuresleft < 0){
msg = "You won!!! Do you wanna play again?";
}else{
msg = "LOSERRRR!!! Do you wanna play again?";
}
temp.pause();
if(confirm(msg, "Game Over!!!!")){
init(20);
}else{
blackberry.app.exit();
};
//customDialog();
return;
}
playtime--;
if(playtime%segtime==0){
document.getElementById("timer").innerHTML = ( playtime / segtime ) + " segs";
shake=2;
if (playtime<600){
document.getElementById("timer").style.color = 'red';
document.getElementById("timer").style.fontSize = '20pt';
}
}
if(playtime%leveltime==0){
if(level<colors.length){level++;}
}
// setTimeout(actualizarTimer, 1000);
//Verificamos si hay que agregar
if(playtime%addtime==0){
quitarEfecto();
populate(2);
}
//Verificamos si hay que reiniciar condiciones
if(playtime%restartTime==0){
rellenar(2);
}
if(randomXToY(0, 100)==bonusProb){
apareceBonus(bonusAumentaTiempo);
}
if (playtime%bonusTime==0) {
desapareceBonus();
}
}
if (typeof window.DeviceMotionEvent != 'undefined') {
// Shake sensitivity (a lower number is more)
var sensitivity = 4;
// Position variables
var x1 = 0, y1 = 0, z1 = 0, x2 = 0, y2 = 0, z2 = 0;
// Listen to motion events and update the position
window.addEventListener('devicemotion', function (e) {
x1 = e.accelerationIncludingGravity.x;
y1 = e.accelerationIncludingGravity.y;
z1 = e.accelerationIncludingGravity.z;
}, false);
// Periodically check the position and fire
// if the change is greater than the sensitivity
setInterval(function () {
var change = Math.abs(x1-x2+y1-y2+z1-z2);
if (change > sensitivity ) {
shake--;
if(shake==0){
var temp = playSound(initSound);
refresh(20, false);
temp.pause();
}
}
// Update new position
x2 = x1;
y2 = y1;
z2 = z1;
}, 200);
}
/*
window.addEventListener("devicemotion", function(event) {
shake--;
if(shake==0){
alert("epa");
}
}, true);
*/
init(20);
//setTimeout(actualizarTimer, 1000);