import { _decorator, CCInteger, Collider2D, Component, instantiate, Label, log, Node, Prefab, Tween, tween, UITransform, v3, Vec3, Animation, sp } from 'cc'; import { EventCustom } from '../../scripts/Common/EventCustom'; import { CosntValue } from '../../Common/Scripts/CosntValue'; import { HsGoodsItem } from './HsGoodsItem'; import { HsPlayer } from './HsPlayer'; import { AudioManage } from '../../scripts/Manager/AudioManage'; import { GameManager } from '../../scripts/Manager/GameManager'; import { Global } from '../../scripts/Common/Global'; const { ccclass, property } = _decorator; @ccclass('HsGame') export class HsGame extends Component { @property(Node) Bg: Node = null; @property(Node) Goods: Node = null; @property(Prefab) GoodsItem: Prefab = null; @property(Node) Player: Node = null; @property(Node) TouchNode: Node = null; @property(CCInteger) MoveTime: number = 10; @property(Prefab) scoreLabelPerfab: Prefab = null; // _lv: number; // set Lv(a: number) { // this._lv = a; // let rle = new EventCustom(CosntValue.EventType_RefreshLevel, true, this._lv); // this.node.dispatchEvent(rle); // let e = new EventCustom(CosntValue.EventType_ShowTips, true, "恭喜,通过关卡"); // this.node.dispatchEvent(e); // } // _sumPoint: number = 0; // _point: number; // set Point(a: number) { // this._point = a; // let e = new EventCustom(CosntValue.EventType_RefreshScore, true, a); // this.node.dispatchEvent(e); // if (a >= this._gotoNextPoint) { // Tween.stopAllByTarget(this.Bg); // for (let i = 0; i < this.Goods.children.length; i++) { // Tween.stopAllByTarget(this.Goods.children[i]) // } // AudioManager.instance.playSound(CosntValue.Audio_Hs_Win); // if (this._lv == CosntValue.MaxLv) { // let e = new EventCustom(CosntValue.EventType_GameAllOver, true); // this.node.dispatchEvent(e); // } else { // this.Bg.setPosition(v3()); // this._lv++; // let e = new EventCustom(CosntValue.EventType_NextLv, true, this._lv); // this.node.dispatchEvent(e); // this.scheduleOnce(this.init, 3); // } // } // } _hp: number = 3; // _gotoNextPoint: number = 100; start() { CosntValue.currGame = "Hs"; this.TouchNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.rec(); } rec() { this.node.on("ContactSomething", (e) => { if (e.data.getComponent(HsGoodsItem)._goodType == 1) { let spine = this.Player.getChildByName("A").getComponent(sp.Skeleton); spine.timeScale = 3.5; let track = spine.setAnimation(0, "animation", false); if (track) { // 注册动画的结束回调 spine.setCompleteListener((trackEntry) => { let name = trackEntry.animation ? trackEntry.animation.name : ''; if (name === "animation") { spine.timeScale = 1; e.data.active = false; } }); } spine.addAnimation(0, "yzou", true); AudioManage.instance.playSound(Global.Audio_Hs_Cg); // this._sumPoint += e.data.getComponent(HsGoodsItem)._point; // this.Point = this._point + e.data.getComponent(HsGoodsItem)._point; let score = e.data.getComponent(HsGoodsItem)._point; GameManager.getInstance().addScore(score); if (GameManager.getInstance().curLevelScore >= GameManager.getInstance().curLevelData.targetScore) { this.stopTween(); } const scoreLabel = instantiate(this.scoreLabelPerfab); scoreLabel.setParent(this.Goods); const pos = e.data.getPosition(); scoreLabel.setPosition(pos.x + 100, pos.y); const label = scoreLabel.getComponentInChildren(Label); const lableAni = scoreLabel.getComponent(Animation) label.string = '+' + score; lableAni.play('addScore'); } else if (e.data.getComponent(HsGoodsItem)._goodType == 2) { AudioManage.instance.playSound(Global.Audio_Hs_Sb); this._hp--; let spine = this.Player.getChildByName("A").getComponent(sp.Skeleton); spine.timeScale = 1.5; let track = spine.setAnimation(0, "shuaidao", false); if (track) { // 注册动画的结束回调 spine.setCompleteListener((trackEntry) => { let name = trackEntry.animation ? trackEntry.animation.name : ''; if (name === "shuaidao") { spine.timeScale = 1; e.data.active = false; } }); } spine.addAnimation(0, "yzou", true); let tipe = new EventCustom(Global.EventType_ShowTips, true, "触碰到障碍物,生命值减1,剩余生命值为" + this._hp); this.node.dispatchEvent(tipe); e.data.active = false; if (this._hp <= 0) { let e = new EventCustom(Global.EventType_ShowTips, true, "很遗憾,生命值为0游戏失败"); this.node.dispatchEvent(e); GameManager.getInstance().gameFail(); // this.scheduleOnce(() => { // let e = new EventCustom(CosntValue.EventType_GameOver, true); // this.node.dispatchEvent(e); // }, 2); } } }); } protected onEnable(): void { this.TouchNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); //this.Lv = CosntValue.StartLv; // this.init(); } protected onDisable(): void { this.TouchNode.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); } onTouchMove(e) { if (this.Player.getChildByPath("A").getComponent(sp.Skeleton).getCurrent(0).animation.name == "yzou") { let posDelta = e.getDelta(); let dir = posDelta.y; if (dir > 1 || dir < -1) { this.Player.getComponent(HsPlayer).move(dir); } } } init() { // this._gotoNextPoint = 100; this._hp = 3; this.Player.getChildByName("A").getComponent(sp.Skeleton).addAnimation(0, "yzou", true); let moveTime = this.MoveTime - GameManager.getInstance().curLevel; //背景移动 this.Bg.setPosition(v3()); let t = tween(this.Bg).set({ position: v3() }).by(moveTime, { position: new Vec3(-(this.Bg.getComponent(UITransform).width / 2), 0, 0) }); tween(this.Bg).repeatForever(t).start(); // let t = tween(this.Bg).set({ position: v3(1819, 0, 0) }).to(moveTime, { position: new Vec3(-1819, 0, 0) }); // tween(this.Bg).repeatForever(t).start(); let tips = "关卡开始,请上下滑动屏幕,捡起贝壳"; // if (GameManager.getInstance().curLevel > 4) { // tips += "商场人流量增大,注意躲避其他客人" // } let tipe = new EventCustom(Global.EventType_ShowTips, true, tips); this.node.dispatchEvent(tipe); this.Goods.removeAllChildren(); this.createGoods(); this.scheduleOnce(() => { this.createGoods(); }, moveTime * 0.5) } createGoods() { log(123); let g = instantiate(this.GoodsItem); this.Goods.addChild(g); // let rx = CosntValue.randomInt(100, 300) let x = this.node.getComponent(UITransform).width / 2; let y = CosntValue.randomInt(0, 1) == 0 ? 120 : -30; g.setPosition(x, y); let tx = -(this.node.getComponent(UITransform).width / 2); let max = GameManager.getInstance().curLevel > 5 ? 5 : GameManager.getInstance().curLevel; let type = CosntValue.randomInt(1, max); g.getComponent(HsGoodsItem).GoodType = type; let moveTime = this.MoveTime - GameManager.getInstance().curLevel; tween(g).to(moveTime, { position: new Vec3(tx, y, 0) }).call(() => { g.destroy(); this.createGoods(); }).start(); } stopTween() { Tween.stopAllByTarget(this.Bg); for (let i = 0; i < this.Goods.children.length; i++) { Tween.stopAllByTarget(this.Goods.children[i]) } this.Goods.removeAllChildren(); } update(deltaTime: number) { } }