VerbView.ts 8.38 KB
import { DummyLabel, DummyNode, DummyRichText, DummySpriteFrame, DummyWidget } from "../../common/CCDummyObjects";
import Typewriter from "../../common/components/Typewriter";
import { AudioManager } from "simba-cc-audio-manager";
import { CompositeDisposable } from "simba-eventkit";
import { GameModelManager } from "../model/GameModelManager";
import { DeepReadonlyObject } from "simba-utils";
import { IItemTbl } from "../../config/ItemTbl";
import { UIManager } from "../../common/gameplay/managers/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class VerbView extends cc.Component {
    @property(cc.RichText)
    title: cc.RichText = DummyRichText;

    @property(cc.RichText)
    desc: cc.RichText = DummyRichText;

    @property(cc.Widget)
    loadingWidget: cc.Widget = DummyWidget;

    @property(cc.Node)
    loadingNode: cc.Node = DummyNode;

    @property(Typewriter)
    typewriter: Typewriter = undefined as unknown as Typewriter;

    @property(cc.Node)
    guideNode: cc.Node = DummyNode;

    @property({ type: cc.Node, displayName: "物品掉落节点" })
    shopNode: cc.Node = DummyNode;


    private _title: string;
    private _desc: string;
    private _callback: Function | undefined;

    private _intervalPixel = 0;

    /**是否正在执行渐显动画 */
    private _isShowAction: boolean = false;

    private _loadingNodeX: number;
    private _loadingNodeY: number;

    private _disposable: CompositeDisposable = new CompositeDisposable();
    /**标志数据是否已经准备就绪 */
    private _forceClickDataReady: boolean = false;

    private _itemSpriteFrame: cc.SpriteFrame = DummySpriteFrame;
    private _showItemCfg: DeepReadonlyObject<IItemTbl> | undefined = undefined;
    itemDropSprFrame: cc.SpriteFrame;
    cfg: DeepReadonlyObject<IItemTbl>;

    onLoad() {
        this.loadingNode.x = this.node.getContentSize().width / 2 + 10;
        this._loadingNodeX = this.node.getContentSize().width / 2 + 10;
        this.loadingNode.y = this.node.getContentSize().height / 2 - 36 - this.loadingNode.height / 2;
        this._loadingNodeY = this.node.getContentSize().height / 2 - 36 - this.loadingNode.height / 2;
        this.guideNode.active = false;
        if (CC_DEV) {
            globalThis['VerbView'] = this;
        }
    }

    onEnable() {
        this._disposable.add(GameModelManager.ForceClickDataReady.on(this.forceClickDataReady));
    }

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEndCallback, this);
    }

    testCallback() {
        this.toDating(() => { }, { title: '', desc: '来电了!!!' });
    }

    /**到聊天 */
    toChat(info: { title: string, desc: string }) {
        console.log('=====报幕到聊天====');
        AudioManager.stopMusic();
        this.restartNode();
        this._title = info.title;
        this._desc = info.desc;
        this.verbAction();
    }

    /**到约会 */
    toDating(callback: Function, info: { title: string, desc: string }) {
        console.log('=====报幕到约会====');
        AudioManager.stopMusic();
        this.restartNode();
        this._title = info.title;
        this._desc = info.desc;
        this._callback = callback;
        this.verbAction();
    }

    /**报幕执行动画 */
    verbAction() {
        this._isShowAction = true;
        let sequence = cc.sequence(
            cc.fadeIn(1),
            cc.callFunc(() => {
                if (this._title) {
                    //打字机效果
                    this.title.node.active = true;
                    this.typewriter.showRichTextTyper(this.title, this._title, () => {
                        if (this._desc) {
                            this.desc.node.active = true;
                            this.typewriter.showRichTextTyper(this.desc, this._desc);
                        }
                    });
                } else {
                    this.title.node.active = false;
                    if (this._desc) {
                        this.desc.node.active = true;
                        this.typewriter.showRichTextTyper(this.desc, this._desc);
                    }
                }
                //加载提示进入
                this.loadingNode.runAction(cc.moveTo(0.3, cc.v2(this._loadingNodeX - this.loadingNode.getContentSize().width - this._intervalPixel, this._loadingNodeY)));
            }),
            cc.delayTime(1),
            cc.callFunc(() => {
                this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndCallback, this);
                this._isShowAction = false;
                this.guideNode.active = true;
                if (this._callback) {
                    this._callback();
                }
            })
        )
        this.node.runAction(sequence);
    }

    /**重置node */
    restartNode() {
        this.node.stopAllActions();
        this.node.active = true;
        let wid = this.node.getComponent(cc.Widget);
        wid.updateAlignment();
        this.node.opacity = 0;
        this._callback = undefined;
        this.guideNode.active = false;
        this.loadingNode.x = this.node.getContentSize().width / 2 + 10;
        this.loadingNode.y = this._loadingNodeY;
        this._forceClickDataReady = false;
        this._isShowAction = false;
    }

    /**
     * 点击回调
     */
    touchEndCallback = () => {
        //如果有物品掉落信息就清空
        if (this._itemSpriteFrame || this._showItemCfg) {
            this._itemSpriteFrame = DummySpriteFrame;
            this._showItemCfg = undefined;
        }
        /**没有显示动画并且数据没有准备完毕 */
        if (!this._isShowAction && this._forceClickDataReady) {
            let sequence = cc.sequence(
                cc.delayTime(0.5),
                cc.spawn(
                    cc.fadeOut(1),
                    cc.callFunc(() => {
                        this.title.node.active = false;
                        this.desc.node.active = false;
                        this.guideNode.active = false;
                        //加载提示退出
                        this.loadingNode.runAction(cc.moveTo(0.3, cc.v2(this._loadingNodeX + this.loadingNode.getContentSize().width + this._intervalPixel, this._loadingNodeY)))
                    })
                ),
                cc.callFunc(() => {
                    this.node.active = false;
                    // AudioManager.resumeMusic();
                })
            )
            this.node.runAction(sequence);
        }
    }

    forceClickDataReady = () => {
        this._forceClickDataReady = true;
        console.log('this._forceClickDataReady', this._forceClickDataReady);
    }

    setItemDropData(spriteFrame: cc.SpriteFrame, cfg: DeepReadonlyObject<IItemTbl>) {
        this.itemDropSprFrame = spriteFrame;
        this.cfg = cfg;
        this.showItemDrop(spriteFrame, cfg.scaletime, cfg.scale, cfg.delaytime, cfg.collecttime);
    }

    showItemDrop(itemSpr: cc.SpriteFrame, dropTime: number, startScale: number, delayTime: number, collectTime: number) {
        this.shopNode.active = true;
        let itemNode = this.shopNode.getChildByName("item");
        let shopSprNode = this.shopNode.getChildByName("shopSpr");
        let itemSprNode = this.shopNode.getChildByName("item").getChildByName("itemSpr");
        let itemBaiSpineNode = this.shopNode.getChildByName("item").getChildByName("itemSpine");
        itemNode.rotation = -90;
        itemSprNode.getComponent(cc.Sprite).spriteFrame = itemSpr;
        cc.tween(itemNode)
            .to(0, { scale: startScale })
            .to(dropTime, { scale: 1, angle: 0 })
            .call(() => {
                itemBaiSpineNode.active = true;
                cc.tween(itemNode)
                    .to(delayTime, {})
                    .call(() => {
                        itemBaiSpineNode.active = false;
                        shopSprNode.active = true;
                        cc.tween(itemNode)
                            .to(collectTime, { scale: 0, angle: -360, x: shopSprNode.x, y: shopSprNode.y })
                            .call(() => {
                                itemNode.x = 0;
                                itemNode.y = 0;
                                this.shopNode.active = false;
                                shopSprNode.active = false;
                                UIManager.showSceneToast("记得去卧室查看它的故事呦~~");
                            }).start();
                    }).start();
            }).start();
    }
}