GuideManager.ts 1.96 KB
import { UIManager } from "../ui/UIManager";

export class GuideManager {

    private static _ins: GuideManager = null;

    static get ins(): GuideManager {
        if (this._ins == null) {
            this._ins = new GuideManager();
        }

        return this._ins;
    }

    private _talkNode: cc.Node;
    private _handNode: cc.Node;

    init(node: cc.Node) {
        this._talkNode = node.getChildByName('talk');
        this._handNode = node.getChildByName('hand');

        this._talkNode.removeFromParent();
        this._handNode.removeFromParent();

        this._talkNode.active = false;
        this._handNode.active = false;

        UIManager.ins.widgetLayer.addChild(this._talkNode);
        UIManager.ins.widgetLayer.addChild(this._handNode);
    }

    showTalk(wpos: cc.Vec2, txt: string) {
        this._talkNode.parent.convertToNodeSpaceAR(wpos, wpos);
        this._talkNode.setPosition(wpos);
        let label = this._talkNode.getComponentInChildren(cc.Label);
        label.string = txt;
        this._talkNode.active = true;
        this._talkNode.height = label.node.height + 30;
        let arrow = this._talkNode.getChildByName('arrow');
        arrow.y = -this._talkNode.height / 2;

        this._talkNode.y += this._talkNode.height / 2;

        cc.Tween.stopAllByTarget(this._talkNode);
        this._talkNode.scale = 0;
        cc.tween(this._talkNode).to(0.2, { scale: 1 }, { easing: 'backOut' }).start();

    }

    hideTalk() {
        cc.Tween.stopAllByTarget(this._talkNode);
        this._talkNode.active = false;
    }

    showHand(wpos: cc.Vec2, down: boolean = true) {
        this._handNode.parent.convertToNodeSpaceAR(wpos, wpos);
        this._handNode.setPosition(wpos);
        this._handNode.active = true;

        let img = this._handNode.getChildByName('btn');
        img.angle = down ? 90 : 270;

        this._handNode.y += (this._handNode.height / 2 * (down ? 1 : -1));
    }

    hideHand() {
        this._handNode.active = false;
    }
}