GameMain.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { _decorator, Component, Prefab, SpriteFrame, math, NodePool, instantiate, Node, Tween, tween, Vec3, v3 } from 'cc';
  2. import { Global } from '../Common/Global';
  3. import { AudioManage } from '../Manager/AudioManage';
  4. import { GameManager } from '../Manager/GameManager';
  5. import { items } from './items';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('GameMain')
  8. export class GameMain extends Component {
  9. @property(Prefab)
  10. itemPre: Prefab = null;
  11. @property(Node)
  12. itemParent: Node = null;
  13. itemNodePool: NodePool = new NodePool()
  14. @property(Node)
  15. winNode: Node = null;
  16. @property(Node)
  17. lossNode: Node = null;
  18. start() {
  19. GameManager.getInstance().eventTarget.on('itemData', this.check, this)
  20. }
  21. check(data) {
  22. console.log(data);
  23. if (this.gameState != 'start') return
  24. this.gameState = 'end'
  25. if(data.data == 1){
  26. this.wins()
  27. }else{
  28. this.loss()
  29. }
  30. }
  31. @property(SpriteFrame)
  32. iconArr: Array<SpriteFrame> = []
  33. lengths: number = 0
  34. width: number = 0
  35. gameState = 'ready'
  36. timerOut:any;
  37. /**
  38. * 初始化逻辑
  39. * 每次GameManager.StartGame()时都会调用
  40. */
  41. initUI() {
  42. this.clearParent()
  43. if(GameManager.getInstance().curLevel ==2){
  44. this.itemParent.setPosition(0,-200)
  45. }else{
  46. this.itemParent.setPosition(0,-80)
  47. }
  48. switch (GameManager.getInstance().curLevel) {
  49. case 1: {
  50. this.lengths = 4
  51. this.width = 2
  52. } break;
  53. case 2: {
  54. this.lengths = 6
  55. this.width = 1.3
  56. } break;
  57. case 3: {
  58. this.lengths = 9
  59. this.width = 1.3
  60. } break;
  61. case 4: {
  62. this.lengths = 12
  63. this.width = 1
  64. } break;
  65. case 5: {
  66. this.lengths = 16
  67. this.width = 1
  68. } break;
  69. }
  70. const iconIndex = math.randomRangeInt(0, 10)
  71. const answer = iconIndex > 4 ? iconIndex - 5 : iconIndex + 5
  72. const answerIndex = math.randomRangeInt(0, this.lengths)
  73. for (let index = 0; index < this.lengths; index++) {
  74. if (this.itemNodePool.size() < 1) {
  75. this.itemNodePool.put(instantiate(this.itemPre))
  76. }
  77. const item = this.itemNodePool.get()
  78. item.setScale(this.width, this.width)
  79. if (answerIndex == index) {
  80. item.getComponent(items).refreshUi(1, this.iconArr[answer], index)
  81. } else {
  82. item.getComponent(items).refreshUi(0, this.iconArr[iconIndex], index)
  83. }
  84. item.parent = this.itemParent
  85. }
  86. clearTimeout(this.timerOut)
  87. this.timerOut = setTimeout(() => {
  88. clearTimeout(this.timerOut)
  89. GameManager.getInstance().showTips('请找出不通的图案!')
  90. this.gameState = 'start'
  91. }, this.lengths*300);
  92. }
  93. /** 清理数据 */
  94. clearParent() {
  95. const list = this.itemParent.children
  96. if (list.length > 0) {
  97. for (let index = list.length; index >= 0; index--) {
  98. this.itemNodePool.put(list[index])
  99. }
  100. }
  101. }
  102. wins() {
  103. AudioManage.instance.playSound(Global.Audio_right);
  104. this.winNode.active = true;
  105. this.winNode.setScale(0.9, 0.9)
  106. tween(this.winNode)
  107. .to(0.3, { scale: v3(1.05, 1.05) })
  108. .to(0.2, { scale: v3(1, 1) })
  109. .call(() => {
  110. this.winNode.active = false;
  111. GameManager.getInstance().addScore(20)
  112. if (GameManager.getInstance().curLevelScore < 100) {
  113. this.initUI()
  114. }
  115. })
  116. .start()
  117. }
  118. loss() {
  119. AudioManage.instance.playSound(Global.Audio_wrong);
  120. GameManager.getInstance().showTips('请继续加油哦!')
  121. this.lossNode.active = true;
  122. this.lossNode.setScale(0.9, 0.9)
  123. tween(this.lossNode)
  124. .to(0.3, { scale: v3(1.05, 1.05) })
  125. .to(0.2, { scale: v3(1, 1) })
  126. .call(() => {
  127. this.lossNode.active = false;
  128. GameManager.getInstance().minusScore(20)
  129. this.initUI()
  130. })
  131. .start()
  132. }
  133. /**
  134. * 游戏结束后清理
  135. */
  136. gameOver() {
  137. }
  138. addGold() {
  139. }
  140. update(deltaTime: number) {
  141. }
  142. }