87b9b32f-7a64-410e-8c50-912113dbe3fb.js 18.6 KB
"use strict";
cc._RF.push(module, '87b9bMvemRBDoxQkSET2+P7', 'GameSceneControl');
// scripts/ui/gameScene/GameSceneControl.ts

"use strict";
// 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
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var SaveDataManager_1 = require("../../component/SaveDataManager");
var Global_1 = require("../../Global");
var BattleConst_1 = require("../../kernel/battle/BattleConst");
var GameSceneObjTableMgr_1 = require("../../kernel/table/GameSceneObjTableMgr");
var TableDefine_1 = require("../../kernel/table/TableDefine");
var MainSceneData_1 = require("../../scenes/MainSceneData");
var GameSceneUnlockTip_1 = require("./GameSceneUnlockTip");
var GameSceneUnlockTool_1 = require("./GameSceneUnlockTool");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameSceneControl = /** @class */ (function (_super) {
    __extends(GameSceneControl, _super);
    function GameSceneControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._curSceneId = 0;
        _this._toolList = [];
        _this._isShow = false;
        _this._page = -1;
        _this._curCharAniId = 0;
        return _this;
        //#endregion
    }
    //#region  LIFE-CYCLE CALLBACKS:
    GameSceneControl.prototype.init = function (page, charNode, unlockTip, par, bgNode, unlockSpine) {
        this._page = page;
        this._charNode = charNode;
        this._unlockTip = unlockTip;
        this._parGreenNode = par;
        this._bgLightNode = bgNode;
        this._unlockSpine = unlockSpine;
    };
    GameSceneControl.prototype.onLoad = function () {
        this._belowLayer = this.node.getChildByName('belowLayer');
        this._charLayer = this.node.getChildByName('charLayer');
        this._upLayer = this.node.getChildByName('upLayer');
        this._unlockLayer = this.node.getChildByName('unlockLayer');
        var 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);
        BattleConst_1.getGlobalNode().on(Global_1.GlobalEvent.OBJ_UNLOCK, this.onObjUnlock, this);
    };
    GameSceneControl.prototype.onDestroy = function () {
        cc.Tween.stopAllByTag(0);
        BattleConst_1.getGlobalNode().off(Global_1.GlobalEvent.OBJ_UNLOCK, this.onObjUnlock, this);
    };
    GameSceneControl.prototype.update = function (dt) {
        // this._backNode.setPosition(this.node.parent.position.x,0);
        // console.log(this.node.parent.position.x);
    };
    //#endregion
    //#region event
    GameSceneControl.prototype.onToolClick = function (e) {
        this.showToolTip(e.target.parent);
    };
    GameSceneControl.prototype.onObjUnlock = function (objId) {
        var _this = this;
        var objTable = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, objId);
        if (objTable.SceneId != this._curSceneId) {
            return;
        }
        /**删除工具 */
        for (var i = 0; i < this._toolList.length; ++i) {
            if (this._toolList[i].getComponent(GameSceneUnlockTool_1.default).config.ID == objId) {
                this._toolList[i].destroy();
                this._toolList.splice(i, 1);
                break;
            }
        }
        var isGarbage = (!GameSceneObjTableMgr_1.GameSceneObjTableMgr.ins.getParentObj(objTable));
        if (this._unlockSpine) {
            if (this._unlockSpine.parent) {
                this._unlockSpine.removeFromParent();
            }
            MainSceneData_1.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(function () {
                MainSceneData_1.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 (var i = 0; i < objTable.Unlock.length; ++i) {
                        var unlock = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, objTable.Unlock[i]);
                        _this.addTool(unlock);
                    }
                }
                BattleConst_1.getGlobalNode().emit(Global_1.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)
    };
    /**滚动到当前页面了 */
    GameSceneControl.prototype.onSlideShow = function () {
        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;
            }
            var interval = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 13).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));
            // }
        }
    };
    GameSceneControl.prototype.onSlideLeave = function () {
        if (this._isShow) {
            this._isShow = false;
            // console.log('page', this._page, 'leave');
            this.unschedule(this.onRandomCharAnimation);
        }
    };
    //#endregion
    //#region public method
    GameSceneControl.prototype.loadScene = function (id) {
        var _this = this;
        this._curSceneId = id;
        cc.resources.load('gameScene/BG_0' + id.toString(), cc.SpriteFrame, function (error, assets) {
            if (!error && _this.isValid) {
                _this.node.getChildByName('img').active = false;
                var 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;
                _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
    GameSceneControl.prototype.renderScene = function () {
        this._belowLayer.removeAllChildren();
        this._upLayer.removeAllChildren();
        var objList = GameSceneObjTableMgr_1.GameSceneObjTableMgr.ins.getByScene(this._curSceneId);
        for (var i = 0; objList && i < objList.length; ++i) {
            var obj = objList[i];
            var parent = GameSceneObjTableMgr_1.GameSceneObjTableMgr.ins.getParentObj(obj);
            if (!parent) {
                //没有父对象,是垃圾
                if (SaveDataManager_1.SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) {
                    //已解锁,不显示
                }
                else {
                    //未解锁,显示
                    this.addObj(obj);
                    this.addTool(obj);
                }
            }
            else {
                //普通对象
                if (SaveDataManager_1.SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) {
                    //已解锁,显示
                    this.addObj(obj);
                }
                else {
                    //未解锁
                    if (SaveDataManager_1.SaveDataManager.ins.runtimeData.unlockObj.includes(parent.ID)) {
                        //父对象已解锁,显示解锁工具图标
                        this.addTool(obj);
                    }
                    else {
                        //父对象未解锁,啥也不显示
                    }
                }
            }
        }
    };
    GameSceneControl.prototype.addTool = function (obj) {
        var tool = cc.instantiate(this._unlockToolPrefab);
        tool.active = true;
        tool.setPosition(obj.PosX, obj.PosY);
        this._unlockLayer.addChild(tool);
        tool.getComponent(GameSceneUnlockTool_1.default).setObjId(obj.ID);
        this._toolList.push(tool);
    };
    GameSceneControl.prototype.addObj = function (obj, ani) {
        var _this = this;
        if (ani === void 0) { ani = false; }
        if (obj.Spine != "") {
            if (obj.ID == 6) {
                //烤肉特殊处理,将本来的营火改为烤肉动画
                var fireObj = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, 3);
                var fireLayer = fireObj.IsBelowChar ? this._belowLayer : this._upLayer;
                var fireSpine = fireLayer.getChildByName('gameSceneObj_' + fireObj.ID.toString());
                fireSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true);
            }
            else if (obj.ID == 21) {
                //猫头鹰特殊处理,将本来的收音机改为猫头鹰动画
                var radio = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, 20);
                var radioLayer = radio.IsBelowChar ? this._belowLayer : this._upLayer;
                var radioSpine = radioLayer.getChildByName('gameSceneObj_' + radio.ID.toString());
                radioSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true);
            }
            else if (obj.ID >= 34 && obj.ID <= 36) {
                //船配件的特殊处理
                var boat = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, 32);
                var boatLayer = boat.IsBelowChar ? this._belowLayer : this._upLayer;
                var boatSpine = boatLayer.getChildByName('gameSceneObj_' + boat.ID.toString());
                boatSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true);
            }
            else if (obj.ID == 23) {
                //烤鱼特殊处理,将本来的烤炉加条鱼
                var cook = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, 17);
                var cookLayer = cook.IsBelowChar ? this._belowLayer : this._upLayer;
                var cookSpine = cookLayer.getChildByName('gameSceneObj_' + cook.ID.toString());
                cookSpine.getComponent(sp.Skeleton).setAnimation(0, obj.Spine, true);
            }
            else {
                var spNode = cc.instantiate(this._objSpine);
                spNode.name = 'gameSceneObj_' + obj.ID.toString();
                spNode.active = true;
                var spine = spNode.getComponent(sp.Skeleton);
                spNode.setPosition(obj.PosX, obj.PosY);
                var 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, function (error, asset) {
                if (!error) {
                    var node = new cc.Node('gameSceneObj_' + obj.ID.toString());
                    var sp = node.addComponent(cc.Sprite);
                    sp.spriteFrame = asset;
                    node.setPosition(obj.PosX, obj.PosY);
                    var 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();
                    }
                }
            });
        }
    };
    GameSceneControl.prototype.showToolTip = function (tool) {
        if (this._curHideTool) {
            this.onScreenTouch();
        }
        this._curHideTool = tool;
        this._unlockTip.setPosition(tool.position);
        var 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_1.default).setObj(tool.getComponent(GameSceneUnlockTool_1.default).config);
        this.node.once(cc.Node.EventType.TOUCH_START, this.onScreenTouch, this);
    };
    GameSceneControl.prototype.onScreenTouch = function () {
        this._unlockTip.active = false;
        this._curHideTool.active = true;
        this._curHideTool = null;
    };
    GameSceneControl.prototype.onRandomCharAnimation = function () {
        var objList = GameSceneObjTableMgr_1.GameSceneObjTableMgr.ins.getByScene(this._curSceneId);
        var idList = [];
        var weightList = [];
        idList.push(0);
        weightList.push(10);
        for (var i = 0; i < objList.length; ++i) {
            var obj = objList[i];
            if (obj.CharSpine != "" && SaveDataManager_1.SaveDataManager.ins.runtimeData.unlockObj.includes(obj.ID)) {
                idList.push(obj.ID);
                weightList.push(10);
            }
        }
        //删除现有的
        var curIndex = idList.indexOf(this._curCharAniId);
        idList.splice(curIndex, 1);
        weightList.splice(curIndex, 1);
        if (idList.length > 0) {
            var id = idList[slib.MathUtil.randomTestByWeightList(weightList)];
            if (id == 0) {
                this._charNode.setPosition(0, -422);
                this._charNode.getComponent(sp.Skeleton).setAnimation(0, 'stay', true); //播默认动画
            }
            else {
                var config = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, id);
                var charSpineName = config.CharSpine;
                this._charNode.setPosition(config.CharSpineX, config.CharSpineY);
                this._charNode.getComponent(sp.Skeleton).setAnimation(0, charSpineName, true);
            }
            //#region 弹吉他,隐藏吉他
            var guitar = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, 38);
            var guitarLayer = guitar.IsBelowChar ? this._belowLayer : this._upLayer;
            var guitarNode = guitarLayer.getChildByName('gameSceneObj_' + guitar.ID.toString());
            if (guitarNode) {
                guitarNode.active = !(id == 38);
            }
            //#endregion
            this._curCharAniId = id;
        }
        else {
            //没得换动画,什么都不做
        }
    };
    GameSceneControl = __decorate([
        ccclass
    ], GameSceneControl);
    return GameSceneControl;
}(cc.Component));
exports.default = GameSceneControl;

cc._RF.pop();