GameSceneControl.ts 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
// 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
}