import { _decorator, Component, Prefab, SpriteFrame, math, NodePool, instantiate, Node, Tween, tween, Vec3, v3, UITransform } from 'cc'; import { Global } from '../Common/Global'; import { AudioManage } from '../Manager/AudioManage'; import { GameManager } from '../Manager/GameManager'; import { items } from './items'; const { ccclass, property } = _decorator; @ccclass('GameMain') export class GameMain extends Component { itemPosition = [ [{ x: 0, y: 248 }, { x: -288, y: -194 }, { x: 288, y: -194 }], [{ x: -288, y: 220 }, { x: 288, y: 220 }, { x: -288, y: -220 }, { x: 288, y: -220 }], [{ x: -330, y: 152 }, { x: 0, y: 290 }, { x: 330, y: 152 }, { x: -246, y: -194 }, { x: 246, y: -194 }], ] @property(Node) parentGroup: Node = null; parentGroupUi: UITransform; parentArr: Array; selfLevel = 0; selfPosition = [] @property(Node) selectNode: Node = null; @property([SpriteFrame]) iconSpre: Array = [] selfMap = [] isNotMove = false answerArr = [] @property(Node) winNode: Node = null; @property(Node) lossNode: Node = null; onLoad() { this.parentArr = this.parentGroup.children this.parentGroupUi = this.parentGroup.getComponent(UITransform) this.parentGroup.on(Node.EventType.TOUCH_START, this.startTouch, this) this.parentGroup.on(Node.EventType.TOUCH_MOVE, this.moveTouch, this) this.parentGroup.on(Node.EventType.TOUCH_END, this.endTouch, this) } blackArr = [] /** * 初始化逻辑 * 每次GameManager.StartGame()时都会调用 */ initUI() { // AudioManage.instance.playSound(Global.Audio_achieve); GameManager.getInstance().showTips('') this.selfLevel = GameManager.getInstance().curLevel - 1 this.selfPosition = this.itemPosition[this.selfLevel] this.selfMap = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] // switch (GameManager.getInstance().curLevel) { // case 1: { // this.selfLevel = GameManager.getInstance().curLevel // this.selfPosition = this.itemPosition[this.selfLevel] // } break; // case 2: { // } break; // case 3: { // } break; // } this.blackArr = [] this.refreshMap() } yanshi: any yanshi1: any refreshMap() { this.answerArr = [] for (let index = 0; index < this.parentArr.length; index++) { if (index < this.selfPosition.length) { this.parentArr[index].active = true this.parentArr[index].setPosition(this.selfPosition[index].x, this.selfPosition[index].y); const id = math.randomRangeInt(0, this.selfMap.length) this.parentArr[index].getComponent(items).resetUI(this.iconSpre[this.selfMap[id]], index) console.log(this.selfMap[id]); this.parentArr[index].name = this.selfMap[id] + '' this.answerArr.push(this.selfMap[id]) this.selfMap.splice(id, 1) } else { this.parentArr[index].active = false } } clearTimeout(this.yanshi1) clearTimeout(this.yanshi) this.yanshi = setTimeout(() => { AudioManage.instance.playSound(Global.Audio_fanZhuan); clearTimeout(this.yanshi) }, 3000); this.selectNode.active = false; this.yanshi1 = setTimeout(() => { this.messageTips() clearTimeout(this.yanshi1) }, 3000); } gameStart = 'start' messageTips() { this.gameStart = 'start' const index = math.randomRangeInt(0, this.answerArr.length) this.selectNode.name = this.answerArr[index] + '' this.selectNode.getComponent(items).ShowUI(this.iconSpre[this.answerArr[index]]) GameManager.getInstance().showTips('请将牌拖到记忆中的位置') this.answerArr.splice(index, 1) // this.selectNode.active = true; this.showSele() } startTouch(e) { if (this.gameStart != 'start') return // AudioManage.instance.playSound(Global.Audio_achieve); this.isNotMove = true let location = e.getUILocation(); let checkPoint = this.parentGroupUi.convertToNodeSpaceAR(v3(location.x, location.y)); if (this.isCollide(this.selectNode.position, checkPoint)) { this.isNotMove = false } } moveTouch(e) { if (this.isNotMove) return let location = e.getUILocation(); let checkPoint = this.parentGroupUi.convertToNodeSpaceAR(v3(location.x, location.y)); this.selectNode.setPosition(checkPoint.x, checkPoint.y) } endTouch() { if (this.gameStart != 'start') return // this.selectNode.active = false if (this.isNotMove) return this.isNotMove = true this.gameStart = 'end' for (let index = 0; index < this.parentArr.length; index++) { let item = this.parentArr[index] if (item.active && this.isCollide(this.parentArr[index].position, this.selectNode.position)) { if (item.name == this.selectNode.name) { item.getComponent(items).showItem() this.selectNode.active = false this.blackArr.push(index) this.wins() return } this.loss() return } } this.gameStart = 'start' this.showSele() } showSele() { this.selectNode.setPosition(0, 0) Tween.stopAllByTarget(this.selectNode) tween(this.selectNode) .to(0.2, { scale: v3(1.2, 1.2) }) .to(0.1, { scale: v3(1, 1) }) .start() } wins() { AudioManage.instance.playSound(Global.Audio_achieve); this.winNode.active = true; this.winNode.setScale(0.9, 0.9) tween(this.winNode) .to(0.3, { scale: v3(1.05, 1.05) }) .to(0.2, { scale: v3(1, 1) }) .call(() => { this.winNode.active = false; if (this.blackArr.length >= this.selfPosition.length) { GameManager.getInstance().addScore(20) if (GameManager.getInstance().curLevelScore >= 100) { } else { this.initUI() } } else { this.messageTips() } }) .start() } loss() { GameManager.getInstance().showTips('请继续加油哦!') AudioManage.instance.playSound(Global.Audio_wrong); this.lossNode.active = true; this.lossNode.setScale(0.9, 0.9) this.answerArr.push(Number(this.selectNode.name)) tween(this.lossNode) .to(0.3, { scale: v3(1.05, 1.05) }) .to(0.2, { scale: v3(1, 1) }) .call(() => { this.lossNode.active = false; // GameManager.getInstance().gameFail() // if (GameManager.getInstance().curLevelScore >= 20) { GameManager.getInstance().minusScore(20) AudioManage.instance.playSound(Global.Audio_wrong); // } // this.messageTips() this.initUI() }) .start() } xCha: number = 107; yCha: number = 154; /** 碰撞了 */ isCollide(node1, node2) { // console.log(node1,node1); const chax = Math.abs(node1.x - node2.x); const chay = Math.abs(node1.y - node2.y); if (chax < this.xCha && chay < this.yCha) { return true } return false } /** * 游戏结束后清理 */ gameOver() { } addGold() { } update(deltaTime: number) { } }