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,
        },
        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.TouchEventScp = cc.find('Canvas').getComponent('TouchEvent');
        this.TouchEventScp.registerListener(this.node);
        this.node.on('touchstart',this.onEventStart,this);
        this.node.on('swipe',this.onEventSwipe,this);

        this.GameStatesScp.startPosition.x = this.PlayerNode.x;
        this.GameStatesScp.startPosition.y = this.PlayerNode.y;

        this.bRuning = false;
    },
    onEventStart(worldPoint)
    {
        if(!this.GameStatesScp.bStart) return;
        console.log('touch start');
        this.Run();
    },
    onEventSwipe(direction)
    {
        if(!this.GameStatesScp.bStart) return;
        if(direction == 'left')
        {
            //console.log('left');
        }
        else if(direction == 'right')
        {
            //console.log('right');
        }
        else if(direction == 'up')
        {
            //console.log('up');
        }
        else if(direction == 'down')
        {
            //console.log('down');
        }
    },
    Run()
    {
        this.CharactorScp.Run();
        this.PlayerNode.x+=3;
        this.GameStatesScp.PunchTimes++;
        this.PuchTimesLabel.string = this.GameStatesScp.PunchTimes;

        this.unschedule(this.StopRun);
        this.scheduleOnce(this.StopRun,0.5);
    },
    StopRun()
    {
        let count = 2;
        this.schedule(function(){
            if(!this.GameStatesScp.bStart)
            {
                this.CharactorScp.Idel();
                return;
            }

            this.PlayerNode.x+=1;
            count--;
            if(count==0)
            {
                this.CharactorScp.Idel();
            }
        }.bind(this),0.1,count)
    },
    Reset()
    {
        this.PlayerNode.x = this.GameStatesScp.startPosition.x;
        this.PlayerNode.y = this.GameStatesScp.startPosition.y;

        this.PuchTimesLabel.string = '0';
    }
});
