import { _decorator, Button, CCInteger, Component, instantiate, log, math, Node, Prefab, tween, Vec3 } from 'cc'; import { CosntValue } from '../../Common/Scripts/CosntValue'; import { MmCardItem } from './MmCardItem'; import { EventCustom } from '../../Common/Scripts/EventCustom'; import { AudioManager } from '../../Common/Scripts/AudioManager'; import { GameManager } from '../../scripts/Manager/GameManager'; const { ccclass, property } = _decorator; @ccclass('MmGame') export class MmGame extends Component { @property(Node) Cards: Node = null; @property(CCInteger) TimeXs: number = 1; @property(Prefab) CardItem: Prefab = null; @property(CCInteger) PointChange: number = 10; _lv: number; _point: number = 0; set Point(a: number) { this._point = a; let e = new EventCustom(CosntValue.EventType_RefreshScore, true, a); this.node.dispatchEvent(e); } _cards: number[]; _curSelectCard: Node; start() { CosntValue.currGame = "Mm"; this.rec(); } protected onEnable(): void { if (CosntValue.StartLv >= 5) { this.Point = 100; } this._lv = CosntValue.StartLv; let rle = new EventCustom(CosntValue.EventType_RefreshLevel, true, this._lv); this.node.dispatchEvent(rle); this.init(); } rec() { this.node.on("CardClick", (e) => { this.cardAni(e.data, true); if (this._curSelectCard) { if (e.data.getComponent(MmCardItem)._cardV != this._curSelectCard.getComponent(MmCardItem)._cardV) { AudioManager.instance.playSound(CosntValue.Audio_Mm_Sb); let node = this._curSelectCard; tween(this.node).delay(this.TimeXs).call(() => { this.cardAni(node, false); this.cardAni(e.data, false); }).start(); this._curSelectCard = null; if (this._lv > 4) { this.Point = this._point - 10; if (this._point < 0) { this.scheduleOnce(() => { let e = new EventCustom(CosntValue.EventType_GameOver, true); this.node.dispatchEvent(e); }, 1); } } } else { AudioManager.instance.playSound(CosntValue.Audio_Mm_Cg); this.Point = this._point + 10; let allO = true; for (let i = 0; i < this.Cards.children.length; i++) { if (!this.Cards.children[i].getComponent(MmCardItem)._isO) { allO = false; break; } } this._curSelectCard = null; if (allO) { tween(this.node).delay(2).call(() => { log(this._lv) let e = new EventCustom(CosntValue.EventType_ShowTips, true, "恭喜,通过关卡"); this.node.dispatchEvent(e); if (this._lv == CosntValue.MaxLv) { let e = new EventCustom(CosntValue.EventType_GameAllOver, true); this.node.dispatchEvent(e); } else { this._lv++; let e = new EventCustom(CosntValue.EventType_NextLv, true, this._lv); this.node.dispatchEvent(e); this.init(); } }).start(); } } } else { this._curSelectCard = e.data; } e.propagationStopped = false; }) } cardAni(n: Node, iso: boolean) { n.getComponent(Button).interactable = !iso; n.getComponent(MmCardItem)._isO = iso; tween(n).to(0.1, { scale: new Vec3(0, 1, 1) }).call(() => { AudioManager.instance.playSound(CosntValue.Audio_Mm_Xz); n.getComponent(MmCardItem).CardB.active = !iso; n.getComponent(MmCardItem).CardO.active = iso; n.getComponent(MmCardItem).CardVSp.node.active = iso; }).to(0.1, { scale: new Vec3(1, 1, 1) }).start(); } init() { let e = new EventCustom(CosntValue.EventType_TimerChange, true, 0); this.node.dispatchEvent(e); let tips = "关卡开始,请点击翻开卡牌,找到相同的卡牌。"; if (this._lv >= 5) { tips += "本关卡难度增加,选择错误的卡组,将会分数将会减少,如果分数小于0,关卡失败,请谨慎选择。"; } let tipe = new EventCustom(CosntValue.EventType_ShowTips, true, tips); this.node.dispatchEvent(tipe); this._cards = []; this._curSelectCard = null; let cardCount = Math.pow(2, this._lv); cardCount = cardCount > 16 ? 16 : cardCount; let _tcards = []; for (let i = 0; i < cardCount / 2; i++) { let n = CosntValue.randomInt(1, 5); _tcards.push(n); _tcards.push(n); } this._cards = CosntValue.randomSort(_tcards); let y = 0; this.Cards.removeAllChildren(); for (let i = 0; i < this._cards.length; i++) { let node = instantiate(this.CardItem); this.Cards.addChild(node); node.getComponent(MmCardItem).CardV = _tcards[i]; switch (_tcards.length) { case 2: { let x = 205 + (i + 1) * 320; node.setPosition(x, y); } break; case 4: { let x = 205 + i * 320; node.setPosition(x, y); } break; case 8: { let x = 205 + i % 4 * 320; switch (Math.floor(i / 4)) { case 0: { y = 140; } break; case 1: { y = -140; } break; default: break; } node.setPosition(x, y); } break; case 12: { let x = 205 + i % 4 * 320; switch (Math.floor(i / 4)) { case 0: { y = 280; } break; case 1: { y = 0; } break; case 2: { y = -280; } default: break; } node.setPosition(x, y); } break; case 16: { let x = 205 + i % 4 * 320; switch (Math.floor(i / 4)) { case 0: { y = 420; } break; case 1: { y = 140; } break; case 2: { y = -140; } break; case 3: { y = -420; } default: break; } node.setPosition(x, y); } default: break; } } } update(deltaTime: number) { } }