import { _decorator, Button, Component, director, Node, Sprite, SpriteFrame } from 'cc';
import { GameInfo } from '../../GameInfo';
const { ccclass, property } = _decorator;

@ccclass('SelectTroopsBottom')
export class SelectTroopsBottom extends Component {
    private _btnNext: Node = null;
    private _characterSlot: Node = null;
    private _names: string[] = [];
    protected onLoad(): void {
        this._btnNext = this.node.getChildByName("BtnNext");
        this._characterSlot = this.node.getChildByName("CharacterSlot");
    }

    start() {
        this._btnNext.getComponent(Button).node.on(Button.EventType.CLICK, this._onBtnNext, this);
    }

    private _onBtnNext() {
        this._names = this._getNames();
        if (this._names) {
            GameInfo.Instance.setRoleImgNames(this._names);
            director.loadScene("GameScene");
        }
    }

    private _getNames(): string[] {
        for (const element of this._characterSlot.children) {
            const name: SpriteFrame = element.getChildByName("Sprite").getComponent(Sprite).spriteFrame;
            if (name) {
                this._names.push(name.name)
            }

        }
        return this._names;
    }
}


