play.component.ts 18.6 KB
Newer Older
Chen Jiping's avatar
Chen Jiping committed
1
import { Component, ElementRef, ViewChild, OnInit, Input, OnDestroy, HostListener } from '@angular/core';
limingzhe's avatar
limingzhe committed
2 3

import {
Chen Jiping's avatar
Chen Jiping committed
4 5
  MySprite,
  ShapeRectNew
limingzhe's avatar
limingzhe committed
6 7

} from './Unit';
Chen Jiping's avatar
Chen Jiping committed
8
import { res, resAudio } from './resources';
limingzhe's avatar
limingzhe committed
9

Chen Jiping's avatar
Chen Jiping committed
10 11
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
liujiangnan's avatar
liujiangnan committed
12

limingzhe's avatar
limingzhe committed
13 14
import TWEEN from '@tweenjs/tween.js';

Chen Jiping's avatar
Chen Jiping committed
15
import { Title } from './Title';
limingzhe's avatar
limingzhe committed
16

Chen Jiping's avatar
Chen Jiping committed
17 18 19 20
import { Listening } from './Listening';
import { Exercises} from './Exercises';
import { getDefaultExercisesBean, getDefaultLetterBean } from '../bean/ExercisesBean';
import { getDefaultTitle } from '../bean/TitleBean';
limingzhe's avatar
limingzhe committed
21

liujiangnan's avatar
liujiangnan committed
22 23 24 25

@Component({
  selector: 'app-play',
  templateUrl: './play.component.html',
liujiaxin's avatar
liujiaxin committed
26
  styleUrls: ['./play.component.css']
liujiangnan's avatar
liujiangnan committed
27
})
limingzhe's avatar
limingzhe committed
28 29
export class PlayComponent implements OnInit, OnDestroy {

Chen Jiping's avatar
Chen Jiping committed
30 31
  @ViewChild('canvas', { static: true }) canvas: ElementRef;
  @ViewChild('wrap', { static: true }) wrap: ElementRef;
limingzhe's avatar
limingzhe committed
32

limingzhe's avatar
limingzhe committed
33 34
  // 数据
  data;
limingzhe's avatar
limingzhe committed
35 36 37

  ctx;

limingzhe's avatar
limingzhe committed
38 39
  canvasWidth = 1280; // canvas实际宽度
  canvasHeight = 720; // canvas实际高度
limingzhe's avatar
limingzhe committed
40

limingzhe's avatar
limingzhe committed
41 42
  canvasBaseW = 1280; // canvas 资源预设宽度
  canvasBaseH = 720;  // canvas 资源预设高度
limingzhe's avatar
limingzhe committed
43

limingzhe's avatar
limingzhe committed
44 45
  mx; // 点击x坐标
  my; // 点击y坐标
limingzhe's avatar
limingzhe committed
46 47 48 49 50 51 52 53 54 55 56 57


  // 资源
  rawImages = new Map(res);
  rawAudios = new Map(resAudio);

  images = new Map();

  animationId: any;
  winResizeEventStream = new Subject();

  audioObj = {};
limingzhe's avatar
limingzhe committed
58

Chen Jiping's avatar
Chen Jiping committed
59 60 61 62
  //A皮肤显示框
  skinRenderArr = [];

  renderArr = [];
limingzhe's avatar
limingzhe committed
63 64 65 66 67
  mapScale = 1;

  canvasLeft;
  canvasTop;

Chen Jiping's avatar
Chen Jiping committed
68
  saveKey = 'YM4-10';
limingzhe's avatar
limingzhe committed
69

Chen Jiping's avatar
Chen Jiping committed
70
  canTouch = true;
limingzhe's avatar
limingzhe committed
71

Chen Jiping's avatar
Chen Jiping committed
72 73
  /**标题*/
  title: Title;
limingzhe's avatar
limingzhe committed
74

Chen Jiping's avatar
Chen Jiping committed
75 76 77 78 79
  listening: Listening;

  curAudio;

  exercisesArr: Array<Exercises> = [];
limingzhe's avatar
limingzhe committed
80

Chen Jiping's avatar
Chen Jiping committed
81
  curIndex = 0;
limingzhe's avatar
limingzhe committed
82 83 84 85 86 87 88

  @HostListener('window:resize', ['$event'])
  onResize(event) {
    this.winResizeEventStream.next();
  }


limingzhe's avatar
limingzhe committed
89
  ngOnInit() {
limingzhe's avatar
limingzhe committed
90

limingzhe's avatar
limingzhe committed
91
    this.data = {};
limingzhe's avatar
limingzhe committed
92

limingzhe's avatar
limingzhe committed
93
    // 获取数据
Chen Jiping's avatar
Chen Jiping committed
94
    const getData = (<any>window).courseware.getData;
limingzhe's avatar
limingzhe committed
95
    getData((data) => {
limingzhe's avatar
limingzhe committed
96

limingzhe's avatar
limingzhe committed
97 98 99 100
      if (data && typeof data == 'object') {
        this.data = data;
      }
      // console.log('data:' , data);
limingzhe's avatar
limingzhe committed
101

limingzhe's avatar
limingzhe committed
102 103
      // 初始化 各事件监听
      this.initListener();
limingzhe's avatar
limingzhe committed
104

limingzhe's avatar
limingzhe committed
105 106
      // 若无数据 则为预览模式 需要填充一些默认数据用来显示
      this.initDefaultData();
limingzhe's avatar
limingzhe committed
107

limingzhe's avatar
limingzhe committed
108 109 110 111 112 113
      // 初始化 音频资源
      this.initAudio();
      // 初始化 图片资源
      this.initImg();
      // 开始预加载资源
      this.load();
limingzhe's avatar
limingzhe committed
114

limingzhe's avatar
limingzhe committed
115
    }, this.saveKey);
limingzhe's avatar
limingzhe committed
116

limingzhe's avatar
limingzhe committed
117
  }
limingzhe's avatar
limingzhe committed
118

limingzhe's avatar
limingzhe committed
119 120 121
  ngOnDestroy() {
    window['curCtx'] = null;
    window.cancelAnimationFrame(this.animationId);
limingzhe's avatar
limingzhe committed
122 123 124
  }


limingzhe's avatar
limingzhe committed
125
  load() {
limingzhe's avatar
limingzhe committed
126

limingzhe's avatar
limingzhe committed
127 128 129 130 131 132
    // 预加载资源
    this.loadResources().then(() => {
      window["air"].hideAirClassLoading(this.saveKey, this.data);
      this.init();
      this.update();
    });
limingzhe's avatar
limingzhe committed
133 134 135
  }


limingzhe's avatar
limingzhe committed
136
  init() {
limingzhe's avatar
limingzhe committed
137

limingzhe's avatar
limingzhe committed
138 139 140
    this.initCtx();
    this.initData();
    this.initView();
Chen Jiping's avatar
Chen Jiping committed
141 142 143 144

    this.canTouch = true;

    this.playAudio('page', true);
limingzhe's avatar
limingzhe committed
145
  }
limingzhe's avatar
limingzhe committed
146

limingzhe's avatar
limingzhe committed
147 148 149 150 151
  initCtx() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.canvas.nativeElement.width = this.wrap.nativeElement.clientWidth;
    this.canvas.nativeElement.height = this.wrap.nativeElement.clientHeight;
limingzhe's avatar
limingzhe committed
152 153


limingzhe's avatar
limingzhe committed
154 155 156
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.canvas.nativeElement.width = this.canvasWidth;
    this.canvas.nativeElement.height = this.canvasHeight;
limingzhe's avatar
limingzhe committed
157

limingzhe's avatar
limingzhe committed
158
    window['curCtx'] = this.ctx;
limingzhe's avatar
limingzhe committed
159 160 161 162 163 164 165
  }






limingzhe's avatar
limingzhe committed
166 167 168 169 170
  updateItem(item) {
    if (item) {
      item.update();
    }
  }
limingzhe's avatar
limingzhe committed
171

limingzhe's avatar
limingzhe committed
172 173 174 175 176 177 178 179
  updateArr(arr) {
    if (!arr) {
      return;
    }
    for (let i = 0; i < arr.length; i++) {
      arr[i].update(this);
    }
  }
limingzhe's avatar
limingzhe committed
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







  initListener() {

    this.winResizeEventStream
      .pipe(debounceTime(500))
      .subscribe(data => {
        this.renderAfterResize();
      });


    // ---------------------------------------------
    const setParentOffset = () => {
      const rect = this.canvas.nativeElement.getBoundingClientRect();
      this.canvasLeft = rect.left;
      this.canvasTop = rect.top;
    };
    const setMxMyByTouch = (event) => {
      if (event.touches.length <= 0) {
        return;
      }
      if (this.canvasLeft == null) {
        setParentOffset();
      }
      this.mx = event.touches[0].pageX - this.canvasLeft;
      this.my = event.touches[0].pageY - this.canvasTop;
    };

    const setMxMyByMouse = (event) => {
      this.mx = event.offsetX;
      this.my = event.offsetY;
    };
    // ---------------------------------------------


    let firstTouch = true;

    const touchDownFunc = (e) => {
      if (firstTouch) {
        firstTouch = false;
        removeMouseListener();
      }
      setMxMyByTouch(e);
      this.mapDown(e);
    };
    const touchMoveFunc = (e) => {
      setMxMyByTouch(e);
      this.mapMove(e);
    };
    const touchUpFunc = (e) => {
      setMxMyByTouch(e);
      this.mapUp(e);
    };

    const mouseDownFunc = (e) => {
      if (firstTouch) {
        firstTouch = false;
        removeTouchListener();
      }
      setMxMyByMouse(e);
      this.mapDown(e);
    };
    const mouseMoveFunc = (e) => {
      setMxMyByMouse(e);
      this.mapMove(e);
    };
    const mouseUpFunc = (e) => {
      setMxMyByMouse(e);
      this.mapUp(e);
    };


    const element = this.canvas.nativeElement;

    const addTouchListener = () => {
      element.addEventListener('touchstart', touchDownFunc);
      element.addEventListener('touchmove', touchMoveFunc);
      element.addEventListener('touchend', touchUpFunc);
      element.addEventListener('touchcancel', touchUpFunc);
    };
    const removeTouchListener = () => {
      element.removeEventListener('touchstart', touchDownFunc);
      element.removeEventListener('touchmove', touchMoveFunc);
      element.removeEventListener('touchend', touchUpFunc);
      element.removeEventListener('touchcancel', touchUpFunc);
    };

    const addMouseListener = () => {
      element.addEventListener('mousedown', mouseDownFunc);
      element.addEventListener('mousemove', mouseMoveFunc);
      element.addEventListener('mouseup', mouseUpFunc);
    };
    const removeMouseListener = () => {
      element.removeEventListener('mousedown', mouseDownFunc);
      element.removeEventListener('mousemove', mouseMoveFunc);
      element.removeEventListener('mouseup', mouseUpFunc);
    };

    addMouseListener();
    addTouchListener();
  }


  playAudio(key, now = false, callback = null) {

    const audio = this.audioObj[key];
    if (audio) {
      if (now) {
        audio.pause();
        audio.currentTime = 0;
      }

      if (callback) {
        audio.onended = () => {
          callback();
        };
      }
      audio.play();
    }
  }

Chen Jiping's avatar
Chen Jiping committed
306 307 308 309
  pauseAudio(key, callback = null) {

    const audio = this.audioObj[key];
    if (audio) {
limingzhe's avatar
limingzhe committed
310

Chen Jiping's avatar
Chen Jiping committed
311 312 313 314 315 316 317 318
      if (callback) {
        audio.onended = () => {
          callback();
        };
      }
      audio.pause();
    }
  }
limingzhe's avatar
limingzhe committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332

  loadResources() {
    const pr = [];
    this.rawImages.forEach((value, key) => {// 预加载图片

      const p = this.preload(value)
        .then(img => {
          this.images.set(key, img);
        })
        .catch(err => console.log(err));

      pr.push(p);
    });

limingzhe's avatar
limingzhe committed
333
    this.rawAudios.forEach((value, key) => {// 预加载音频
limingzhe's avatar
limingzhe committed
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

      const a = this.preloadAudio(value)
        .then(() => {
          // this.images.set(key, img);
        })
        .catch(err => console.log(err));

      pr.push(a);
    });
    return Promise.all(pr);
  }

  preload(url) {
    return new Promise((resolve, reject) => {
      const img = new Image();
      // img.crossOrigin = "anonymous";
      img.onload = () => resolve(img);
      img.onerror = reject;
      img.src = url;
    });
  }

  preloadAudio(url) {
    return new Promise((resolve, reject) => {
      const audio = new Audio();
      audio.oncanplay = (a) => {
        resolve();
      };
      audio.onerror = () => {
        reject();
      };
      audio.src = url;
      audio.load();
    });
  }


  renderAfterResize() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.init();
  }



limingzhe's avatar
limingzhe committed
379 380


limingzhe's avatar
limingzhe committed
381 382 383
  checkClickTarget(target) {

    const rect = target.getBoundingBox();
Chen Jiping's avatar
Chen Jiping committed
384
    
limingzhe's avatar
limingzhe committed
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
    if (this.checkPointInRect(this.mx, this.my, rect)) {
      return true;
    }
    return false;
  }

  getWorlRect(target) {

    let rect = target.getBoundingBox();

    if (target.parent) {

      const pRect = this.getWorlRect(target.parent);
      rect.x += pRect.x;
      rect.y += pRect.y;
    }
    return rect;
  }

  checkPointInRect(x, y, rect) {
    if (x >= rect.x && x <= rect.x + rect.width) {
      if (y >= rect.y && y <= rect.y + rect.height) {
        return true;
      }
    }
    return false;
  }



limingzhe's avatar
limingzhe committed
415 416 417 418 419 420 421 422 423 424


  addUrlToAudioObj(key, url = null, vlomue = 1, loop = false, callback = null) {

    const audioObj = this.audioObj;

    if (url == null) {
      url = key;
    }

Chen Jiping's avatar
Chen Jiping committed
425 426 427 428
    if (!key) {
      return;
    }

limingzhe's avatar
limingzhe committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    this.rawAudios.set(key, url);

    const audio = new Audio();
    audio.src = url;
    audio.load();
    audio.loop = loop;
    audio.volume = vlomue;

    audioObj[key] = audio;

    if (callback) {
      audio.onended = () => {
        callback();
      };
    }
  }

  addUrlToImages(url) {
    this.rawImages.set(url, url);
  }

Chen Jiping's avatar
Chen Jiping committed
450 451 452 453
  addUrlToImage(url, key) {
    if (!key) {
      key = url;
    }
limingzhe's avatar
limingzhe committed
454

Chen Jiping's avatar
Chen Jiping committed
455 456
    this.rawImages.set(key, url);
  }
limingzhe's avatar
limingzhe committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471




  // ======================================================编写区域==========================================================================





  /**
   * 添加默认数据 便于无数据时的展示
   */
  initDefaultData() {

Chen Jiping's avatar
Chen Jiping committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    if (!this.data.exercisesArr) {

      this.data.exercisesArr = [];

      for (let i = 0; i < 4; ++i) {

        let exercises = getDefaultExercisesBean();

        exercises.answerIndex = i + 1;
   

        this.data.exercisesArr.push(exercises);

      }

    }

    if (!this.data.title) {
      this.data.title = getDefaultTitle();
    }

    //设置默认皮肤
    if (!this.data.skin) {
      this.data.skin = 'A';
limingzhe's avatar
limingzhe committed
496 497 498 499 500 501 502 503 504
    }
  }


  /**
   * 添加预加载图片
   */
  initImg() {

Chen Jiping's avatar
Chen Jiping committed
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
    //练习题图片
    for (let i = 0; i < this.data.exercisesArr.length; ++i) {

      let exercises = this.data.exercisesArr[i];

      if (exercises.picUrl) {
        this.addUrlToImages(exercises.picUrl);
      }

    }

    const loadBg = (skin, skinType) => {
      if (skin.lt.picUrl) {
        this.addUrlToImage(skin.lt.picUrl, skinType + "_" + skin.lt.picUrl);
      }

      if (skin.lm.picUrl) {
        this.addUrlToImage(skin.lm.picUrl, skinType + "_" + skin.lm.picUrl);
      }

      if (skin.lb.picUrl) {
        this.addUrlToImage(skin.lb.picUrl, skinType + "_" + skin.lb.picUrl);
      }

      if (skin.mt.picUrl) {
        this.addUrlToImage(skin.mt.picUrl, skinType + "_" + skin.mt.picUrl);
      }

      if (skin.mb.picUrl) {
        this.addUrlToImage(skin.mb.picUrl, skinType + "_" + skin.mb.picUrl);
      }

      if (skin.rt.picUrl) {
        this.addUrlToImage(skin.rt.picUrl, skinType + "_" + skin.rt.picUrl);
      }

      if (skin.rm.picUrl) {
        this.addUrlToImage(skin.rm.picUrl, skinType + "_" + skin.rm.picUrl);
      }

      if (skin.rb.picUrl) {
        this.addUrlToImage(skin.rb.picUrl, skinType + "_" + skin.rb.picUrl);
      }
    }

    if (this.data.skinA) {
      loadBg(this.data.skinA, 'A');
    }

    if (this.data.skinB) {
      loadBg(this.data.skinB, 'B');
    }
limingzhe's avatar
limingzhe committed
557 558 559 560 561 562 563 564 565 566 567

  }

  /**
   * 添加预加载音频
   */
  initAudio() {

    // 音效
    this.addUrlToAudioObj('click', this.rawAudios.get('click'), 0.3);

Chen Jiping's avatar
Chen Jiping committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    this.addUrlToAudioObj('page', this.rawAudios.get('page'));

    this.addUrlToAudioObj('right', this.rawAudios.get('right'));


    //标题发音
    if (this.data.title.audioUrl) {
      this.addUrlToAudioObj('titleAudio', this.data.title.audioUrl);
    }


    //听力材料
    if (this.data.listenAudioUrl) {
      this.addUrlToAudioObj('listenAudio', this.data.listenAudioUrl);
    }


    //练习题音效
    for (let i = 0; i < this.data.exercisesArr.length; ++i) {

      let exercises = this.data.exercisesArr[i];

      this.addUrlToAudioObj('index_' + i, exercises.audioUrl);

      let wordArr = exercises.wordArr;

      for(let j = 0; j < wordArr.length; ++ j){
        this.addUrlToAudioObj('word_' + i + '_' + j, wordArr[j].audioUrl);
      }
    }

limingzhe's avatar
limingzhe committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612
  }



  /**
   * 初始化数据
   */
  initData() {

    const sx = this.canvasWidth / this.canvasBaseW;
    const sy = this.canvasHeight / this.canvasBaseH;
    const s = Math.min(sx, sy);
    this.mapScale = s;

Chen Jiping's avatar
Chen Jiping committed
613
    this.renderArr = [];
limingzhe's avatar
limingzhe committed
614

Chen Jiping's avatar
Chen Jiping committed
615
    this.skinRenderArr = [];
limingzhe's avatar
limingzhe committed
616

Chen Jiping's avatar
Chen Jiping committed
617 618 619 620 621 622 623 624 625
    this.listening = null;

    this.curIndex = 0;

    if (this.curAudio) {
      this.curAudio.pause();
    }

    this.curAudio = null;
limingzhe's avatar
limingzhe committed
626

Chen Jiping's avatar
Chen Jiping committed
627
    this.exercisesArr = [];
limingzhe's avatar
limingzhe committed
628 629 630 631 632 633 634 635 636 637 638 639


  }



  /**
   * 初始化试图
   */
  initView() {


Chen Jiping's avatar
Chen Jiping committed
640 641 642
    this.initBg();

    this.initTitle();
limingzhe's avatar
limingzhe committed
643 644 645

    this.initBottomPart();

Chen Jiping's avatar
Chen Jiping committed
646
    this.initExercises();
limingzhe's avatar
limingzhe committed
647 648
  }

Chen Jiping's avatar
Chen Jiping committed
649
  initBg() {
limingzhe's avatar
limingzhe committed
650

Chen Jiping's avatar
Chen Jiping committed
651 652 653 654 655 656 657
    const getPic = (key, anchorX: number = 0.5, anchorY: number = 0.5) => {
      const pic = new MySprite();
      pic.init(this.images.get(key), anchorX, anchorY);
      pic.setScaleXY(this.mapScale);
      this.skinRenderArr.push(pic);
      return pic;
    }
limingzhe's avatar
limingzhe committed
658

Chen Jiping's avatar
Chen Jiping committed
659
    const initBg = (skin, skinType) => {
limingzhe's avatar
limingzhe committed
660

Chen Jiping's avatar
Chen Jiping committed
661 662 663 664 665
      //左上
      if (skin.lt.picUrl) {
        const pic = getPic(skinType + '_' + skin.lt.picUrl, 0, 0);
        pic.x = 0;
        pic.y = 0;
limingzhe's avatar
limingzhe committed
666

Chen Jiping's avatar
Chen Jiping committed
667
      }
limingzhe's avatar
limingzhe committed
668

Chen Jiping's avatar
Chen Jiping committed
669 670 671 672 673 674
      //左中
      if (skin.lm.picUrl) {
        const pic = getPic(skinType + '_' + skin.lm.picUrl, 0);
        pic.x = 0;
        pic.y = (this.canvasHeight - pic.getBoundingBox().height) / 2;
      }
limingzhe's avatar
limingzhe committed
675

Chen Jiping's avatar
Chen Jiping committed
676 677 678 679 680 681
      //左下
      if (skin.lb.picUrl) {
        const pic = getPic(skinType + '_' + skin.lb.picUrl, 0, 1);
        pic.x = 0;
        pic.y = this.canvasHeight;
      }
limingzhe's avatar
limingzhe committed
682

Chen Jiping's avatar
Chen Jiping committed
683 684 685 686 687 688
      //中上
      if (skin.mt.picUrl) {
        const pic = getPic(skinType + '_' + skin.mt.picUrl, 0.5, 0);
        pic.x = this.canvasWidth / 2;
        pic.y = 0;
      }
limingzhe's avatar
limingzhe committed
689

Chen Jiping's avatar
Chen Jiping committed
690 691 692 693 694 695
      //中下
      if (skin.mb.picUrl) {
        const pic = getPic(skinType + '_' + skin.mb.picUrl, 0.5, 1);
        pic.x = this.canvasWidth / 2;
        pic.y = this.canvasHeight;
      }
limingzhe's avatar
limingzhe committed
696

Chen Jiping's avatar
Chen Jiping committed
697 698 699 700 701 702
      //右上
      if (skin.rt.picUrl) {
        const pic = getPic(skinType + '_' + skin.rt.picUrl, 1, 0);
        pic.x = this.canvasWidth;
        pic.y = 0;
      }
limingzhe's avatar
limingzhe committed
703

Chen Jiping's avatar
Chen Jiping committed
704 705 706 707 708 709
      //右中
      if (skin.rm.picUrl) {
        const pic = getPic(skinType + '_' + skin.rm.picUrl, 1, 0.5);
        pic.x = this.canvasWidth;
        pic.y = this.canvasHeight / 2;
      }
limingzhe's avatar
limingzhe committed
710

Chen Jiping's avatar
Chen Jiping committed
711 712 713 714 715 716 717
      //右下
      if (skin.rb.picUrl) {
        const pic = getPic(skinType + '_' + skin.rb.picUrl, 1, 1);
        pic.x = this.canvasWidth;
        pic.y = this.canvasHeight;
      }
    }
limingzhe's avatar
limingzhe committed
718

Chen Jiping's avatar
Chen Jiping committed
719 720 721 722 723 724 725 726 727 728 729
    if (this.data.skin === 'A') {
      if (this.data.skinA) {
        initBg(this.data.skinA, 'A');
      }
    }
    else {
      if (this.data.skinB) {
        initBg(this.data.skinB, 'B');
      }
    }
  }
limingzhe's avatar
limingzhe committed
730

Chen Jiping's avatar
Chen Jiping committed
731
  initBottomPart() {
limingzhe's avatar
limingzhe committed
732

Chen Jiping's avatar
Chen Jiping committed
733 734
    const listening = new Listening(this.audioObj['listenAudio'], this.images);
    listening.init(this.mapScale, this.mapScale);
limingzhe's avatar
limingzhe committed
735

Chen Jiping's avatar
Chen Jiping committed
736 737 738
    listening.bg.x = this.canvasWidth - listening.bg.width - 10 * this.mapScale;
    listening.bg.y = this.canvasHeight - listening.bg.height - 8 * this.mapScale;
    this.listening = listening;
limingzhe's avatar
limingzhe committed
739

Chen Jiping's avatar
Chen Jiping committed
740 741
    this.renderArr.push(listening.bg);
  }
limingzhe's avatar
limingzhe committed
742

Chen Jiping's avatar
Chen Jiping committed
743 744
  initTitle() {
    const title = new Title(this.data.title, this.images);
limingzhe's avatar
limingzhe committed
745

Chen Jiping's avatar
Chen Jiping committed
746 747 748
    title.init(this.mapScale, this.mapScale);
    title.audio = this.audioObj['titleAudio'];
    this.title = title;
limingzhe's avatar
limingzhe committed
749

Chen Jiping's avatar
Chen Jiping committed
750 751
    title.titleBg.y = 0;
    title.titleBg.x = 0;
limingzhe's avatar
limingzhe committed
752

Chen Jiping's avatar
Chen Jiping committed
753
    this.renderArr.push(title.titleBg);
limingzhe's avatar
limingzhe committed
754 755
  }

Chen Jiping's avatar
Chen Jiping committed
756
  initExercises() {
limingzhe's avatar
limingzhe committed
757 758


Chen Jiping's avatar
Chen Jiping committed
759
    var dW = 586 * this.mapScale;
limingzhe's avatar
limingzhe committed
760

Chen Jiping's avatar
Chen Jiping committed
761
    var dH = 311 * this.mapScale;
limingzhe's avatar
limingzhe committed
762

Chen Jiping's avatar
Chen Jiping committed
763
    let col = 2;
limingzhe's avatar
limingzhe committed
764

Chen Jiping's avatar
Chen Jiping committed
765
    let rows = Math.ceil(this.data.exercisesArr.length / col);
limingzhe's avatar
limingzhe committed
766

Chen Jiping's avatar
Chen Jiping committed
767
    let startX = (this.canvasWidth - (col - 1) * dW) / 2;
limingzhe's avatar
limingzhe committed
768

Chen Jiping's avatar
Chen Jiping committed
769
    let startY = 7 * this.mapScale + (this.canvasHeight - 7 * this.mapScale - ((rows - 1) * 63 * this.mapScale + rows * 248 * this.mapScale)) / 2;
limingzhe's avatar
limingzhe committed
770 771


Chen Jiping's avatar
Chen Jiping committed
772
    let tempX = startX;
limingzhe's avatar
limingzhe committed
773

Chen Jiping's avatar
Chen Jiping committed
774
    for (let i = 0; i < this.data.exercisesArr.length; ++i) {
limingzhe's avatar
limingzhe committed
775

Chen Jiping's avatar
Chen Jiping committed
776 777 778 779 780 781 782
      let item = this.data.exercisesArr[i];
      
      let exercises = new Exercises();
      exercises.index = i + 1;
      exercises.init(this.images, item);
      exercises.bg.setScaleXY(this.mapScale);
      this.renderArr.push(exercises.bg);
limingzhe's avatar
limingzhe committed
783

Chen Jiping's avatar
Chen Jiping committed
784
      this.exercisesArr.push(exercises);
limingzhe's avatar
limingzhe committed
785

Chen Jiping's avatar
Chen Jiping committed
786 787
      exercises.bg.x = startX - exercises.bg.getBoundingBox().width / 2;
      exercises.bg.y = startY;
limingzhe's avatar
limingzhe committed
788

Chen Jiping's avatar
Chen Jiping committed
789 790 791 792 793 794 795 796
      //换行
      if ((i + 1) % col == 0) {
        startX = tempX;
        startY += dH;
      }
      else {
        startX += dW;
      }
limingzhe's avatar
limingzhe committed
797

Chen Jiping's avatar
Chen Jiping committed
798
    }
limingzhe's avatar
limingzhe committed
799

Chen Jiping's avatar
Chen Jiping committed
800
  }
limingzhe's avatar
limingzhe committed
801 802 803 804 805 806 807 808 809



  mapDown(event) {

    if (!this.canTouch) {
      return;
    }

Chen Jiping's avatar
Chen Jiping committed
810 811 812 813
    if (this.curAudio) {
      if (this.curAudio != this.listening.audio) {
        this.curAudio.pause();
      }
limingzhe's avatar
limingzhe committed
814 815
    }

Chen Jiping's avatar
Chen Jiping committed
816 817 818 819 820 821
    if (this.checkClickTarget(this.listening.bg)) {

      this.curAudio = this.listening.play(() => {
        this.listening.reset();
      });

limingzhe's avatar
limingzhe committed
822 823 824
      return;
    }

Chen Jiping's avatar
Chen Jiping committed
825 826 827 828 829 830 831 832 833 834 835 836
    if (this.curAudio) {
      if (this.curAudio == this.listening.audio) {
        this.listening.pauseAudio();
      }
      else {
        this.curAudio.pause();
      }

    }

    if (this.checkClickTarget(this.title.titleBg)) {
      this.curAudio = this.title.playAudio();
limingzhe's avatar
limingzhe committed
837 838 839
      return;
    }

Chen Jiping's avatar
Chen Jiping committed
840 841 842 843 844 845 846 847 848 849 850 851
    let clicked = false;
    
    for (let i = 0; i < this.exercisesArr.length; ++i) {
      
      let exercises = this.exercisesArr[i];

      if(this.checkClickTarget(exercises.bg)){
        this.clickExercises(exercises, i);
      }
    }

    if (clicked) {
limingzhe's avatar
limingzhe committed
852 853 854 855 856 857 858 859 860 861
      return;
    }

  }

  mapMove(event) {

  }

  mapUp(event) {
limingzhe's avatar
limingzhe committed
862 863 864 865

  }


limingzhe's avatar
limingzhe committed
866 867 868 869 870 871 872 873 874 875 876

  update() {

    // ----------------------------------------------------------
    this.animationId = window.requestAnimationFrame(this.update.bind(this));
    // 清除画布内容
    this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
    // tween 更新动画
    TWEEN.update();
    // ----------------------------------------------------------

Chen Jiping's avatar
Chen Jiping committed
877
    this.updateArr(this.skinRenderArr);
limingzhe's avatar
limingzhe committed
878 879 880 881

    this.updateArr(this.renderArr);


limingzhe's avatar
limingzhe committed
882 883
  }

Chen Jiping's avatar
Chen Jiping committed
884
  clickExercises(exercises: Exercises, index:number){
limingzhe's avatar
limingzhe committed
885

Chen Jiping's avatar
Chen Jiping committed
886 887 888 889 890 891 892 893 894 895 896 897
    if(!exercises){
      return;
    }

    if(!exercises.answered){

      if(this.checkClickTarget(exercises.indexBg1)){
        let flag = exercises.checkAnswer(this.curIndex + 1);
        
        if(flag){

          this.curIndex ++;
limingzhe's avatar
limingzhe committed
898

Chen Jiping's avatar
Chen Jiping committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
          this.playAudio('right', true, ()=>{
            let key = 'index_' + index;
            this.curAudio = this.audioObj[key];
  
            this.playAudio(key, true);
          });
        }
      }

      
    }
    else{
      if(this.checkClickTarget(exercises.indexBg1)){
        let key = 'index_' + index;
        this.curAudio = this.audioObj[key];
  
        this.playAudio(key, true);
        return;
      }

      for(let i = 0; i < exercises.wordBgArr.length; ++ i){

        let wordBg = exercises.wordBgArr[i];

        if(this.checkClickTarget(wordBg.bg)){

          exercises.showWord(i);
          let key = 'word_' + index + '_' + i;
          this.curAudio = this.audioObj[key];
          this.playAudio(key, true);        

          break;
        }

      }
    }
  }
liujiangnan's avatar
liujiangnan committed
936
}