123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { _decorator, Component, Prefab, SpriteFrame, math, NodePool, instantiate, Node, Tween, tween, Vec3, v3 } 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 {
- @property(Prefab)
- itemPre: Prefab = null;
- @property(Node)
- itemParent: Node = null;
- itemNodePool: NodePool = new NodePool()
- @property(Node)
- winNode: Node = null;
- @property(Node)
- lossNode: Node = null;
-
- start() {
- GameManager.getInstance().eventTarget.on('itemData', this.check, this)
- }
- check(data) {
- console.log(data);
- if (this.gameState != 'start') return
- this.gameState = 'end'
- if(data.data == 1){
- this.wins()
- }else{
- this.loss()
- }
- }
- @property(SpriteFrame)
- iconArr: Array<SpriteFrame> = []
- lengths: number = 0
- width: number = 0
- gameState = 'ready'
- timerOut:any;
- /**
- * 初始化逻辑
- * 每次GameManager.StartGame()时都会调用
- */
- initUI() {
- this.clearParent()
- if(GameManager.getInstance().curLevel ==2){
- this.itemParent.setPosition(0,-200)
- }else{
- this.itemParent.setPosition(0,-80)
- }
- switch (GameManager.getInstance().curLevel) {
- case 1: {
- this.lengths = 4
- this.width = 2
- } break;
- case 2: {
- this.lengths = 6
- this.width = 1.3
- } break;
- case 3: {
- this.lengths = 9
- this.width = 1.3
- } break;
- case 4: {
- this.lengths = 12
- this.width = 1
- } break;
- case 5: {
- this.lengths = 16
- this.width = 1
- } break;
- }
- const iconIndex = math.randomRangeInt(0, 10)
- const answer = iconIndex > 4 ? iconIndex - 5 : iconIndex + 5
- const answerIndex = math.randomRangeInt(0, this.lengths)
- for (let index = 0; index < this.lengths; index++) {
- if (this.itemNodePool.size() < 1) {
- this.itemNodePool.put(instantiate(this.itemPre))
- }
- const item = this.itemNodePool.get()
- item.setScale(this.width, this.width)
- if (answerIndex == index) {
- item.getComponent(items).refreshUi(1, this.iconArr[answer], index)
- } else {
- item.getComponent(items).refreshUi(0, this.iconArr[iconIndex], index)
- }
- item.parent = this.itemParent
- }
- clearTimeout(this.timerOut)
- this.timerOut = setTimeout(() => {
- clearTimeout(this.timerOut)
- GameManager.getInstance().showTips('请找出不通的图案!')
- this.gameState = 'start'
- }, this.lengths*300);
-
- }
- /** 清理数据 */
- clearParent() {
- const list = this.itemParent.children
- if (list.length > 0) {
- for (let index = list.length; index >= 0; index--) {
- this.itemNodePool.put(list[index])
- }
- }
- }
- wins() {
- AudioManage.instance.playSound(Global.Audio_right);
- 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;
- GameManager.getInstance().addScore(20)
- if (GameManager.getInstance().curLevelScore < 100) {
- this.initUI()
- }
- })
- .start()
- }
- loss() {
- AudioManage.instance.playSound(Global.Audio_wrong);
- GameManager.getInstance().showTips('请继续加油哦!')
- this.lossNode.active = true;
- this.lossNode.setScale(0.9, 0.9)
- 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().minusScore(20)
- this.initUI()
-
- })
- .start()
- }
- /**
- * 游戏结束后清理
- */
- gameOver() {
- }
- addGold() {
- }
- update(deltaTime: number) {
- }
- }
|