import { _decorator, Component, Node, director } from 'cc';
import { SoundManager } from '../../../../engines/sounds/SoundManager';
import { GameController } from '../GameController';
import { WeaponBase } from '../weapons/WeaponBase';
import { SkillBase } from './SkillBase';
const { ccclass, property } = _decorator;

@ccclass('KillAllMonsterSkill')
export class KillAllMonsterSkill extends SkillBase {

    constructor(){
        super();
    }

    private startTime:number;
    /**
     * 使用技能
     * @param weapon 
     * @param data 
     */
    UseSkill(weapon:WeaponBase,data?:any):void{
        super.UseSkill(weapon,data);
        this.weapon.fireKey=2;
        this.weapon.MoveFirePosition();
        this.startTime=director.getCurrentTime();
    }
    
    private lastTime:number=0;
    /**
     * 更新
     * @param dt
     */
    Update(dt:number):void{
        super.Update(dt);
        let current:number=director.getCurrentTime();
        if(current-this.startTime>this.time){
            this.DispatchEvent(SkillBase.SKILL_COMPLETE);
            return;
        }
        let posType:number=GameController.single.FindAttackMonsterPosType;
        if(posType<0){
            SoundManager.single.StopSound("Weapon");
            SoundManager.single.StopSound("WeaponLeft");
            SoundManager.single.StopSound("WeaponRight");
            return;
        }
        if(current-this.lastTime>this.fireInterval){
            if(posType==0){
                this.weapon.fireKey=1;
            }else if(posType==1){
                this.weapon.fireKey=2;
            }else if(posType==2){
                this.weapon.fireKey=3;
            }
            this.weapon.MoveFirePosition();
            this.weapon.Fire(this.infiniteAmmo);
            this.lastTime=current;
        }
    }
    
    /**
     * 销毁
     */
    Dispose():void{
        super.Dispose();
        SoundManager.single.StopSound("Weapon");
        SoundManager.single.StopSound("WeaponLeft");
        SoundManager.single.StopSound("WeaponRight");
    }
}
