GameMain.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { _decorator, Component, Prefab, SpriteFrame, math, NodePool, instantiate, Node, Tween, tween, Vec3, v3, UITransform } 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. itemPosition = [
  10. [{ x: 0, y: 248 }, { x: -288, y: -194 }, { x: 288, y: -194 }],
  11. [{ x: -288, y: 220 }, { x: 288, y: 220 }, { x: -288, y: -220 }, { x: 288, y: -220 }],
  12. [{ x: -330, y: 152 }, { x: 0, y: 290 }, { x: 330, y: 152 }, { x: -246, y: -194 }, { x: 246, y: -194 }],
  13. ]
  14. @property(Node)
  15. parentGroup: Node = null;
  16. parentGroupUi: UITransform;
  17. parentArr: Array<Node>;
  18. selfLevel = 0;
  19. selfPosition = []
  20. @property(Node)
  21. selectNode: Node = null;
  22. @property([SpriteFrame])
  23. iconSpre: Array<SpriteFrame> = []
  24. selfMap = []
  25. isNotMove = false
  26. answerArr = []
  27. @property(Node)
  28. winNode: Node = null;
  29. @property(Node)
  30. lossNode: Node = null;
  31. onLoad() {
  32. this.parentArr = this.parentGroup.children
  33. this.parentGroupUi = this.parentGroup.getComponent(UITransform)
  34. this.parentGroup.on(Node.EventType.TOUCH_START, this.startTouch, this)
  35. this.parentGroup.on(Node.EventType.TOUCH_MOVE, this.moveTouch, this)
  36. this.parentGroup.on(Node.EventType.TOUCH_END, this.endTouch, this)
  37. }
  38. blackArr = []
  39. /**
  40. * 初始化逻辑
  41. * 每次GameManager.StartGame()时都会调用
  42. */
  43. initUI() {
  44. // AudioManage.instance.playSound(Global.Audio_achieve);
  45. GameManager.getInstance().showTips('')
  46. this.selfLevel = GameManager.getInstance().curLevel - 1
  47. this.selfPosition = this.itemPosition[this.selfLevel]
  48. this.selfMap = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  49. // switch (GameManager.getInstance().curLevel) {
  50. // case 1: {
  51. // this.selfLevel = GameManager.getInstance().curLevel
  52. // this.selfPosition = this.itemPosition[this.selfLevel]
  53. // } break;
  54. // case 2: {
  55. // } break;
  56. // case 3: {
  57. // } break;
  58. // }
  59. this.blackArr = []
  60. this.refreshMap()
  61. }
  62. yanshi: any
  63. yanshi1: any
  64. refreshMap() {
  65. this.answerArr = []
  66. for (let index = 0; index < this.parentArr.length; index++) {
  67. if (index < this.selfPosition.length) {
  68. this.parentArr[index].active = true
  69. this.parentArr[index].setPosition(this.selfPosition[index].x, this.selfPosition[index].y);
  70. const id = math.randomRangeInt(0, this.selfMap.length)
  71. this.parentArr[index].getComponent(items).resetUI(this.iconSpre[this.selfMap[id]], index)
  72. console.log(this.selfMap[id]);
  73. this.parentArr[index].name = this.selfMap[id] + ''
  74. this.answerArr.push(this.selfMap[id])
  75. this.selfMap.splice(id, 1)
  76. } else {
  77. this.parentArr[index].active = false
  78. }
  79. }
  80. clearTimeout(this.yanshi1)
  81. clearTimeout(this.yanshi)
  82. this.yanshi = setTimeout(() => {
  83. AudioManage.instance.playSound(Global.Audio_fanZhuan);
  84. clearTimeout(this.yanshi)
  85. }, 3000);
  86. this.selectNode.active = false;
  87. this.yanshi1 = setTimeout(() => {
  88. this.messageTips()
  89. clearTimeout(this.yanshi1)
  90. }, 3000);
  91. }
  92. gameStart = 'start'
  93. messageTips() {
  94. this.gameStart = 'start'
  95. const index = math.randomRangeInt(0, this.answerArr.length)
  96. this.selectNode.name = this.answerArr[index] + ''
  97. this.selectNode.getComponent(items).ShowUI(this.iconSpre[this.answerArr[index]])
  98. GameManager.getInstance().showTips('请将牌拖到记忆中的位置')
  99. this.answerArr.splice(index, 1)
  100. // this.selectNode.active = true;
  101. this.showSele()
  102. }
  103. startTouch(e) {
  104. if (this.gameStart != 'start') return
  105. // AudioManage.instance.playSound(Global.Audio_achieve);
  106. this.isNotMove = true
  107. let location = e.getUILocation();
  108. let checkPoint = this.parentGroupUi.convertToNodeSpaceAR(v3(location.x, location.y));
  109. if (this.isCollide(this.selectNode.position, checkPoint)) {
  110. this.isNotMove = false
  111. }
  112. }
  113. moveTouch(e) {
  114. if (this.isNotMove) return
  115. let location = e.getUILocation();
  116. let checkPoint = this.parentGroupUi.convertToNodeSpaceAR(v3(location.x, location.y));
  117. this.selectNode.setPosition(checkPoint.x, checkPoint.y)
  118. }
  119. endTouch() {
  120. if (this.gameStart != 'start') return
  121. // this.selectNode.active = false
  122. if (this.isNotMove) return
  123. this.isNotMove = true
  124. this.gameStart = 'end'
  125. for (let index = 0; index < this.parentArr.length; index++) {
  126. let item = this.parentArr[index]
  127. if (item.active && this.isCollide(this.parentArr[index].position, this.selectNode.position)) {
  128. if (item.name == this.selectNode.name) {
  129. item.getComponent(items).showItem()
  130. this.selectNode.active = false
  131. this.blackArr.push(index)
  132. this.wins()
  133. return
  134. }
  135. this.loss()
  136. return
  137. }
  138. }
  139. this.gameStart = 'start'
  140. this.showSele()
  141. }
  142. showSele() {
  143. this.selectNode.setPosition(0, 0)
  144. Tween.stopAllByTarget(this.selectNode)
  145. tween(this.selectNode)
  146. .to(0.2, { scale: v3(1.2, 1.2) })
  147. .to(0.1, { scale: v3(1, 1) })
  148. .start()
  149. }
  150. wins() {
  151. AudioManage.instance.playSound(Global.Audio_achieve);
  152. this.winNode.active = true;
  153. this.winNode.setScale(0.9, 0.9)
  154. tween(this.winNode)
  155. .to(0.3, { scale: v3(1.05, 1.05) })
  156. .to(0.2, { scale: v3(1, 1) })
  157. .call(() => {
  158. this.winNode.active = false;
  159. if (this.blackArr.length >= this.selfPosition.length) {
  160. GameManager.getInstance().addScore(20)
  161. if (GameManager.getInstance().curLevelScore >= 100) {
  162. } else {
  163. this.initUI()
  164. }
  165. } else {
  166. this.messageTips()
  167. }
  168. })
  169. .start()
  170. }
  171. loss() {
  172. GameManager.getInstance().showTips('请继续加油哦!')
  173. AudioManage.instance.playSound(Global.Audio_wrong);
  174. this.lossNode.active = true;
  175. this.lossNode.setScale(0.9, 0.9)
  176. this.answerArr.push(Number(this.selectNode.name))
  177. tween(this.lossNode)
  178. .to(0.3, { scale: v3(1.05, 1.05) })
  179. .to(0.2, { scale: v3(1, 1) })
  180. .call(() => {
  181. this.lossNode.active = false;
  182. // GameManager.getInstance().gameFail()
  183. // if (GameManager.getInstance().curLevelScore >= 20) {
  184. GameManager.getInstance().minusScore(20)
  185. AudioManage.instance.playSound(Global.Audio_wrong);
  186. // }
  187. // this.messageTips()
  188. this.initUI()
  189. })
  190. .start()
  191. }
  192. xCha: number = 107;
  193. yCha: number = 154;
  194. /** 碰撞了 */
  195. isCollide(node1, node2) {
  196. // console.log(node1,node1);
  197. const chax = Math.abs(node1.x - node2.x);
  198. const chay = Math.abs(node1.y - node2.y);
  199. if (chax < this.xCha && chay < this.yCha) {
  200. return true
  201. }
  202. return false
  203. }
  204. /**
  205. * 游戏结束后清理
  206. */
  207. gameOver() {
  208. }
  209. addGold() {
  210. }
  211. update(deltaTime: number) {
  212. }
  213. }