GameLogic.js 28.6 KB
Newer Older
李维's avatar
李维 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import Cartoon from "./Cartoon"
import defaultData from "./defaultData"
import { jelly } from "./utils"

export default class GameLogic {
  g_cartoon = null;
  g_formData = null
  g_enableClick = true;
  g_canvas = null;
  g_systemOption = null;
  g_useDefaultData = false;
  g_curDragonDisplay = null;
  
  m_questionAudioPlayed = false;
  m_currentQuestion = -1;
  m_questionDone = false;
李维's avatar
李维 committed
17
  m_currentQuestionArray = []
李维's avatar
李维 committed
18
  m_gameEnd = false;
李维's avatar
李维 committed
19 20 21 22 23 24 25 26 27 28 29 30 31
  constructor(formData, systemOption) {
    this.g_systemOption = systemOption;
    this.g_canvas = cc.find('Canvas');
    this.g_cartoon = new Cartoon(cc);

    if(!formData.contentObj.dataArray || formData.contentObj.dataArray.length == 0) {
      this.g_useDefaultData = true;
      this.g_formData = defaultData
      console.warn("User default data =>", this.g_formData)
    } else {
      this.g_formData = formData.contentObj;
      console.log("User saved data =>", this.g_formData)
    }
李维's avatar
李维 committed
32
    
李维's avatar
李维 committed
33 34

    this.g_enableClick = false;
李维's avatar
李维 committed
35
    this.g_cartoon.initLocalAudios([
李维's avatar
李维 committed
36 37 38 39 40 41
    	"test_audio",
      "bird_fly",
      "enter",
      "good_job",
      "star_boom",
      "try_again"
李维's avatar
李维 committed
42
    ])
李维's avatar
李维 committed
43 44 45 46 47 48 49 50 51 52 53 54 55
    this.g_cartoon.playAudio("enter", ()=>{
      if(this.g_formData.questionAudio_url) {
        this.g_cartoon.playAudio(this.g_formData.questionAudio_url, ()=>{
          this.initBGMusic();
          this.loadQuestion();
        })
      } else {
        this.initBGMusic();
        this.loadQuestion();
      }
      
    })
    
李维's avatar
李维 committed
56
    this.initBg()
李维's avatar
李维 committed
57
    this.initNodes();
李维's avatar
李维 committed
58
    this.resetPosition();
李维's avatar
李维 committed
59 60
    this.initBottomButtons();
    this.initBirds();
李维's avatar
李维 committed
61 62
    this.initImageCards();
    this.initTitle();
李维's avatar
李维 committed
63

李维's avatar
李维 committed
64
    this.initMaskLayer();
李维's avatar
李维 committed
65 66 67
  }

  initNodes() {
李维's avatar
李维 committed
68
  	let dataLength = this.g_formData.dataArray.length;
李维's avatar
李维 committed
69 70
  	this.g_cartoon.initCartoonElement("title_text", this.g_cartoon.getNodeByPath(`Canvas/title/text`))
    for(let index=0; index<10; index++) {
李维's avatar
李维 committed
71
      const card = this.g_cartoon.initCartoonElement(`card_${index+1}`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}`))
李维's avatar
李维 committed
72 73 74 75
      if(index>=dataLength) {
      	card.node.active = false;
      }

李维's avatar
李维 committed
76 77 78 79 80 81 82
      this.g_cartoon.initCartoonElement(`card_${index+1}_normal`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/normal`));
      this.g_cartoon.initCartoonElement(`card_${index+1}_selected`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/selected`));
      this.g_cartoon.initCartoonElement(`card_${index+1}_bell`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/bell`))
      this.g_cartoon.initCartoonElement(`card_${index+1}_bell_text`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/bell/index`))
      this.g_cartoon.initCartoonElement(`card_${index+1}_bell_outline`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/bell/bell_outline`));
      this.g_cartoon.initCartoonElement(`card_${index+1}_image_container`, this.g_cartoon.getNodeByPath(`Canvas/card_${index+1}/container/image`));
    }
李维's avatar
李维 committed
83 84 85 86 87 88 89 90

    if(dataLength==0) {
    	this.g_cartoon.getNodeByPath(`Canvas/shengzi/line_1`).active = false;
      this.g_cartoon.getNodeByPath(`Canvas/shengzi/line_2`).active = false;
    } else if(dataLength<=5) {
    	this.g_cartoon.getNodeByPath(`Canvas/shengzi/line_2`).active = false;
    }

李维's avatar
李维 committed
91 92
    this.g_cartoon.initCartoonElement(`snow_container`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/snow_container`));
    
李维's avatar
李维 committed
93 94
		this.g_cartoon.initCartoonElement(`bottomButtons`, this.g_cartoon.getNodeByPath(`Canvas/bottomButtons`));
    this.g_cartoon.initCartoonElement(`restart_button`, this.g_cartoon.getNodeByPath(`Canvas/bottomButtons/restart`));
李维's avatar
李维 committed
95 96 97

    this.g_cartoon.initCartoonElement(`bird_1`, this.g_cartoon.getNodeByPath(`Canvas/bird_1`));
    this.g_cartoon.initCartoonElement(`bird_2`, this.g_cartoon.getNodeByPath(`Canvas/bird_2`));
李维's avatar
李维 committed
98 99 100 101 102 103
    this.g_cartoon.initCartoonElement(`birdStatic_1`, this.g_cartoon.getNodeByPath(`Canvas/bird_1/static`));
    this.g_cartoon.initCartoonElement(`birdStatic_2`, this.g_cartoon.getNodeByPath(`Canvas/bird_2/static`));
    this.g_cartoon.initCartoonElement(`birdAniFly_1`, this.g_cartoon.getNodeByPath(`Canvas/bird_1/fly`));
    this.g_cartoon.initCartoonElement(`birdAniFly_2`, this.g_cartoon.getNodeByPath(`Canvas/bird_2/fly`));
    this.g_cartoon.initCartoonElement(`birdAniWrong_1`, this.g_cartoon.getNodeByPath(`Canvas/bird_1/wrong`));
    this.g_cartoon.initCartoonElement(`birdAniWrong_2`, this.g_cartoon.getNodeByPath(`Canvas/bird_2/wrong`));
李维's avatar
李维 committed
104

李维's avatar
李维 committed
105
    this.g_cartoon.initCartoonElement(`birdAudio`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/bird_audio`));
李维's avatar
李维 committed
106 107 108 109
    this.g_cartoon.initCartoonElement(`birdAudioClickTarget`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/bird_audio/clickTarget`));
    this.g_cartoon.initCartoonElement(`birdAudioNormal`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/bird_audio/normal`));
    this.g_cartoon.initCartoonElement(`birdAudioWrong`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/bird_audio/wrong`));
    this.g_cartoon.initCartoonElement(`birdAudioPlaying`, this.g_cartoon.getNodeByPath(`Canvas/bgMain/bird_audio/playing`));
李维's avatar
李维 committed
110

李维's avatar
李维 committed
111 112 113 114
  }

  initBg() {
    const bgNode = cc.find('Canvas/bgMain/bg');
李维's avatar
李维 committed
115
    const bgSnowContainer = cc.find('Canvas/bgMain/snow_container');
李维's avatar
李维 committed
116 117 118 119 120 121
    const bgNodeSnow = cc.find('Canvas/bgMain/bg_snow');
    const tree1 = cc.find('Canvas/bgMain/tree_1');
    const tree2 = cc.find('Canvas/bgMain/tree_2/1')
    const tree3 = cc.find('Canvas/bgMain/tree_2/2')

    bgNode.scale = this.g_systemOption.mapScaleMax;
李维's avatar
李维 committed
122
    bgSnowContainer.scale = this.g_systemOption.mapScaleMax;
李维's avatar
李维 committed
123 124 125 126
    bgNodeSnow.width = cc.winSize.width
    tree1.height = cc.winSize.height
    tree2.height = cc.winSize.height
    tree3.height = cc.winSize.height
李维's avatar
李维 committed
127 128
  }

李维's avatar
李维 committed
129 130
  initBGMusic() {
  	if(this.g_formData.bgMusic) {
李维's avatar
李维 committed
131 132 133 134
			this.g_cartoon.playAudio(this.g_formData.bgMusic).then(audioID => {
        cc.audioEngine.setLoop(audioID, true);
        cc.audioEngine.setVolume(audioID, 0.3);
			})
李维's avatar
李维 committed
135 136 137
  	}
  }

李维's avatar
李维 committed
138
  resetPosition() {
李维's avatar
李维 committed
139 140 141
    const bgNodeTitle= cc.find('Canvas/title');
    const bgNodeSnow = cc.find('Canvas/bgMain/bg_snow');
    const bgNodeShuicao = cc.find('Canvas/bgMain/shuicao');
李维's avatar
李维 committed
142
    bgNodeTitle.y = cc.winSize.height / 2 - bgNodeTitle.height / 2 - 50
李维's avatar
李维 committed
143 144
    bgNodeSnow.y = -cc.winSize.height / 2 + bgNodeSnow.height / 2
    bgNodeShuicao.y = -cc.winSize.height / 2 + bgNodeShuicao.height / 2
李维's avatar
李维 committed
145 146 147

    const cardPos = [
  		{x: -500, y: 128}, {x: -250, y: 128}, {x: 0, y: 128}, {x: 250, y: 128}, {x: 500, y: 128},
李维's avatar
李维 committed
148
  		{x: -500, y: -114}, {x: -250, y: -114}, {x: 0, y: -114}, {x: 250, y: -114}, {x: 500, y: -114}
李维's avatar
李维 committed
149 150 151 152 153 154
  	]

		for(let index=0; index<10; index++) {
      const card = this.g_cartoon.getCartoonElement(`card_${index+1}`)
      card.node.setPosition(cardPos[index].x, cardPos[index].y)
    }
李维's avatar
李维 committed
155 156 157
  }

  initImageCards() {
李维's avatar
李维 committed
158
  	let dataLength = this.g_formData.dataArray.length;
李维's avatar
李维 committed
159 160 161 162 163 164 165 166
    if(dataLength == 0) {
      return 
    }
    let newIndex = [0];
    if(dataLength!=1) {
      newIndex = this.getRandomArray(dataLength)
    }

李维's avatar
李维 committed
167 168 169 170 171 172
    this.m_currentQuestionArray = []
    for(let index=0; index<dataLength; index++) {
    	this.m_currentQuestionArray.push(index)
    }

  	for (let index = 0; index<dataLength; index++) {
李维's avatar
李维 committed
173
      let dataItem = this.g_formData.dataArray[newIndex[index]];
李维's avatar
李维 committed
174
      let card = this.g_cartoon.getCartoonElement(`card_${index+1}`)
李维's avatar
李维 committed
175 176
      card.open = false;

李维's avatar
李维 committed
177 178 179
      let cardNormal = this.g_cartoon.getCartoonElement(`card_${index+1}_normal`)
      let cardSelected = this.g_cartoon.getCartoonElement(`card_${index+1}_selected`)
      let imageContainer = this.g_cartoon.getCartoonElement(`card_${index+1}_image_container`)
李维's avatar
李维 committed
180 181
      imageContainer.cardIndex = index;
      imageContainer.correctIndex = newIndex[index];
李维's avatar
李维 committed
182

李维's avatar
李维 committed
183 184 185 186 187
      let bellOutline = this.g_cartoon.getCartoonElement(`card_${index+1}_bell_outline`)
      let bellText = this.g_cartoon.getCartoonElement(`card_${index+1}_bell_text`)
      bellText.node.getComponent(cc.Label).font = this.g_cartoon.getFontFamily("Comic Sans MS")
    	bellText.node.getComponent(cc.Label).fontSize = 20

李维's avatar
李维 committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
      card.playAudio = (callback) => {
        if(dataItem.audio_url) {
          this.g_cartoon.playAudio(dataItem.audio_url, ()=>{
            if(callback) {
              callback()
            }
          })
        } else {
          if(callback) {
            callback()
          }
        }
      }

李维's avatar
李维 committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
      if(this.g_useDefaultData || !dataItem) {
        let fr = this.getSpriteFrimeByResName("card_demo")
        imageContainer.node.setScale(1)
        imageContainer.node.getComponent(cc.Sprite).spriteFrame = fr
      } else {
        this.getSpriteFrimeByUrl(dataItem.image_url, (fr)=>{
          let sx = 220 / fr.getRect().width;
          let sy = 110 / fr.getRect().height;
          let s = Math.min(sx, sy)
          imageContainer.node.width = s * fr.getRect().width;
          imageContainer.node.height = s * fr.getRect().height;
          imageContainer.node.getComponent(cc.Sprite).spriteFrame = fr
        })
      }

      bellOutline.node.setScale(0);

      this.g_cartoon.subscribeTouchEvent(`card_${index+1}_image_container`, ()=>{
李维's avatar
李维 committed
220
      	if(card.open || this.m_gameEnd || !this.g_enableClick) {
李维's avatar
李维 committed
221 222
      		return;
      	}
李维's avatar
李维 committed
223
        this.g_enableClick = false;
李维's avatar
李维 committed
224
        if(this.m_currentQuestion == imageContainer.correctIndex ) {
李维's avatar
李维 committed
225
          this.g_cartoon.playAudio("good_job")
李维's avatar
李维 committed
226 227 228
        	card.open = true;
        	this.g_cartoon.getCartoonElement(`card_${imageContainer.cardIndex+1}_normal`).node.opacity = 0;
			    this.g_cartoon.getCartoonElement(`card_${imageContainer.cardIndex+1}_selected`).node.opacity = 255;
李维's avatar
李维 committed
229
			    this.g_cartoon.getCartoonElement("bird_2").flyTo(imageContainer.cardIndex, ()=>{
李维's avatar
李维 committed
230 231 232 233
			    	this.switchCard(this.m_currentQuestion, imageContainer.cardIndex, ()=>{
              this.g_enableClick = true;
              this.loadQuestion()
            })
李维's avatar
李维 committed
234
			    })
李维's avatar
李维 committed
235
        } else {
李维's avatar
李维 committed
236 237
          this.g_cartoon.getCartoonElement("bird_1").wrong()
          this.g_cartoon.getCartoonElement("bird_2").wrong()
李维's avatar
李维 committed
238 239 240 241 242 243
          this.g_cartoon.playAudio("try_again", ()=>{
            setTimeout(()=>{
              this.g_cartoon.playAudio(this.g_formData.dataArray[this.m_currentQuestion].audio_url)
              this.g_enableClick = true;
            }, 1000)
          })
李维's avatar
李维 committed
244
        }
李维's avatar
李维 committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      })

      card.tweenHandle = null;
      const runBell = () => {
      	bellOutline.node.setScale(1, 1)
      	bellOutline.node.opacity = 255;
      	card.tweenHandle = this.g_cartoon.tweenChange(bellOutline.node, {scaleX: 2, scaleY: 2, opacity: 0}, 0.4, ()=>{
      		runBell()
      	})
      }

      const stopBell = () => {
      	if(card.tweenHandle) {
      		card.tweenHandle.stop();
      		card.tweenHandle = null
      	}
      	bellOutline.node.setScale(0, 0)
      	bellOutline.node.opacity = 0;
      }

      this.g_cartoon.subscribeTouchEvent(`card_${index+1}_bell`, ()=>{
李维's avatar
李维 committed
266 267 268 269 270 271 272
        if(dataItem.audio_url) {
          this.g_cartoon.stopAllAudio()
          card.playAudio(()=>{
            stopBell()
          })
          runBell()
        }
李维's avatar
李维 committed
273 274 275 276
      })
    }
  }

李维's avatar
李维 committed
277
  switchCard(fromIndex, toIndex, callback) {
李维's avatar
李维 committed
278 279 280
    let i1 = this.m_currentQuestionArray[fromIndex]+1;
    let i2 = this.m_currentQuestionArray[toIndex]
    let card_1 = this.g_cartoon.getCartoonElement(`card_${i1}`)
李维's avatar
李维 committed
281
    let card_2 = this.g_cartoon.getCartoonElement(`card_${toIndex+1}`)
李维's avatar
李维 committed
282 283 284 285 286
    // console.log("交换",`card_${i1}`,`card_${toIndex+1}`)
    let x_1 = card_1.node.x
    let y_1 = card_1.node.y
    let x_2 = card_2.node.x
    let y_2 = card_2.node.y
李维's avatar
李维 committed
287
    
李维's avatar
李维 committed
288 289
    // card_1.node.setSiblingIndex(10);
    // card_2.node.setSiblingIndex(10);
李维's avatar
李维 committed
290 291 292

 		let bell1 = this.g_cartoon.getCartoonElement(`card_${i1}_bell`)
    let bell2 = this.g_cartoon.getCartoonElement(`card_${toIndex+1}_bell`)
李维's avatar
李维 committed
293 294 295 296 297 298 299
    
    this.g_cartoon.tweenChange(bell1.node, {scaleX: 1.2, scaleY: 1.2}, 0.3, ()=>{
      this.g_cartoon.tweenChange(bell1.node, {scaleX: 0, scaleY: 0}, 0.4, ()=>{
        this.g_cartoon.tweenChange(card_1.node, {x: x_2, y: y_2}, 1, ()=>{
          this.g_cartoon.tweenChange(bell1.node, {scaleX: 1, scaleY: 1}, 0.2);
        });
      });
李维's avatar
李维 committed
300 301
    });

李维's avatar
李维 committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    this.g_cartoon.tweenChange(bell2.node, {scaleX: 1.2, scaleY: 1.2}, 0.3, ()=>{
      this.g_cartoon.tweenChange(bell2.node, {scaleX: 0, scaleY: 0}, 0.4, ()=>{
        this.g_cartoon.tweenChange(card_2.node, {x: x_1, y: y_1}, 1, ()=>{
          this.g_cartoon.tweenChange(bell2.node, {scaleX: 1, scaleY: 1}, 0.2, ()=>{
            this.g_cartoon.getCartoonElement("bird_1").flyBack(()=>{
              setTimeout(()=>{
                if(callback) {
                  callback();
                }
              }, 1500)
            })
            this.g_cartoon.getCartoonElement("bird_2").flyBack()
          });
        });
      });
李维's avatar
李维 committed
317
    });
李维's avatar
李维 committed
318 319

    let bellText1 = this.g_cartoon.getCartoonElement(`card_${i1}_bell_text`)
李维's avatar
李维 committed
320
    let bellText2 = this.g_cartoon.getCartoonElement(`card_${toIndex+1}_bell_text`)
李维's avatar
李维 committed
321
    toIndex = this.getCurrentIndex(toIndex)
李维's avatar
李维 committed
322 323
    bellText1.node.getComponent(cc.Label).string = "" + (toIndex+1)
    bellText2.node.getComponent(cc.Label).string = "" + (fromIndex+1)
李维's avatar
李维 committed
324 325 326 327 328 329 330 331 332 333

    let temp = this.m_currentQuestionArray[fromIndex]
    this.m_currentQuestionArray[fromIndex] = this.m_currentQuestionArray[toIndex]
    this.m_currentQuestionArray[toIndex] = temp;
  }

  getCurrentIndex(index) {
    return this.m_currentQuestionArray.indexOf(index)
  }

李维's avatar
李维 committed
334
  initTitle() {
李维's avatar
李维 committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		let textNode = this.g_cartoon.getCartoonElement("title_text")
    let fontSize = 36
    if(this.g_formData.titleText.length>=32 && this.g_formData.titleText.length<48) {
      fontSize = 26
    } else if(this.g_formData.titleText.length>=48) {
      fontSize = 20
    }
    textNode.node.getComponent(cc.Label).string = this.g_formData.titleText;
		textNode.node.getComponent(cc.Label).font = this.g_cartoon.getFontFamily("Comic Sans MS")
    textNode.node.getComponent(cc.Label).fontSize = fontSize

    textNode.audioID = null
    this.g_cartoon.subscribeTouchEvent(textNode.key, ()=>{
      if(textNode.audioID) {
        cc.audioEngine.stop(textNode.audioID);
        textNode.audioID = null
      }
      this.g_cartoon.playAudio(this.g_formData.questionAudio_url).then((audio_id) =>{
        textNode.audioID = audio_id
      })
    })
李维's avatar
李维 committed
356 357
  }

李维's avatar
李维 committed
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
  getRandomArray(length) {
    Array.prototype.shuffle = function() {
      let m = this.length, i;
      while (m) {
          i = (Math.random() * m--) >>> 0;
          [this[m], this[i]] = [this[i], this[m]]
      }
      return this;
    }
    
    const gameLogicCheck = (array, startIndex) => {
      let res = true;
      for(let index=startIndex; index<array.length; index++) {
        let changeIndex = array.indexOf(index)
        if(array[index]==index) {
          res = false
          break;
        }
      }
      return res;
    }

    let indexArray = []
    for(let index=0; index<length; index++) {
      indexArray.push(index)
    }
    var arr = indexArray.shuffle()
    var result = false;
    var temp = null;
    var count = 0;
    var arrayCanUse = []
    do{
      result = false;
      arr = indexArray.shuffle()
      arrayCanUse = JSON.parse(JSON.stringify(arr))

      for(let index=0; index<arr.length; index++) {
        if(index==arr[index]) {
          result = true;
          break
        }
      }
    
      if(!result) {
        for(let index=0; index<arr.length-2; index++) {
          var indexChange = arr.indexOf(index)
          temp = arr[index]
          arr[index] = index;
          arr[indexChange] = temp;
          if(!gameLogicCheck(arr, index+1)) {
            result = true;
            break
          }
        }

      }
      if(count>20000) {
        console.log("已经尝试超过两万次,终止生成", arr)
        result = false
      }
    } while(result)

    return arrayCanUse
  }

  loadQuestion() {
李维's avatar
李维 committed
424
    let index = null;
李维's avatar
李维 committed
425
    let dataLength = this.g_formData.dataArray.length;
李维's avatar
李维 committed
426 427 428
    for(let i=0; i<10; i++) {
      this.g_cartoon.getCartoonElement(`card_${i+1}_normal`).node.opacity = 255;
      this.g_cartoon.getCartoonElement(`card_${i+1}_selected`).node.opacity = 0;
李维's avatar
李维 committed
429 430
    }
    this.m_currentQuestion++;
李维's avatar
李维 committed
431 432 433 434
    if((this.m_currentQuestion+1)==dataLength) {
    	this.endGame()
    } else {
    	index = this.m_currentQuestionArray[this.m_currentQuestion]
李维's avatar
李维 committed
435
    	this.g_cartoon.getCartoonElement("bird_1").flyTo(index, ()=>{
李维's avatar
李维 committed
436
        this.g_enableClick = true;
李维's avatar
李维 committed
437 438
        this.g_cartoon.playAudio(this.g_formData.dataArray[this.m_currentQuestion].audio_url)
      })
李维's avatar
李维 committed
439 440 441 442 443 444
	    this.g_cartoon.getCartoonElement(`card_${index+1}_normal`).node.opacity = 0;
	    this.g_cartoon.getCartoonElement(`card_${index+1}_selected`).node.opacity = 255;
    }
  }

  endGame() {
李维's avatar
李维 committed
445 446
    this.m_gameEnd = true;
    this.snowing(5000)
李维's avatar
李维 committed
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
  	let resetBtn = this.g_cartoon.getCartoonElement("restart_button")
  	resetBtn.node.active = true;
  }

  restartGame(callback) {
  	this.m_questionAudioPlayed = false;
	  this.m_currentQuestion = -1;
	  this.m_questionDone = false;
	  this.m_currentQuestionArray = []
	  this.m_gameEnd = false;
		for(let index=0; index<10; index++) {
      const card = this.g_cartoon.getCartoonElement(`card_${index+1}`)
    }
  	this.g_cartoon.stopAllAudio()
    this.showMaskLayer(()=>{
    	this.resetPosition();
    	this.initImageCards();
	    this.initTitle();
	    this.loadQuestion();
      this.hideMaskLayer(()=>{ 
        if(callback) {
          callback()
        }
      })
    })
  }

  initBottomButtons() {
  	let bottomButtons = this.g_cartoon.getCartoonElement("bottomButtons")
  	bottomButtons.node.x = cc.winSize.width / 2 - 100
  	bottomButtons.node.y = -cc.winSize.height / 2 + 50

  	let resetBtn = this.g_cartoon.getCartoonElement("restart_button")
  	resetBtn.node.active = false;

  	this.g_cartoon.subscribeTouchEvent(resetBtn.key, ()=>{
  		jelly(resetBtn.node).then(()=>{
  			resetBtn.node.active = false;
  		})
			this.restartGame()
  	})

  }

李维's avatar
李维 committed
491
  initBirds() {
李维's avatar
李维 committed
492
  	let bird_1 = this.g_cartoon.getCartoonElement("bird_1");
李维's avatar
李维 committed
493
    let bird_2 = this.g_cartoon.getCartoonElement("bird_2");
李维's avatar
李维 committed
494 495 496 497 498 499 500 501
    let birdAniFly_1 = this.g_cartoon.getCartoonElement("birdAniFly_1");
    let birdAniFly_2 = this.g_cartoon.getCartoonElement("birdAniFly_2");
    let birdStatic_1 = this.g_cartoon.getCartoonElement("birdStatic_1");
    let birdStatic_2 = this.g_cartoon.getCartoonElement("birdStatic_2");
    let birdAniWrong_1 = this.g_cartoon.getCartoonElement("birdAniWrong_1");
    let birdAniWrong_2 = this.g_cartoon.getCartoonElement("birdAniWrong_2");

    let birdAudioClickTarget = this.g_cartoon.getCartoonElement("birdAudioClickTarget");
李维's avatar
李维 committed
502
    let birdAudio = this.g_cartoon.getCartoonElement("birdAudio");
李维's avatar
李维 committed
503 504 505
    let birdAudioNormal = this.g_cartoon.getCartoonElement("birdAudioNormal");
    let birdAudioWrong = this.g_cartoon.getCartoonElement("birdAudioWrong");
    let birdAudioPlaying = this.g_cartoon.getCartoonElement("birdAudioPlaying");
李维's avatar
李维 committed
506

李维's avatar
李维 committed
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
    const birdDragonBones_1 = {
      "texPngData": {
        "url": "http://staging-teach.cdn.ireadabc.com/3fa1882c54e339da6dd269c97f04bb2a.png",
        "name": "bird1_tex.png"
      },
      "texJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/59b30e639be3a22584acec712dd78574.json",
        "name": "bird1_tex.json"
      },
      "skeJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/decfe2180607b27160bd34d07448898c.json",
        "name": "bird1_ske.json"
      }
    }
    
    const birdDragonBones_2 = {
      "texPngData": {
        "url": "http://staging-teach.cdn.ireadabc.com/09ff5666c731f45808e1c0f3d0bfc124.png",
        "name": "bird2_tex.png"
      },
      "texJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/49231d10601dfadffedb9ee25dac164b.json",
        "name": "bird2_tex.json"
      },
      "skeJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/c39541350a2828337d5cecb59deac5e8.json",
        "name": "bird2_ske.json"
      }
    }

李维's avatar
李维 committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    const audioBird = {
      "texPngData": {
        "url": "http://staging-teach.cdn.ireadabc.com/24a4f73fd0ba329427cc3a9c07430ede.png",
        "name": "playbird_tex.png"
      },
      "texJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/7b855d3ca30aa8833b9c460424ca2e5c.json",
        "name": "playbird_tex.json"
      },
      "skeJsonData": {
        "url": "http://staging-teach.cdn.ireadabc.com/a611415a85a614a3f3a2c9620633bc2a.json",
        "name": "playbird_ske.json"
      }
    }

    // Normal
    birdAniFly_1.node.opacity = 0;
    birdAniFly_2.node.opacity = 0;

    birdAniFly_1.dragonBones.setDragonBones(birdDragonBones_1, "Armature鸟1", ()=>{
      birdAniFly_1.dragonBones.showDragonBones(-1, "normal")
李维's avatar
李维 committed
558
    })
李维's avatar
李维 committed
559 560 561 562 563 564 565
    birdAniFly_2.dragonBones.setDragonBones(birdDragonBones_2,"Armature鸟2", ()=>{
      birdAniFly_2.dragonBones.showDragonBones(-1, "normal")
    })

    // Wrong
    birdAniWrong_1.node.opacity = 0;
    birdAniWrong_2.node.opacity = 0;
李维's avatar
李维 committed
566

李维's avatar
李维 committed
567 568 569 570 571
    birdAniWrong_1.dragonBones.setDragonBones(birdDragonBones_1, "Armature鸟1", ()=>{
      birdAniWrong_1.dragonBones.showDragonBones(-1, "wrong")
    })
    birdAniWrong_2.dragonBones.setDragonBones(birdDragonBones_2,"Armature鸟2", ()=>{
      birdAniWrong_2.dragonBones.showDragonBones(-1, "wrong")
李维's avatar
李维 committed
572 573
    })
    
李维's avatar
李维 committed
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    // Bird audio
    // birdAudioNormal.node.opacity = 0;
    birdAudioNormal.dragonBones.setDragonBones(audioBird, "Armature播放", ()=>{
      birdAudioNormal.dragonBones.showDragonBones(-1, "normal")
    })

    birdAudioWrong.node.opacity = 0;
    birdAudioWrong.dragonBones.setDragonBones(audioBird, "Armature播放", ()=>{
      birdAudioWrong.dragonBones.showDragonBones(-1, "wrong")
    })

    birdAudioPlaying.node.opacity = 0;
    birdAudioPlaying.dragonBones.setDragonBones(audioBird, "Armature播放", ()=>{
      birdAudioPlaying.dragonBones.showDragonBones(-1, "playing")
    })

    birdAudio.switchAni = (type) => {
      switch(type) {
        case 0:
          birdAudioNormal.node.opacity = 255;
          birdAudioWrong.node.opacity = 0;
          birdAudioPlaying.node.opacity = 0;
          break;
        case 1:
          birdAudioNormal.node.opacity = 0;
          birdAudioWrong.node.opacity = 255;
          birdAudioPlaying.node.opacity = 0;
          break;
        case 2:
          birdAudioNormal.node.opacity = 0;
          birdAudioWrong.node.opacity = 0;
          birdAudioPlaying.node.opacity = 255;
          break;
        default: 
          birdAudioNormal.node.opacity = 255;
          birdAudioWrong.node.opacity = 0;
          birdAudioPlaying.node.opacity = 0;
          break;
      }
    }

李维's avatar
李维 committed
615
  	bird_1.flyTo = (cardIndex, callback)=>{
李维's avatar
李维 committed
616 617 618
      let card = this.g_cartoon.getCartoonElement(`card_${cardIndex+1}`)
      card.node.setSiblingIndex(100)

李维's avatar
李维 committed
619 620 621 622 623 624
  		const worldPos = cc.find("Canvas").convertToWorldSpaceAR(cc.v2(bird_1.node.x, bird_1.node.y));
			const localPos = card.node.convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));

			bird_1.node.x = localPos.x;
  		bird_1.node.y = localPos.y;
  		bird_1.node.parent = card.node;
李维's avatar
李维 committed
625 626 627 628 629 630
      birdAniFly_1.node.opacity = 255;
      birdStatic_1.node.opacity = 0;
      
  		this.g_cartoon.tweenChange(bird_1.node, {x: -80, y: 90}, 2, ()=>{
        birdAniFly_1.node.opacity = 0;
        birdStatic_1.node.opacity = 255;
李维's avatar
李维 committed
631 632 633 634 635 636 637 638 639 640 641 642 643
  			if(callback) {
  				callback()
  			}
  		})
  	}

  	bird_1.flyBack = (callback)=>{
  		let parent = bird_1.node.parent
  		const worldPos = parent.convertToWorldSpaceAR(cc.v2(bird_1.node.x, bird_1.node.y));
			const localPos = cc.find("Canvas").convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));
			bird_1.node.x = localPos.x;
  		bird_1.node.y = localPos.y;
  		bird_1.node.parent = cc.find("Canvas");
李维's avatar
李维 committed
644 645
      birdAniFly_1.node.opacity = 255;
      birdStatic_1.node.opacity = 0;
李维's avatar
李维 committed
646

李维's avatar
李维 committed
647
  		this.g_cartoon.tweenChange(bird_1.node, {x: -100, y: -cc.winSize.height / 2 + bird_2.node.height + 10}, 0.5, ()=>{
李维's avatar
李维 committed
648 649
        birdAniFly_1.node.opacity = 0;
        birdStatic_1.node.opacity = 255;
李维's avatar
李维 committed
650 651 652 653 654 655 656
  			if(callback) {
  				callback()
  			}
  		})
  	}

  	bird_2.flyTo = (cardIndex, callback)=>{
李维's avatar
李维 committed
657 658
      let card = this.g_cartoon.getCartoonElement(`card_${cardIndex+1}`)

李维's avatar
李维 committed
659 660
      card.node.setSiblingIndex(100)

李维's avatar
李维 committed
661 662 663 664 665 666
  		const worldPos = cc.find("Canvas").convertToWorldSpaceAR(cc.v2(bird_2.node.x, bird_2.node.y));
			const localPos = card.node.convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));

			bird_2.node.x = localPos.x;
  		bird_2.node.y = localPos.y;
  		bird_2.node.parent = card.node;
李维's avatar
李维 committed
667 668 669 670 671 672
      birdAniFly_2.node.opacity = 255;
      birdStatic_2.node.opacity = 0;
      
  		this.g_cartoon.tweenChange(bird_2.node, {x: 80, y: 90}, 2, ()=>{
        birdAniFly_2.node.opacity = 0;
        birdStatic_2.node.opacity = 255;
李维's avatar
李维 committed
673 674 675 676 677 678 679 680 681 682 683 684 685
  			if(callback) {
  				callback()
  			}
  		})
  	}

  	bird_2.flyBack = (callback)=>{
  		let parent = bird_2.node.parent
  		const worldPos = parent.convertToWorldSpaceAR(cc.v2(bird_2.node.x, bird_2.node.y));
			const localPos = cc.find("Canvas").convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));
			bird_2.node.x = localPos.x;
  		bird_2.node.y = localPos.y;
  		bird_2.node.parent = cc.find("Canvas");
李维's avatar
李维 committed
686 687
      birdAniFly_2.node.opacity = 255;
      birdStatic_2.node.opacity = 0;
李维's avatar
李维 committed
688

李维's avatar
李维 committed
689
  		this.g_cartoon.tweenChange(bird_2.node, {x: 105, y: -cc.winSize.height / 2 + bird_2.node.height}, 0.5, ()=>{
李维's avatar
李维 committed
690 691
        birdAniFly_2.node.opacity = 0;
        birdStatic_2.node.opacity = 255;
李维's avatar
李维 committed
692 693 694 695 696 697
  			if(callback) {
  				callback()
  			}
  		})
  	}

李维's avatar
李维 committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    bird_1.wrong = ()=>{
      birdAniWrong_1.node.opacity = 255;
      birdStatic_1.node.opacity = 0;
      birdAudio.switchAni(1)
      setTimeout(() => {
        birdAniWrong_1.node.opacity = 0;
        birdStatic_1.node.opacity = 255;
        birdAudio.switchAni(0)
      }, 1000);
    }

    bird_2.wrong = ()=>{
      birdAniWrong_2.node.opacity = 255;
      birdStatic_2.node.opacity = 0;

      setTimeout(() => {
        birdAniWrong_2.node.opacity = 0;
        birdStatic_2.node.opacity = 255;
      }, 1000);
    }

李维's avatar
李维 committed
719 720 721 722
    bird_1.node.y = -cc.winSize.height / 2 + bird_1.node.height + 10
    bird_2.node.y = -cc.winSize.height / 2 + bird_2.node.height
    birdAudio.node.y = -cc.winSize.height / 2 + bird_2.node.height + 20
    birdAudio.audioID = null
李维's avatar
李维 committed
723 724 725 726
    // bird_1.node.setSiblingIndex(20);
    // bird_2.node.setSiblingIndex(20);
    // bird_1.node.zIndex = 20
    // bird_2.node.zIndex = 20
李维's avatar
李维 committed
727

李维's avatar
李维 committed
728
    this.g_cartoon.subscribeTouchEvent(birdAudioClickTarget.key, ()=>{
李维's avatar
李维 committed
729 730 731 732 733
      if(birdAudio.audioID) {
        cc.audioEngine.stop(birdAudio.audioID);
        birdAudio.audioID = null;
      }

李维's avatar
李维 committed
734 735
      if(this.m_currentQuestion!=-1 && this.g_formData.dataArray[this.m_currentQuestion].audio_url) {
        birdAudio.switchAni(2)
李维's avatar
李维 committed
736
        this.g_cartoon.playAudio(this.g_formData.dataArray[this.m_currentQuestion].audio_url, ()=>{
李维's avatar
李维 committed
737
          birdAudio.switchAni(0)
李维's avatar
李维 committed
738 739 740 741 742
        }).then(audio_id=>{
          birdAudio.audioID = audio_id
        })
      }
    })
李维's avatar
李维 committed
743
  }
李维's avatar
李维 committed
744

李维's avatar
李维 committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758
  snowing(time=5000) {
    const parent = this.g_cartoon.getCartoonElement("snow_container")
    const getSnow = (index) => {
      const snowArray = [
        this.getSprNode('snow_1'), this.getSprNode('snow_2'), this.getSprNode('snow_3'), this.getSprNode('snow_4')
      ]
      const sf = this.getSprNode(`snow_${index}`).getComponent(cc.Sprite).spriteFrame;
      const node = new cc.Node();
      node.addComponent(cc.Sprite).spriteFrame = sf;
      node.y = cc.winSize.height/2 + node.height - 10;
      node.x = Math.ceil(Math.random() * cc.winSize.width) - cc.winSize.width / 2
      node.parent = parent.node

      return node;
李维's avatar
李维 committed
759 760
    }
    
李维's avatar
李维 committed
761 762 763 764 765 766
   
    let intervalID = setInterval(() => {
      let snow = getSnow( Math.ceil(Math.random()*4) )
      this.g_cartoon.tweenChange(snow, {y: -cc.winSize.height / 2}, 2000)
      cc.tween(snow).to(3, {y: -cc.winSize.height / 2}).start()
    }, 100);
李维's avatar
李维 committed
767

李维's avatar
李维 committed
768 769 770
    setTimeout(() => {
      clearInterval(intervalID)
    }, time);
李维's avatar
李维 committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

  }














  // Common function
  maskLayer = null;
  initMaskLayer() {
李维's avatar
李维 committed
790
    const parentNode = this.g_cartoon.getNodeByPath("MaskLayer")
李维's avatar
李维 committed
791 792 793 794 795 796 797
    parentNode.x = cc.winSize.width / 2
    parentNode.y = cc.winSize.height / 2
    const bNode = this.getSprNode('black');
    bNode.scaleX = cc.winSize.width / bNode.width;
    bNode.scaleY = cc.winSize.height / bNode.height;
    bNode.x = 0
    bNode.y = 0
李维's avatar
李维 committed
798
    parentNode.setSiblingIndex(255)
李维's avatar
李维 committed
799
    bNode.parent = cc.find('MaskLayer');
李维's avatar
李维 committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
    bNode.active = false;
    this.maskLayer = bNode;
  }

  showMaskLayer(cb) {
    this.maskLayer.opacity = 0;
    this.maskLayer.active = true;
    cc.tween(this.maskLayer)
      .to(0.5, {opacity: 255}, {easing: 'sineOut'})
      .call(() => {
        cb()
      })
      .start()
  }

  hideMaskLayer(cb = null) {
    this.maskLayer.opacity = 255;
    cc.tween(this.maskLayer)
    .to(0.5, {opacity: 0}, {easing: 'sineIn'})
    .call(() => {
      this.maskLayer.active = false;
      if (cb) {
        cb()
      }
    })
    .start()
  }





  // Common API
  getSpriteFrimeByUrl(url, cb) {
李维's avatar
李维 committed
834
		cc.assetManager.loadRemote(url, (err, img) => {
李维's avatar
李维 committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
			const spriteFrame = new cc.SpriteFrame(img)
			if (cb) {
				cb(spriteFrame);
			}
		})
  }

  getSpriteFrimeByResName(name) {
    const sf = cc.find('Canvas/res/img/' + name).getComponent(cc.Sprite).spriteFrame;
    // const node = new cc.Node();
    // node.addComponent(cc.Sprite).spriteFrame = sf;
    return sf;
  }

  getSprNode(resName) {
    const sf = cc.find('Canvas/res/img/' + resName).getComponent(cc.Sprite).spriteFrame;
    const node = new cc.Node();
    node.addComponent(cc.Sprite).spriteFrame = sf;
    return node;
  }
}