GameMain.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { _decorator, Button, Component, EventTouch, instantiate, Layout, log, Node, Prefab, sp, Sprite, SpriteAtlas, SpriteFrame, tween, UIOpacity, v3 } from 'cc';
  2. import { GameManager } from '../Manager/GameManager';
  3. import { Global } from '../Common/Global';
  4. import { AudioManage } from '../Manager/AudioManage';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('GameMain')
  7. export class GameMain extends Component {
  8. @property(Node)
  9. Game: Node = null;
  10. @property(Node)
  11. Down: Node = null;
  12. @property(Node)
  13. Items: Node = null;
  14. @property(Prefab)
  15. Item: Prefab = null;
  16. @property(Node)
  17. GameWin: Node = null;
  18. @property(Node)
  19. GameFail: Node = null;
  20. @property(Node)
  21. Mesh: Node = null;
  22. @property(SpriteAtlas)
  23. ItemsAtlas: SpriteAtlas = null;
  24. _datas: string[] = [];
  25. _successC: number = 0;
  26. start() {
  27. }
  28. /**
  29. * 初始化逻辑
  30. * 每次GameManager.StartGame()时都会调用
  31. */
  32. async initUI() {
  33. this.Mesh.active = true;
  34. this.Game.active = false;
  35. this.Down.active = false;
  36. this.GameWin.active = false;
  37. this.GameFail.active = false;
  38. this.Game.removeAllChildren();
  39. this.Items.removeAllChildren();
  40. this._successC = 0;
  41. let c = 0;
  42. let max = 0;
  43. let lvName = "";
  44. let spacingX = 0
  45. switch (GameManager.getInstance().curLevel) {
  46. case 1:
  47. c = 3;
  48. max = 4;
  49. lvName = "Lv1";
  50. spacingX = 300;
  51. break;
  52. case 2:
  53. c = 4;
  54. max = 5;
  55. lvName = "Lv2";
  56. spacingX = 100;
  57. break;
  58. case 3:
  59. c = 5;
  60. max = 7;
  61. lvName = "Lv3";
  62. spacingX = 80;
  63. break;
  64. case 4:
  65. c = 6;
  66. max = 8;
  67. lvName = "Lv4";
  68. spacingX = 50;
  69. break;
  70. case 5:
  71. c = 7;
  72. max = 9;
  73. lvName = "Lv5";
  74. spacingX = 30;
  75. break;
  76. default:
  77. break;
  78. }
  79. this.Items.getComponent(Layout).spacingX = spacingX;
  80. let lvp = await Global.LoadPrefab("prefabs/Lv" + GameManager.getInstance().curLevel);
  81. let lv = instantiate(lvp);
  82. this.Game.addChild(lv);
  83. for (let i = 0; i < lv.children.length; i++) {
  84. lv.children[i].getChildByName("Y").getComponent(sp.Skeleton).clearTracks();
  85. lv.children[i].getComponent(Button).interactable = false;
  86. lv.children[i].getChildByName("Y").active = false;
  87. }
  88. this._datas = [];
  89. for (let i = 0; i < c; i++) {
  90. let str = "";
  91. do {
  92. let index = Global.getRandom(1, max);
  93. str = "Item" + GameManager.getInstance().curLevel + "_" + index;
  94. } while (this._datas.indexOf(str) != -1);
  95. this._datas.push(str);
  96. }
  97. for (let i = 0; i < this._datas.length; i++) {
  98. for (let j = 0; j < lv.children.length; j++) {
  99. if (lv.children[j].name == this._datas[i]) {
  100. // lv.children[j].getComponent(UIOpacity).opacity = 0;
  101. lv.children[j].getComponent(Button).interactable = true;
  102. lv.children[j].on(Button.EventType.CLICK, this.itemClicked, this);
  103. continue;
  104. }
  105. }
  106. }
  107. for (let i = 0; i < this._datas.length; i++) {
  108. let n = instantiate(this.Item);
  109. n.name = this._datas[i];
  110. n.getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame(this._datas[i]);
  111. this.Items.addChild(n);
  112. }
  113. this.Game.active = true;
  114. tween(this.Game).set({ scale: v3() }).to(0.5, { scale: v3(1, 1, 1) }).start();
  115. this.Down.active = true;
  116. tween(this.Down.getComponent(UIOpacity)).set({ opacity: 0 }).delay(0.5).to(0.5, { opacity: 255 }).call(() => {
  117. this.Mesh.active = false;
  118. }).start();
  119. }
  120. itemClicked(e: EventTouch) {
  121. AudioManage.instance.playSound(Global.Audio_AddScore);
  122. if (this._datas.indexOf(e.target.name) != -1) {
  123. tween(this.Items.children[this._datas.indexOf(e.target.name)].getComponent(UIOpacity)).to(0.25, { opacity: 0 }).start();
  124. GameManager.getInstance().showTips("你真是太厉害了");
  125. e.target.getChildByName("Y").active = true;
  126. e.target.getComponent(Button).interactable = false;
  127. e.target.getChildByName("Y").getComponent(sp.Skeleton).setAnimation(0, "yuan_65f", false);
  128. e.target.getChildByName("Y").getComponent(sp.Skeleton).setCompleteListener((trackEntry, loopCount) => {
  129. this._successC++;
  130. if (this._successC >= this._datas.length) {
  131. this.gameWin();
  132. }
  133. });
  134. // tween(e.target.getComponent(UIOpacity)).to(0.25, { opacity: 255 }).call(() => {
  135. // }).start();
  136. } else {
  137. this.gameFail();
  138. }
  139. }
  140. gameWin() {
  141. this.GameWin.active = true;
  142. // AudioManage.instance.playSound(Global.Audio_AddScore);
  143. tween(this.GameWin.getComponent(UIOpacity)).set({ opacity: 0 }).to(0.25, { opacity: 255 }).delay(0.5).to(0.25, { opacity: 0 }).call(() => {
  144. GameManager.getInstance().addScore(100)
  145. }).start();
  146. }
  147. gameFail() {
  148. AudioManage.instance.playSound(Global.Audio_MinusScore);
  149. this.GameFail.active = true;
  150. tween(this.GameFail.getComponent(UIOpacity)).set({ opacity: 0 }).to(0.25, { opacity: 255 }).delay(0.5).to(0.25, { opacity: 0 }).call(() => {
  151. this.initUI();
  152. }).start();
  153. }
  154. /**
  155. * 游戏结束后清理
  156. */
  157. gameOver() {
  158. }
  159. update(deltaTime: number) {
  160. }
  161. }