123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 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<Node>;
- selfLevel = 0;
- selfPosition = []
- @property(Node)
- selectNode: Node = null;
- @property([SpriteFrame])
- iconSpre: Array<SpriteFrame> = []
- 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) {
- }
- }
|