| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481 |
- // Learn cc.Class:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
- // Learn Attribute:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
- var reGameStates = require('GameStates');
- cc.Class({
- extends: cc.Component,
- editor: {
- requireComponent: 'AI_worker_Animation'
- },
- properties: {
- // EndPoint: {
- // default: null,
- // type: cc.Node,
- // serializable: true,
- // },
- _path: {
- default: [],
- serializable: false,
- },
- moveDirection: {
- default: reGameStates.moveType.none,
- type: cc.Enum(reGameStates.moveType),
- serializable: false,
- },
- AIAnimation: {
- default: null,
- serializable: false,
- visible: false,
- },
- DrawNode: cc.Node,
- // //提示框
- // TipLabel: cc.Label,
- //提示字符串
- TipHousing: '没有房子',
- TipFactory: '没有工厂',
- TipWorker: '没有工作地点',
- // //提示框
- // ItemLabel: cc.Label,
- //信息弹窗
- WorkerTips: cc.Node,
- //是否开始工作
- _isStartWorking: {
- default: false,
- visible: false,
- },
- //是否结束工作
- _isEndWorking: {
- default: false,
- visible: false,
- },
- //所去的目标,比如工厂
- transTarget: {
- default: null,
- serializable: false,
- visible: false,
- },
- //需要变化的体力值
- _Strength: {
- default: 0,
- serializable: false,
- visible: false,
- },
- //正在工作的地方
- //记录当前在哪里工作
- _workPlace: {
- default: reGameStates.BuildType.Farmland,
- type: cc.Enum(reGameStates.BuildType),
- serializable: false,
- visible: false,
- },
- //是左边还是右边,默认是左边
- _workerAniLeftOrRight: { default: true, visible: false },
- _workerIndex: {
- default: 0,
- type: cc.Integer,
- serializable: false,
- visible: false,
- },
- //初始化时候添加到空闲数组中去
- // isAddWorkerArray: false,
- //记录最近的点
- recordTheNearestPoint: { default: null, visible: false },
- // 需要隐藏的节点
- HideArray: [cc.Node],
- //是不是收集状态
- _isColletion: { default: false, visible: false },
- //当前收集到的物品
- materialAlreadyCollected: { default: null, type: reGameStates.goodsMaterialClass, visible: true },
- //需要收集的目标
- _targetCollectedCrops: null,
- _targetCollectedWood: null,
- _targetCollectedMineral: null,
- //记录最后一次行走的位置
- _moveEndTiledPos: { default: null, visible: false },
- //绘制路径
- darwGraphicsPath: { default: false },
- //渲染的index
- _targetRenderingIndex: {
- default: -1,
- type: cc.Integer,
- visible: false,
- },
- //是否处于离开农田状态
- _isLeaveFarmland: { default: false, visible: false },
- //工作前的旧位置
- _workingOldPosition: { default: null, visible: false },
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- //动画脚本组件
- this.AIAnimation = this.getComponent('AI_worker_Animation');
- //AI人物属性组件
- this.AIAttribute = this.getComponent('AI_worker_Attribute');
- },
- start() {
- this.AIAnimation.switchAnimation(this.moveDirection);
- },
- //设置人物的生成点位置
- onSetSpawnStartPoiont(_startTilePos) {
- let spawnPos
- // cc.log('设置第一个点', spawnPos);
- spawnPos = this._convertPos(_startTilePos.x, _startTilePos.y);
- //设置深度检测
- GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, _startTilePos);
- this.node.setPosition(spawnPos);
- },
- //在内存初始化数据时候调用
- onInitSetHouse(targetBuildingsInfo) {
- let _housing = targetBuildingsInfo;
- //此房子设置为占用
- _housing.buildInfo.isItOccupied = true;
- _housing.buildInfo.occupantPlayerInfo = this;
- this.AIAttribute.MyHouse = _housing;
- this.AIAttribute.targetBuildingsInfo = _housing;
- this.AIAttribute.isThereAHouse = true;
- this.isFind = true;
- // cc.log('初始化人物时候设置的了房子',targetBuildingsInfo);
- this.onFindHouse();
- },
- //解雇后移除重置自己的对象
- onWorkerRemoveMyself() {
- if (this.AIAttribute.MyHouse) {
- //此房子设置为占用
- this.AIAttribute.MyHouse.buildInfo.isItOccupied = false;
- //清空房子状态
- this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSleepAnim();
- }
- //
- this._onDestroyMySelf();
- },
- //清空这个对象的父节点
- _onDestroyMySelf() {
- this.node.parent.destroy();
- },
- //拆除建筑后,重置自己的房子信息
- onDismantleBuilding() {
- // cc.log('自己的房子被强拆了!');
- this.AIAttribute.isThereAHouse = false;
- this.AIAttribute.MyHouse = null;
- this.isFind = false;
- // //显示
- // this.onHideChildrenNode(true);
- },
- //寻找工作
- onAutoFindWork() {
- // q清除一下
- this.unschedule(this.findWorkCallback);
- this.findWorkCallback = function () {
- if (!this.AIAttribute.MyHouse) {
- this.onUpdateFindHouse();
- this.unschedule(this.findWorkCallback);
- return;
- }
- //寻找工作
- //工厂
- if (this.onFindFactoryWorking()) {
- if (this.AIAttribute.MyHouse) {
- this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSleepAnim();
- }
- // 在寻找到工作后
- this.unschedule(this.findWorkCallback);
- //显示
- this.onHideChildrenNode(true);
- //可以解雇
- this.AIAttribute.isCanDismissed = false;
- }
- //材料生产地
- else if (this.onFindWorkingFromMaterial()) {
- if (this.AIAttribute.MyHouse) {
- this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSleepAnim();
- }
- // 在寻找到工作后
- this.unschedule(this.findWorkCallback);
- //显示
- this.onHideChildrenNode(true);
- //可以解雇
- this.AIAttribute.isCanDismissed = false;
- // cc.log('寻找到工作地点', this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
- }
- }
- this.schedule(this.findWorkCallback, 3);
- },
- onUpdateFindHouse() {
- // cc.log('onUpdateFindHouse');
- // 在寻找到房子后,停止调用
- this.unschedule(this.callback);
- this.isFind = false;
- this.callback = function () {
- if (this.isFind) {
- // 在寻找到房子后,停止调用
- this.unschedule(this.callback);
- this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
- }
- //寻找房子
- if (!this.AIAttribute.isThereAHouse) {
- let length = GlobalD.game.housingArray.length;
- if (length == 0) {
- // this.TipLabel.string = '没有房子';
- //可以解雇
- this.AIAttribute.isCanDismissed = true;
- this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
- return;
- } else if (length != 0) {
- for (let i = 0; i < length; i++) {
- let _housing = GlobalD.game.housingArray[i];
- let isOccupied = _housing.buildInfo.isItOccupied
- if (!isOccupied) {
- //此房子设置为占用
- _housing.buildInfo.isItOccupied = true;
- _housing.buildInfo.occupantPlayerInfo = this;
- this.AIAttribute.MyHouse = _housing;
- this.AIAttribute.targetBuildingsInfo = _housing;
- this.AIAttribute.isThereAHouse = true;
- // cc.log('寻找到房子,停止调用更新目标函数。');
- this.isFind = true;
- this.onFindHouse();
- break;
- } else {
- // this.TipLabel.string = '请建筑房子';
- this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
- // cc.log('请建筑房子');
- }
- }
- }
- }
- // this.count++;
- }
- this.schedule(this.callback, 1);
- },
- //如果工人没有房子
- _workerPlayerNotHouse() {
- //显示
- this.onHideChildrenNode(true);
- //切换站立动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
- this.moveDirection = reGameStates.moveType.moveLeft;
- this.AIAnimation.switchAnimation(this.moveDirection);
- //提示没有房子
- this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
- //没房子人士
- this.AIAttribute.isThereAHouse = false;
- this.AIAttribute.MyHouse = null;
- },
- // 回到自己房子
- onFindHouse() {
- // cc.log('回到自己房子,');
- if (this.AIAttribute.isThereAHouse) {
- this.unschedule(this.NotHouseTipCallBack);
- //可以解雇
- this.AIAttribute.isCanDismissed = true;
- this.AIAttribute.targetBuildingsInfo = this.AIAttribute.MyHouse;
- let isReach = this.onReachTheTargetOrnot(this.AIAttribute.MyHouse.buildInfo._gotoPosistion);
- if (!isReach) {
- //回到自己房子的路
- let isHasHighWay = this.onSearchPathFromPositionArray(this.AIAttribute.MyHouse.buildInfo._gotoPosistion);
- if (isHasHighWay) {
- //不设置状态
- // this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- if (this.AIAnimation.AI_Animation_Type == reGameStates.AI_Animation_Type.None
- || this.AIAnimation.AI_Animation_Type == reGameStates.AI_Animation_Type.Idle)
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
- // this.onSwitchAnimation(this.AIAttribute.targetBuildingsInfo.buildInfo);
- this.moveDirection = reGameStates.moveType.moveLeft;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
- // cc.log('当前房子');
- } else {
- //切换站立动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
- this.moveDirection = reGameStates.moveType.moveLeft;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this.NotHouseTipCallBack = function () {
- //提示没有房子
- this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim('房子没道路!');
- //没有道路,寻找房子
- this.onFindHouse();
- }
- this.schedule(this.NotHouseTipCallBack, 3);
- }
- } else {
- this.onHideChildrenNode(false);
- //不设置状态
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
- this.moveDirection = reGameStates.moveType.moveLeft;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
- //cc.log('已经在房子里面!', this.AIAttribute.physicalStrength, this.AIAttribute.totalPhysicalStrength);
- if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
- this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
- this.AIAttribute.isResting = false;
- //开始寻找工作了
- this.onAutoFindWork();
- } else {
- this._onCurrentAreaIs();
- }
- }
- } else {
- //目标已经为空(已经拆除?),
- // cc.log('房子已经拆了,');
- this._workerPlayerNotHouse();
- //如果工作时候房子被拆了,随机寻找一个点,行走
- // this.onRandomWalkToRoad();
- //启动计时器更新寻找房子目标
- this.onUpdateFindHouse();
- }
- },
- //寻找工作的地方
- //原料场地
- onFindWorkingFromMaterial() {
- //寻找农田,采木场或矿洞
- let materialArray = GlobalD.game.MaterialsArray;
- let length = materialArray.length;
- if (length != 0) {
- for (let i = 0; i < length; i++) {
- let workBuildingMaterial = materialArray[i];
- //如果没激活,跳过下面代码
- if (!workBuildingMaterial.buildInfo.isItActive) continue;
- //如果体力值不支持工作的话
- // if (workBuildingMaterial.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
- // this.onFindHouse();
- // return false;
- // } else {
- //是否占用,就是工人是否在工作
- let isOccupied = workBuildingMaterial.buildInfo.isItOccupied
- //检测是否已经生产,没有就去制造生产
- if (!isOccupied && workBuildingMaterial.buildInfo._inventory == 0) {
- let isHasHighWay = this.onSearchPathFromPositionArray(workBuildingMaterial.buildInfo._gotoPosistion);
- if (isHasHighWay) {
- //寻找到立即设置占用
- workBuildingMaterial.buildInfo.isItOccupied = true;
- workBuildingMaterial.buildInfo.occupantPlayerInfo = this;
- //cc.log('寻找到工作的地方:原料场地', workBuildingMaterial.buildInfo.startTilePos, workBuildingMaterial);
- //生产地方没有作物,工作状态
- this.AIAttribute.isWorking = true;
- // this.AIAttribute.isTransport = false;
- this.AIAttribute.targetBuildingsInfo = workBuildingMaterial;
- this.onSwitchAnimation(this.AIAttribute.targetBuildingsInfo.buildInfo);
- return true;
- }
- //如果没有路,
- return false;
- }
- // else {
- // cc.log('工作地方不够,请建筑工作的地方:', workBuildingMaterial.buildInfo.buildingName);
- // }
- // }
- }
- }
- return false;
- },
- //寻找工厂,
- onFindFactoryWorking() {
- let length = GlobalD.game.FactoryArray.length;
- if (length == 0) {
- // this.TipLabel.string = '没有工作的工厂';
- //工厂提示
- return false;
- } else if (length != 0) {
- for (let i = 0; i < length; i++) {
- let workBuilding = GlobalD.game.FactoryArray[i];
- //如果体力值不支持工作的话
- // if (workBuilding.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
- // this.onFindHouse();
- // return false;
- // } else {
- // cc.log('寻找到工作的工厂,是否设置占用', workBuilding);
- //是否占用,就是工人是否在工作
- let isOccupied = workBuilding.buildInfo.isItOccupied;
- let isStopOperation = workBuilding.buildInfo.isItStopOperation;
- //检测是否已经占用,并且是否有商品
- if (!isOccupied && !isStopOperation && workBuilding.buildInfo._goods) {
- let targetGoods = workBuilding.buildInfo._goods;
- //选中原料
- if (this._onSearchMaterialsArray(targetGoods)) {
- //寻找了到原料地方,设置工厂占用
- workBuilding.buildInfo.isItOccupied = true;
- workBuilding.buildInfo.occupantPlayerInfo = this;
- // cc.log('寻找到工作的工厂,是否设置占用', workBuilding.buildInfo.isItOccupied);
- //工作状态,收集需要的物品
- this.AIAttribute.isWorking = true;
- // this.AIAttribute.isTransport = false;
- // crops: tooltip: '农作物', wood: tooltip: '木材',mineral: tooltip: '矿石',
- this._isColletion = true;
- //记录一下目标工厂
- this.transTarget = workBuilding;
- // cc.log('开始工作?');
- this._onSearchMaterialTarget();
- return true;
- } else {
- this._isColletion = false;
- //当前是原料的状态
- // cc.log('没有过多的原料地,可以暂停销售原料');
- this._ClearCollectedItems();
- return false;
- }
- }
- // }
- }
- return false;
- }
- },
- _ClearCollectedItems() {
- this._isLeaveFarmland = false;
- if (this._targetCollectedCrops)
- this._targetCollectedCrops.buildInfo.isItOccupied = false;
- if (this._targetCollectedWood)
- this._targetCollectedWood.buildInfo.isItOccupied = false;
- if (this._targetCollectedMineral)
- this._targetCollectedMineral.buildInfo.isItOccupied = false;
- this._targetCollectedCrops = null;
- this._targetCollectedWood = null;
- this._targetCollectedMineral = null;
- this.materialAlreadyCollected.crops = 0;
- this.materialAlreadyCollected.wood = 0;
- this.materialAlreadyCollected.mineral = 0;
- },
- //选中原料的地方
- _onSearchMaterialsArray(_targetGoods) {
- //收集前先清理一下
- this._ClearCollectedItems();
- //判断需要什么原料,在去对应的场地收集
- let shopBuildings = GlobalD.game.MaterialsArray;
- let length = shopBuildings.length;
- if (length == 0) {
- // this.TipLabel.string = '没有原料地!';
- return false;
- } else {
- for (let i = 0; i < length; i++) {
- //只要有原料,不可销售,没有占用,工人就可以收集
- if (
- // !shopBuildings[i].buildInfo.isItSaleable &&
- !shopBuildings[i].buildInfo.isItOccupied &&
- 0 < shopBuildings[i].buildInfo._inventory) {
- if (reGameStates.BuildType.Farmland == shopBuildings[i].buildInfo.buildType
- && !this._targetCollectedCrops
- && _targetGoods.goodsMaterial.crops != this.materialAlreadyCollected.crops) {
- this._targetCollectedCrops = shopBuildings[i];
- //设置占用
- shopBuildings[i].buildInfo.isItOccupied = true;
- } else if (reGameStates.BuildType.TimberYard == shopBuildings[i].buildInfo.buildType
- && !this._targetCollectedWood
- && _targetGoods.goodsMaterial.wood != this.materialAlreadyCollected.wood) {
- this._targetCollectedWood = shopBuildings[i];
- //设置占用
- shopBuildings[i].buildInfo.isItOccupied = true;
- } else if (reGameStates.BuildType.MiningPit == shopBuildings[i].buildInfo.buildType
- && !this._targetCollectedMineral
- && _targetGoods.goodsMaterial.mineral != this.materialAlreadyCollected.mineral) {
- this._targetCollectedMineral = shopBuildings[i];
- //设置占用
- shopBuildings[i].buildInfo.isItOccupied = true;
- }
- }
- }
- // if (this._targetCollectedCrops && this._targetCollectedWood && this._targetCollectedMineral)
- // return true;
- // else
- // return false;
- //todo...后面缺少什么提示
- let _return = true;
- if (!this._targetCollectedCrops && _targetGoods.goodsMaterial.crops != 0)
- _return = false;
- // cc.log('没有农产品!,请建造农田!', _targetGoods.goodsMaterial.crops);
- if (!this._targetCollectedWood && _targetGoods.goodsMaterial.wood != 0)
- _return = false;
- // cc.log('没有木材!,请建造木材厂!');
- if (!this._targetCollectedMineral && _targetGoods.goodsMaterial.mineral != 0)
- _return = false;
- // cc.log('没有矿石!,请建造矿厂!');
- return _return;
- }
- },
- //返回要去的原料地方
- _onSearchMaterialTarget() {
- if (this._targetCollectedCrops == null && this._targetCollectedWood == null && this._targetCollectedMineral == null) {
- //没有东西收集了
- this._isColletion = false;
- return false;
- }
- if (this._targetCollectedCrops) {
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
- this.AIAttribute.targetBuildingsInfo = this._targetCollectedCrops;
- this._targetCollectedCrops = null;
- // cc.log('收集农作物!');
- } else if (this._targetCollectedWood) {
- this.AIAttribute.targetBuildingsInfo = this._targetCollectedWood;
- this._targetCollectedWood = null;
- // cc.log('收集木材!');
- } else if (this._targetCollectedMineral) {
- this.AIAttribute.targetBuildingsInfo = this._targetCollectedMineral;
- this._targetCollectedMineral = null;
- // cc.log('收集矿物!');
- }
- let isHasHighWay = this.onSearchPathFromPositionArray(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
- if (!isHasHighWay) {
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this.onResetFactoryState();
- this.onResetSelfState();
- if (this._isColletion)
- this._isColletion = false;
- this.onFindHouse();
- }
- return true;
- },
- //更新对应的动画
- onSwitchAnimation(buildInfo) {
- switch (buildInfo.buildType) {
- //如果是农田,采木场,矿坑,加工厂。
- case reGameStates.BuildType.Farmland:
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
- break;
- case reGameStates.BuildType.TimberYard:
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Hatchet);
- break;
- case reGameStates.BuildType.MiningPit:
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Pickaxe);
- break;
- }
- },
- //随机行走一下
- onRandomWalkToRoad() {
- //在道路上随机生成一个点
- let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
- let randomSpawn = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);
- this.onSetPath(randomSpawn);
- //行走一段距离后开始寻找房子
- setTimeout(() => {
- //启动计时器更新寻找房子目标
- this.onUpdateFindHouse();
- }, 2000);
- },
- //直接设置行走路径
- onSetPath(endPos) {
- var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
- if (startPos.sub(endPos).mag() == 0) {
- cc.log('onSetPath 起始点和终点同一个图块!');
- //回到工作的起始点后
- this._onCurrentAreaIs();
- return;
- }
- this._path = [];
- this._path.push(startPos);
- this._path.push(endPos);
- this.vmove = -1;
- this.stepindex = 1;
- this.smallstepindex = true;
- },
- //根据数组判断是否到达目标位置
- onReachTheTargetOrnot(positionArray) {
- let selfPos = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
- let length = positionArray.length;
- for (let i = 0; i < length; i++) {
- // cc.log("根据数组判断是否到达目标位置",selfPos,positionArray[i]);
- if (selfPos.sub(positionArray[i]).mag() == 0) {
- // cc.log("根据数组判断是否到达目标位置");
- return true;
- }
- }
- return false;
- },
- //根据数组寻找路径
- onSearchPathFromPositionArray(positionArray) {
- this._path = [];
- // this._isThereAWay = false;
- let isThereAWay = false;
- let length = positionArray.length;
- for (let i = 0; i < length; i++) {
- if (this.onSearchPathFromPosition(positionArray[i])) {
- // this._isThereAWay = true;
- isThereAWay = true;
- // cc.log("getPath1", this._path);
- continue;
- }
- }
- // cc.log('positionArray', positionArray, this._isThereAWay);
- return isThereAWay;
- },
- //根据位置寻找目标
- onSearchPathFromPosition(endPos) {
- var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
- if (startPos.sub(endPos).mag() == 0) {
- cc.warn('onSearchPathFromPosition 起始点和终点同一个图块!');
- // this._onCurrentAreaIs();
- return false;
- }
- let isHasPath = false;
- isHasPath = this.getPath(startPos, endPos);
- this.vmove = -1;
- this.stepindex = 1;
- this.smallstepindex = true;
- // cc.log("isHasPath", isHasPath);
- return isHasPath;
- },
- //获得寻路路径
- getPath(start, end) {
- AStar.setStart(start);
- let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
- // cc.log("getPath", getPath);
- //路径取反
- getPath.reverse();
- if (getPath.length == 0) {
- return false;
- } else {
- if (this._path.length == 0 || this._path.length > getPath.length) {
- this._path = getPath;
- // cc.log("this.recordTheNearestPoint2", this.recordTheNearestPoint, end);
- this.recordTheNearestPoint = end;
- }
- if (this.darwGraphicsPath) {
- var ctx = this.node.parent.getComponent(cc.Graphics);
- ctx.clear();
- ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
- for (let i = 0; i < this._path.length; i++) {
- let location = this._convertPos(this._path[i].x, this._path[i].y);
- if (i == 0) ctx.moveTo(location.x, location.y);
- else ctx.lineTo(location.x, location.y);
- }
- ctx.stroke();
- }
- return true;
- }
- },
- //设置工人的工作位置
- _setWorkerPosition() {
- let location = GlobalD.TiledMap._locationFromtilePos(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
- location = this.node.parent.convertToNodeSpaceAR(location);
- //Canvas 的坐标是(360,540),图块的大小是(165,96)
- //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
- let CanvasPos = GlobalD.game.Canvas.position;
- let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y);
- this.node.setPosition(endLocation);
- },
- //坐标转换
- _convertPos(tiledX, tiledY) {
- let location = GlobalD.TiledMap._locationFromtilePos(cc.v2(tiledX, tiledY));
- location = this.node.parent.convertToNodeSpaceAR(location);
- //Canvas 的坐标是(360,540),图块的大小是(165,96)
- //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
- let CanvasPos = GlobalD.game.Canvas.position;
- let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y - 48);
- return endLocation;
- },
- update(dt) {
- //恢复体力值
- if (this.AIAttribute.isResting) {
- if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
- this.AIAttribute.isResting = false;
- // cc.log('房子是空的');
- this.onFindHouse();
- } else {
- this.AIAttribute.physicalStrength += (
- this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength
- + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
- ) * dt;
- // this.TipLabel.string = Math.floor(this.AIAttribute.physicalStrength) + '';
- if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
- this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
- this.AIAttribute.isResting = false;
- // cc.log('休息完毕!');
- //开始寻找工作了
- this.onAutoFindWork();
- }
- }
- }
- //正在工作
- if (this._isStartWorking) {
- //如果正在工作的时候拆除建筑
- //如果有对象
- if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
- if (this._workingOldPosition != null)
- this.node.setPosition(this._convertPos(this._workingOldPosition));
- //目标已经为空(已经拆除?),
- this._isStartWorking = false;
- this.onResetSelfState();
- if (this._isColletion) {
- //重置工厂占用
- this._isColletion = false;
- }
- this.onFindHouse();
- return;
- }
- //消耗值是劳动力*0.5+对应房子的消耗力*0.5+特殊建筑影响力
- this._Strength -= (
- this.AIAttribute.labour * 0.5
- + Math.abs(this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength) * 0.5
- + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
- ) * dt;
- if (reGameStates.BuildType.Farmland == this._workPlace
- || reGameStates.BuildType.MiningPit == this._workPlace
- || reGameStates.BuildType.TimberYard == this._workPlace) {
- this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onChangeFromAIWorker(this._workPlace, this._Strength);
- }
- //如果在农田工作
- if (reGameStates.BuildType.Farmland == this._workPlace) {
- //设置父节点的渲染值节点
- this.node.parent.zIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex + 1;
- }
- // this.TipLabel.string = Math.floor(this._Strength) + '';
- //小于此建筑消耗的值
- if (this._Strength <= 0) {
- //减去体力值,重置工作状态
- this.AIAttribute.physicalStrength = 0;
- this.onHideChildrenNode(true);
- this._isStartWorking = false;
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- //如果在农田工作
- if (reGameStates.BuildType.Farmland == this._workPlace
- || reGameStates.BuildType.MiningPit == this._workPlace
- || reGameStates.BuildType.TimberYard == this._workPlace) {
- //设置农田的数量
- this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 5;
- GlobalD.game.onUpdatePersonInventory(this._workPlace, 5);
- //更新原料数组
- // cc.log('更新原料地:', this.AIAttribute.targetBuildingsInfo);
- GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
- // cc.log('工作完毕,返回休息。');
- this._isEndWorking = true;
- // this.onSetPath(this.recordTheNearestPoint);
- if (this._workingOldPosition != null)
- this.node.setPosition(this._convertPos(this._workingOldPosition));
- this.onFindHouse();
- } else if (reGameStates.BuildType.Factory == this._workPlace) {
- //停止工厂烟雾
- this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").stopSmokeAnim();
- if (this.transTarget) {
- let isHasHighWay = false;
- if (this.transTarget.buildInfo._targetBuildingsInfo)
- isHasHighWay = this.onSearchPathFromPositionArray(this.transTarget.buildInfo._targetBuildingsInfo.buildInfo._gotoPosistion);
- if (isHasHighWay) {
- // this.ItemLabel.string = '运送成品';
- //工作完之后运送成品去商店
- //设置工厂对应的商店
- //运送成品时候,目标点设置为 工厂最终的目标
- this.AIAttribute.targetBuildingsInfo = this.transTarget.buildInfo._targetBuildingsInfo;
- } else {
- //如果没有路,清空对象,
- // 如果商店存在
- if (this.transTarget.buildInfo._targetBuildingsInfo)
- // 清空商店的对应工厂信息
- this.transTarget.buildInfo._targetBuildingsInfo.onResetFromWorkersSuspend();
- //需要清空自身状态
- this.onResetSelfState();
- this.onFindHouse();
- }
- //不管是运送还是不运送,工厂状态提前清除
- //清除工厂对应的目标,商店等
- this.transTarget.onResetProductionRequest();
- } else {
- //需要清空自身状态
- this.onResetSelfState();
- this.onFindHouse();
- }
- }
- }
- }
- //根据路径移动
- this._MoveFromPath(dt);
- },
- _MoveFromPath(dt) {
- //寻路
- if (this._path.length != 0) {
- var herop = this.node.getPosition();
- //根据路径移动
- if (this.stepindex >= 1) {
- if (this.smallstepindex) {
- // cc.log(this._path);
- // 第一个点
- var startPosX = this._path[this.stepindex - 1].x;
- var startPosY = this._path[this.stepindex - 1].y;
- // 第二个点
- var nextPosX = this._path[this.stepindex].x;
- var nextPosY = this._path[this.stepindex].y;
- if (startPosX == nextPosX) {
- if (startPosY > nextPosY) {
- //往右移动
- this.vmove = 2;
- this.moveDirection = reGameStates.moveType.moveRight;
- } else if (startPosY < nextPosY) {
- this.vmove = 4;
- this.moveDirection = reGameStates.moveType.moveLeft;
- } else {
- // this.moveDirection = reGameStates.moveType.none;
- this.vmove = -1;
- }
- } else if (startPosY == nextPosY) {
- if (startPosX > nextPosX) {
- this.moveDirection = reGameStates.moveType.moveUp;
- this.vmove = 1;
- } else if (startPosX < nextPosX) {
- this.moveDirection = reGameStates.moveType.moveDown;
- this.vmove = 0;
- } else {
- this.moveDirection = reGameStates.moveType.none;
- this.vmove = -1;
- }
- } else {
- // this.moveDirection = reGameStates.moveType.none;
- this.vmove = -1;
- }
- this.AIAnimation.switchAnimation(this.moveDirection);
- herop = this._convertPos(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y);
- this.smallstepindex = false;
- //设置第一个点并且画线
- if (this.stepindex == 1) {
- // cc.log('this._targetRenderingIndex:', this._targetRenderingIndex);
- //设置旧的渲染值
- if (this._isLeaveFarmland && this._targetRenderingIndex != -1) {
- // if(this._targetRenderingIndex != -1)
- this.node.parent.zIndex = this._targetRenderingIndex;
- // this._isLeaveFarmland = false;
- }
- this.node.setPosition(herop);
- if (this.DrawNode && this.darwGraphicsPath)
- this.DrawNode.getComponent('Draw').DrawLineFromTarget();
- }
- }
- if (this.stepindex != -1) {
- this._moveEndTiledPos = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
- herop = this._convertPos(this._path[this.stepindex].x, this._path[this.stepindex].y);
- var oldPos = this.node.position;
- // get move direction
- var direction = herop.sub(oldPos).normalize();
- // multiply direction with distance to get new position
- var newPos = oldPos.add(direction.mul(this.AIAttribute.characterSpeed * dt));
- // set new position
- this.node.setPosition(newPos);
- }
- if (herop.sub(this.node.position).mag() <= 10) {
- this.smallstepindex = true;
- if (this.stepindex >= 1)
- this.onHideChildrenNode(true);
- // if (this._isLeaveFarmland && this.stepindex >= 1) {
- GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y));
- if (this.stepindex >= this._path.length - 1) {
- //记录最后行走的位置
- this._workingOldPosition = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
- this.stepindex = -1;
- this.vmove = -1;
- // this.moveDirection = reGameStates.moveType.none;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this._onCurrentAreaIs();
- } else {
- this.stepindex++;
- this.vmove = -1;
- }
- }
- }
- }
- },
- //判断到达的区域是什么地方
- _onCurrentAreaIs() {
- // cc.log('到达目标区域。_onCurrentAreaIs', this.AIAttribute.targetBuildingsInfo);
- //如果有对象
- if (this.AIAttribute.targetBuildingsInfo) {
- if (!this.AIAttribute.targetBuildingsInfo.node) {
- //目标已经为空(已经拆除?),
- this.onResetSelfState();
- this.onResetFactoryState();
- if (this._isColletion) {
- //重置工厂占用
- this._isColletion = false;
- }
- this.onFindHouse();
- return;
- }
- //判断是否到达目标,因为目标随时有可能会移动的
- let isReach = this.onReachTheTargetOrnot(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
- if (!isReach) {
- if (this.AIAttribute.targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.Housing) {
- //如果没有到达,清空对象,走人
- cc.log('回到房子地方,房子不在了,给提示,定时查找道路');
- this.onFindHouse();
- } else {
- //如果没有到达,清空对象,走人
- cc.log('如果没有路,清空对象,离开!');
- if (this.transTarget) {
- // 清空商店的对应工厂信息
- this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
- }
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this.AIAttribute.targetBuildingsInfo = null;
- this.onResetSelfState();
- this.onResetFactoryState();
- if (this._isColletion) {
- //重置工厂占用
- this._isColletion = false;
- }
- this.onFindHouse();
- }
- return;
- }
- } else {
- if (this.transTarget) {
- // 清空商店的对应工厂信息
- this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
- }
- this.onResetFactoryState();
- this.onResetSelfState();
- if (this._isColletion) {
- this._isColletion = false;
- }
- //如果到达区域后,目标已经为空(已经拆除?),
- this.onFindHouse();
- return;
- }
- if (this._isEndWorking) {
- this._isEndWorking = false;
- this.onFindHouse();
- return;
- }
- //如果正在工作了就不用判断在什么区域了
- if (this._isStartWorking) {
- this.onCurrrentWorkerAnimation();
- return;
- }
- let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
- // cc.log('到达目的地', this.AIAttribute.targetBuildingsInfo);
- //需要减少的体力值
- this._Strength = this.AIAttribute.physicalStrength;
- switch (buildInfo.buildType) {
- //住房
- case reGameStates.BuildType.Housing:
- if (this._moveEndTiledPos && this._moveEndTiledPos.sub(this.AIAttribute.characterInfo.startTilePos).mag() !== 0) {
- //每次重新寻找房子,都更新一下人物信息位置信息
- this.AIAttribute.characterInfo.startTilePos = this._moveEndTiledPos;
- //当前建筑的id
- // cc.log('当前建筑的id', buildInfo.id);
- this.AIAttribute.characterInfo.occupantBuildingID = buildInfo.id;
- GlobalD.game.onUpdateWorkerCharacterInfoToArray(this.AIAttribute.characterInfo);
- }
- //隐藏
- this.onHideChildrenNode(false);
- //回复体力
- this.AIAttribute.isResting = true;
- setTimeout(() => {
- if (!this.AIAttribute.isThereAHouse) return;
- //初始化时候,
- this.AIAttribute.MyHouse.node.getComponent("buildingTips").playSleepAnim();
- }, 500);
- break;
- //如果是农田,采木场,矿坑,加工厂。
- case reGameStates.BuildType.Farmland:
- if (this._isColletion) {
- this._isLeaveFarmland = true;
- this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
- //运输.goodsMaterial.crops
- //this.materialAlreadyCollected.crops = this.transTarget.buildInfo._goods.goodsMaterial.crops;
- // if (!this.transTarget.buildInfo
- // && !this.transTarget.buildInfo._goods
- // && !this.transTarget.buildInfo._goods.goodsMaterial)
- if (this.transTarget == null) {
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this._isColletion = false;
- this.onResetSelfState();
- this.onFindHouse();
- return;
- }
- let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.crops);
- //工人来收集材料的时候,根据工厂的目标来运送特定的数量
- //目前商店都是定死一个值,5,原料场地也是 5
- this.materialAlreadyCollected.crops = purchasingNum;
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
- //重置农田
- this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
- GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
- //设置目标点的销售状态
- GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
- if (!this._onSearchMaterialTarget()) {
- //如果原料都相等了。运送
- let targetBuildInfo = this.transTarget.buildInfo;
- let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
- if (isHasHighWay) {
- // 设置目标为工厂
- this.AIAttribute.targetBuildingsInfo = this.transTarget;
- } else {
- this._isColletion = false;
- this.onResetFactoryState();
- this.onResetSelfState();
- this.onFindHouse();
- }
- }
- ;
- } else {
- this._isStartWorking = true;
- //在农田工作
- this._workPlace = reGameStates.BuildType.Farmland;
- this._workerIndex = 0;
- this.onAnimationDire(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Poke);
- this.onCurrrentWorkerAnimation();
- this._setWorkerPosition();
- }
- break;
- case reGameStates.BuildType.TimberYard:
- if (this._isColletion) {
- this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
- //运输.goodsMaterial.
- // this.materialAlreadyCollected.wood = this.transTarget.buildInfo._goods.goodsMaterial.wood;
- if (this.transTarget == null) {
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this._isColletion = false;
- this.onResetSelfState();
- this.onFindHouse();
- return;
- }
- let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.wood);
- //工人来收集材料的时候,根据工厂的目标来运送特定的数量
- //目前商店都是定死一个值,5,原料场地也是 5
- this.materialAlreadyCollected.wood = purchasingNum;
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
- //重置农田
- this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
- GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
- //设置目标点的销售状态
- GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
- if (!this._onSearchMaterialTarget()) {
- cc.log('运送矿物');
- //如果原料都相等了。运送
- // 设置目标为工厂
- let targetBuildInfo = this.transTarget.buildInfo;
- let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
- if (isHasHighWay) {
- // 设置目标为工厂
- this.AIAttribute.targetBuildingsInfo = this.transTarget;
- } else {
- this._isColletion = false;
- this.onResetFactoryState();
- this.onResetSelfState();
- this.onFindHouse();
- }
- }
- ;
- } else {
- this._isStartWorking = true;
- this._workPlace = reGameStates.BuildType.TimberYard;
- this._workerAniLeftOrRight = false;
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Hatchet);
- this.onCurrrentWorkerAnimation();
- this._workerIndex = 0;
- this._setWorkerPosition();
- }
- break;
- case reGameStates.BuildType.MiningPit:
- if (this._isColletion) {
- this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
- //运输.goodsMaterial.this.transTarget.buildInfo._goods.goodsMaterial.mineral;
- if (this.transTarget == null) {
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this._isColletion = false;
- this.onResetSelfState();
- this.onFindHouse();
- return;
- }
- let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.mineral);
- //工人来收集材料的时候,根据工厂的目标来运送特定的数量
- //目前商店都是定死一个值,5,原料场地也是 5
- this.materialAlreadyCollected.mineral = purchasingNum;
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
- //重置农田
- this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
- //设置目标点的占用状态
- this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
- this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
- GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
- //设置目标点的销售状态
- GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
- if (!this._onSearchMaterialTarget()) {
- //如果原料都相等了。运送
- // 设置目标为工厂
- let targetBuildInfo = this.transTarget.buildInfo;
- let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
- if (isHasHighWay) {
- // 设置目标为工厂
- this.AIAttribute.targetBuildingsInfo = this.transTarget;
- } else {
- this._isColletion = false;
- this.onResetFactoryState();
- this.onResetSelfState();
- this.onFindHouse();
- }
- }
- ;
- } else {
- this._isStartWorking = true;
- this._workPlace = reGameStates.BuildType.MiningPit;
- this._workerIndex = 0;
- //隐藏
- this.onHideChildrenNode(false);
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Pickaxe);
- }
- break;
- case reGameStates.BuildType.Factory:
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryBox);
- //到达工厂后,收集的物品清零
- this.materialAlreadyCollected.crops = 0;
- this.materialAlreadyCollected.wood = 0;
- this.materialAlreadyCollected.mineral = 0;
- //设置工作状态
- this._isStartWorking = true;
- this.AIAttribute.isWorking = true;
- this._workPlace = reGameStates.BuildType.Factory;
- //隐藏
- this.onHideChildrenNode(false);
- //播放工厂烟
- this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSmokeAnim();
- break;
- case reGameStates.BuildType.Shop:
- //隐藏
- this.onHideChildrenNode(false);
- //到达商店后,放置成品,重置空闲状态,或者回家
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
- this.AIAttribute.isWorking = false;
- this._workPlace = reGameStates.BuildType.Shop;
- //商店设置
- this.AIAttribute.targetBuildingsInfo.onResetFromWorkersFinished();
- this.onFindHouse();
- break;
- }
- },
- //重置对工厂的状态
- onResetFactoryState() {
- //重置工厂占用
- if (this.transTarget)
- this.transTarget.buildInfo.isItOccupied = false;
- // this.AIAttribute.isTransport = false;
- //记录一下目标工厂
- this.transTarget = null;
- },
- //重新设置自身状态
- onResetSelfState() {
- //收集的物品清零
- this.materialAlreadyCollected.crops = 0;
- this.materialAlreadyCollected.wood = 0;
- this.materialAlreadyCollected.mineral = 0;
- //设置动画
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
- //工作状态重置
- this.AIAttribute.isWorking = false;
- },
- //农场
- //播放动画前判断方向
- onAnimationDire(_targetPoint) {
- //已2*2 为主,上面的点
- this._workerAniLeftOrRight = false;
- // let rightY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - this.AIAttribute.targetBuildingsInfo.buildInfo.occupyArea.y + 1;
- let leftY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y;
- if (_targetPoint.y == leftY) {
- this._workerAniLeftOrRight = true;
- }
- return this._workerAniLeftOrRight;
- },
- //农场内行走的点,只能在同一条线上
- onFarmlandMovePoint() {
- let OriginPoint = this.recordTheNearestPoint;
- let gotoPoints = this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion;
- let length = gotoPoints.length;
- for (let i = 0; i < length; i++) {
- if (this.recordTheNearestPoint.y == gotoPoints[i].y || this.recordTheNearestPoint.x == gotoPoints[i].x) {
- OriginPoint = gotoPoints[i];
- break;
- }
- }
- return OriginPoint;
- },
- onSwitchWokerStatusFromFarmland(consume) {
- let index = 50 - consume;
- if (index >= 10 && index <= 25) { //先播放左边动画
- if (this._workerIndex == 0) {
- this._workerIndex++;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this.onSetPath(this.onFarmlandMovePoint());
- }
- } else if (index > 25 && index <= 40) {
- if (this._workerIndex == 1) {
- this._workerIndex++;
- this.AIAnimation.switchAnimation(this.moveDirection);
- this.onSetPath(this.recordTheNearestPoint);
- }
- }
- },
- // 伐木场
- //播放动画前选中最近的点
- onSelectNearestPoint() {
- //已2*3 为主,上面的点
- let tilePosArray = [];
- tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y));
- tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 1));
- tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 2));
- let movePoint = tilePosArray[0];
- this._workerAniLeftOrRight = true;
- for (let i = 1; i < tilePosArray.length; i++) {
- if (this.recordTheNearestPoint.sub(movePoint).mag() >
- this.recordTheNearestPoint.sub(tilePosArray[i]).mag()) {
- if (i == tilePosArray.length - 1) {
- //如果是第三个点,动画是面向左边砍树
- this._workerAniLeftOrRight = false;
- }
- movePoint = tilePosArray[i];
- }
- }
- // cc.log('movePoint == ', movePoint);
- return movePoint;
- },
- onHideChildrenNode(isActive) {
- let length = this.HideArray.length;
- for (let i = 0; i < length; i++) {
- this.HideArray[i].active = isActive;
- }
- },
- //如果正在工作的话,就切换动画咯
- onCurrrentWorkerAnimation() {
- if (this._workerIndex == 0) {
- this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
- } else if (this._workerIndex == 1) { //先播放左边动画
- this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
- } else if (this._workerIndex == 2) {
- this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
- } else if (this._workerIndex == 3) {
- this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
- this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
- this.AIAnimation.switchAnimation(this.moveDirection);
- }
- },
- });
|