let aWebView = require("WebView");
let aLib = require("Library");
cc.Class({
    extends: cc.Component,

    properties: {
        GameMode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        GameStates: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        PlayerNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        CharactorNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        PlayerStatesNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        PunchTimes: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        PuchTimesLabel: {
            default: null, 
            type: cc.Label, 
            serializable: true,
        },
    },
    start () {
        //初始化
        this.GameModeScp = this.GameMode.getComponent('GameMode');
        this.GameStatesScp = this.GameStates.getComponent('GameStates');
        this.CharactorScp = this.CharactorNode.getComponent('Charactor');
        this.PlayerStatesScp = this.PlayerStatesNode.getComponent('PlayerStates');
        this.PunchTimesAnim = this.PunchTimes.getComponent(cc.Animation);
        //触摸事件
        this.TouchEventScp = this.PlayerNode.getComponent('TouchEvent');
        this.TouchEventScp.registerListener(this.node);
        this.node.on('touchstart',this.onEventStart,this);
        //记录开始位置
        this.GameStatesScp.startPosition.x = this.PlayerNode.x;
        this.GameStatesScp.startPosition.y = this.PlayerNode.y;

        this.PlayerStatesScp.state = this.PlayerStatesScp.stateTag.idle;//idle
        //减慢玩家速度
        this.schedule(function(){
            this.SpeedDown();
        }.bind(this),0.1,cc.macro.REPEAT_FOREVER);

         //是否在pC
         if(!aLib.isMobile()) return;

         let self = this;
 
         aWebView.init(self.node, ()=>{
             aWebView.onBindHitBoxingPost();
             self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
         });
    },
    onBoxingPostHit(data)
    {
        if(this.GameStatesScp.progress != this.GameStatesScp.progressTag.start)//2 start game
        {
            this.GameModeScp.StarGame();
        }
        else
        {
            this.Run(1);
        }
    },
    onEventStart(worldPoint)
    {
        this.Run(0);
    },
    Run(bPunch)
    {
         //是否游戏开始
         if(this.GameStatesScp.progress != this.GameStatesScp.progressTag.start) return;

        let time = new Date();
        if(this.PlayerStatesScp.lastPucnTime!=0)
        {
            let dtTime = time.getMilliseconds() - this.PlayerStatesScp.lastPucnTime.getMilliseconds();

            if(bPunch)
            {
                if(dtTime < 500)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.1;
                }
                else if(dtTime >= 500 && dtTime < 1000)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.2;
                }
                else if(dtTime >= 1000 && dtTime < 2000)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.5;
                }
            }
            else
            {
                if(dtTime < 200)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.1;
                }
                else if(dtTime >= 200 && dtTime < 300)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.2;
                }
                else if(dtTime >= 300 && dtTime < 500)
                {
                    this.PlayerStatesScp.dtDownSpeed = 0.5;
                }
            }
            
        }
        this.PlayerStatesScp.lastPucnTime = time;
        this.GameStatesScp.PunchTimes++;
        this.PuchTimesLabel.string = this.GameStatesScp.PunchTimes;

        if(this.PlayerStatesScp.speed+this.PlayerStatesScp.dtSpeed<=10)
        {
            this.PlayerStatesScp.speed+=this.PlayerStatesScp.dtSpeed;
        }
        //console.log('this.PlayerStatesScp.speed=',this.PlayerStatesScp.speed)
        this.unschedule(this.StopRun);
        if(bPunch)
        {
            this.scheduleOnce(this.StopRun,2);
        }
        else
        {
            this.scheduleOnce(this.StopRun,0.5);
        }
    },
    StopRun()
    {
        this.PlayerStatesScp.speed = 0;
        this.CharactorScp.Idel();
    },
    SpeedDown()
    {
        if(this.PlayerStatesScp.speed<=0) return;
        this.PlayerStatesScp.speed-=this.PlayerStatesScp.dtDownSpeed;
    },
    Reset()
    {
        this.PlayerNode.x = this.GameStatesScp.startPosition.x;
        this.PlayerNode.y = this.GameStatesScp.startPosition.y;

        this.PuchTimesLabel.string = '0';
    },
    update (dt) 
    {
        if(this.PlayerStatesScp.speed>0 && this.PlayerStatesScp.speed<3)//run
        {
            this.CharactorScp.Run(1);
        }
        else if(this.PlayerStatesScp.speed>=3 && this.PlayerStatesScp.speed<8)
        {
            this.CharactorScp.Run(2);
        }
        else if(this.PlayerStatesScp.speed>=9)
        {
            this.CharactorScp.Run(3);
        }

        this.PlayerNode.x += this.PlayerStatesScp.speed;
    },
});
