123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- import { _decorator, Button, Component, EventTouch, instantiate, Layout, log, Node, Prefab, sp, Sprite, SpriteAtlas, SpriteFrame, tween, UIOpacity, v3 } from 'cc';
- import { GameManager } from '../Manager/GameManager';
- import { Global } from '../Common/Global';
- import { AudioManage } from '../Manager/AudioManage';
- const { ccclass, property } = _decorator;
- @ccclass('GameMain')
- export class GameMain extends Component {
- @property(Node)
- Game: Node = null;
- @property(Node)
- Down: Node = null;
- @property(Node)
- Items: Node = null;
- @property(Prefab)
- Item: Prefab = null;
- @property(Node)
- GameWin: Node = null;
- @property(Node)
- GameFail: Node = null;
- @property(Node)
- Mesh: Node = null;
- @property(SpriteAtlas)
- ItemsAtlas: SpriteAtlas = null;
- _datas: string[] = [];
- _successC: number = 0;
- start() {
- }
- /**
- * 初始化逻辑
- * 每次GameManager.StartGame()时都会调用
- */
- async initUI() {
- this.Mesh.active = true;
- this.Game.active = false;
- this.Down.active = false;
- this.GameWin.active = false;
- this.GameFail.active = false;
- this.Game.removeAllChildren();
- this.Items.removeAllChildren();
- this._successC = 0;
- let c = 0;
- let max = 0;
- let lvName = "";
- let spacingX = 0
- switch (GameManager.getInstance().curLevel) {
- case 1:
- c = 3;
- max = 4;
- lvName = "Lv1";
- spacingX = 300;
- break;
- case 2:
- c = 4;
- max = 5;
- lvName = "Lv2";
- spacingX = 100;
- break;
- case 3:
- c = 5;
- max = 7;
- lvName = "Lv3";
- spacingX = 80;
- break;
- case 4:
- c = 6;
- max = 8;
- lvName = "Lv4";
- spacingX = 50;
- break;
- case 5:
- c = 7;
- max = 9;
- lvName = "Lv5";
- spacingX = 30;
- break;
- default:
- break;
- }
- this.Items.getComponent(Layout).spacingX = spacingX;
- let lvp = await Global.LoadPrefab("prefabs/Lv" + GameManager.getInstance().curLevel);
- let lv = instantiate(lvp);
- this.Game.addChild(lv);
- for (let i = 0; i < lv.children.length; i++) {
- lv.children[i].getChildByName("Y").getComponent(sp.Skeleton).clearTracks();
- lv.children[i].getComponent(Button).interactable = false;
- lv.children[i].getChildByName("Y").active = false;
- }
- this._datas = [];
- for (let i = 0; i < c; i++) {
- let str = "";
- do {
- let index = Global.getRandom(1, max);
- str = "Item" + GameManager.getInstance().curLevel + "_" + index;
- } while (this._datas.indexOf(str) != -1);
- this._datas.push(str);
- }
- for (let i = 0; i < this._datas.length; i++) {
- for (let j = 0; j < lv.children.length; j++) {
- if (lv.children[j].name == this._datas[i]) {
- // lv.children[j].getComponent(UIOpacity).opacity = 0;
- lv.children[j].getComponent(Button).interactable = true;
- lv.children[j].on(Button.EventType.CLICK, this.itemClicked, this);
- continue;
- }
- }
- }
- for (let i = 0; i < this._datas.length; i++) {
- let n = instantiate(this.Item);
- n.name = this._datas[i];
- n.getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame(this._datas[i]);
- this.Items.addChild(n);
- }
- this.Game.active = true;
- tween(this.Game).set({ scale: v3() }).to(0.5, { scale: v3(1, 1, 1) }).start();
- this.Down.active = true;
- tween(this.Down.getComponent(UIOpacity)).set({ opacity: 0 }).delay(0.5).to(0.5, { opacity: 255 }).call(() => {
- this.Mesh.active = false;
- }).start();
- }
- itemClicked(e: EventTouch) {
- AudioManage.instance.playSound(Global.Audio_AddScore);
- if (this._datas.indexOf(e.target.name) != -1) {
- tween(this.Items.children[this._datas.indexOf(e.target.name)].getComponent(UIOpacity)).to(0.25, { opacity: 0 }).start();
- GameManager.getInstance().showTips("你真是太厉害了");
- e.target.getChildByName("Y").active = true;
- e.target.getComponent(Button).interactable = false;
- e.target.getChildByName("Y").getComponent(sp.Skeleton).setAnimation(0, "yuan_65f", false);
- e.target.getChildByName("Y").getComponent(sp.Skeleton).setCompleteListener((trackEntry, loopCount) => {
- this._successC++;
- if (this._successC >= this._datas.length) {
- this.gameWin();
- }
- });
- // tween(e.target.getComponent(UIOpacity)).to(0.25, { opacity: 255 }).call(() => {
- // }).start();
- } else {
- this.gameFail();
- }
- }
- gameWin() {
- this.GameWin.active = true;
- // AudioManage.instance.playSound(Global.Audio_AddScore);
- tween(this.GameWin.getComponent(UIOpacity)).set({ opacity: 0 }).to(0.25, { opacity: 255 }).delay(0.5).to(0.25, { opacity: 0 }).call(() => {
- GameManager.getInstance().addScore(100)
- }).start();
- }
- gameFail() {
- AudioManage.instance.playSound(Global.Audio_MinusScore);
- this.GameFail.active = true;
- tween(this.GameFail.getComponent(UIOpacity)).set({ opacity: 0 }).to(0.25, { opacity: 255 }).delay(0.5).to(0.25, { opacity: 0 }).call(() => {
- this.initUI();
- }).start();
- }
- /**
- * 游戏结束后清理
- */
- gameOver() {
- }
- update(deltaTime: number) {
- }
- }
|