cc.Class({
    extends: cc.Component,

    properties: {
        Hero: {
            default: null,
            type: cc.Node,
            LevelIdex:0
        },
    },
    start : function() {
        this.LevelIdex=0;
        this.GameStates = cc.find("Canvas").getComponent("GameStates");

    },
    onCollisionEnter: function (other) {
        // if(!bDebug)return;
        //
        // if(this.node.parent.parent.name == 'Hero') return;

        //获取场景
        var scene = cc.director.getScene();

        if (this.GameStates.isRobot) {
            cc.find("AIBot").getComponent("AIBot").Enters(other,this);
        }
    },
    GetSyncObj:function(ParentName,ObjName,PositionX)
    {
        var Level = cc.find("Level");
        var aX = parseFloat(PositionX);

        var Obj = null;
        var LevelChildren = Level.children;
        // cc.log('Level'+length);
        for(var i=0;i<LevelChildren.length;i++)
        {
            if(LevelChildren[i].name === ParentName)
            {
                var aObj =  LevelChildren[i].getChildByName(ObjName);

                if(aObj && aX===aObj.convertToWorldSpaceAR(cc.Vec2.ZERO).x)
                {
                    Obj = aObj;
                }
            }
        }
        return Obj;
    },
    update : function (dt) {

        if(bDebug)return;
        // if(this.node.parent.parent.name != 'Hero') return;
        if(!cc.find('Canvas').getComponent('GameStates').bServer) return;

        var ColliderInfo = null;
        if(this.node.parent.parent.name == 'Hero')
        {
            ColliderInfo = this.node.getComponent('HeroColliderInfo');
        }
        else
        {
            ColliderInfo = this.node.getComponent('RivelColliderInfo');
        }

        if (ColliderInfo==null) {
            return;
        }

        var CurrentCollider = ColliderInfo.data[this.LevelIdex];
        if(this.LevelIdex==ColliderInfo.data.length) return;

        var aX = parseFloat(CurrentCollider.PositionX);

        if(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO).x<aX+1 && this.node.convertToWorldSpaceAR(cc.Vec2.ZERO).x>aX-1)
        {
            var Obj= this.GetSyncObj(CurrentCollider.ParentName,CurrentCollider.ObjName,CurrentCollider.BarrierPositionX);
            if (Obj==null) {
                return;
            }
            var ObjScript = Obj.getComponent('BarrierSuper');

            if(CurrentCollider.State=='Enter')
            {
                ObjScript.onCollisionEnter(this);
            }
            else
            {
                ObjScript.onCollisionExit(this);
            }

            this.LevelIdex++;
        }
    }

});
