AI_worker_player.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. var reGameStates = require('GameStates');
  11. cc.Class({
  12. extends: cc.Component,
  13. editor: {
  14. requireComponent: 'AI_worker_Animation'
  15. },
  16. properties: {
  17. // EndPoint: {
  18. // default: null,
  19. // type: cc.Node,
  20. // serializable: true,
  21. // },
  22. _path: {
  23. default: [],
  24. serializable: false,
  25. },
  26. moveDirection: {
  27. default: reGameStates.moveType.none,
  28. type: cc.Enum(reGameStates.moveType),
  29. serializable: false,
  30. },
  31. AIAnimation: {
  32. default: null,
  33. serializable: false,
  34. visible: false,
  35. },
  36. DrawNode: cc.Node,
  37. // //提示框
  38. // TipLabel: cc.Label,
  39. //提示字符串
  40. TipHousing: '没有房子',
  41. TipFactory: '没有工厂',
  42. TipWorker: '没有工作地点',
  43. // //提示框
  44. // ItemLabel: cc.Label,
  45. //信息弹窗
  46. WorkerTips: cc.Node,
  47. //是否开始工作
  48. _isStartWorking: {
  49. default: false,
  50. visible: false,
  51. },
  52. //是否结束工作
  53. _isEndWorking: {
  54. default: false,
  55. visible: false,
  56. },
  57. //所去的目标,比如工厂
  58. transTarget: {
  59. default: null,
  60. serializable: false,
  61. visible: false,
  62. },
  63. //需要变化的体力值
  64. _Strength: {
  65. default: 0,
  66. serializable: false,
  67. visible: false,
  68. },
  69. //正在工作的地方
  70. //记录当前在哪里工作
  71. _workPlace: {
  72. default: reGameStates.BuildType.Farmland,
  73. type: cc.Enum(reGameStates.BuildType),
  74. serializable: false,
  75. visible: false,
  76. },
  77. //是左边还是右边,默认是左边
  78. _workerAniLeftOrRight: { default: true, visible: false },
  79. _workerIndex: {
  80. default: 0,
  81. type: cc.Integer,
  82. serializable: false,
  83. visible: false,
  84. },
  85. //初始化时候添加到空闲数组中去
  86. // isAddWorkerArray: false,
  87. //记录最近的点
  88. recordTheNearestPoint: { default: null, visible: false },
  89. // 需要隐藏的节点
  90. HideArray: [cc.Node],
  91. //是不是收集状态
  92. _isColletion: { default: false, visible: false },
  93. //当前收集到的物品
  94. materialAlreadyCollected: { default: null, type: reGameStates.goodsMaterialClass, visible: true },
  95. //需要收集的目标
  96. _targetCollectedCrops: null,
  97. _targetCollectedWood: null,
  98. _targetCollectedMineral: null,
  99. //记录最后一次行走的位置
  100. _moveEndTiledPos: { default: null, visible: false },
  101. //绘制路径
  102. darwGraphicsPath: { default: false },
  103. //渲染的index
  104. _targetRenderingIndex: {
  105. default: -1,
  106. type: cc.Integer,
  107. visible: false,
  108. },
  109. //是否处于离开农田状态
  110. _isLeaveFarmland: { default: false, visible: false },
  111. //工作前的旧位置
  112. _workingOldPosition: { default: null, visible: false },
  113. },
  114. // LIFE-CYCLE CALLBACKS:
  115. onLoad() {
  116. //动画脚本组件
  117. this.AIAnimation = this.getComponent('AI_worker_Animation');
  118. //AI人物属性组件
  119. this.AIAttribute = this.getComponent('AI_worker_Attribute');
  120. },
  121. start() {
  122. this.AIAnimation.switchAnimation(this.moveDirection);
  123. },
  124. //设置人物的生成点位置
  125. onSetSpawnStartPoiont(_startTilePos) {
  126. let spawnPos
  127. // cc.log('设置第一个点', spawnPos);
  128. spawnPos = this._convertPos(_startTilePos.x, _startTilePos.y);
  129. //设置深度检测
  130. GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, _startTilePos);
  131. this.node.setPosition(spawnPos);
  132. },
  133. //在内存初始化数据时候调用
  134. onInitSetHouse(targetBuildingsInfo) {
  135. let _housing = targetBuildingsInfo;
  136. //此房子设置为占用
  137. _housing.buildInfo.isItOccupied = true;
  138. _housing.buildInfo.occupantPlayerInfo = this;
  139. this.AIAttribute.MyHouse = _housing;
  140. this.AIAttribute.targetBuildingsInfo = _housing;
  141. this.AIAttribute.isThereAHouse = true;
  142. this.isFind = true;
  143. // cc.log('初始化人物时候设置的了房子',targetBuildingsInfo);
  144. this.onFindHouse();
  145. },
  146. //解雇后移除重置自己的对象
  147. onWorkerRemoveMyself() {
  148. if (this.AIAttribute.MyHouse) {
  149. //此房子设置为占用
  150. this.AIAttribute.MyHouse.buildInfo.isItOccupied = false;
  151. //清空房子状态
  152. this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSmokeAnim();
  153. }
  154. //
  155. this._onDestroyMySelf();
  156. },
  157. //清空这个对象的父节点
  158. _onDestroyMySelf() {
  159. this.node.parent.destroy();
  160. },
  161. //拆除建筑后,重置自己的房子信息
  162. onDismantleBuilding() {
  163. // cc.log('自己的房子被强拆了!');
  164. this.AIAttribute.isThereAHouse = false;
  165. this.AIAttribute.MyHouse = null;
  166. this.isFind = false;
  167. // //显示
  168. // this.onHideChildrenNode(true);
  169. },
  170. //寻找工作
  171. onAutoFindWork() {
  172. // q清除一下
  173. this.unschedule(this.findWorkCallback);
  174. this.findWorkCallback = function () {
  175. if (!this.AIAttribute.MyHouse) {
  176. this.onUpdateFindHouse();
  177. this.unschedule(this.findWorkCallback);
  178. return;
  179. }
  180. //寻找工作
  181. //工厂
  182. if (this.onFindFactoryWorking()) {
  183. if (this.AIAttribute.MyHouse) {
  184. this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSmokeAnim();
  185. }
  186. // 在寻找到工作后
  187. this.unschedule(this.findWorkCallback);
  188. //显示
  189. this.onHideChildrenNode(true);
  190. //可以解雇
  191. this.AIAttribute.isCanDismissed = false;
  192. }
  193. //材料生产地
  194. else if (this.onFindWorkingFromMaterial()) {
  195. if (this.AIAttribute.MyHouse) {
  196. this.AIAttribute.MyHouse.node.getComponent("buildingTips").stopSmokeAnim();
  197. }
  198. // 在寻找到工作后
  199. this.unschedule(this.findWorkCallback);
  200. //显示
  201. this.onHideChildrenNode(true);
  202. //可以解雇
  203. this.AIAttribute.isCanDismissed = false;
  204. // cc.log('寻找到工作地点', this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  205. }
  206. }
  207. this.schedule(this.findWorkCallback, 3);
  208. },
  209. onUpdateFindHouse() {
  210. // cc.log('onUpdateFindHouse');
  211. // 在寻找到房子后,停止调用
  212. this.unschedule(this.callback);
  213. this.isFind = false;
  214. this.callback = function () {
  215. if (this.isFind) {
  216. // 在寻找到房子后,停止调用
  217. this.unschedule(this.callback);
  218. this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
  219. }
  220. //寻找房子
  221. if (!this.AIAttribute.isThereAHouse) {
  222. let length = GlobalD.game.housingArray.length;
  223. if (length == 0) {
  224. // this.TipLabel.string = '没有房子';
  225. //可以解雇
  226. this.AIAttribute.isCanDismissed = true;
  227. this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
  228. return;
  229. } else if (length != 0) {
  230. for (let i = 0; i < length; i++) {
  231. let _housing = GlobalD.game.housingArray[i];
  232. let isOccupied = _housing.buildInfo.isItOccupied
  233. if (!isOccupied) {
  234. //此房子设置为占用
  235. _housing.buildInfo.isItOccupied = true;
  236. _housing.buildInfo.occupantPlayerInfo = this;
  237. this.AIAttribute.MyHouse = _housing;
  238. this.AIAttribute.targetBuildingsInfo = _housing;
  239. this.AIAttribute.isThereAHouse = true;
  240. // cc.log('寻找到房子,停止调用更新目标函数。');
  241. this.isFind = true;
  242. this.onFindHouse();
  243. break;
  244. } else {
  245. // this.TipLabel.string = '请建筑房子';
  246. this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
  247. // cc.log('请建筑房子');
  248. }
  249. }
  250. }
  251. }
  252. // this.count++;
  253. }
  254. this.schedule(this.callback, 1);
  255. },
  256. //如果工人没有房子
  257. _workerPlayerNotHouse() {
  258. //显示
  259. this.onHideChildrenNode(true);
  260. //切换站立动画
  261. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.Idle;
  262. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  263. this.moveDirection = reGameStates.moveType.moveLeft;
  264. this.AIAnimation.switchAnimation(this.moveDirection);
  265. //提示没有房子
  266. this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim(this.TipHousing);
  267. //没房子人士
  268. this.AIAttribute.isThereAHouse = false;
  269. this.AIAttribute.MyHouse = null;
  270. },
  271. // 回到自己房子
  272. onFindHouse() {
  273. // cc.log('回到自己房子,');
  274. if (this.AIAttribute.isThereAHouse) {
  275. this.unschedule(this.NotHouseTipCallBack);
  276. //可以解雇
  277. this.AIAttribute.isCanDismissed = true;
  278. this.AIAttribute.targetBuildingsInfo = this.AIAttribute.MyHouse;
  279. let isReach = this.onReachTheTargetOrnot(this.AIAttribute.MyHouse.buildInfo._gotoPosistion);
  280. if (!isReach) {
  281. //回到自己房子的路
  282. let isHasHighWay = this.onSearchPathFromPositionArray(this.AIAttribute.MyHouse.buildInfo._gotoPosistion);
  283. if (isHasHighWay) {
  284. //不设置状态
  285. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.none;
  286. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  287. this.moveDirection = reGameStates.moveType.moveLeft;
  288. this.AIAnimation.switchAnimation(this.moveDirection);
  289. this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
  290. // cc.log('当前房子');
  291. } else {
  292. //切换站立动画
  293. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.Idle;
  294. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  295. this.moveDirection = reGameStates.moveType.moveLeft;
  296. this.AIAnimation.switchAnimation(this.moveDirection);
  297. this.NotHouseTipCallBack = function () {
  298. //提示没有房子
  299. this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim('房子没道路!');
  300. //没有道路,寻找房子
  301. this.onFindHouse();
  302. }
  303. this.schedule(this.NotHouseTipCallBack, 3);
  304. }
  305. } else {
  306. this.onHideChildrenNode(false);
  307. //不设置状态
  308. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.none;
  309. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  310. this.moveDirection = reGameStates.moveType.moveLeft;
  311. this.AIAnimation.switchAnimation(this.moveDirection);
  312. this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
  313. //cc.log('已经在房子里面!', this.AIAttribute.physicalStrength, this.AIAttribute.totalPhysicalStrength);
  314. if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
  315. this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
  316. this.AIAttribute.isResting = false;
  317. //开始寻找工作了
  318. this.onAutoFindWork();
  319. } else {
  320. this._onCurrentAreaIs();
  321. }
  322. }
  323. } else {
  324. //目标已经为空(已经拆除?),
  325. // cc.log('房子已经拆了,');
  326. this._workerPlayerNotHouse();
  327. //如果工作时候房子被拆了,随机寻找一个点,行走
  328. // this.onRandomWalkToRoad();
  329. //启动计时器更新寻找房子目标
  330. this.onUpdateFindHouse();
  331. }
  332. },
  333. //寻找工作的地方
  334. //原料场地
  335. onFindWorkingFromMaterial() {
  336. //寻找农田,采木场或矿洞
  337. let materialArray = GlobalD.game.MaterialsArray;
  338. let length = materialArray.length;
  339. if (length != 0) {
  340. for (let i = 0; i < length; i++) {
  341. let workBuildingMaterial = materialArray[i];
  342. //如果没激活,跳过下面代码
  343. if (!workBuildingMaterial.buildInfo.isItActive) continue;
  344. //如果体力值不支持工作的话
  345. // if (workBuildingMaterial.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
  346. // this.onFindHouse();
  347. // return false;
  348. // } else {
  349. //是否占用,就是工人是否在工作
  350. let isOccupied = workBuildingMaterial.buildInfo.isItOccupied
  351. //检测是否已经生产,没有就去制造生产
  352. if (!isOccupied && workBuildingMaterial.buildInfo._inventory == 0) {
  353. let isHasHighWay = this.onSearchPathFromPositionArray(workBuildingMaterial.buildInfo._gotoPosistion);
  354. if (isHasHighWay) {
  355. //寻找到立即设置占用
  356. workBuildingMaterial.buildInfo.isItOccupied = true;
  357. workBuildingMaterial.buildInfo.occupantPlayerInfo = this;
  358. //cc.log('寻找到工作的地方:原料场地', workBuildingMaterial.buildInfo.startTilePos, workBuildingMaterial);
  359. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  360. //生产地方没有作物,工作状态
  361. this.AIAttribute.isWorking = true;
  362. // this.AIAttribute.isTransport = false;
  363. this.AIAttribute.targetBuildingsInfo = workBuildingMaterial;
  364. this.onSwitchAnimation(this.AIAttribute.targetBuildingsInfo.buildInfo);
  365. return true;
  366. }
  367. //如果没有路,
  368. return false;
  369. }
  370. // else {
  371. // cc.log('工作地方不够,请建筑工作的地方:', workBuildingMaterial.buildInfo.buildingName);
  372. // }
  373. // }
  374. }
  375. }
  376. return false;
  377. },
  378. //寻找工厂,
  379. onFindFactoryWorking() {
  380. let length = GlobalD.game.FactoryArray.length;
  381. if (length == 0) {
  382. // this.TipLabel.string = '没有工作的工厂';
  383. //工厂提示
  384. return false;
  385. } else if (length != 0) {
  386. for (let i = 0; i < length; i++) {
  387. let workBuilding = GlobalD.game.FactoryArray[i];
  388. //如果体力值不支持工作的话
  389. // if (workBuilding.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
  390. // this.onFindHouse();
  391. // return false;
  392. // } else {
  393. // cc.log('寻找到工作的工厂,是否设置占用', workBuilding);
  394. //是否占用,就是工人是否在工作
  395. let isOccupied = workBuilding.buildInfo.isItOccupied;
  396. let isStopOperation = workBuilding.buildInfo.isItStopOperation;
  397. //检测是否已经占用,并且是否有商品
  398. if (!isOccupied && !isStopOperation && workBuilding.buildInfo._goods) {
  399. let targetGoods = workBuilding.buildInfo._goods;
  400. //选中原料
  401. if (this._onSearchMaterialsArray(targetGoods)) {
  402. //寻找了到原料地方,设置工厂占用
  403. workBuilding.buildInfo.isItOccupied = true;
  404. workBuilding.buildInfo.occupantPlayerInfo = this;
  405. // cc.log('寻找到工作的工厂,是否设置占用', workBuilding.buildInfo.isItOccupied);
  406. //工作状态,收集需要的物品
  407. this.AIAttribute.isWorking = true;
  408. // this.AIAttribute.isTransport = false;
  409. // crops: tooltip: '农作物', wood: tooltip: '木材',mineral: tooltip: '矿石',
  410. this._isColletion = true;
  411. //记录一下目标工厂
  412. this.transTarget = workBuilding;
  413. // cc.log('开始工作?');
  414. this._onSearchMaterialTarget();
  415. return true;
  416. } else {
  417. this._isColletion = false;
  418. //当前是原料的状态
  419. // cc.log('没有过多的原料地,可以暂停销售原料');
  420. this._ClearCollectedItems();
  421. return false;
  422. }
  423. }
  424. // }
  425. }
  426. return false;
  427. }
  428. },
  429. _ClearCollectedItems() {
  430. this._isLeaveFarmland = false;
  431. if (this._targetCollectedCrops)
  432. this._targetCollectedCrops.buildInfo.isItOccupied = false;
  433. if (this._targetCollectedWood)
  434. this._targetCollectedWood.buildInfo.isItOccupied = false;
  435. if (this._targetCollectedMineral)
  436. this._targetCollectedMineral.buildInfo.isItOccupied = false;
  437. this._targetCollectedCrops = null;
  438. this._targetCollectedWood = null;
  439. this._targetCollectedMineral = null;
  440. this.materialAlreadyCollected.crops = 0;
  441. this.materialAlreadyCollected.wood = 0;
  442. this.materialAlreadyCollected.mineral = 0;
  443. },
  444. //选中原料的地方
  445. _onSearchMaterialsArray(_targetGoods) {
  446. //收集前先清理一下
  447. this._ClearCollectedItems();
  448. //判断需要什么原料,在去对应的场地收集
  449. let shopBuildings = GlobalD.game.MaterialsArray;
  450. let length = shopBuildings.length;
  451. if (length == 0) {
  452. // this.TipLabel.string = '没有原料地!';
  453. return false;
  454. } else {
  455. for (let i = 0; i < length; i++) {
  456. //只要有原料,不可销售,没有占用,工人就可以收集
  457. if (
  458. // !shopBuildings[i].buildInfo.isItSaleable &&
  459. !shopBuildings[i].buildInfo.isItOccupied &&
  460. 0 < shopBuildings[i].buildInfo._inventory) {
  461. if (reGameStates.BuildType.Farmland == shopBuildings[i].buildInfo.buildType
  462. && !this._targetCollectedCrops
  463. && _targetGoods.goodsMaterial.crops != this.materialAlreadyCollected.crops) {
  464. this._targetCollectedCrops = shopBuildings[i];
  465. //设置占用
  466. shopBuildings[i].buildInfo.isItOccupied = true;
  467. } else if (reGameStates.BuildType.TimberYard == shopBuildings[i].buildInfo.buildType
  468. && !this._targetCollectedWood
  469. && _targetGoods.goodsMaterial.wood != this.materialAlreadyCollected.wood) {
  470. this._targetCollectedWood = shopBuildings[i];
  471. //设置占用
  472. shopBuildings[i].buildInfo.isItOccupied = true;
  473. } else if (reGameStates.BuildType.MiningPit == shopBuildings[i].buildInfo.buildType
  474. && !this._targetCollectedMineral
  475. && _targetGoods.goodsMaterial.mineral != this.materialAlreadyCollected.mineral) {
  476. this._targetCollectedMineral = shopBuildings[i];
  477. //设置占用
  478. shopBuildings[i].buildInfo.isItOccupied = true;
  479. }
  480. }
  481. }
  482. // if (this._targetCollectedCrops && this._targetCollectedWood && this._targetCollectedMineral)
  483. // return true;
  484. // else
  485. // return false;
  486. //todo...后面缺少什么提示
  487. let _return = true;
  488. if (!this._targetCollectedCrops && _targetGoods.goodsMaterial.crops != 0)
  489. _return = false;
  490. // cc.log('没有农产品!,请建造农田!', _targetGoods.goodsMaterial.crops);
  491. if (!this._targetCollectedWood && _targetGoods.goodsMaterial.wood != 0)
  492. _return = false;
  493. // cc.log('没有木材!,请建造木材厂!');
  494. if (!this._targetCollectedMineral && _targetGoods.goodsMaterial.mineral != 0)
  495. _return = false;
  496. // cc.log('没有矿石!,请建造矿厂!');
  497. return _return;
  498. }
  499. },
  500. //返回要去的原料地方
  501. _onSearchMaterialTarget() {
  502. if (this._targetCollectedCrops == null && this._targetCollectedWood == null && this._targetCollectedMineral == null) {
  503. //没有东西收集了
  504. this._isColletion = false;
  505. return false;
  506. }
  507. if (this._targetCollectedCrops) {
  508. //设置动画
  509. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.CargoCarryEmpty;
  510. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  511. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  512. this.AIAttribute.targetBuildingsInfo = this._targetCollectedCrops;
  513. this._targetCollectedCrops = null;
  514. // cc.log('收集农作物!');
  515. } else if (this._targetCollectedWood) {
  516. this.AIAttribute.targetBuildingsInfo = this._targetCollectedWood;
  517. this._targetCollectedWood = null;
  518. // cc.log('收集木材!');
  519. } else if (this._targetCollectedMineral) {
  520. this.AIAttribute.targetBuildingsInfo = this._targetCollectedMineral;
  521. this._targetCollectedMineral = null;
  522. // cc.log('收集矿物!');
  523. }
  524. let isHasHighWay = this.onSearchPathFromPositionArray(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  525. if (!isHasHighWay) {
  526. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  527. this.onResetFactoryState();
  528. this.onResetSelfState();
  529. if (this._isColletion)
  530. this._isColletion = false;
  531. this.onFindHouse();
  532. }
  533. return true;
  534. },
  535. //更新对应的动画
  536. onSwitchAnimation(buildInfo) {
  537. switch (buildInfo.buildType) {
  538. //如果是农田,采木场,矿坑,加工厂。
  539. case reGameStates.BuildType.Farmland:
  540. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  541. break;
  542. case reGameStates.BuildType.TimberYard:
  543. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
  544. break;
  545. case reGameStates.BuildType.MiningPit:
  546. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
  547. break;
  548. }
  549. },
  550. //随机行走一下
  551. onRandomWalkToRoad() {
  552. //在道路上随机生成一个点
  553. let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
  554. let randomSpawn = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);
  555. this.onSetPath(randomSpawn);
  556. //行走一段距离后开始寻找房子
  557. setTimeout(() => {
  558. //启动计时器更新寻找房子目标
  559. this.onUpdateFindHouse();
  560. }, 2000);
  561. },
  562. //直接设置行走路径
  563. onSetPath(endPos) {
  564. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  565. if (startPos.sub(endPos).mag() == 0) {
  566. cc.log('onSetPath 起始点和终点同一个图块!');
  567. //回到工作的起始点后
  568. this._onCurrentAreaIs();
  569. return;
  570. }
  571. this._path = [];
  572. this._path.push(startPos);
  573. this._path.push(endPos);
  574. this.vmove = -1;
  575. this.stepindex = 1;
  576. this.smallstepindex = true;
  577. },
  578. //根据数组判断是否到达目标位置
  579. onReachTheTargetOrnot(positionArray) {
  580. let selfPos = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
  581. let length = positionArray.length;
  582. for (let i = 0; i < length; i++) {
  583. // cc.log("根据数组判断是否到达目标位置",selfPos,positionArray[i]);
  584. if (selfPos.sub(positionArray[i]).mag() == 0) {
  585. // cc.log("根据数组判断是否到达目标位置");
  586. return true;
  587. }
  588. }
  589. return false;
  590. },
  591. //根据数组寻找路径
  592. onSearchPathFromPositionArray(positionArray) {
  593. this._path = [];
  594. // this._isThereAWay = false;
  595. let isThereAWay = false;
  596. let length = positionArray.length;
  597. for (let i = 0; i < length; i++) {
  598. if (this.onSearchPathFromPosition(positionArray[i])) {
  599. // this._isThereAWay = true;
  600. isThereAWay = true;
  601. // cc.log("getPath1", this._path);
  602. continue;
  603. }
  604. }
  605. // cc.log('positionArray', positionArray, this._isThereAWay);
  606. return isThereAWay;
  607. },
  608. //根据位置寻找目标
  609. onSearchPathFromPosition(endPos) {
  610. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  611. if (startPos.sub(endPos).mag() == 0) {
  612. cc.warn('onSearchPathFromPosition 起始点和终点同一个图块!');
  613. // this._onCurrentAreaIs();
  614. return false;
  615. }
  616. let isHasPath = false;
  617. isHasPath = this.getPath(startPos, endPos);
  618. this.vmove = -1;
  619. this.stepindex = 1;
  620. this.smallstepindex = true;
  621. // cc.log("isHasPath", isHasPath);
  622. return isHasPath;
  623. },
  624. //获得寻路路径
  625. getPath(start, end) {
  626. AStar.setStart(start);
  627. let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
  628. // cc.log("getPath", getPath);
  629. //路径取反
  630. getPath.reverse();
  631. if (getPath.length == 0) {
  632. return false;
  633. } else {
  634. if (this._path.length == 0 || this._path.length > getPath.length) {
  635. this._path = getPath;
  636. // cc.log("this.recordTheNearestPoint2", this.recordTheNearestPoint, end);
  637. this.recordTheNearestPoint = end;
  638. }
  639. if (this.darwGraphicsPath) {
  640. var ctx = this.node.parent.getComponent(cc.Graphics);
  641. ctx.clear();
  642. ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
  643. for (let i = 0; i < this._path.length; i++) {
  644. let location = this._convertPos(this._path[i].x, this._path[i].y);
  645. if (i == 0) ctx.moveTo(location.x, location.y);
  646. else ctx.lineTo(location.x, location.y);
  647. }
  648. ctx.stroke();
  649. }
  650. return true;
  651. }
  652. },
  653. //设置工人的工作位置
  654. _setWorkerPosition() {
  655. let location = GlobalD.TiledMap._locationFromtilePos(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
  656. location = this.node.parent.convertToNodeSpaceAR(location);
  657. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  658. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  659. let CanvasPos = GlobalD.game.Canvas.position;
  660. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y);
  661. this.node.setPosition(endLocation);
  662. },
  663. //坐标转换
  664. _convertPos(tiledX, tiledY) {
  665. let location = GlobalD.TiledMap._locationFromtilePos(cc.v2(tiledX, tiledY));
  666. location = this.node.parent.convertToNodeSpaceAR(location);
  667. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  668. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  669. let CanvasPos = GlobalD.game.Canvas.position;
  670. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y - 48);
  671. return endLocation;
  672. },
  673. update(dt) {
  674. //恢复体力值
  675. if (this.AIAttribute.isResting) {
  676. if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
  677. this.AIAttribute.isResting = false;
  678. // cc.log('房子是空的');
  679. this.onFindHouse();
  680. } else {
  681. this.AIAttribute.physicalStrength += (
  682. this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength
  683. + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
  684. ) * dt;
  685. // this.TipLabel.string = Math.floor(this.AIAttribute.physicalStrength) + '';
  686. if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
  687. this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
  688. this.AIAttribute.isResting = false;
  689. // cc.log('休息完毕!');
  690. //开始寻找工作了
  691. this.onAutoFindWork();
  692. }
  693. }
  694. }
  695. //正在工作
  696. if (this._isStartWorking) {
  697. //如果正在工作的时候拆除建筑
  698. //如果有对象
  699. if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
  700. if (this._workingOldPosition != null)
  701. this.node.setPosition(this._convertPos(this._workingOldPosition));
  702. //目标已经为空(已经拆除?),
  703. this._isStartWorking = false;
  704. this.onResetSelfState();
  705. if (this._isColletion) {
  706. //重置工厂占用
  707. this._isColletion = false;
  708. }
  709. this.onFindHouse();
  710. return;
  711. }
  712. //消耗值是劳动力*0.5+对应房子的消耗力*0.5+特殊建筑影响力
  713. this._Strength -= (
  714. this.AIAttribute.labour * 0.5
  715. + Math.abs(this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength) * 0.5
  716. + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
  717. ) * dt;
  718. if (reGameStates.BuildType.Farmland == this._workPlace
  719. || reGameStates.BuildType.MiningPit == this._workPlace
  720. || reGameStates.BuildType.TimberYard == this._workPlace) {
  721. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onChangeFromAIWorker(this._workPlace, this._Strength);
  722. }
  723. //如果在农田工作
  724. if (reGameStates.BuildType.Farmland == this._workPlace) {
  725. //设置父节点的渲染值节点
  726. this.node.parent.zIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex + 1;
  727. }
  728. // this.TipLabel.string = Math.floor(this._Strength) + '';
  729. //小于此建筑消耗的值
  730. if (this._Strength <= 0) {
  731. //减去体力值,重置工作状态
  732. this.AIAttribute.physicalStrength = 0;
  733. this.onHideChildrenNode(true);
  734. this._isStartWorking = false;
  735. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  736. //如果在农田工作
  737. if (reGameStates.BuildType.Farmland == this._workPlace
  738. || reGameStates.BuildType.MiningPit == this._workPlace
  739. || reGameStates.BuildType.TimberYard == this._workPlace) {
  740. //设置农田的数量
  741. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 5;
  742. GlobalD.game.onUpdatePersonInventory(this._workPlace, 5);
  743. //更新原料数组
  744. // cc.log('更新原料地:', this.AIAttribute.targetBuildingsInfo);
  745. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  746. // cc.log('工作完毕,返回休息。');
  747. this._isEndWorking = true;
  748. // this.onSetPath(this.recordTheNearestPoint);
  749. if (this._workingOldPosition != null)
  750. this.node.setPosition(this._convertPos(this._workingOldPosition));
  751. this.onFindHouse();
  752. } else if (reGameStates.BuildType.Factory == this._workPlace) {
  753. if (this.transTarget) {
  754. let isHasHighWay = false;
  755. if (this.transTarget.buildInfo._targetBuildingsInfo)
  756. isHasHighWay = this.onSearchPathFromPositionArray(this.transTarget.buildInfo._targetBuildingsInfo.buildInfo._gotoPosistion);
  757. if (isHasHighWay) {
  758. // this.ItemLabel.string = '运送成品';
  759. //工作完之后运送成品去商店
  760. //设置工厂对应的商店
  761. //运送成品时候,目标点设置为 工厂最终的目标
  762. this.AIAttribute.targetBuildingsInfo = this.transTarget.buildInfo._targetBuildingsInfo;
  763. } else {
  764. //如果没有路,清空对象,
  765. // 如果商店存在
  766. if (this.transTarget.buildInfo._targetBuildingsInfo)
  767. // 清空商店的对应工厂信息
  768. this.transTarget.buildInfo._targetBuildingsInfo.onResetFromWorkersSuspend();
  769. //需要清空自身状态
  770. this.onResetSelfState();
  771. this.onFindHouse();
  772. }
  773. //不管是运送还是不运送,工厂状态提前清除
  774. //清除工厂对应的目标,商店等
  775. this.transTarget.onResetProductionRequest();
  776. } else {
  777. //需要清空自身状态
  778. this.onResetSelfState();
  779. this.onFindHouse();
  780. }
  781. }
  782. }
  783. }
  784. //根据路径移动
  785. this._MoveFromPath(dt);
  786. },
  787. _MoveFromPath(dt) {
  788. //寻路
  789. if (this._path.length != 0) {
  790. var herop = this.node.getPosition();
  791. //根据路径移动
  792. if (this.stepindex >= 1) {
  793. if (this.smallstepindex) {
  794. // cc.log(this._path);
  795. // 第一个点
  796. var startPosX = this._path[this.stepindex - 1].x;
  797. var startPosY = this._path[this.stepindex - 1].y;
  798. // 第二个点
  799. var nextPosX = this._path[this.stepindex].x;
  800. var nextPosY = this._path[this.stepindex].y;
  801. if (startPosX == nextPosX) {
  802. if (startPosY > nextPosY) {
  803. //往右移动
  804. this.vmove = 2;
  805. this.moveDirection = reGameStates.moveType.moveRight;
  806. } else if (startPosY < nextPosY) {
  807. this.vmove = 4;
  808. this.moveDirection = reGameStates.moveType.moveLeft;
  809. } else {
  810. // this.moveDirection = reGameStates.moveType.none;
  811. this.vmove = -1;
  812. }
  813. } else if (startPosY == nextPosY) {
  814. if (startPosX > nextPosX) {
  815. this.moveDirection = reGameStates.moveType.moveUp;
  816. this.vmove = 1;
  817. } else if (startPosX < nextPosX) {
  818. this.moveDirection = reGameStates.moveType.moveDown;
  819. this.vmove = 0;
  820. } else {
  821. this.moveDirection = reGameStates.moveType.none;
  822. this.vmove = -1;
  823. }
  824. } else {
  825. // this.moveDirection = reGameStates.moveType.none;
  826. this.vmove = -1;
  827. }
  828. this.AIAnimation.switchAnimation(this.moveDirection);
  829. herop = this._convertPos(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y);
  830. this.smallstepindex = false;
  831. //设置第一个点并且画线
  832. if (this.stepindex == 1) {
  833. // cc.log('this._targetRenderingIndex:', this._targetRenderingIndex);
  834. //设置旧的渲染值
  835. if (this._isLeaveFarmland && this._targetRenderingIndex != -1) {
  836. // if(this._targetRenderingIndex != -1)
  837. this.node.parent.zIndex = this._targetRenderingIndex;
  838. // this._isLeaveFarmland = false;
  839. }
  840. this.node.setPosition(herop);
  841. if (this.DrawNode && this.darwGraphicsPath)
  842. this.DrawNode.getComponent('Draw').DrawLineFromTarget();
  843. }
  844. }
  845. if (this.stepindex != -1) {
  846. this._moveEndTiledPos = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
  847. herop = this._convertPos(this._path[this.stepindex].x, this._path[this.stepindex].y);
  848. var oldPos = this.node.position;
  849. // get move direction
  850. var direction = herop.sub(oldPos).normalize();
  851. // multiply direction with distance to get new position
  852. var newPos = oldPos.add(direction.mul(this.AIAttribute.characterSpeed * dt));
  853. // set new position
  854. this.node.setPosition(newPos);
  855. }
  856. if (herop.sub(this.node.position).mag() <= 10) {
  857. this.smallstepindex = true;
  858. if (this.stepindex >= 1)
  859. this.onHideChildrenNode(true);
  860. // if (this._isLeaveFarmland && this.stepindex >= 1) {
  861. GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y));
  862. if (this.stepindex >= this._path.length - 1) {
  863. //记录最后行走的位置
  864. this._workingOldPosition = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
  865. this.stepindex = -1;
  866. this.vmove = -1;
  867. // this.moveDirection = reGameStates.moveType.none;
  868. this.AIAnimation.switchAnimation(this.moveDirection);
  869. this._onCurrentAreaIs();
  870. } else {
  871. this.stepindex++;
  872. this.vmove = -1;
  873. }
  874. }
  875. }
  876. }
  877. },
  878. //判断到达的区域是什么地方
  879. _onCurrentAreaIs() {
  880. // cc.log('到达目标区域。_onCurrentAreaIs', this.AIAttribute.targetBuildingsInfo);
  881. //如果有对象
  882. if (this.AIAttribute.targetBuildingsInfo) {
  883. if (!this.AIAttribute.targetBuildingsInfo.node) {
  884. //目标已经为空(已经拆除?),
  885. this.onResetSelfState();
  886. this.onResetFactoryState();
  887. if (this._isColletion) {
  888. //重置工厂占用
  889. this._isColletion = false;
  890. }
  891. this.onFindHouse();
  892. return;
  893. }
  894. //判断是否到达目标,因为目标随时有可能会移动的
  895. let isReach = this.onReachTheTargetOrnot(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  896. if (!isReach) {
  897. if (this.AIAttribute.targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.Housing) {
  898. //如果没有到达,清空对象,走人
  899. cc.log('回到房子地方,房子不在了,给提示,定时查找道路');
  900. this.onFindHouse();
  901. } else {
  902. //如果没有到达,清空对象,走人
  903. cc.log('如果没有路,清空对象,离开!');
  904. if (this.transTarget) {
  905. // 清空商店的对应工厂信息
  906. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
  907. }
  908. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  909. this.AIAttribute.targetBuildingsInfo = null;
  910. this.onResetSelfState();
  911. this.onResetFactoryState();
  912. if (this._isColletion) {
  913. //重置工厂占用
  914. this._isColletion = false;
  915. }
  916. this.onFindHouse();
  917. }
  918. return;
  919. }
  920. } else {
  921. if (this.transTarget) {
  922. // 清空商店的对应工厂信息
  923. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
  924. }
  925. this.onResetFactoryState();
  926. this.onResetSelfState();
  927. if (this._isColletion) {
  928. this._isColletion = false;
  929. }
  930. //如果到达区域后,目标已经为空(已经拆除?),
  931. this.onFindHouse();
  932. return;
  933. }
  934. if (this._isEndWorking) {
  935. this._isEndWorking = false;
  936. this.onFindHouse();
  937. return;
  938. }
  939. //如果正在工作了就不用判断在什么区域了
  940. if (this._isStartWorking) {
  941. this.onCurrrentWorkerAnimation();
  942. return;
  943. }
  944. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  945. // cc.log('到达目的地', this.AIAttribute.targetBuildingsInfo);
  946. //需要减少的体力值
  947. this._Strength = this.AIAttribute.physicalStrength;
  948. switch (buildInfo.buildType) {
  949. //住房
  950. case reGameStates.BuildType.Housing:
  951. if (this._moveEndTiledPos && this._moveEndTiledPos.sub(this.AIAttribute.characterInfo.startTilePos).mag() !== 0) {
  952. //每次重新寻找房子,都更新一下人物信息位置信息
  953. this.AIAttribute.characterInfo.startTilePos = this._moveEndTiledPos;
  954. //当前建筑的id
  955. // cc.log('当前建筑的id', buildInfo.id);
  956. this.AIAttribute.characterInfo.occupantBuildingID = buildInfo.id;
  957. GlobalD.game.onUpdateWorkerCharacterInfoToArray(this.AIAttribute.characterInfo);
  958. }
  959. //隐藏
  960. this.onHideChildrenNode(false);
  961. //回复体力
  962. this.AIAttribute.isResting = true;
  963. setTimeout(() => {
  964. if (!this.AIAttribute.isThereAHouse) return;
  965. //初始化时候,
  966. this.AIAttribute.MyHouse.node.getComponent("buildingTips").playSmokeAnim();
  967. }, 500);
  968. break;
  969. //如果是农田,采木场,矿坑,加工厂。
  970. case reGameStates.BuildType.Farmland:
  971. if (this._isColletion) {
  972. this._isLeaveFarmland = true;
  973. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  974. //运输.goodsMaterial.crops
  975. //this.materialAlreadyCollected.crops = this.transTarget.buildInfo._goods.goodsMaterial.crops;
  976. // if (!this.transTarget.buildInfo
  977. // && !this.transTarget.buildInfo._goods
  978. // && !this.transTarget.buildInfo._goods.goodsMaterial)
  979. if (this.transTarget == null) {
  980. //设置目标点的占用状态
  981. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  982. this._isColletion = false;
  983. this.onResetSelfState();
  984. this.onFindHouse();
  985. return;
  986. }
  987. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.crops);
  988. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  989. //目前商店都是定死一个值,5,原料场地也是 5
  990. this.materialAlreadyCollected.crops = purchasingNum;
  991. //设置动画
  992. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.CargoCarryRaw;
  993. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  994. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  995. //重置农田
  996. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  997. //设置目标点的占用状态
  998. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  999. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1000. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1001. //设置目标点的销售状态
  1002. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1003. if (!this._onSearchMaterialTarget()) {
  1004. //如果原料都相等了。运送
  1005. let targetBuildInfo = this.transTarget.buildInfo;
  1006. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1007. if (isHasHighWay) {
  1008. // 设置目标为工厂
  1009. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1010. } else {
  1011. this._isColletion = false;
  1012. this.onResetFactoryState();
  1013. this.onResetSelfState();
  1014. this.onFindHouse();
  1015. }
  1016. }
  1017. ;
  1018. } else {
  1019. this._isStartWorking = true;
  1020. //在农田工作
  1021. this._workPlace = reGameStates.BuildType.Farmland;
  1022. this._workerIndex = 0;
  1023. this.onAnimationDire(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
  1024. this.onCurrrentWorkerAnimation();
  1025. this._setWorkerPosition();
  1026. }
  1027. break;
  1028. case reGameStates.BuildType.TimberYard:
  1029. if (this._isColletion) {
  1030. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  1031. //运输.goodsMaterial.
  1032. // this.materialAlreadyCollected.wood = this.transTarget.buildInfo._goods.goodsMaterial.wood;
  1033. if (this.transTarget == null) {
  1034. //设置目标点的占用状态
  1035. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1036. this._isColletion = false;
  1037. this.onResetSelfState();
  1038. this.onFindHouse();
  1039. return;
  1040. }
  1041. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.wood);
  1042. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  1043. //目前商店都是定死一个值,5,原料场地也是 5
  1044. this.materialAlreadyCollected.wood = purchasingNum;
  1045. //设置动画
  1046. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.CargoCarry;
  1047. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  1048. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
  1049. //重置农田
  1050. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  1051. //设置目标点的占用状态
  1052. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1053. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1054. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1055. //设置目标点的销售状态
  1056. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1057. if (!this._onSearchMaterialTarget()) {
  1058. cc.log('运送矿物');
  1059. //如果原料都相等了。运送
  1060. // 设置目标为工厂
  1061. let targetBuildInfo = this.transTarget.buildInfo;
  1062. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1063. if (isHasHighWay) {
  1064. // 设置目标为工厂
  1065. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1066. } else {
  1067. this._isColletion = false;
  1068. this.onResetFactoryState();
  1069. this.onResetSelfState();
  1070. this.onFindHouse();
  1071. }
  1072. }
  1073. ;
  1074. } else {
  1075. this._isStartWorking = true;
  1076. this._workPlace = reGameStates.BuildType.TimberYard;
  1077. this._workerAniLeftOrRight = false;
  1078. this.onCurrrentWorkerAnimation();
  1079. this._workerIndex = 0;
  1080. this._setWorkerPosition();
  1081. }
  1082. break;
  1083. case reGameStates.BuildType.MiningPit:
  1084. if (this._isColletion) {
  1085. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  1086. //运输.goodsMaterial.this.transTarget.buildInfo._goods.goodsMaterial.mineral;
  1087. if (this.transTarget == null) {
  1088. //设置目标点的占用状态
  1089. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1090. this._isColletion = false;
  1091. this.onResetSelfState();
  1092. this.onFindHouse();
  1093. return;
  1094. }
  1095. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.mineral);
  1096. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  1097. //目前商店都是定死一个值,5,原料场地也是 5
  1098. this.materialAlreadyCollected.mineral = purchasingNum;
  1099. //设置动画
  1100. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.CargoCarry;
  1101. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  1102. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
  1103. //重置农田
  1104. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  1105. //设置目标点的占用状态
  1106. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1107. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1108. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1109. //设置目标点的销售状态
  1110. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1111. if (!this._onSearchMaterialTarget()) {
  1112. //如果原料都相等了。运送
  1113. // 设置目标为工厂
  1114. let targetBuildInfo = this.transTarget.buildInfo;
  1115. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1116. if (isHasHighWay) {
  1117. // 设置目标为工厂
  1118. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1119. } else {
  1120. this._isColletion = false;
  1121. this.onResetFactoryState();
  1122. this.onResetSelfState();
  1123. this.onFindHouse();
  1124. }
  1125. }
  1126. ;
  1127. } else {
  1128. this._isStartWorking = true;
  1129. this._workPlace = reGameStates.BuildType.MiningPit;
  1130. this._workerIndex = 0;
  1131. //隐藏
  1132. this.onHideChildrenNode(false);
  1133. }
  1134. break;
  1135. case reGameStates.BuildType.Factory:
  1136. //设置动画
  1137. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.CargoCarry;
  1138. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  1139. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1140. //到达工厂后,收集的物品清零
  1141. this.materialAlreadyCollected.crops = 0;
  1142. this.materialAlreadyCollected.wood = 0;
  1143. this.materialAlreadyCollected.mineral = 0;
  1144. //设置工作状态
  1145. this._isStartWorking = true;
  1146. this.AIAttribute.isWorking = true;
  1147. this._workPlace = reGameStates.BuildType.Factory;
  1148. //隐藏
  1149. this.onHideChildrenNode(false);
  1150. break;
  1151. case reGameStates.BuildType.Shop:
  1152. //隐藏
  1153. this.onHideChildrenNode(false);
  1154. //到达商店后,放置成品,重置空闲状态,或者回家
  1155. //设置动画
  1156. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.None;
  1157. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  1158. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1159. this.AIAttribute.isWorking = false;
  1160. this._workPlace = reGameStates.BuildType.Shop;
  1161. //商店设置
  1162. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersFinished();
  1163. this.onFindHouse();
  1164. break;
  1165. }
  1166. },
  1167. //重置对工厂的状态
  1168. onResetFactoryState() {
  1169. //重置工厂占用
  1170. if (this.transTarget)
  1171. this.transTarget.buildInfo.isItOccupied = false;
  1172. // this.AIAttribute.isTransport = false;
  1173. //记录一下目标工厂
  1174. this.transTarget = null;
  1175. },
  1176. //重新设置自身状态
  1177. onResetSelfState() {
  1178. //收集的物品清零
  1179. this.materialAlreadyCollected.crops = 0;
  1180. this.materialAlreadyCollected.wood = 0;
  1181. this.materialAlreadyCollected.mineral = 0;
  1182. //设置动画
  1183. this.AIAnimation.AI_Animation_Type = reGameStates.AI_Animation_Type.None;
  1184. this.AIAnimation.onSwitchWorkerState(this.AIAnimation.AI_Animation_Type);
  1185. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1186. //工作状态重置
  1187. this.AIAttribute.isWorking = false;
  1188. },
  1189. //农场
  1190. //播放动画前判断方向
  1191. onAnimationDire(_targetPoint) {
  1192. //已2*2 为主,上面的点
  1193. this._workerAniLeftOrRight = false;
  1194. // let rightY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - this.AIAttribute.targetBuildingsInfo.buildInfo.occupyArea.y + 1;
  1195. let leftY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y;
  1196. if (_targetPoint.y == leftY) {
  1197. this._workerAniLeftOrRight = true;
  1198. }
  1199. return this._workerAniLeftOrRight;
  1200. },
  1201. //农场内行走的点,只能在同一条线上
  1202. onFarmlandMovePoint() {
  1203. let OriginPoint = this.recordTheNearestPoint;
  1204. let gotoPoints = this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion;
  1205. let length = gotoPoints.length;
  1206. for (let i = 0; i < length; i++) {
  1207. if (this.recordTheNearestPoint.y == gotoPoints[i].y || this.recordTheNearestPoint.x == gotoPoints[i].x) {
  1208. OriginPoint = gotoPoints[i];
  1209. break;
  1210. }
  1211. }
  1212. return OriginPoint;
  1213. },
  1214. onSwitchWokerStatusFromFarmland(consume) {
  1215. let index = 50 - consume;
  1216. if (index >= 10 && index <= 25) { //先播放左边动画
  1217. if (this._workerIndex == 0) {
  1218. this._workerIndex++;
  1219. this.AIAnimation.switchAnimation(this.moveDirection);
  1220. this.onSetPath(this.onFarmlandMovePoint());
  1221. }
  1222. } else if (index > 25 && index <= 40) {
  1223. if (this._workerIndex == 1) {
  1224. this._workerIndex++;
  1225. this.AIAnimation.switchAnimation(this.moveDirection);
  1226. this.onSetPath(this.recordTheNearestPoint);
  1227. }
  1228. }
  1229. },
  1230. // 伐木场
  1231. //播放动画前选中最近的点
  1232. onSelectNearestPoint() {
  1233. //已2*3 为主,上面的点
  1234. let tilePosArray = [];
  1235. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y));
  1236. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 1));
  1237. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 2));
  1238. let movePoint = tilePosArray[0];
  1239. this._workerAniLeftOrRight = true;
  1240. for (let i = 1; i < tilePosArray.length; i++) {
  1241. if (this.recordTheNearestPoint.sub(movePoint).mag() >
  1242. this.recordTheNearestPoint.sub(tilePosArray[i]).mag()) {
  1243. if (i == tilePosArray.length - 1) {
  1244. //如果是第三个点,动画是面向左边砍树
  1245. this._workerAniLeftOrRight = false;
  1246. }
  1247. movePoint = tilePosArray[i];
  1248. }
  1249. }
  1250. // cc.log('movePoint == ', movePoint);
  1251. return movePoint;
  1252. },
  1253. onHideChildrenNode(isActive) {
  1254. let length = this.HideArray.length;
  1255. for (let i = 0; i < length; i++) {
  1256. this.HideArray[i].active = isActive;
  1257. }
  1258. },
  1259. //如果正在工作的话,就切换动画咯
  1260. onCurrrentWorkerAnimation() {
  1261. if (this._workerIndex == 0) {
  1262. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1263. } else if (this._workerIndex == 1) { //先播放左边动画
  1264. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1265. } else if (this._workerIndex == 2) {
  1266. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1267. } else if (this._workerIndex == 3) {
  1268. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  1269. this.AIAnimation.switchAnimation(this.moveDirection);
  1270. }
  1271. },
  1272. });