// pg.event.emit('img_voice_play_voice_start')
// pg.event.emit('img_voice_play_voice_end')

import pg from "../pg_jj16_lianci";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ImgAuto extends cc.Component {
    @property([cc.Node])
    imgList: Array<cc.Node> = [];

    onLoad() {
        this.initEvent();
    }
    protected onDestroy(): void {
        this.unscheduleAllCallbacks();
    }
    initEvent() {
        pg.event.on('img_voice_play_voice_start', () => {
            this.playVoiceStart();
        })
        pg.event.on('img_voice_play_voice_end', () => {
            this.playVoiceEnd();
        })
    }
    private playVoiceCount: number;
    playVoiceStart() {
        if (this.playVoiceCount > 0) {
            this.playVoiceEnd();
        }
        this.playVoiceCount = 0;
        this.schedule(this.playVoiceRunning, 0.3)
    }
    updateImgList() {
        for (let i = 0; i < this.imgList.length; i++) {
            let item = this.imgList[i];
            item.active = this.playVoiceCount % this.imgList.length == i;
        }
    }
    playVoiceRunning() {
        this.playVoiceCount++;
        if (!this.node) return;
        this.updateImgList();
    }
    playVoiceEnd() {
        this.unschedule(this.playVoiceRunning)
        this.playVoiceCount = 0;
        this.updateImgList();
    }
}










