import { _decorator, Component, Node, ModelComponent, Vec4, GraphicsComponent, Size, view, Color } from 'cc';
import { EventDispatcher } from '../../../../engines/events/EventDispatcher';
import { WeaponBase } from '../weapons/WeaponBase';
const { ccclass, property } = _decorator;

export class SkillBase extends EventDispatcher{
    public static SKILL_COMPLETE:string="SKILL_COMPLETE";
    
    config:any;

    data:any;

    weapon:WeaponBase;

    leftWeapon:ModelComponent;
    rightWeapon:ModelComponent;

    private color4:Vec4;
    private colorIndex:number=0;
    /**
     * 使用技能
     * @param weapon 
     * @param data
     */
    UseSkill(weapon:WeaponBase,data?:any):void{
        this.weapon=weapon;
        this.data=data;

        if(this.weaponFlicker){
            this.color4=new Vec4(1,1,1,1)
            this.leftWeapon=this.weapon.leftWeapon.getComponentInChildren(ModelComponent);
            this.rightWeapon=this.weapon.rightWeapon.getComponentInChildren(ModelComponent);
        }
    }

    /**
     * 更新
     * @param dt 
     */
    Update(dt:number):void{
        if(this.weaponFlicker){
            this.colorIndex+=0.1;
            let value:number=Math.sin(this.colorIndex);
            this.color4.w=value;
            this.leftWeapon.sharedMaterial.setProperty('rimColor',this.color4);
            this.rightWeapon.sharedMaterial.setProperty('rimColor',this.color4);
        }
        this.DrawingSkillState();
    }
    
    /**
     * 销毁
     */
    Dispose():void{
        if(this.weaponFlicker){
            this.color4.set(1,1,1,0);
            if(this.leftWeapon.isValid){
                this.leftWeapon.sharedMaterial.setProperty('rimColor',this.color4);
            }
            if(this.rightWeapon.isValid){
                this.rightWeapon.sharedMaterial.setProperty('rimColor',this.color4);
            }
        }
        
        this.opacityIndex=0;
        this.DrawingSkillState();

        this.weapon=null;
        this.data=null;
    }


    private opacityIndex:number=0;
    private DrawingSkillState():void{
        let a:number=Math.sin(this.opacityIndex)*255;
        let VisibleSize:Size=view.getVisibleSize();
        this.drawingBoard.clear(true);
        this.drawingBoard.fillColor=new Color(255,0,0,a);
        
        let w3:number=VisibleSize.width/3;
        let h4:number=VisibleSize.height/4;
        this.drawingBoard.moveTo(0,0);
        this.drawingBoard.lineTo(w3,0);
        this.drawingBoard.lineTo(0,h4);
        this.drawingBoard.fill();

        this.drawingBoard.moveTo(VisibleSize.width-w3,0);
        this.drawingBoard.lineTo(VisibleSize.width,0);
        this.drawingBoard.lineTo(VisibleSize.width,h4);
        this.drawingBoard.fill();

        this.drawingBoard.moveTo(0,VisibleSize.height);
        this.drawingBoard.lineTo(0,VisibleSize.height-h4);
        this.drawingBoard.lineTo(w3,VisibleSize.height);
        this.drawingBoard.fill();

        this.drawingBoard.moveTo(VisibleSize.width-w3,VisibleSize.height);
        this.drawingBoard.lineTo(VisibleSize.width,VisibleSize.height);
        this.drawingBoard.lineTo(VisibleSize.width,VisibleSize.height-h4);
        this.drawingBoard.fill();

        this.opacityIndex+=0.1;
    }


    protected get drawingBoard():GraphicsComponent{
        return this.data.drawingBoard;
    }
    /**
     * 射击间隔
     */
    get fireInterval():number{
        return this.config.fireInterval;
    }

    get damage():number{
        return this.config.damage;
    }

    get time():number{
        return this.config.time;
    }
    
    /**
     * 是否阻断玩家操作
     */
    get unController():boolean{
        return this.config.unController==1;
    }

    /**
     * 武器闪烁
     */
    get weaponFlicker():boolean{
        return this.config.weaponFlicker==1;
    }

    /**
     * 界面闪烁
     */
    get viewFlicker():boolean{
        return this.config.viewFlicker==1;
    }

    /**
     * 无限子弹
     */
    get infiniteAmmo():boolean{
        return this.config.infiniteAmmo==1;
    }
}