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

import pg from "./pg_jj15_zww";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ImgAuto4 extends cc.Component {
    private img_npc_voice: cc.Node;
    onLoad() {
        this.initView();
        this.initEvent();
    }
    protected onDestroy(): void {
        this.unscheduleAllCallbacks();
    }
    initView() {
        this.img_npc_voice = this.node;
    }
    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) return;
        this.playVoiceCount = 0;
        this.schedule(this.playVoiceRunning, 0.3)
    }
    playVoiceRunning() {
        this.playVoiceCount++;
        if (!this.img_npc_voice) return;
        let p1 = this.img_npc_voice.getChildByName('p1')
        let p2 = this.img_npc_voice.getChildByName('p2')
        let p3 = this.img_npc_voice.getChildByName('p3')
        let p4 = this.img_npc_voice.getChildByName('p4')
        p1.active = this.playVoiceCount % 4 == 0;
        p2.active = this.playVoiceCount % 4 == 1;
        p3.active = this.playVoiceCount % 4 == 2;
        p4.active = this.playVoiceCount % 4 == 3;
    }
    playVoiceEnd() {
        this.unschedule(this.playVoiceRunning)
        this.playVoiceCount = 0;
        let p1 = this.img_npc_voice.getChildByName('p1')
        let p2 = this.img_npc_voice.getChildByName('p2')
        let p3 = this.img_npc_voice.getChildByName('p3')
        let p4 = this.img_npc_voice.getChildByName('p4')
        p1.active = true;
        p2.active = false;
        p3.active = false;
        p4.active = false;
    }
}











