play.component.ts 22.1 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 4

import {
  Label,
Chen Jiping's avatar
Chen Jiping committed
5
  MySprite, ShapeRectNew, tweenChange,
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
import TWEEN from '@tweenjs/tween.js';
Chen Jiping's avatar
Chen Jiping committed
14 15 16 17 18
import { Title } from './Title';
import { getDefaultTile } from '../bean/TitleBean';
import { Listening } from './Listening';
import { Letter } from './Letter';
import { Exercises } from './Exercises.spreat';
Chen Jiping's avatar
Chen Jiping committed
19
import { AnswerBean, getDefaultExercises, getDefaultLetter } from '../bean/Exercises';
limingzhe's avatar
limingzhe committed
20 21 22



liujiangnan's avatar
liujiangnan committed
23 24 25 26

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

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

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

  ctx;

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

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

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


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

  images = new Map();

  animationId: any;
  winResizeEventStream = new Subject();

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

limingzhe's avatar
limingzhe committed
60 61 62 63 64 65
  renderArr;
  mapScale = 1;

  canvasLeft;
  canvasTop;

Chen Jiping's avatar
Chen Jiping committed
66
  saveKey = 'YM5-3';
limingzhe's avatar
limingzhe committed
67 68


limingzhe's avatar
limingzhe committed
69
  canTouch = true;
limingzhe's avatar
limingzhe committed
70

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

Chen Jiping's avatar
Chen Jiping committed
74
  listening: Listening;
Chen Jiping's avatar
Chen Jiping committed
75

Chen Jiping's avatar
Chen Jiping committed
76
  curAudio;
Chen Jiping's avatar
Chen Jiping committed
77

Chen Jiping's avatar
Chen Jiping committed
78
  letterArr: Array<Letter>;
Chen Jiping's avatar
Chen Jiping committed
79

Chen Jiping's avatar
Chen Jiping committed
80 81 82
  exercisesArr: Array<Exercises> = [];

  curIndex = 0;
limingzhe's avatar
limingzhe committed
83 84 85 86 87 88 89

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


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

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

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

limingzhe's avatar
limingzhe committed
98 99 100
      if (data && typeof data == 'object') {
        this.data = data;
      }
Chen Jiping's avatar
Chen Jiping committed
101 102 103 104 105 106
      else{
        let defaultData = '{"title":{"part1":"C","part2":"Read and point","audioUrl":"http://staging-teach.cdn.ireadabc.com/55addbbc6a4da4a63bc22988063bdded.mp3"},"letterArr":[{"isFill":"0","val":"cust"},{"isFill":"0","val":"ir"},{"isFill":"0","val":"er"}],"zoneArr":[{"exercisesArr":[{"answer":{"letterArr":[{"isFill":"0","val":"c"},{"isFill":"1","val":"a"},{"isFill":"0","val":"t"}],"audioUrl":"http://staging-teach.cdn.ireadabc.com/a252c069d04f8e04f91b2a1394072f30.mp3"},"answerIndex":1,"indexAudioUrl":"http://staging-teach.cdn.ireadabc.com/f95fd3997cdd67abd51a1ab45efd855d.mp3"},{"answer":{"letterArr":[{"isFill":"0","val":"m"},{"isFill":"1","val":"a"},{"isFill":"0","val":"p"}],"audioUrl":"http://staging-teach.cdn.ireadabc.com/8b5ecca5d4509e1b8a7e81e69d8db615.mp3"},"answerIndex":2,"indexAudioUrl":"http://staging-teach.cdn.ireadabc.com/30621df106455523573e3871a5a5c598.mp3"},{"answer":{"letterArr":[{"isFill":"0","val":"d"},{"isFill":"1","val":"a"},{"isFill":"0","val":"rk"}]},"answerIndex":3}]},{"exercisesArr":[{"answer":{"letterArr":[{"isFill":"0","val":"b"},{"isFill":"1","val":"ar"},{"isFill":"0","val":"t"}]},"answerIndex":4},{"answer":{"letterArr":[{"isFill":"0","val":"d"},{"isFill":"1","val":"ar"},{"isFill":"0","val":"k"}]},"answerIndex":5}]},{"exercisesArr":[{"answer":{"letterArr":[{"isFill":"0","val":"c"},{"isFill":"1","val":"u"},{"isFill":"0","val":"t"}]},"answerIndex":6},{"answer":{"letterArr":[{"isFill":"0","val":"b"},{"isFill":"1","val":"u"},{"isFill":"0","val":"t"}]},"answerIndex":7},{"answer":{"letterArr":[{"isFill":"0","val":"d"},{"isFill":"1","val":"u"},{"isFill":"0","val":"ck"}]},"answerIndex":8}]}],"backgroudColor":"#ab82b8","audioUrl":"http://staging-teach.cdn.ireadabc.com/8970ccce02d3a37640c80e02626fefb8.mp3","skin":"A","skinA":{"rb_pic_url":"http://staging-teach.cdn.ireadabc.com/e12a0f642438bcdc926cac2d12c71ca4.png"}}';
        this.data = JSON.parse(defaultData);

      }
      // console.log('data:' , JSON.stringify(data));
limingzhe's avatar
limingzhe committed
107

limingzhe's avatar
limingzhe committed
108 109
      // 初始化 各事件监听
      this.initListener();
limingzhe's avatar
limingzhe committed
110

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

limingzhe's avatar
limingzhe committed
114 115 116 117 118 119
      // 初始化 音频资源
      this.initAudio();
      // 初始化 图片资源
      this.initImg();
      // 开始预加载资源
      this.load();
limingzhe's avatar
limingzhe committed
120

limingzhe's avatar
limingzhe committed
121
    }, this.saveKey);
limingzhe's avatar
limingzhe committed
122

limingzhe's avatar
limingzhe committed
123
  }
limingzhe's avatar
limingzhe committed
124

limingzhe's avatar
limingzhe committed
125 126 127
  ngOnDestroy() {
    window['curCtx'] = null;
    window.cancelAnimationFrame(this.animationId);
128 129 130 131 132 133 134 135 136 137
    this.cleanAudio();
  }


  cleanAudio() {
    if (this.audioObj) {
      for (const key in this.audioObj) {
        this.audioObj[key].pause();
      }
    }
limingzhe's avatar
limingzhe committed
138 139 140
  }


limingzhe's avatar
limingzhe committed
141
  load() {
limingzhe's avatar
limingzhe committed
142

limingzhe's avatar
limingzhe committed
143 144 145 146 147 148
    // 预加载资源
    this.loadResources().then(() => {
      window["air"].hideAirClassLoading(this.saveKey, this.data);
      this.init();
      this.update();
    });
limingzhe's avatar
limingzhe committed
149 150 151
  }


limingzhe's avatar
limingzhe committed
152
  init() {
limingzhe's avatar
limingzhe committed
153

limingzhe's avatar
limingzhe committed
154 155 156 157
    this.initCtx();
    this.initData();
    this.initView();
  }
limingzhe's avatar
limingzhe committed
158

limingzhe's avatar
limingzhe committed
159 160 161 162 163
  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
164 165


limingzhe's avatar
limingzhe committed
166 167 168
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.canvas.nativeElement.width = this.canvasWidth;
    this.canvas.nativeElement.height = this.canvasHeight;
limingzhe's avatar
limingzhe committed
169

limingzhe's avatar
limingzhe committed
170
    window['curCtx'] = this.ctx;
171
    window['curImages'] = this.images;
limingzhe's avatar
limingzhe committed
172 173 174 175 176 177 178
  }






limingzhe's avatar
limingzhe committed
179 180 181 182 183
  updateItem(item) {
    if (item) {
      item.update();
    }
  }
limingzhe's avatar
limingzhe committed
184

limingzhe's avatar
limingzhe committed
185 186 187 188 189 190 191 192
  updateArr(arr) {
    if (!arr) {
      return;
    }
    for (let i = 0; i < arr.length; i++) {
      arr[i].update(this);
    }
  }
limingzhe's avatar
limingzhe committed
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







  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
317 318

    return audio;
limingzhe's avatar
limingzhe committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  }



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

      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) => {
Chen Jiping's avatar
Chen Jiping committed
363
        resolve(a);
limingzhe's avatar
limingzhe committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
      };
      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
382 383


limingzhe's avatar
limingzhe committed
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
  checkClickTarget(target) {

    const rect = target.getBoundingBox();

    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
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


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

    const audioObj = this.audioObj;

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

    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
449 450 451 452 453 454 455
  addUrlToImage(url, key) {
    if (!key) {
      key = url;
    }

    this.rawImages.set(key, url);
  }
limingzhe's avatar
limingzhe committed
456 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
    if (!this.data.title) {
      this.data.title = getDefaultTile();
    }

Chen Jiping's avatar
Chen Jiping committed
476
    if (!this.data.backgroudColor) {
Chen Jiping's avatar
Chen Jiping committed
477 478 479 480
      this.data.backgroudColor = "#B5C75F";
    }


Chen Jiping's avatar
Chen Jiping committed
481
    if (!this.data.letterArr) {
Chen Jiping's avatar
Chen Jiping committed
482 483 484
      this.data.letterArr = [];
    }

Chen Jiping's avatar
Chen Jiping committed
485 486 487 488 489 490 491 492
    if(!this.data.skin){
      this.data.skin = 'A';
    }

    if (!this.data.skinA) {
      this.data.skinA = {};
    }

Chen Jiping's avatar
Chen Jiping committed
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
    if (!this.data.zoneArr) {
      this.data.zoneArr = [];
    }

    for (let i = this.data.zoneArr.length; i < 3; ++i) {

      let zone = {
        exercisesArr: []
      };

      this.data.zoneArr.push(zone);
    }

    for (let i = 0; i < this.data.zoneArr.length; ++i) {
      let zone = this.data.zoneArr[i];

      if (!zone) {
        zone = {
          exercisesArr: []
        };

        this.data.zoneArr[i] = zone;
      }

      for (let j = 0; j < zone.exercisesArr.length; ++j) {
        if (!zone.exercisesArr[j]) {
          zone.exercisesArr[j] = getDefaultExercises();
        }

        if (!zone.exercisesArr[j].answer) {
Chen Jiping's avatar
Chen Jiping committed
523 524 525
          let answer = new AnswerBean();

          zone.exercisesArr[j].answer = answer;
Chen Jiping's avatar
Chen Jiping committed
526 527
        }
      }
limingzhe's avatar
limingzhe committed
528 529 530 531 532 533 534 535 536
    }
  }


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

Chen Jiping's avatar
Chen Jiping committed
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
    const loadBg = (skin, skinType) => {
      if (skin.lt_pic_url) {
        this.addUrlToImage(skin.lt_pic_url, skinType + "_" + skin.lt_pic_url);
      }

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

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

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

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

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

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

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

    if (this.data.skinA) {
      loadBg(this.data.skinA, 'A');
    }
Chen Jiping's avatar
Chen Jiping committed
574

limingzhe's avatar
limingzhe committed
575 576 577 578 579 580 581 582

  }

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

Chen Jiping's avatar
Chen Jiping committed
583

limingzhe's avatar
limingzhe committed
584 585 586 587

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

Chen Jiping's avatar
Chen Jiping committed
588 589 590 591 592 593 594 595 596 597 598 599 600
    this.addUrlToAudioObj('right', this.rawAudios.get('right'));

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


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

Chen Jiping's avatar
Chen Jiping committed
601
    for (let i = 0; i < this.data.letterArr.length; ++i) {
Chen Jiping's avatar
Chen Jiping committed
602 603
      let letter = this.data.letterArr[i];

Chen Jiping's avatar
Chen Jiping committed
604
      if (letter.audioUrl) {
Chen Jiping's avatar
Chen Jiping committed
605 606 607 608
        this.addUrlToAudioObj(letter.audioUrl);
      }
    }

Chen Jiping's avatar
Chen Jiping committed
609 610
    for (let i = 0; i < this.data.zoneArr.length; ++i) {
      let zone = this.data.zoneArr[i];
Chen Jiping's avatar
Chen Jiping committed
611

Chen Jiping's avatar
Chen Jiping committed
612 613
      //练习题音效
      for (let i = 0; i < zone.exercisesArr.length; ++i) {
Chen Jiping's avatar
Chen Jiping committed
614

Chen Jiping's avatar
Chen Jiping committed
615 616 617
        let exercises = zone.exercisesArr[i];

        if (exercises.answer && exercises.answer.audioUrl) {
618
          this.addUrlToAudioObj(exercises.answer.audioUrl);
Chen Jiping's avatar
Chen Jiping committed
619 620 621
        }

        if(exercises.indexAudioUrl){
622
          this.addUrlToAudioObj(exercises.indexAudioUrl);
Chen Jiping's avatar
Chen Jiping committed
623
        }
Chen Jiping's avatar
Chen Jiping committed
624 625 626 627

      }
    }

Chen Jiping's avatar
Chen Jiping committed
628

limingzhe's avatar
limingzhe committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
  }



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

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


    this.renderArr = [];

Chen Jiping's avatar
Chen Jiping committed
646 647 648 649 650
    if (this.curAudio) {
      this.curAudio.pause();
    }

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

Chen Jiping's avatar
Chen Jiping committed
652
    this.letterArr = [];
limingzhe's avatar
limingzhe committed
653

Chen Jiping's avatar
Chen Jiping committed
654
    this.exercisesArr = [];
Chen Jiping's avatar
Chen Jiping committed
655 656

    this.curIndex = 0;
limingzhe's avatar
limingzhe committed
657 658 659 660 661 662 663 664 665
  }



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

Chen Jiping's avatar
Chen Jiping committed
666
    this.initBg();
limingzhe's avatar
limingzhe committed
667

Chen Jiping's avatar
Chen Jiping committed
668
    this.initTitle();
limingzhe's avatar
limingzhe committed
669 670 671

    this.initBottomPart();

Chen Jiping's avatar
Chen Jiping committed
672 673 674
    this.initLetter();

    this.initExercises();
limingzhe's avatar
limingzhe committed
675 676
  }

Chen Jiping's avatar
Chen Jiping committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  initBg() {

    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.renderArr.push(pic);
      return pic;
    }

    const initBg = (skin, skinType) => {

      //左上
      if (skin.lt_pic_url) {
        const pic = getPic(skinType + '_' + skin.lt_pic_url, 0, 0);
        pic.x = 0;
        pic.y = 0;

      }

      //左中
      if (skin.lm_pic_url) {
        const pic = getPic(skinType + '_' + skin.lm_pic_url, 0);
        pic.x = 0;
        pic.y = (this.canvasHeight - pic.getBoundingBox().height) / 2;
      }

      //左下
      if (skin.lb_pic_url) {
        const pic = getPic(skinType + '_' + skin.lb_pic_url, 0, 1);
        pic.x = 0;
        pic.y = this.canvasHeight;
      }

      //中上
      if (skin.mt_pic_url) {
        const pic = getPic(skinType + '_' + skin.mt_pic_url, 0.5, 0);
        pic.x = this.canvasWidth / 2;
        pic.y = 0;
      }

      //中下
      if (skin.mb_pic_url) {
        const pic = getPic(skinType + '_' + skin.mb_pic_url, 0.5, 1);
        pic.x = this.canvasWidth / 2;
        pic.y = this.canvasHeight;
      }

      //右上
      if (skin.rt_pic_url) {
        const pic = getPic(skinType + '_' + skin.rt_pic_url, 1, 0);
        pic.x = this.canvasWidth;
        pic.y = 0;
      }

      //右中
      if (skin.rm_pic_url) {
        const pic = getPic(skinType + '_' + skin.rm_pic_url, 1, 0.5);
        pic.x = this.canvasWidth;
        pic.y = this.canvasHeight / 2;
      }

      //右下
      if (skin.rb_pic_url) {
        const pic = getPic(skinType + '_' + skin.rb_pic_url, 1, 1);
        pic.x = this.canvasWidth;
        pic.y = this.canvasHeight;
      }
    }

    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
760 761
  initBottomPart() {

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

Chen Jiping's avatar
Chen Jiping committed
765 766 767
    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
768

Chen Jiping's avatar
Chen Jiping committed
769 770
    this.renderArr.push(listening.bg);
  }
limingzhe's avatar
limingzhe committed
771

Chen Jiping's avatar
Chen Jiping committed
772
  initTitle() {
limingzhe's avatar
limingzhe committed
773

Chen Jiping's avatar
Chen Jiping committed
774
    const title = new Title(this.data.title, this.images);
limingzhe's avatar
limingzhe committed
775

Chen Jiping's avatar
Chen Jiping committed
776 777 778
    title.init(this.mapScale, this.mapScale);
    title.audio = this.audioObj['titleAudio'];
    this.title = title;
limingzhe's avatar
limingzhe committed
779

Chen Jiping's avatar
Chen Jiping committed
780 781
    title.titleBg.y = 0;
    title.titleBg.x = 0;
limingzhe's avatar
limingzhe committed
782

Chen Jiping's avatar
Chen Jiping committed
783
    this.renderArr.push(title.titleBg);
limingzhe's avatar
limingzhe committed
784 785 786
  }


Chen Jiping's avatar
Chen Jiping committed
787
  initLetter() {
limingzhe's avatar
limingzhe committed
788

Chen Jiping's avatar
Chen Jiping committed
789
    let letterArr = this.data.letterArr;
limingzhe's avatar
limingzhe committed
790

Chen Jiping's avatar
Chen Jiping committed
791
    let dW = (47 + 104) * this.mapScale;
limingzhe's avatar
limingzhe committed
792

Chen Jiping's avatar
Chen Jiping committed
793
    let tW = (letterArr.length - 1) * dW;
limingzhe's avatar
limingzhe committed
794

Chen Jiping's avatar
Chen Jiping committed
795
    let startX = (this.canvasWidth - tW) / 2;
limingzhe's avatar
limingzhe committed
796

Chen Jiping's avatar
Chen Jiping committed
797
    let y = this.canvasHeight / 2 - 218 * this.mapScale;
limingzhe's avatar
limingzhe committed
798

Chen Jiping's avatar
Chen Jiping committed
799
    for (let i = 0; i < letterArr.length; ++i) {
limingzhe's avatar
limingzhe committed
800

Chen Jiping's avatar
Chen Jiping committed
801 802 803 804 805 806 807 808
      let letter = new Letter();
      letter.init(this.images, letterArr[i], this.data.backgroudColor, this.mapScale, this.mapScale);
      letter.bg.x = startX + dW * i;
      letter.bg.y = y;
      this.renderArr.push(letter.bg);
      this.letterArr.push(letter);
    }
  }
limingzhe's avatar
limingzhe committed
809 810


Chen Jiping's avatar
Chen Jiping committed
811
  initExercises() {
limingzhe's avatar
limingzhe committed
812

Chen Jiping's avatar
Chen Jiping committed
813
    let w = 344 * this.mapScale;
limingzhe's avatar
limingzhe committed
814

Chen Jiping's avatar
Chen Jiping committed
815
    let h = 344 * this.mapScale;
limingzhe's avatar
limingzhe committed
816

Chen Jiping's avatar
Chen Jiping committed
817
    let r = 20 * this.mapScale;
limingzhe's avatar
limingzhe committed
818

Chen Jiping's avatar
Chen Jiping committed
819
    let y = this.canvasHeight / 2 - 119 * this.mapScale;
limingzhe's avatar
limingzhe committed
820

Chen Jiping's avatar
Chen Jiping committed
821
    let dW = 50;
limingzhe's avatar
limingzhe committed
822

Chen Jiping's avatar
Chen Jiping committed
823
    let startX = (this.canvasWidth - (w * 3 + dW * (3 - 1))) / 2;
Chen Jiping's avatar
Chen Jiping committed
824 825


Chen Jiping's avatar
Chen Jiping committed
826 827
    const getView = (item, x = 0, y = 0) => {
      let exercises = new Exercises();
Chen Jiping's avatar
Chen Jiping committed
828

Chen Jiping's avatar
Chen Jiping committed
829
      exercises.init(this.images, this.data.backgroudColor, item, this.mapScale, this.mapScale);
limingzhe's avatar
limingzhe committed
830

Chen Jiping's avatar
Chen Jiping committed
831
      exercises.bg.x = x;
limingzhe's avatar
limingzhe committed
832

Chen Jiping's avatar
Chen Jiping committed
833
      exercises.bg.y = y;
Chen Jiping's avatar
Chen Jiping committed
834

Chen Jiping's avatar
Chen Jiping committed
835 836
      return exercises;
    }
Chen Jiping's avatar
Chen Jiping committed
837

Chen Jiping's avatar
Chen Jiping committed
838 839 840 841 842 843 844 845 846 847 848
    for (let i = 0; i < this.data.zoneArr.length; ++i) {
      const bg = new ShapeRectNew();
      bg.setSize(w, h, r);
      bg.fillColor = this.data.backgroudColor;
      bg.x = startX;
      bg.y = y;
      bg.alpha = 0.1;

      startX += w + dW;

      this.renderArr.push(bg);
Chen Jiping's avatar
Chen Jiping committed
849

Chen Jiping's avatar
Chen Jiping committed
850
      let exercisesArr = this.data.zoneArr[i].exercisesArr;
Chen Jiping's avatar
Chen Jiping committed
851

Chen Jiping's avatar
Chen Jiping committed
852 853
      if(!exercisesArr || exercisesArr.length == 0){
        continue;
Chen Jiping's avatar
Chen Jiping committed
854
      }
Chen Jiping's avatar
Chen Jiping committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

      let x = 29 * this.mapScale;

      let disH = 57 * this.mapScale;

      let startY = (h - exercisesArr.length * 48 * this.mapScale - (exercisesArr.length - 1) * disH) / 2;


      for (let j = 0; j < exercisesArr.length; ++j) {
       

        let exercises = getView(exercisesArr[j], x, startY);

        bg.addChild(exercises.bg);

        this.exercisesArr.push(exercises);

        startY += exercises.bg.height + disH;
Chen Jiping's avatar
Chen Jiping committed
873
      }
Chen Jiping's avatar
Chen Jiping committed
874

Chen Jiping's avatar
Chen Jiping committed
875
    }
Chen Jiping's avatar
Chen Jiping committed
876 877 878 879




limingzhe's avatar
limingzhe committed
880 881 882
  }


Chen Jiping's avatar
Chen Jiping committed
883 884 885
  mapDown(event) {

    if (!this.canTouch) {
limingzhe's avatar
limingzhe committed
886 887 888
      return;
    }

Chen Jiping's avatar
Chen Jiping committed
889 890 891 892 893
    if (this.curAudio) {
      if (this.curAudio != this.listening.audio) {
        this.curAudio.pause();
      }
    }
limingzhe's avatar
limingzhe committed
894

Chen Jiping's avatar
Chen Jiping committed
895
    if (this.checkClickTarget(this.listening.bg)) {
limingzhe's avatar
limingzhe committed
896

Chen Jiping's avatar
Chen Jiping committed
897 898 899
      this.curAudio = this.listening.play(() => {
        this.listening.reset();
      });
limingzhe's avatar
limingzhe committed
900 901 902 903

      return;
    }

Chen Jiping's avatar
Chen Jiping committed
904 905 906 907 908 909 910
    if (this.curAudio) {
      if (this.curAudio == this.listening.audio) {
        this.listening.pauseAudio();
      }
      else {
        this.curAudio.pause();
      }
limingzhe's avatar
limingzhe committed
911

Chen Jiping's avatar
Chen Jiping committed
912
    }
limingzhe's avatar
limingzhe committed
913

Chen Jiping's avatar
Chen Jiping committed
914 915 916 917
    if (this.checkClickTarget(this.title.titleBg)) {
      this.curAudio = this.title.playAudio();
      return;
    }
limingzhe's avatar
limingzhe committed
918

Chen Jiping's avatar
Chen Jiping committed
919
    for (let i = 0; i < this.letterArr.length; ++i) {
Chen Jiping's avatar
Chen Jiping committed
920
      let letter = this.letterArr[i];
limingzhe's avatar
limingzhe committed
921

Chen Jiping's avatar
Chen Jiping committed
922
      if (this.checkClickTarget(letter.bg)) {
Chen Jiping's avatar
Chen Jiping committed
923 924 925
        this.curAudio = this.playAudio(letter.data.audioUrl, true);
        break;
      }
limingzhe's avatar
limingzhe committed
926

Chen Jiping's avatar
Chen Jiping committed
927
    }
limingzhe's avatar
limingzhe committed
928

Chen Jiping's avatar
Chen Jiping committed
929
    let clicked = false;
limingzhe's avatar
limingzhe committed
930

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

Chen Jiping's avatar
Chen Jiping committed
933
      let exercises = this.exercisesArr[i];
limingzhe's avatar
limingzhe committed
934

Chen Jiping's avatar
Chen Jiping committed
935
      if (this.checkClickTarget(exercises.answer) || this.checkClickTarget(exercises.indexLabelBg)) {
limingzhe's avatar
limingzhe committed
936

Chen Jiping's avatar
Chen Jiping committed
937
        clicked = true;
limingzhe's avatar
limingzhe committed
938

Chen Jiping's avatar
Chen Jiping committed
939
        this.clickExercises(exercises, i);
Chen Jiping's avatar
Chen Jiping committed
940 941 942

        break;
      }
limingzhe's avatar
limingzhe committed
943 944
    }

Chen Jiping's avatar
Chen Jiping committed
945
    if (clicked) {
limingzhe's avatar
limingzhe committed
946 947 948 949 950 951 952 953 954
      return;
    }
  }

  mapMove(event) {

  }

  mapUp(event) {
limingzhe's avatar
limingzhe committed
955 956 957 958

  }


limingzhe's avatar
limingzhe committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

  update() {

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



    this.updateArr(this.renderArr);


limingzhe's avatar
limingzhe committed
975 976
  }

Chen Jiping's avatar
Chen Jiping committed
977
  clickExercises(exercises: Exercises, index: number) {
Chen Jiping's avatar
Chen Jiping committed
978 979

    const play = () => {
980
      let key = exercises.data.answer.audioUrl;
Chen Jiping's avatar
Chen Jiping committed
981 982 983 984 985 986 987 988 989 990

      const audio = this.audioObj[key];

      this.curAudio = audio;

      this.playAudio(key, true);
    }

    if (exercises.answered) {
      play();
limingzhe's avatar
limingzhe committed
991

Chen Jiping's avatar
Chen Jiping committed
992 993
    }
    else {
Chen Jiping's avatar
Chen Jiping committed
994
      if (exercises.checkAnswer(this.curIndex + 1)) {
Chen Jiping's avatar
Chen Jiping committed
995

996 997
        this.canTouch = false;

Chen Jiping's avatar
Chen Jiping committed
998
        this.playAudio('right', true, () => {
999
          let key = exercises.data.indexAudioUrl;
Chen Jiping's avatar
Chen Jiping committed
1000 1001 1002 1003 1004

          const audio = this.audioObj[key];

          this.curAudio = audio;

1005 1006 1007 1008 1009 1010
          if(audio){
            this.playAudio(key, true, () =>{
              this.canTouch = true;
            });
          }
          else{
1011
            this.canTouch = true;
1012 1013
          }
                
Chen Jiping's avatar
Chen Jiping committed
1014
        });
Chen Jiping's avatar
Chen Jiping committed
1015 1016

        this.curIndex += 1;
Chen Jiping's avatar
Chen Jiping committed
1017 1018 1019
      }
    }
  }
limingzhe's avatar
limingzhe committed
1020

liujiangnan's avatar
liujiangnan committed
1021
}