|
13 | 13 | 'stages/04': stage4, |
14 | 14 | 'stages/05': stage5, |
15 | 15 | 'stages/06': stage6, |
16 | | - 'stages/07': stage7 |
| 16 | + 'stages/07': stage7, |
| 17 | + 'stages/08': stage8 |
17 | 18 | }; |
18 | 19 |
|
19 | 20 | demoDoc_ = demo.contentDocument; |
|
511 | 512 |
|
512 | 513 | } |
513 | 514 |
|
| 515 | + /** |
| 516 | + * 調整超音波在 <10,10~20,20~30,30~40,>40,五個區間,會判斷是否符合對應的速度,符合則過關 |
| 517 | + */ |
| 518 | + function stage8() { |
| 519 | + var demoWin = demoDoc_.defaultView; |
| 520 | + var ultrasonics = engine_.list().ultrasonic; |
| 521 | + var check = [false, false, false, false, false]; |
| 522 | + var isPassed = false; |
| 523 | + var youtube; |
| 524 | + |
| 525 | + if (!ultrasonics.length) return; |
| 526 | + |
| 527 | + Promise.resolve() |
| 528 | + .then(getYoutube) |
| 529 | + .then(doSomething) |
| 530 | + .catch(errorHandler); |
| 531 | + |
| 532 | + function getYoutube() { |
| 533 | + return new Promise(function (resolve, reject) { |
| 534 | + var TIMES = 10; |
| 535 | + var times = 0; |
| 536 | + |
| 537 | + setTimeout(function aa() { |
| 538 | + times++; |
| 539 | + |
| 540 | + if (demoWin.youtube_) { |
| 541 | + youtube = demoWin.youtube_; |
| 542 | + resolve(); |
| 543 | + return; |
| 544 | + } |
| 545 | + |
| 546 | + if (times > TIMES) { |
| 547 | + reject(); |
| 548 | + return; |
| 549 | + } |
| 550 | + |
| 551 | + setTimeout(aa, 1000); |
| 552 | + }, 1000); |
| 553 | + |
| 554 | + }); |
| 555 | + } |
| 556 | + |
| 557 | + function doSomething() { |
| 558 | + ultrasonics.forEach(function (us) { |
| 559 | + us.setSendHandler(send); |
| 560 | + }); |
| 561 | + } |
| 562 | + |
| 563 | + function errorHandler() { |
| 564 | + return; |
| 565 | + } |
| 566 | + |
| 567 | + // 當超音波距離不同時,做檢查 |
| 568 | + function send(distance) { |
| 569 | + var rate = youtube.getPlaybackRate(); |
| 570 | + var area = [0, 10, 20, 30, 40]; |
| 571 | + |
| 572 | + // 找出距離落在的區間,區間 1: 0~10,區間 2: 10~20,以此類推,大於 40 後,算區間 5 |
| 573 | + var areaLevel = area.reduce(function (acc, val, idx, ary) { |
| 574 | + if (distance > val) { |
| 575 | + return idx + 1; |
| 576 | + } |
| 577 | + return acc; |
| 578 | + }, 0); |
| 579 | + |
| 580 | + if (areaLevel === 1 && rate === 0.5) check[0] = true; |
| 581 | + if (areaLevel === 2 && rate === 1) check[1] = true; |
| 582 | + if (areaLevel === 3 && rate === 1.25) check[2] = true; |
| 583 | + if (areaLevel === 4 && rate === 1.5) check[3] = true; |
| 584 | + if (areaLevel === 5 && rate === 2) check[4] = true; |
| 585 | + |
| 586 | + checking(); |
| 587 | + } |
| 588 | + |
| 589 | + function checking() { |
| 590 | + if (isPassed) return; |
| 591 | + |
| 592 | + isPassed = check.reduce(function (acc, val) { |
| 593 | + return acc && val; |
| 594 | + }, true); |
| 595 | + |
| 596 | + if (isPassed) { |
| 597 | + checkOver(); |
| 598 | + |
| 599 | + // 考量畫面有些動畫未完成,所以延遲 1 秒執行 |
| 600 | + setTimeout(function () { |
| 601 | + alert('第 8 關 過關!'); |
| 602 | + }, 1000); |
| 603 | + } |
| 604 | + } |
| 605 | + |
| 606 | + function checkOver() { |
| 607 | + ultrasonics.forEach(function (us) { |
| 608 | + us.setSendHandler(null); |
| 609 | + }); |
| 610 | + } |
| 611 | + |
| 612 | + } |
| 613 | + |
514 | 614 |
|
515 | 615 | Code.stageCheck = { |
516 | 616 | init: init |
|
0 commit comments