// Scene的控制器
// 管理角色和球的信息
var Role = require("role")
var RivalServe = require("rivalserve")
var MinePatLtoR = require("minepatltor")
var MinePatLtoL = require("minepatltol")
var MinePatRtoR = require("minepatrtor")
var RivalPatRtoL = require("rivalpatrtol")
var MinePeelRtoR = require("minepeelrtor")

cc.Class({
    extends: cc.Component,

    properties: {
        rival: {
            default: null,
            type: Role,
        },

        mine: {
            default: null,
            type: Role,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        var self = this;
        this.rival.serveStart = function() {
            // self.rivalserve.ballServe();
        }

        this.initValues();
        this.initListener();

        // 游戏开始，对方开始发球
        this.rival.roleServe();
    },

    // 帧序列，在这里面处理各种状态
    update (dt) {

        if (this.ballstate == this.ballconfig.BALL_STATE_MINE_LEFT_TO_RIGHT_FLIP) {
            this.rival.setDir(4);
            this.stopAllBall();
            this.rivalpatrtol.ballPat();
            this.rival.pat.pat();

            this.ballstate = this.ballconfig.BALL_STATE_NONE;
        }
    },

    // 初始化数据
    initValues:function() {
        // 球的状态信息
        this.ballconfig = require("ballconfig");
        // 当前球的状态，初始化为发球开始状态
        this.ballstate = this.ballconfig.BALL_STATE_SERVE_START;
    },

    // 初始化监听
    initListener:function() {
        // 添加触摸开始事件
        var self = this;
        this.node.on (cc.Node.EventType.TOUCH_START, function(event) {
            self.mine.pat.pat();

            var touches = event.getTouches();
            var loc = self.node.convertToNodeSpaceAR(touches[0].getLocation());

            if (loc.x > 0) {
                self.mine.setDir(2);
            } else {
                self.mine.setDir(1);
            }

            // 如果状态符合，则打回去
            if (self.ballstate == self.ballconfig.BALL_STATE_SERVE_LEFT ||
                self.ballstate == self.ballconfig.BALL_STATE_RIVAL_RIGHT_TO_LEFT_FLIP) {
                self.stopAllBall();
                self.minepatltor.ballPat();
            }
        });

        // // 配置球的状态判断回调
        // this.rivalserve.stateCallback = function(state) {
        //     self.ballstate = state;
        // };
        // this.minepatltor.stateCallback = function(state) {
        //     self.ballstate = state;
        // };
        // this.minepatltol.stateCallback = function(state) {
        //     self.ballstate = state;
        // };
        // this.minepatrtor.stateCallback = function(state) {
        //     self.ballstate = state;
        // };
        // this.rivalpatrtol.stateCallback = function(state) {
        //     self.ballstate = state;
        // };
    },

    // 停止各种球的运动
    stopAllBall:function() {
        // this.rivalpatrtol.ballStop();
        // this.minepatltor.ballStop();
        // this.minepatltol.ballStop();
        // this.minepatrtor.ballStop();
    },
});
