// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import { SaveDataManager } from "../../component/SaveDataManager"; import { GlobalEvent, tMgr } from "../../Global"; import { getGlobalNode } from "../../kernel/battle/BattleConst"; import { GameSceneObjTableMgr } from "../../kernel/table/GameSceneObjTableMgr"; import { GamePlayTable, GameSceneObjTable, TableName } from "../../kernel/table/TableDefine"; import { MainSceneData } from "../../scenes/MainSceneData"; import GameSceneUnlockTip from "./GameSceneUnlockTip"; import GameSceneUnlockTool from "./GameSceneUnlockTool"; const { ccclass, property } = cc._decorator; @ccclass export default class GameSceneControl extends cc.Component { private _curSceneId: number = 0; // private _container: cc.Node; private _unlockToolPrefab: cc.Node; private _unlockTip: cc.Node; private _belowLayer: cc.Node; private _charLayer: cc.Node; private _upLayer: cc.Node; private _unlockLayer: cc.Node; private _toolList: cc.Node[] = []; private _objSpine: cc.Node; private _charNode: cc.Node; private _parGreenNode: cc.Node; private _bgLightNode: cc.Node; private _unlockSpine: cc.Node; private _isShow: boolean = false; private _page: number = -1; /**背景节点,特殊处理 */ private _backNode: cc.Node; //#region LIFE-CYCLE CALLBACKS: init(page: number, charNode: cc.Node, unlockTip: cc.Node, par: cc.Node, bgNode: cc.Node, unlockSpine: cc.Node): void { this._page = page; this._charNode = charNode; this._unlockTip = unlockTip; this._parGreenNode = par; this._bgLightNode = bgNode; this._unlockSpine = unlockSpine; } onLoad() { this._belowLayer = this.node.getChildByName('belowLayer'); this._charLayer = this.node.getChildByName('charLayer'); this._upLayer = this.node.getChildByName('upLayer'); this._unlockLayer = this.node.getChildByName('unlockLayer'); let sceneRoot = cc.find('Canvas/gameSceneRoot', cc.director.getScene()); this._unlockToolPrefab = sceneRoot.getChildByName('unlockTool'); this._unlockToolPrefab.active = false; this._objSpine = sceneRoot.getChildByName('sceneSpine'); this._backNode = new cc.Node(); this._backNode.addComponent(cc.Sprite); getGlobalNode().on(GlobalEvent.OBJ_UNLOCK, this.onObjUnlock, this); } protected onDestroy(): void { cc.Tween.stopAllByTag(0); getGlobalNode().off(GlobalEvent.OBJ_UNLOCK, this.onObjUnlock, this); } update(dt) { // this._backNode.setPosition(this.node.parent.position.x,0); // console.log(this.node.parent.position.x); } //#endregion //#region event onToolClick(e: cc.Event.EventTouch) { this.showToolTip(e.target.parent); } onObjUnlock(objId: number) { let objTable = tMgr.getConfig(TableName.GAME_SCENE_OBJ, objId) as GameSceneObjTable; if (objTable.SceneId != this._curSceneId) { return; } /**删除工具 */ for (let i = 0; i < this._toolList.length; ++i) { if (this._toolList[i].getComponent(GameSceneUnlockTool).config.ID == objId) { this._toolList[i].destroy(); this._toolList.splice(i, 1); break; } } let isGarbage = (!GameSceneObjTableMgr.ins.getParentObj(objTable)); if (this._unlockSpine) { if (this._unlockSpine.parent) { this._unlockSpine.removeFromParent(); } MainSceneData.isUnlockAni = true; this._unlockSpine.active = true; this._unlockSpine.setPosition(objTable.PosX, objTable.PosY); this._unlockLayer.addChild(this._unlockSpine); this._unlockSpine.getComponent(sp.Skeleton).setCompleteListener(() => { MainSceneData.isUnlockAni = false; this._unlockSpine.active = false; if (!isGarbage) { //普通物品 this.addObj(objTable, true); } else { //垃圾 this._upLayer.removeAllChildren(); this._belowLayer.removeAllChildren(); } if (objTable.Unlock && objTable.Unlock.length > 0) { for (let i = 0; i < objTable.Unlock.length; ++i) { let unlock = tMgr.getConfig(TableName.GAME_SCENE_OBJ, objTable.Unlock[i]) as GameSceneObjTable; this.addTool(unlock); } } getGlobalNode().emit(GlobalEvent.OBJ_UNLOCK_ANI_FINISH, objId); }); this._unlockSpine.getComponent(sp.Skeleton).setAnimation(0, isGarbage ? 'saoba' : 'chuizi', false); } // this._unlockSpine.getComponent(sp.Skeleton). // let sp = this._unlockSpine.getComponent(sp.Skeleton) // fireSpine.getComponent(sp.Skeleton) } /**滚动到当前页面了 */ onSlideShow() { if (this._isShow == false) { this._isShow = true; if (this._charNode && this._charNode.parent) { this._charNode.removeFromParent(); } if (this._charNode) { this._charLayer.addChild(this._charNode); this._charNode.active = true; } if (this._unlockTip && this._unlockTip.parent) { this._unlockTip.removeFromParent(); } if (this._unlockTip) { this._unlockLayer.addChild(this._unlockTip); this._unlockTip.active = false; } let interval = (tMgr.getConfig(TableName.GAME_PLAY, 13) as GamePlayTable).Value; this.schedule(this.onRandomCharAnimation, interval); // this.onRandomCharAnimation(); this._curCharAniId = 0; this._charNode.setPosition(0, -422); this._charNode.getComponent(sp.Skeleton).setAnimation(0, 'stay', true); //播默认动画 this.onRandomCharAnimation();//立刻随机一次 //绿色粒子效果 if (this._parGreenNode && this._parGreenNode.parent) { this._parGreenNode.removeFromParent(); } if (this._parGreenNode) { this.node.getChildByName('parLayer').addChild(this._parGreenNode); this._parGreenNode.active = true; } // let showList: string[]; // if (this._curSceneId == 1) { // showList = ['light4', 'light4a']; // } // else if (this._curSceneId == 2) { // showList = []; // } // else if (this._curSceneId == 3) { // showList = ['light4b', 'light4c']; // } // for (let i = 0; i < this._bgLightNode.childrenCount; ++i) { // let node = this._bgLightNode.children[i]; // node.active = (showList.includes(node.name)); // } } } onSlideLeave() { if (this._isShow) { this._isShow = false; // console.log('page', this._page, 'leave'); this.unschedule(this.onRandomCharAnimation); } } //#endregion //#region public method loadScene(id: number): void { this._curSceneId = id; cc.resources.load('gameScene/BG_0' + id.toString(), cc.SpriteFrame, (error, assets) => { if (!error && this.isValid) { this.node.getChildByName('img').active = false; let backContainer = this.node.parent.parent.getChildByName('backImgContainer'); // let backNode = new cc.Node(); // let sprite = backNode.addComponent(cc.Sprite); this._backNode.getComponent(cc.Sprite).spriteFrame = assets as cc.SpriteFrame; this._backNode.setContentSize(790,1800); backContainer.addChild(this._backNode); this._backNode.setSiblingIndex(this._curSceneId-1); // this._backNode.setSiblingIndex() // let totalWidth = SaveDataManager.ins.runtimeData.curGameScene * 790; // this.node.getChildByName('img').getComponent(cc.Sprite).spriteFrame = assets as cc.SpriteFrame; this.renderScene(); } }); } //#endregion //#region private method private renderScene(): void { this._belowLayer.removeAllChildren(); this._upLayer.removeAllChildren(); let objList = GameSceneObjTableMgr.ins.getByScene(this._curSceneId); for (let i = 0; objList && i < objList.length; ++i) { let obj = objList[i]; let parent = GameSceneObjTableMgr.ins.getParentObj(obj); if (!parent) { //没有父对象,是垃圾 if (SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) { //已解锁,不显示 } else { //未解锁,显示 this.addObj(obj); this.addTool(obj); } } else { //普通对象 if (SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) { //已解锁,显示 this.addObj(obj); } else { //未解锁 if (SaveDataManager.ins.runtimeData.unlockObj.includes(parent.ID)) { //父对象已解锁,显示解锁工具图标 this.addTool(obj); } else { //父对象未解锁,啥也不显示 } } } } } private addTool(obj: GameSceneObjTable) { let tool = cc.instantiate(this._unlockToolPrefab); tool.active = true; tool.setPosition(obj.PosX, obj.PosY); this._unlockLayer.addChild(tool); tool.getComponent(GameSceneUnlockTool).setObjId(obj.ID); this._toolList.push(tool); } private addObj(obj: GameSceneObjTable, ani: boolean = false) { if (obj.Spine != "") { if (obj.ID == 6) { //烤肉特殊处理,将本来的营火改为烤肉动画 let fireObj = tMgr.getConfig(TableName.GAME_SCENE_OBJ, 3) as GameSceneObjTable; let fireLayer = fireObj.IsBelowChar ? this._belowLayer : this._upLayer; let fireSpine = fireLayer.getChildByName('gameSceneObj_' + fireObj.ID.toString()); fireSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true); } else if (obj.ID == 21) { //猫头鹰特殊处理,将本来的收音机改为猫头鹰动画 let radio = tMgr.getConfig(TableName.GAME_SCENE_OBJ, 20) as GameSceneObjTable; let radioLayer = radio.IsBelowChar ? this._belowLayer : this._upLayer; let radioSpine = radioLayer.getChildByName('gameSceneObj_' + radio.ID.toString()); radioSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true); } else if (obj.ID >= 34 && obj.ID <= 36) { //船配件的特殊处理 let boat = tMgr.getConfig(TableName.GAME_SCENE_OBJ, 32) as GameSceneObjTable; let boatLayer = boat.IsBelowChar ? this._belowLayer : this._upLayer; let boatSpine = boatLayer.getChildByName('gameSceneObj_' + boat.ID.toString()); boatSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true); } else if (obj.ID == 23) { //烤鱼特殊处理,将本来的烤炉加条鱼 let cook = tMgr.getConfig(TableName.GAME_SCENE_OBJ, 17) as GameSceneObjTable; let cookLayer = cook.IsBelowChar ? this._belowLayer : this._upLayer; let cookSpine = cookLayer.getChildByName('gameSceneObj_' + cook.ID.toString()); cookSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true); } else { let spNode = cc.instantiate(this._objSpine); spNode.name = 'gameSceneObj_' + obj.ID.toString(); spNode.active = true; let spine = spNode.getComponent(sp.Skeleton); spNode.setPosition(obj.PosX, obj.PosY); let layer = obj.IsBelowChar ? this._belowLayer : this._upLayer; layer.addChild(spNode, obj.ID); spine.setAnimation(0, obj.Spine, true); //需要先将节点加到舞台,再设置动画才生效 } } else { cc.resources.load('gameSceneSheet/' + obj.Img, cc.SpriteFrame, (error, asset) => { if (!error) { let node = new cc.Node('gameSceneObj_' + obj.ID.toString()); let sp = node.addComponent(cc.Sprite); sp.spriteFrame = asset as cc.SpriteFrame; node.setPosition(obj.PosX, obj.PosY); let layer = obj.IsBelowChar ? this._belowLayer : this._upLayer; layer.addChild(node, obj.ID); // node.setSiblingIndex(obj.ID); if (ani) { node.scale = 0; cc.tween(node).tag(0).to(0.1, { scale: 1 }, { easing: 'backOut' }).start(); } } }); } } private _curHideTool: cc.Node; private showToolTip(tool: cc.Node) { if (this._curHideTool) { this.onScreenTouch(); } this._curHideTool = tool; this._unlockTip.setPosition(tool.position); let root = cc.find('Canvas/gameSceneRoot', cc.director.getScene()); if (this._unlockTip.x + this._unlockTip.width / 2 > root.width / 2) { this._unlockTip.x = root.width / 2 - this._unlockTip.width / 2; } else if (this._unlockTip.x - this._unlockTip.width / 2 < -root.width / 2) { this._unlockTip.x = -root.width / 2 + this._unlockTip.width / 2; } tool.active = false; this._unlockTip.active = true; this._unlockTip.getComponent(GameSceneUnlockTip).setObj(tool.getComponent(GameSceneUnlockTool).config); this.node.once(cc.Node.EventType.TOUCH_START, this.onScreenTouch, this); } private onScreenTouch() { this._unlockTip.active = false; this._curHideTool.active = true; this._curHideTool = null; } private _curCharAniId: number = 0; private onRandomCharAnimation() { let objList = GameSceneObjTableMgr.ins.getByScene(this._curSceneId); let idList: number[] = []; let weightList: number[] = []; idList.push(0); weightList.push(10); for (let i = 0; i < objList.length; ++i) { let obj = objList[i]; if (obj.CharSpine != "" && SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) { idList.push(obj.ID); weightList.push(10); } } //删除现有的 let curIndex = idList.indexOf(this._curCharAniId); idList.splice(curIndex, 1); weightList.splice(curIndex, 1); if (idList.length > 0) { let id = idList[slib.MathUtil.randomTestByWeightList(weightList)]; if (id == 0) { this._charNode.setPosition(0, -422); this._charNode.getComponent(sp.Skeleton).setAnimation(0, 'stay', true); //播默认动画 } else { let config = tMgr.getConfig(TableName.GAME_SCENE_OBJ, id) as GameSceneObjTable; let charSpineName: string = config.CharSpine; this._charNode.setPosition(config.CharSpineX, config.CharSpineY); this._charNode.getComponent(sp.Skeleton).setAnimation(0, charSpineName, true); } //#region 弹吉他,隐藏吉他 let guitar = tMgr.getConfig(TableName.GAME_SCENE_OBJ, 38) as GameSceneObjTable; let guitarLayer = guitar.IsBelowChar ? this._belowLayer : this._upLayer; let guitarNode = guitarLayer.getChildByName('gameSceneObj_' + guitar.ID.toString()); if (guitarNode) { guitarNode.active = !(id == 38); } //#endregion this._curCharAniId = id; } else { //没得换动画,什么都不做 } } //#endregion }