const { ccclass, property } = cc._decorator;

enum AnimationType {
    AUTO = 0,
    CLICK = 1
}

@ccclass
export default class NameplateAnimation extends cc.Component {

    @property({
        type: cc.Enum(AnimationType),
        displayName: '触发类型',
    })
    animationType: AnimationType = AnimationType.AUTO;

    @property({
        displayName: '停留时间',
        visible() { return this.animationType == AnimationType.AUTO },
    })
    stopTime: number = 3;

    @property({
        type: cc.Node,
        displayName: '点击节点',
        visible() { return this.animationType == AnimationType.CLICK },
    })
    touchNode: cc.Node = null;

    start() {
        if (this.animationType == AnimationType.AUTO) {
            cc.tween(this.node)
                // .set({ active: true, opacity: 0 })
                .to(0.5, { opacity: 255 })
                .delay(this.stopTime)
                .to(0.5, { opacity: 0 })
                .hide()
                .start();
        } else if (this.animationType == AnimationType.CLICK) {
            if (!this.touchNode) {
                this.touchNode = this.node;
            }
            this.touchNode.on(cc.Node.EventType.TOUCH_START, () => {
                this.node['_touchListener'].setSwallowTouches(false);
                cc.tween(this.node)
                    .to(0.5, { opacity: 0 })
                    .hide()
                    .start();
            }, this);
            cc.tween(this.node)
                .set({ active: true, opacity: 0 })
                .to(0.5, { opacity: 255 })
                .start();
        }
    }

}