

const { ccclass, property } = cc._decorator;

@ccclass
export default class TheController extends cc.Component {
    @property([cc.SpriteFrame])
    private frames: cc.SpriteFrame[] = [];

    private _spine: sp.Skeleton = null;
    private _skins: string[] = ["goi", "the"];
    private _textSprite: cc.Sprite = null;

    protected onLoad(): void {
        this._textSprite = this.getComponentInChildren(cc.Sprite);
        this._spine = this.node.getComponent(sp.Skeleton);
        
        this._spine.setCompleteListener((trackEntry) => {
            if (trackEntry.animation.name == 'Open') {
                this._spine.setAnimation(0, 'Idle', true);
            }
        })
    }
    protected onEnable(): void {
        this._spine.setAnimation(0, 'Open', false);
        this._spine.timeScale = 0.5;
        const skinIndex = this.getRandomInt(0, this._skins.length);
        this._spine.setSkin(this._skins[skinIndex]);
        this._textSprite.spriteFrame = this.frames[skinIndex];
    }

    private getRandomInt(min: number, max: number): number {
        return Math.floor(Math.random() * (max - min)) + min;
    }
}
