-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
588 lines (532 loc) · 13.7 KB
/
objects.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
var orbit={
r:170,
x:320,
y:940,
angle:0,
color:'grey',
drawOrbit:function(){
context.save();
context.strokeStyle=this.color;
context.beginPath();
context.arc(orbit.x,orbit.y,orbit.r,0,Math.PI*2,false);
context.stroke();
context.restore();
},
}
var startInterface={
drawTitle:function(){
context.save();
context.fillStyle='white';//画标题
context.font="250px arial ";
context.fillText('ZWEI',0,220);
context.restore();
},
drawStartButton:function(){
context.save();
var px=320,
py=920,
r1=64,
r2=62;
b=30.32;
a=35;
context.beginPath();
context.translate(px,py);
context.arc(0,0,r1,0,Math.PI*2,false);
context.arc(0,0,r2,0,Math.PI*2,true);
context.moveTo(-24,30);
context.lineTo(38,0);
context.lineTo(-24,-30);
context.lineTo(-24,30);
context.fillStyle='white';
context.fill();
context.restore();
},
drawNotice:function(){
context.save();
context.font='30px 宋体';
context.fillStyle='white';
context.fillText('拖动屏幕空白处可以调整游戏界面大小',70,1106)
context.restore();
}
}
var Rect=function(){
//var rect=new Object();
this.height=60;
this.width=315;
this.x=0;
this.y=0;
this.deltaY=0;
this.velocityY=420;
this.color='white';
this.viberation=0;
this.shadowHeight=240;
this.shadowWidth=315;
this.shadowViberation=10;
this.interval=null;
this.makeItSpecial=function() {
var rand=Math.floor(Math.random()*6+1);
switch (rand){
case 1:
this.x=35;
break;
case 2:
this.x=260;
break;
case 3:
this.x=225;
this.width=190;
this.shadowWidth=190;
this.velocityY=470;
break;
case 4:
this.x=60;
this.width=140;
this.height=140;
this.velocityY=350;
this.shadowWidth=140;
break;
case 5:
this.x=440;
this.width=140;
this.height=140;
this.velocityY=350;
this.shadowWidth=140;
break;
case 6:
this.x=300;
this.width=40;
this.height=160;
this.shadowWidth=40;
break;
}
if(Math.random()>0.5){
//this.velocityY=2*this.velocityY;
}
}
this.update=function(){
this.deltaY=(fps.elapsedTime/1000)*this.velocityY;//乘以一个随时间增长的系数增加难度
this.y+=this.deltaY;
this.viberation=Math.sin(this.y/30)*1;
this.height-=this.viberation;
this.width-=this.viberation;
this.shadowWidth-=this.viberation;
this.shadowHeight-=this.viberation;
this.shadowViberation=Math.sin(this.y/30)*10;//调整阴影抖动的频率和幅度
}
this.drawRect=function(){
context.save();
context.fillStyle=this.color;
context.fillRect(this.x,this.y,this.width,this.height);
context.restore();
}
this.drawShadow=function(){
context.save();
gradient=context.createLinearGradient(this.x,this.y,this.x,this.y-this.shadowHeight);
gradient.addColorStop(0,'rgba(200,200,200,0.2)');
gradient.addColorStop(1,'rgba(200,200,200,0)');
context.fillStyle=gradient;
context.beginPath();
context.moveTo(this.x,this.y+this.height);
context.quadraticCurveTo(this.x-this.shadowViberation,this.y-this.shadowHeight/4,
this.x,this.y-this.shadowHeight/2);
context.quadraticCurveTo(this.x+this.shadowViberation,this.y-this.shadowHeight*3/4,
this.x,this.y-this.shadowHeight);
context.lineTo(this.x+this.shadowWidth,this.y-this.shadowHeight);
context.quadraticCurveTo(this.x+this.shadowWidth-this.shadowViberation,this.y-this.shadowHeight*3/4,
this.x+this.shadowWidth,this.y-this.shadowHeight/2);
context.quadraticCurveTo(this.x+this.shadowWidth+this.shadowViberation,this.y-this.shadowHeight/4,
this.x+this.shadowWidth,this.y);
context.lineTo(this.x,this.y+this.height);
context.fill();
context.restore();
}
this.ceshi="Rect"
//return rect;
}
var CreateRect=function(){};
CreateRect.prototype=new Rect();//继承了Rect的所有属性和方法,在调用继承的方法的时候,是不会改动原型的,
//会创造一个新的属性。如果没有自己的属性,就会从原型访问
CreateRect.prototype.constructor='CreateRect';
CreateRect.prototype.ceshi='CreateRect';
var CreateBall=function(){
//var this = new Object();
this.r=25;
this.x=50;
this.y=0;
this.blueX=0;
this.blueY=0;
this.redX=0;
this.redY=0;
this.angle=0;
this.deltaAngle=0;
this.rotate=false;
this.rotateDirection=1;
this.timePerRound=1.1;
this.blueBoom=new Image();
this.blueBoom.src='blue.png';
this.redBoom=new Image();
this.redBoom.src='red.png';
this.redBoomed=false;
this.blueBoomed=false;
this.getTrueLocation=function(){
this.redX=Math.cos(orbit.angle)*orbit.r+orbit.x;
this.redY=Math.sin(orbit.angle)*orbit.r+orbit.y;
this.blueX=Math.cos(orbit.angle+Math.PI)*orbit.r+orbit.x;
this.blueY=Math.sin(orbit.angle+Math.PI)*orbit.r+orbit.y;
}
this.updateBall=function(){
orbit.deltaAngle=(fps.elapsedTime/1000)*2*Math.PI/this.timePerRound
*this.rotateDirection;
orbit.angle=orbit.angle+orbit.deltaAngle;
};
this.drawBall=function(){
context.save();
context.translate(orbit.x,orbit.y);
context.rotate(orbit.angle);
//画红球
if(test.boomColor!='red'){
context.fillStyle='red';
context.beginPath();
context.arc(orbit.r,0,this.r,0,Math.PI*2);
context.closePath();
context.fill();
}
//画蓝球
if(test.boomColor!='blue'){
context.fillStyle='blue';
context.beginPath();
context.arc(-orbit.r,0,this.r,0,Math.PI*2);
context.closePath();
context.fill();
}
context.restore();
};
this.drawBoom=function(rn,color){//矩形编号和圆的颜色
var thisx,thisy,thisimg,rx,ry,rw,rh,opacity=1;
if(color==='red'){
thisx=this.redX;
thisy=this.redY;
thisimg=this.redBoom;
}
else if(color==='blue'){
thisx=this.blueX;
thisy=this.blueY;
thisimg=this.blueBoom;
}
rx=rectArray[rn].x;
ry=rectArray[rn].y;
rw=rectArray[rn].width;
rh=rectArray[rn].height;
context.save();
context.beginPath();
context.rect(rx,ry,rw,rh);
context.clip()
context.drawImage(thisimg,thisx-60,thisy-60,120,120)
context.restore();
}
//return this;
}
var Fps=function(){
//var fps=new Object();
this.timer=0;
this.boomTimer=0;
this.lastTime=0;
this.time=2000;
this.this=6;
this.elapsedTime=1000;
this.lastUpdateTime=0;
this.lastRectTime=0;
this.caculateFps=function(time){
this.time=time;
this.elapsedTime=this.time-this.lastTime;
this.lastTime=this.time;
if(time-this.lastUpdateTime>1000){
this.this=Math.floor(1000/this.elapsedTime);
this.lastUpdateTime=this.time;
}
}
this.caculateTimer=function(time){
this.timer+=this.elapsedTime;
}
this.drawFps=function(){
context.save();
context.fillStyle="white";
context.font="50px Arial";
context.fillText('FPS:'+this.this,0,50);
context.fillText('TIME:'+(this.timer/1000).toFixed(2),0,100);
context.restore();
}
//return fps;
}
var Controller=function(){
//var ctrl=new Object();
this.order='start';
this.location='start';
this.x=0;
this.y=0;
var that=this;
this.returnMouseLocation=function(e){
e.preventDefault();
//这样一来电脑,ios安卓都可以获得page的xy
if(e.pageX){
that.x=e.pageX;
that.y=e.pageY;
}
else{
that.x=e.touches[0].pageX;//ios可以识别pagex,但是安卓就不行,必须要touches.pagex
that.y=e.touches[0].pageY;
}
}
this.returnOrder=function(e){
e.preventDefault();
that.returnMouseLocation(e);
if(that.location==='game'){
if(that.x<0.5*canvas.width){
that.order='left';
}
else if(that.x>=0.5*canvas.width){
that.order='right';
}
}
else if(that.location==='start'){//点击圆形三角
if(that.x>260&&that.x<380&&that.y>860&&that.y<980){
that.order='startToGame';
}
}
else if(that.location==='end'){//点击圆形三角
if(that.x>260&&that.x<380&&that.y>860&&that.y<980){
ani.reset();
that.order='startToGame';//卡可能是这里有问题,这是两个函数
}
}
}
this.eventHandler=function(e){
e.preventDefault();
that.returnOrder(e);
switch(that.order){
case 'left':
ball.rotateDirection=-1;
ball.rotate=true;
break;
case 'right':
ball.rotateDirection=1;
ball.rotate=true;
break;
case 'startToGame':
that.location='startToGame';
break;
case 'game':
that.location='game';
break;
}
}
//return ctrl;
}
var Animitation=function(){
//var ani=new Object();
this.i=0;
this.j=0;
this.EndText='';
this.tip='';
var that=this;
this.getEndText=function(){
if(fps.timer<=10000){
this.EndText='弱爆啦,再试一次吧';
this.tip='判定点只有圆心';
}
else if(fps.timer<=20000){
this.EndText='还行吧,在加把劲吧';
this.tip='所有组合都有解';
}
else if(fps.timer<=30000){
this.EndText='你超越了80%的小伙伴';
this.tip='见缝就钻不要怂';
}
else if(fps.timer>30000){
this.EndText='不愧是单身20年的手速';
this.tip='我已经无话可说';
}
}
this.playStartInterface=function(time){
context.clearRect(0,0,canvas.width,canvas.height);
fps.caculateFps(time);
orbit.x=0.5*canvas.width;
orbit.y=0.5*canvas.height;
ball.updateBall();
ball.drawBall();
orbit.drawOrbit();
startInterface.drawTitle();
startInterface.drawStartButton();
startInterface.drawNotice();
window.requestNextAnimationFrame(that.playAnimitation);
}
this.playStartToGame=function(time){
context.clearRect(0,0,canvas.width,canvas.height);
fps.caculateFps(time);
context.save();
//调整从开始到游戏的过场动画
orbit.y=orbit.y<940?orbit.y+(fps.elapsedTime/1000)*300:940;//调整过场动画速度
ball.drawBall();
orbit.drawOrbit();
ball.updateBall();
context.restore();
hhh=window.requestNextAnimationFrame(that.playStartToGame);
if(orbit.y===940&&orbit.angle%(Math.PI*2)<0.1){
orbit.angle=0;
window.cancelAnimationFrame(hhh);
ctrl.location='game';
fps.timer=0;
window.requestNextAnimationFrame(that.playAnimitation);
}
}
this.playGame=function(time){
context.clearRect(0,0,canvas.width,canvas.height);
fps.caculateFps(time);
fps.caculateTimer(time);
fps.drawFps();
if(fps.time-fps.lastRectTime>900){//矩形产生的频率
fps.lastRectTime=fps.time;
createRectArray();
}
if(ball.rotate===true){
ball.updateBall();
}
for (that.j=0; that.j<rectArray.length ; that.j++) {
rectArray[that.j].update();
rectArray[that.j].drawRect();
rectArray[that.j].drawShadow();
};
orbit.drawOrbit();
ball.drawBall();
ball.getTrueLocation();
test.moveSystem();
window.requestNextAnimationFrame(that.playAnimitation);
}
this.playBoom=function(time){
fps.boomTimer+=fps.elapsedTime;
window.clearInterval(rect.interval);
context.clearRect(0,0,canvas.width,canvas.height);
fps.caculateFps(time);
fps.drawFps();
for (var j=0; j<rectArray.length ; j++) {
rectArray[j].drawRect();
rectArray[j].drawShadow();
};
if(ball.rotate===true){
ball.getTrueLocation();
}
orbit.drawOrbit();
ball.drawBall();
test.moveSystem();
ball.drawBoom(test.boomRect,test.boomColor);
if(fps.boomTimer>1500){
ctrl.location='end';
that.getEndText();
}
window.requestNextAnimationFrame(that.playAnimitation);
}
this.playEnd=function(time){
context.clearRect(0,0,canvas.width,canvas.height);
context.save();
context.fillStyle="white";
context.font="50px Arial";
context.fillText('你坚持了'+(fps.timer/1000).toFixed(2)+'秒',145,145);
context.fillText(that.EndText,100,220);
context.fillText('快分享到朋友圈',150,450);
context.fillText('比比谁手速更快~',150,520);
context.font='30px 楷体';
context.fillText('小提示:'+that.tip,150,700);
context.font='15px 宋体';
context.fillText('Copy from DUET',265,1106);
var px=320,//画开始按钮
py=920,
r1=64,
r2=62;
b=30.32;
a=35;
context.beginPath();
context.translate(px,py);
context.arc(0,0,r1,0,Math.PI*2,false);
context.arc(0,0,r2,0,Math.PI*2,true);
context.moveTo(-24,30);
context.lineTo(38,0);
context.lineTo(-24,-30);
context.lineTo(-24,30);
context.fill();
context.restore();
window.requestNextAnimationFrame(that.playAnimitation);
}
this.playAnimitation=function(time){
switch(ctrl.location){
case 'start':
that.playStartInterface(time);
break;
case 'startToGame':
that.playStartToGame(time);
break;
case 'game':
that.playGame(time);
break;
case 'boom':
that.playBoom(time);
break;
case 'end':
document.title=('测试你是不是手残的时候到了,我坚持了'+(fps.timer/1000).toFixed(2)+'秒!');
that.playEnd(time);
break;
}
}
this.reset=function(time){
rectArray=new Array();//重置游戏
that.i=0;
that.j=0;
fps.boomTimer=0;
fps.timer=0;
orbit.angle=0;
test.boomColor=null;
ball.redX=0;
ball.redY=0;
ball.blueX=0;
ball.blueY=0;
}
//return ani;
}
var ImpactTest=function () {
//var test=new Object();
this.boomColor=null;
this.boomRect=null;
this.moveSystem=function (){
var offsetX,offsetY;
for (var k = 0 ; k <= rectArray.length-1; k++) {
offsetY=rectArray[k].y+0.5*rectArray[k].height;
offsetX=rectArray[k].x+0.5*rectArray[k].width;
ball.ballRRelativeX=ball.redX-offsetX;
ball.ballRRelativeY=ball.redY-offsetY;
ball.ballBRelativeX=ball.blueX-offsetX;
ball.ballBRelativeY=ball.blueY-offsetY;
this.impactTest(ball.ballRRelativeX,ball.ballRRelativeY,rectArray[k].width,'red',k);
this.impactTest(ball.ballBRelativeX,ball.ballBRelativeY,rectArray[k].width,'blue',k);
}
}
this.impactTest=function(x,y,width,color,k){
x=Math.abs(x);
y=Math.abs(y);
var result;
//下面不能用switch,只能用if
if(x<0.5*width){//这里为了降低难度,碰撞判定只有圆心
result=0.5*rect.height;
}
//else if(x>0.5*.width&&x<.width+ball.r){
// result=Math.sqrt(ball.r*ball.r-(x-0.5*.width)*(x-0.5*.width))+0.5*.height;
// }
else if(x>0.5*width){
result=0;
}
if(y<result){
ctrl.location='boom';
this.boomColor=color;
this.boomRect=k;
}
}
//return test;
}