
cc.Class({
    extends: cc.Component,

    properties: {
        spawnStartTiledTile1: {
            default: new cc.Vec2(),
            serializable: true,
            tooltip: '生成AI的第一条线起始点！',
        },
        spawnEndTiledTile1: {
            default: new cc.Vec2(),
            serializable: true,
            tooltip: 'AI消失第一条线的终点！',
        },
        spawnStartTiledTile2: {
            default: new cc.Vec2(),
            serializable: true,
            tooltip: '生成AI的第二条线的起点！',
        },
        spawnEndTiledTile2: {
            default: new cc.Vec2(),
            serializable: true,
            tooltip: 'AI消失第二条线的终点！',
        },
        spawnAiNum:
        {
            default: 1,
            type: cc.Integer,
            serializable: true,
            tooltip: '生成的游客数量',
        },
        spawnAiInterval:
        {
            default: 1.0,
            type: cc.Float,
            serializable: true,
            tooltip: '生成游客的间隔',
        },

        spawnAiSpeed:
        {
            default: 0,
            type: cc.Integer,
            serializable: true,
            tooltip: '游客附加的速度',
        },

        spawnAiPrefab:
        {
            default: null,
            type: cc.Prefab,
            serializable: true,
            tooltip: '游客的预制',
        },

        spawnParentNode:
        {
            default: null,
            type: cc.Node,
            serializable: true,
            tooltip: '生成游客的父节点',
        },

        //工人
        spawnWorkerAiPrefab:
        {
            default: null,
            type: cc.Prefab,
            serializable: true,
            tooltip: '工人的预制',
        },

        //初始化传递的属性
        InitCurrentGradeJson: null,


        //当前给AI分配的商店目标
        currentDistributionSubscript: {
            default: 0,
            type: cc.Integer,
            visible: false,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad() {

    // },

    start() {

        //生成AI
        this.callback = function () {
            this.onSpawnAI();
        }
        this.schedule(this.callback, this.spawnAiInterval);


        //测试用
        this.spawnAiSpeed = 100;

    },

    //按顺序设置游客购物的商店
    _SetTouristAITarget() {

        let _length = GlobalD.game.shopBuildingSalesArray.length;

        //如果没有商店
        if (_length == 0)
            return null;

        let _getShopTarget = null;
        //如果记录的下标超过数组长度
        if (this.currentDistributionSubscript >= _length) {
            this.currentDistributionSubscript = 0;//重置
            _getShopTarget = GlobalD.game.shopBuildingSalesArray[this.currentDistributionSubscript];
        }
        else {
            _getShopTarget = GlobalD.game.shopBuildingSalesArray[this.currentDistributionSubscript];
            this.currentDistributionSubscript++;
        }
        // cc.log('_getShopTarget',_getShopTarget.buildInfo.id);
        //返回下标对应的商店
        return _getShopTarget;
    },

    onSpawnAI() {

        // if (this.currentDistributionSubscript >= 1) return;

        if (task.TaskIconCountClick < 13) {//如果是没走完新手教程，不出游客
            return;
        }

        let touristAI;
        touristAI = cc.instantiate(this.spawnAiPrefab);
        touristAI.parent = this.spawnParentNode;

        let AITouristPlayer = touristAI.getChildByName('AI').getComponent("AI_tourist_player");






        //设置工人在外面行走的道路
        if (Math.random() >= 0.5) {
            //第一条路
            //单纯切换下两个位置
            // if (Math.random() >= 0.5)
            //     AITouristPlayer.onGotoEndTiledTile(this.spawnEndTiledTile1, this.spawnStartTiledTile1);
            // else
            AITouristPlayer.onGotoEndTiledTile(this.spawnStartTiledTile1, this.spawnEndTiledTile1);


        } else {
            //第二条路
            // AITouristPlayer.onGotoEndTiledTile(this.spawnStartTiledTile2, this.spawnEndTiledTile2);

            //单纯切换下两个位置
            // if (Math.random() >= 0.5)
            AITouristPlayer.onGotoEndTiledTile(this.spawnStartTiledTile2, this.spawnEndTiledTile2);
            // else
            // AITouristPlayer.onGotoEndTiledTile(this.spawnEndTiledTile2, this.spawnStartTiledTile2);


        }




        if (task.TaskIconCountClick != 13 //如果过了新手教程游客购买步骤
            && Math.random() >= 0.35) {//这里决定是去商店还是去材料场地
            //设置游客的判定值，目标为商店
            AITouristPlayer.isGoToStore = true;
            //设置AI需要去的商店
            AITouristPlayer.distributionOfTargetStores = this._SetTouristAITarget();
        }

        let AIAnimation = touristAI.getChildByName('AI').getComponent('AI_tourist_Animation');

        let AITypeIndex = Math.ceil(Math.random() * 6);

        if (task.TaskIconCountClick == 13) {//如果是新手教程，生成第三类人
            AITypeIndex = Math.ceil(Math.random() * 3);
            if (AITypeIndex != 3) {
                //设置游客的判定值，目标为商店
                AITouristPlayer.isGoToStore = true;
                //设置AI需要去的商店
                AITouristPlayer.distributionOfTargetStores = this._SetTouristAITarget();
            }
        }
        // cc.log('游客下标:', AITypeIndex);
        AIAnimation.onSwitchTourist(AITypeIndex + 1);

        let AIAttribute = touristAI.getChildByName('AI').getComponent('AI_tourist_Attribute');
        //isCanTasksbeTriggered



        switch (AITypeIndex) {
            case 0:
                //人物速度
                AIAttribute.characterSpeed = 20 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 15;
            case 1:
                //人物速度
                AIAttribute.characterSpeed = 40 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 20;
                break;
            case 2:
                //人物速度
                AIAttribute.characterSpeed = 70 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 25;
                break;
            case 3:
                //这类人可以触发购买任务
                AIAttribute.isCanTasksbeTriggered = true;
                //人物速度
                AIAttribute.characterSpeed = 70 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 30;
                break;
            case 4:
                //人物速度
                AIAttribute.characterSpeed = 100 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 35;
                break;
            default:
                //人物速度
                AIAttribute.characterSpeed = 100 + this.spawnAiSpeed;
                //游客购买力
                AIAttribute.purchasingPower = 5;
                //游客购买力
                AIAttribute.totalQuantityOfGoodsPurchased = 35;
                break;
        }
    },

    //生成工人AI
    onSpawnWorkerAI() {
        let workerAI;
        workerAI = cc.instantiate(this.spawnWorkerAiPrefab);
        workerAI.parent = this.spawnParentNode;

        let AIWorkerPlayer = workerAI.getChildByName('AI').getComponent("AI_worker_player");


        // //在道路上随机生成
        // let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
        //
        // let randomSpawn = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);

        let randomSpawn = new cc.v2(16, 16);
        // if (GlobalD.GameData.GameYear == 0 && GlobalD.GameData.GameMonth == 0 && GlobalD.GameData.GameDay < 2) {
        //     randomSpawn.x = 22;
        //     randomSpawn.y = 22;
        // }

        AIWorkerPlayer.onSetSpawnStartPoiont(randomSpawn);
        //设置AI添加数组
        GlobalD.game.onSetWorkerArray(AIWorkerPlayer);

        //随机寻找房屋
        AIWorkerPlayer.onUpdateFindHouse();

        let AIWorkerAttribute = workerAI.getChildByName('AI').getComponent("AI_worker_Attribute");
        AIWorkerAttribute.characterInfo.id = new Date().getTime();
        AIWorkerAttribute.characterInfo.startTilePos = randomSpawn;


        // cc.log('InitCurrentGradeJson',this.InitCurrentGradeJson,parseInt(this.InitCurrentGradeJson.MoveSpeed));
        //当前属性赋值
        AIWorkerAttribute.totalPhysicalStrength = parseInt(this.InitCurrentGradeJson.PhysicalStrength);
        AIWorkerAttribute.characterSpeed = parseInt(this.InitCurrentGradeJson.MoveSpeed);
        AIWorkerAttribute.labour = parseInt(this.InitCurrentGradeJson.Labor);

        //记录工人信息
        GlobalD.game.onAddWorkerCharacterInfoToArray(AIWorkerAttribute.characterInfo);


    },

    //移除工人AI，每次都移除一个工人
    onRemoveWorkerAI() {

        let workerPlayer = GlobalD.game.WorkerArray;
        let length = workerPlayer.length;

        for (let i = 0; i < length; i++) {

            if (workerPlayer[i].AIAttribute.isCanDismissed) {
                GlobalD.game.onRemoveAIFromWorkerArray(i);
                return true;
            }

        }
        return false;
    },

    //读取的内存数据生成AI
    onSpawnWorkerAIFromStoredData() {
        let characterArray = GlobalD.GameData.GetWorkerCharacterInfoArray();
        // console.log(characterArray);
        for (let i = 0; i < characterArray.length; i++) {
            //生成点
            let spawnPos = characterArray[i].startTilePos;
            /**
             * 判断这个点是否 被占用， 等下生成人物的点在固定土地内，处理人物位置
             */
            if (GlobalD.game.getManageGameIndexArrayAt(spawnPos)) {
                //暂时主路随意找一个点
                spawnPos = characterArray[i].startTilePos = cc.v2(17,16);
            }
            let workerAI = cc.instantiate(this.spawnWorkerAiPrefab);
            workerAI.parent = this.spawnParentNode;
            let AIWorkerPlayer = workerAI.getChildByName('AI').getComponent("AI_worker_player");
            AIWorkerPlayer.onSetSpawnStartPoiont(spawnPos);
            //设置AI添加数组
            GlobalD.game.onSetWorkerArray(AIWorkerPlayer);
            let AIWorkerAttribute = workerAI.getChildByName('AI').getComponent("AI_worker_Attribute");
            AIWorkerAttribute.characterInfo = characterArray[i];
            AIWorkerAttribute.totalPhysicalStrength = parseInt(this.InitCurrentGradeJson.PhysicalStrength);
            AIWorkerAttribute.characterSpeed = parseInt(this.InitCurrentGradeJson.MoveSpeed);
            AIWorkerAttribute.labour = parseInt(this.InitCurrentGradeJson.Labor);
            //设置工人房屋
            let buildinsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromBuildID(AIWorkerAttribute.characterInfo.occupantBuildingID);
            if (buildinsInfo)
                AIWorkerPlayer.onInitSetHouse(buildinsInfo);
            else
                AIWorkerPlayer.onUpdateFindHouse();
            //记录工人信息,这是初始化的数据，不记录到内存中
            GlobalD.game.onAddWorkerCharacterInfoToArray(AIWorkerAttribute.characterInfo, true);
        }
    },


    //全部工人属性升级
    onUpgradeAllWorker() {
        let workerArray = GlobalD.game.WorkerArray;
        for (let i = 0; i < workerArray.length; i++) {
            let AIWorkerAttribute = workerArray[i].node.getComponent("AI_worker_Attribute");
            AIWorkerAttribute.totalPhysicalStrength = parseInt(this.InitCurrentGradeJson.PhysicalStrength);
            AIWorkerAttribute.characterSpeed = parseInt(this.InitCurrentGradeJson.MoveSpeed);
            AIWorkerAttribute.labour = parseInt(this.InitCurrentGradeJson.Labor);
            // cc.log('AIWorkerAttribute',  AIWorkerAttribute.totalPhysicalStrength, AIWorkerAttribute.characterSpeed);

        }
    }
});
