import { EventMgr } from "../Core/Base/EventMgr";
import Start from "../Start";
import { Touch_Start, Touch_Move, Touch_End } from "./eventDef";
import { Barrage } from "./nest/barrage";

import { GameLogic } from "./update/GameLogic";
import MoveDest from "./update/moveDest";

const {ccclass, property} = cc._decorator;

export let PlayerAttack = true;

@ccclass
export default class player extends GameLogic {

    /*===========================自动绑定组件开始===========================*/
    /*自动生成*/
    @property({type:cc.Node, displayName:""})
    private $role_node:cc.Node = null;
    
	/*===========================自动绑定组件结束===========================*/

    /*===========================自动生成按钮事件开始==========================*/

	/*===========================自动生成按钮事件结束==========================*/

    // moveCpt: move = null;
    moveCpt: MoveDest = null;
    // dirction: cc.Vec2 = cc.v2(0,1);
    bulletCD:number = 0.2;
    bulletCDTime: number = 0;

    airStartPos: cc.Vec2;

    static PlayerAttack = false;

    protected onLoad(): void {
        // this.moveCpt = this.node.getComponent(move);
        // if(!this.moveCpt) {
        //     this.moveCpt = this.node.addComponent(move);
        // }
        // this.moveCpt.speed=500;

        this.moveCpt = this.node.getComponent(MoveDest);
        if(!this.moveCpt) {
            this.moveCpt = this.node.addComponent(MoveDest);
        }
        this.moveCpt.speed= 800;
        this.moveCpt.enabled = false;
        
    }


    protected onEnable(): void {
        // 摇杆操作
        // EventMgr.inst.on(Roker_Move, this.onRokerMove, this);
        // EventMgr.inst.on(Roker_End, this.onRokerEnd, this);

        // 跟随手指移动
        EventMgr.inst.on(Touch_Start, this.onTouchStart, this);
        EventMgr.inst.on(Touch_Move, this.onTouchMove, this);
        EventMgr.inst.on(Touch_End, this.onTouchEnd, this);
    }
    
    protected onDisable(): void {
        // EventMgr.inst.off(Roker_Move, this.onRokerMove, this);
        // EventMgr.inst.off(Roker_End, this.onRokerEnd, this);
        
    }

    gameUpdate(dt: number): void {
        this.bulletCDTime -= dt;
        if(this.bulletCDTime <= 0){
            this.bulletCDTime = this.bulletCD;
            player.PlayerAttack && Barrage.inst.createPlayerBullet(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
        }
    }

    // onRokerMove(dirction: cc.Vec2){
    //     this.moveCpt.dirction = dirction;
    //     this.dirction = dirction;
    // }

    // onRokerEnd(){
    //     this.moveCpt.dirction = cc.v2(0,0);
    // }

    onTouchStart(){
        this.airStartPos = this.node.getPosition();
        this.moveCpt.destination = this.airStartPos;
        this.moveCpt.enabled = true;
    }

    onTouchMove(offset: cc.Vec2){
        this.moveCpt.destination = this.airStartPos.add(offset);
    }

    onTouchEnd(){
        this.moveCpt.enabled = false;
    }
}