MmGame.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { _decorator, Button, CCInteger, Component, instantiate, log, math, Node, Prefab, tween, Vec3 } from 'cc';
  2. import { CosntValue } from '../../Common/Scripts/CosntValue';
  3. import { MmCardItem } from './MmCardItem';
  4. import { EventCustom } from '../../Common/Scripts/EventCustom';
  5. import { AudioManager } from '../../Common/Scripts/AudioManager';
  6. import { GameManager } from '../../scripts/Manager/GameManager';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('MmGame')
  9. export class MmGame extends Component {
  10. @property(Node)
  11. Cards: Node = null;
  12. @property(CCInteger)
  13. TimeXs: number = 1;
  14. @property(Prefab)
  15. CardItem: Prefab = null;
  16. @property(CCInteger)
  17. PointChange: number = 10;
  18. _lv: number;
  19. _point: number = 0;
  20. set Point(a: number) {
  21. this._point = a;
  22. let e = new EventCustom(CosntValue.EventType_RefreshScore, true, a);
  23. this.node.dispatchEvent(e);
  24. }
  25. _cards: number[];
  26. _curSelectCard: Node;
  27. start() {
  28. CosntValue.currGame = "Mm";
  29. this.rec();
  30. }
  31. protected onEnable(): void {
  32. if (CosntValue.StartLv >= 5) {
  33. this.Point = 100;
  34. }
  35. this._lv = CosntValue.StartLv;
  36. let rle = new EventCustom(CosntValue.EventType_RefreshLevel, true, this._lv);
  37. this.node.dispatchEvent(rle);
  38. this.init();
  39. }
  40. rec() {
  41. this.node.on("CardClick", (e) => {
  42. this.cardAni(e.data, true);
  43. if (this._curSelectCard) {
  44. if (e.data.getComponent(MmCardItem)._cardV != this._curSelectCard.getComponent(MmCardItem)._cardV) {
  45. AudioManager.instance.playSound(CosntValue.Audio_Mm_Sb);
  46. let node = this._curSelectCard;
  47. tween(this.node).delay(this.TimeXs).call(() => {
  48. this.cardAni(node, false);
  49. this.cardAni(e.data, false);
  50. }).start();
  51. this._curSelectCard = null;
  52. if (this._lv > 4) {
  53. this.Point = this._point - 10;
  54. if (this._point < 0) {
  55. this.scheduleOnce(() => {
  56. let e = new EventCustom(CosntValue.EventType_GameOver, true);
  57. this.node.dispatchEvent(e);
  58. }, 1);
  59. }
  60. }
  61. } else {
  62. AudioManager.instance.playSound(CosntValue.Audio_Mm_Cg);
  63. this.Point = this._point + 10;
  64. let allO = true;
  65. for (let i = 0; i < this.Cards.children.length; i++) {
  66. if (!this.Cards.children[i].getComponent(MmCardItem)._isO) {
  67. allO = false;
  68. break;
  69. }
  70. }
  71. this._curSelectCard = null;
  72. if (allO) {
  73. tween(this.node).delay(2).call(() => {
  74. log(this._lv)
  75. let e = new EventCustom(CosntValue.EventType_ShowTips, true, "恭喜,通过关卡");
  76. this.node.dispatchEvent(e);
  77. if (this._lv == CosntValue.MaxLv) {
  78. let e = new EventCustom(CosntValue.EventType_GameAllOver, true);
  79. this.node.dispatchEvent(e);
  80. } else {
  81. this._lv++;
  82. let e = new EventCustom(CosntValue.EventType_NextLv, true, this._lv);
  83. this.node.dispatchEvent(e);
  84. this.init();
  85. }
  86. }).start();
  87. }
  88. }
  89. } else {
  90. this._curSelectCard = e.data;
  91. }
  92. e.propagationStopped = false;
  93. })
  94. }
  95. cardAni(n: Node, iso: boolean) {
  96. n.getComponent(Button).interactable = !iso;
  97. n.getComponent(MmCardItem)._isO = iso;
  98. tween(n).to(0.1, { scale: new Vec3(0, 1, 1) }).call(() => {
  99. AudioManager.instance.playSound(CosntValue.Audio_Mm_Xz);
  100. n.getComponent(MmCardItem).CardB.active = !iso;
  101. n.getComponent(MmCardItem).CardO.active = iso;
  102. n.getComponent(MmCardItem).CardVSp.node.active = iso;
  103. }).to(0.1, { scale: new Vec3(1, 1, 1) }).start();
  104. }
  105. init() {
  106. let e = new EventCustom(CosntValue.EventType_TimerChange, true, 0);
  107. this.node.dispatchEvent(e);
  108. let tips = "关卡开始,请点击翻开卡牌,找到相同的卡牌。";
  109. if (this._lv >= 5) {
  110. tips += "本关卡难度增加,选择错误的卡组,将会分数将会减少,如果分数小于0,关卡失败,请谨慎选择。";
  111. }
  112. let tipe = new EventCustom(CosntValue.EventType_ShowTips, true, tips);
  113. this.node.dispatchEvent(tipe);
  114. this._cards = [];
  115. this._curSelectCard = null;
  116. let cardCount = Math.pow(2, this._lv);
  117. cardCount = cardCount > 16 ? 16 : cardCount;
  118. let _tcards = [];
  119. for (let i = 0; i < cardCount / 2; i++) {
  120. let n = CosntValue.randomInt(1, 5);
  121. _tcards.push(n);
  122. _tcards.push(n);
  123. }
  124. this._cards = CosntValue.randomSort(_tcards);
  125. let y = 0;
  126. this.Cards.removeAllChildren();
  127. for (let i = 0; i < this._cards.length; i++) {
  128. let node = instantiate(this.CardItem);
  129. this.Cards.addChild(node);
  130. node.getComponent(MmCardItem).CardV = _tcards[i];
  131. switch (_tcards.length) {
  132. case 2: {
  133. let x = 205 + (i + 1) * 320;
  134. node.setPosition(x, y);
  135. }
  136. break;
  137. case 4: {
  138. let x = 205 + i * 320;
  139. node.setPosition(x, y);
  140. }
  141. break;
  142. case 8: {
  143. let x = 205 + i % 4 * 320;
  144. switch (Math.floor(i / 4)) {
  145. case 0: {
  146. y = 140;
  147. }
  148. break;
  149. case 1: {
  150. y = -140;
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. node.setPosition(x, y);
  157. }
  158. break;
  159. case 12: {
  160. let x = 205 + i % 4 * 320;
  161. switch (Math.floor(i / 4)) {
  162. case 0: {
  163. y = 280;
  164. }
  165. break;
  166. case 1: {
  167. y = 0;
  168. }
  169. break;
  170. case 2: {
  171. y = -280;
  172. }
  173. default:
  174. break;
  175. }
  176. node.setPosition(x, y);
  177. }
  178. break;
  179. case 16: {
  180. let x = 205 + i % 4 * 320;
  181. switch (Math.floor(i / 4)) {
  182. case 0: {
  183. y = 420;
  184. }
  185. break;
  186. case 1: {
  187. y = 140;
  188. }
  189. break;
  190. case 2: {
  191. y = -140;
  192. }
  193. break;
  194. case 3: {
  195. y = -420;
  196. }
  197. default:
  198. break;
  199. }
  200. node.setPosition(x, y);
  201. }
  202. default:
  203. break;
  204. }
  205. }
  206. }
  207. update(deltaTime: number) {
  208. }
  209. }