AI_worker_player.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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").stopSleepAnim();
  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").stopSleepAnim();
  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").stopSleepAnim();
  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.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  262. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  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.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  286. if (this.AIAnimation.AI_Animation_Type == reGameStates.AI_Animation_Type.None
  287. || this.AIAnimation.AI_Animation_Type == reGameStates.AI_Animation_Type.Idle)
  288. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
  289. // this.onSwitchAnimation(this.AIAttribute.targetBuildingsInfo.buildInfo);
  290. this.moveDirection = reGameStates.moveType.moveLeft;
  291. this.AIAnimation.switchAnimation(this.moveDirection);
  292. this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
  293. // cc.log('当前房子');
  294. } else {
  295. //切换站立动画
  296. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  297. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  298. this.moveDirection = reGameStates.moveType.moveLeft;
  299. this.AIAnimation.switchAnimation(this.moveDirection);
  300. this.NotHouseTipCallBack = function () {
  301. //提示没有房子
  302. this.WorkerTips.getComponent("WorkerTips").playNoHouseInfoAnim('房子没道路!');
  303. //没有道路,寻找房子
  304. this.onFindHouse();
  305. }
  306. this.schedule(this.NotHouseTipCallBack, 3);
  307. }
  308. } else {
  309. this.onHideChildrenNode(false);
  310. //不设置状态
  311. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  312. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  313. this.moveDirection = reGameStates.moveType.moveLeft;
  314. this.AIAnimation.switchAnimation(this.moveDirection);
  315. this.WorkerTips.getComponent("WorkerTips").stopNoHouseInfoAnim();
  316. //cc.log('已经在房子里面!', this.AIAttribute.physicalStrength, this.AIAttribute.totalPhysicalStrength);
  317. if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
  318. this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
  319. this.AIAttribute.isResting = false;
  320. //开始寻找工作了
  321. this.onAutoFindWork();
  322. } else {
  323. this._onCurrentAreaIs();
  324. }
  325. }
  326. } else {
  327. //目标已经为空(已经拆除?),
  328. // cc.log('房子已经拆了,');
  329. this._workerPlayerNotHouse();
  330. //如果工作时候房子被拆了,随机寻找一个点,行走
  331. // this.onRandomWalkToRoad();
  332. //启动计时器更新寻找房子目标
  333. this.onUpdateFindHouse();
  334. }
  335. },
  336. //寻找工作的地方
  337. //原料场地
  338. onFindWorkingFromMaterial() {
  339. //寻找农田,采木场或矿洞
  340. let materialArray = GlobalD.game.MaterialsArray;
  341. let length = materialArray.length;
  342. if (length != 0) {
  343. for (let i = 0; i < length; i++) {
  344. let workBuildingMaterial = materialArray[i];
  345. //如果没激活,跳过下面代码
  346. if (!workBuildingMaterial.buildInfo.isItActive) continue;
  347. //如果体力值不支持工作的话
  348. // if (workBuildingMaterial.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
  349. // this.onFindHouse();
  350. // return false;
  351. // } else {
  352. //是否占用,就是工人是否在工作
  353. let isOccupied = workBuildingMaterial.buildInfo.isItOccupied
  354. //检测是否已经生产,没有就去制造生产
  355. if (!isOccupied && workBuildingMaterial.buildInfo._inventory == 0) {
  356. let isHasHighWay = this.onSearchPathFromPositionArray(workBuildingMaterial.buildInfo._gotoPosistion);
  357. if (isHasHighWay) {
  358. //寻找到立即设置占用
  359. workBuildingMaterial.buildInfo.isItOccupied = true;
  360. workBuildingMaterial.buildInfo.occupantPlayerInfo = this;
  361. //cc.log('寻找到工作的地方:原料场地', workBuildingMaterial.buildInfo.startTilePos, workBuildingMaterial);
  362. //生产地方没有作物,工作状态
  363. this.AIAttribute.isWorking = true;
  364. // this.AIAttribute.isTransport = false;
  365. this.AIAttribute.targetBuildingsInfo = workBuildingMaterial;
  366. this.onSwitchAnimation(this.AIAttribute.targetBuildingsInfo.buildInfo);
  367. return true;
  368. }
  369. //如果没有路,
  370. return false;
  371. }
  372. // else {
  373. // cc.log('工作地方不够,请建筑工作的地方:', workBuildingMaterial.buildInfo.buildingName);
  374. // }
  375. // }
  376. }
  377. }
  378. return false;
  379. },
  380. //寻找工厂,
  381. onFindFactoryWorking() {
  382. let length = GlobalD.game.FactoryArray.length;
  383. if (length == 0) {
  384. // this.TipLabel.string = '没有工作的工厂';
  385. //工厂提示
  386. return false;
  387. } else if (length != 0) {
  388. for (let i = 0; i < length; i++) {
  389. let workBuilding = GlobalD.game.FactoryArray[i];
  390. //如果体力值不支持工作的话
  391. // if (workBuilding.buildInfo.totalConsumption > this.AIAttribute.physicalStrength) {
  392. // this.onFindHouse();
  393. // return false;
  394. // } else {
  395. // cc.log('寻找到工作的工厂,是否设置占用', workBuilding);
  396. //是否占用,就是工人是否在工作
  397. let isOccupied = workBuilding.buildInfo.isItOccupied;
  398. let isStopOperation = workBuilding.buildInfo.isItStopOperation;
  399. //检测是否已经占用,并且是否有商品
  400. if (!isOccupied && !isStopOperation && workBuilding.buildInfo._goods) {
  401. let targetGoods = workBuilding.buildInfo._goods;
  402. //选中原料
  403. if (this._onSearchMaterialsArray(targetGoods)) {
  404. //寻找了到原料地方,设置工厂占用
  405. workBuilding.buildInfo.isItOccupied = true;
  406. workBuilding.buildInfo.occupantPlayerInfo = this;
  407. // cc.log('寻找到工作的工厂,是否设置占用', workBuilding.buildInfo.isItOccupied);
  408. //工作状态,收集需要的物品
  409. this.AIAttribute.isWorking = true;
  410. // this.AIAttribute.isTransport = false;
  411. // crops: tooltip: '农作物', wood: tooltip: '木材',mineral: tooltip: '矿石',
  412. this._isColletion = true;
  413. //记录一下目标工厂
  414. this.transTarget = workBuilding;
  415. // cc.log('开始工作?');
  416. this._onSearchMaterialTarget();
  417. return true;
  418. } else {
  419. this._isColletion = false;
  420. //当前是原料的状态
  421. // cc.log('没有过多的原料地,可以暂停销售原料');
  422. this._ClearCollectedItems();
  423. return false;
  424. }
  425. }
  426. // }
  427. }
  428. return false;
  429. }
  430. },
  431. _ClearCollectedItems() {
  432. this._isLeaveFarmland = false;
  433. if (this._targetCollectedCrops)
  434. this._targetCollectedCrops.buildInfo.isItOccupied = false;
  435. if (this._targetCollectedWood)
  436. this._targetCollectedWood.buildInfo.isItOccupied = false;
  437. if (this._targetCollectedMineral)
  438. this._targetCollectedMineral.buildInfo.isItOccupied = false;
  439. this._targetCollectedCrops = null;
  440. this._targetCollectedWood = null;
  441. this._targetCollectedMineral = null;
  442. this.materialAlreadyCollected.crops = 0;
  443. this.materialAlreadyCollected.wood = 0;
  444. this.materialAlreadyCollected.mineral = 0;
  445. },
  446. //选中原料的地方
  447. _onSearchMaterialsArray(_targetGoods) {
  448. //收集前先清理一下
  449. this._ClearCollectedItems();
  450. //判断需要什么原料,在去对应的场地收集
  451. let shopBuildings = GlobalD.game.MaterialsArray;
  452. let length = shopBuildings.length;
  453. if (length == 0) {
  454. // this.TipLabel.string = '没有原料地!';
  455. return false;
  456. } else {
  457. for (let i = 0; i < length; i++) {
  458. //只要有原料,不可销售,没有占用,工人就可以收集
  459. if (
  460. // !shopBuildings[i].buildInfo.isItSaleable &&
  461. !shopBuildings[i].buildInfo.isItOccupied &&
  462. 0 < shopBuildings[i].buildInfo._inventory) {
  463. if (reGameStates.BuildType.Farmland == shopBuildings[i].buildInfo.buildType
  464. && !this._targetCollectedCrops
  465. && _targetGoods.goodsMaterial.crops != this.materialAlreadyCollected.crops) {
  466. this._targetCollectedCrops = shopBuildings[i];
  467. //设置占用
  468. shopBuildings[i].buildInfo.isItOccupied = true;
  469. } else if (reGameStates.BuildType.TimberYard == shopBuildings[i].buildInfo.buildType
  470. && !this._targetCollectedWood
  471. && _targetGoods.goodsMaterial.wood != this.materialAlreadyCollected.wood) {
  472. this._targetCollectedWood = shopBuildings[i];
  473. //设置占用
  474. shopBuildings[i].buildInfo.isItOccupied = true;
  475. } else if (reGameStates.BuildType.MiningPit == shopBuildings[i].buildInfo.buildType
  476. && !this._targetCollectedMineral
  477. && _targetGoods.goodsMaterial.mineral != this.materialAlreadyCollected.mineral) {
  478. this._targetCollectedMineral = shopBuildings[i];
  479. //设置占用
  480. shopBuildings[i].buildInfo.isItOccupied = true;
  481. }
  482. }
  483. }
  484. // if (this._targetCollectedCrops && this._targetCollectedWood && this._targetCollectedMineral)
  485. // return true;
  486. // else
  487. // return false;
  488. //todo...后面缺少什么提示
  489. let _return = true;
  490. if (!this._targetCollectedCrops && _targetGoods.goodsMaterial.crops != 0)
  491. _return = false;
  492. // cc.log('没有农产品!,请建造农田!', _targetGoods.goodsMaterial.crops);
  493. if (!this._targetCollectedWood && _targetGoods.goodsMaterial.wood != 0)
  494. _return = false;
  495. // cc.log('没有木材!,请建造木材厂!');
  496. if (!this._targetCollectedMineral && _targetGoods.goodsMaterial.mineral != 0)
  497. _return = false;
  498. // cc.log('没有矿石!,请建造矿厂!');
  499. return _return;
  500. }
  501. },
  502. //返回要去的原料地方
  503. _onSearchMaterialTarget() {
  504. if (this._targetCollectedCrops == null && this._targetCollectedWood == null && this._targetCollectedMineral == null) {
  505. //没有东西收集了
  506. this._isColletion = false;
  507. return false;
  508. }
  509. if (this._targetCollectedCrops) {
  510. //设置动画
  511. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  512. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
  513. this.AIAttribute.targetBuildingsInfo = this._targetCollectedCrops;
  514. this._targetCollectedCrops = null;
  515. // cc.log('收集农作物!');
  516. } else if (this._targetCollectedWood) {
  517. this.AIAttribute.targetBuildingsInfo = this._targetCollectedWood;
  518. this._targetCollectedWood = null;
  519. // cc.log('收集木材!');
  520. } else if (this._targetCollectedMineral) {
  521. this.AIAttribute.targetBuildingsInfo = this._targetCollectedMineral;
  522. this._targetCollectedMineral = null;
  523. // cc.log('收集矿物!');
  524. }
  525. let isHasHighWay = this.onSearchPathFromPositionArray(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  526. if (!isHasHighWay) {
  527. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  528. this.onResetFactoryState();
  529. this.onResetSelfState();
  530. if (this._isColletion)
  531. this._isColletion = false;
  532. this.onFindHouse();
  533. }
  534. return true;
  535. },
  536. //更新对应的动画
  537. onSwitchAnimation(buildInfo) {
  538. switch (buildInfo.buildType) {
  539. //如果是农田,采木场,矿坑,加工厂。
  540. case reGameStates.BuildType.Farmland:
  541. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  542. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
  543. break;
  544. case reGameStates.BuildType.TimberYard:
  545. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
  546. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Hatchet);
  547. break;
  548. case reGameStates.BuildType.MiningPit:
  549. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
  550. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Pickaxe);
  551. break;
  552. }
  553. },
  554. //随机行走一下
  555. onRandomWalkToRoad() {
  556. //在道路上随机生成一个点
  557. let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
  558. let randomSpawn = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);
  559. this.onSetPath(randomSpawn);
  560. //行走一段距离后开始寻找房子
  561. setTimeout(() => {
  562. //启动计时器更新寻找房子目标
  563. this.onUpdateFindHouse();
  564. }, 2000);
  565. },
  566. //直接设置行走路径
  567. onSetPath(endPos) {
  568. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  569. if (startPos.sub(endPos).mag() == 0) {
  570. cc.log('onSetPath 起始点和终点同一个图块!');
  571. //回到工作的起始点后
  572. this._onCurrentAreaIs();
  573. return;
  574. }
  575. this._path = [];
  576. this._path.push(startPos);
  577. this._path.push(endPos);
  578. this.vmove = -1;
  579. this.stepindex = 1;
  580. this.smallstepindex = true;
  581. },
  582. //根据数组判断是否到达目标位置
  583. onReachTheTargetOrnot(positionArray) {
  584. let selfPos = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
  585. let length = positionArray.length;
  586. for (let i = 0; i < length; i++) {
  587. // cc.log("根据数组判断是否到达目标位置",selfPos,positionArray[i]);
  588. if (selfPos.sub(positionArray[i]).mag() == 0) {
  589. // cc.log("根据数组判断是否到达目标位置");
  590. return true;
  591. }
  592. }
  593. return false;
  594. },
  595. //根据数组寻找路径
  596. onSearchPathFromPositionArray(positionArray) {
  597. this._path = [];
  598. // this._isThereAWay = false;
  599. let isThereAWay = false;
  600. let length = positionArray.length;
  601. for (let i = 0; i < length; i++) {
  602. if (this.onSearchPathFromPosition(positionArray[i])) {
  603. // this._isThereAWay = true;
  604. isThereAWay = true;
  605. // cc.log("getPath1", this._path);
  606. continue;
  607. }
  608. }
  609. // cc.log('positionArray', positionArray, this._isThereAWay);
  610. return isThereAWay;
  611. },
  612. //根据位置寻找目标
  613. onSearchPathFromPosition(endPos) {
  614. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  615. if (startPos.sub(endPos).mag() == 0) {
  616. cc.warn('onSearchPathFromPosition 起始点和终点同一个图块!');
  617. // this._onCurrentAreaIs();
  618. return false;
  619. }
  620. let isHasPath = false;
  621. isHasPath = this.getPath(startPos, endPos);
  622. this.vmove = -1;
  623. this.stepindex = 1;
  624. this.smallstepindex = true;
  625. // cc.log("isHasPath", isHasPath);
  626. return isHasPath;
  627. },
  628. //获得寻路路径
  629. getPath(start, end) {
  630. AStar.setStart(start);
  631. let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
  632. // cc.log("getPath", getPath);
  633. //路径取反
  634. getPath.reverse();
  635. if (getPath.length == 0) {
  636. return false;
  637. } else {
  638. if (this._path.length == 0 || this._path.length > getPath.length) {
  639. this._path = getPath;
  640. // cc.log("this.recordTheNearestPoint2", this.recordTheNearestPoint, end);
  641. this.recordTheNearestPoint = end;
  642. }
  643. if (this.darwGraphicsPath) {
  644. var ctx = this.node.parent.getComponent(cc.Graphics);
  645. ctx.clear();
  646. ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
  647. for (let i = 0; i < this._path.length; i++) {
  648. let location = this._convertPos(this._path[i].x, this._path[i].y);
  649. if (i == 0) ctx.moveTo(location.x, location.y);
  650. else ctx.lineTo(location.x, location.y);
  651. }
  652. ctx.stroke();
  653. }
  654. return true;
  655. }
  656. },
  657. //设置工人的工作位置
  658. _setWorkerPosition() {
  659. let location = GlobalD.TiledMap._locationFromtilePos(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
  660. location = this.node.parent.convertToNodeSpaceAR(location);
  661. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  662. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  663. let CanvasPos = GlobalD.game.Canvas.position;
  664. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y);
  665. this.node.setPosition(endLocation);
  666. },
  667. //坐标转换
  668. _convertPos(tiledX, tiledY) {
  669. let location = GlobalD.TiledMap._locationFromtilePos(cc.v2(tiledX, tiledY));
  670. location = this.node.parent.convertToNodeSpaceAR(location);
  671. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  672. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  673. let CanvasPos = GlobalD.game.Canvas.position;
  674. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y - 48);
  675. return endLocation;
  676. },
  677. update(dt) {
  678. //恢复体力值
  679. if (this.AIAttribute.isResting) {
  680. if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
  681. this.AIAttribute.isResting = false;
  682. // cc.log('房子是空的');
  683. this.onFindHouse();
  684. } else {
  685. this.AIAttribute.physicalStrength += (
  686. this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength
  687. + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
  688. ) * dt;
  689. // this.TipLabel.string = Math.floor(this.AIAttribute.physicalStrength) + '';
  690. if (this.AIAttribute.physicalStrength >= this.AIAttribute.totalPhysicalStrength) {
  691. this.AIAttribute.physicalStrength = this.AIAttribute.totalPhysicalStrength;
  692. this.AIAttribute.isResting = false;
  693. // cc.log('休息完毕!');
  694. //开始寻找工作了
  695. this.onAutoFindWork();
  696. }
  697. }
  698. }
  699. //正在工作
  700. if (this._isStartWorking) {
  701. //如果正在工作的时候拆除建筑
  702. //如果有对象
  703. if (this.AIAttribute.targetBuildingsInfo && !this.AIAttribute.targetBuildingsInfo.node) {
  704. if (this._workingOldPosition != null)
  705. this.node.setPosition(this._convertPos(this._workingOldPosition));
  706. //目标已经为空(已经拆除?),
  707. this._isStartWorking = false;
  708. this.onResetSelfState();
  709. if (this._isColletion) {
  710. //重置工厂占用
  711. this._isColletion = false;
  712. }
  713. this.onFindHouse();
  714. return;
  715. }
  716. //消耗值是劳动力*0.5+对应房子的消耗力*0.5+特殊建筑影响力
  717. this._Strength -= (
  718. this.AIAttribute.labour * 0.5
  719. + Math.abs(this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrength) * 0.5
  720. + this.AIAttribute.targetBuildingsInfo.buildInfo.consumeStrengthAddValue
  721. ) * dt;
  722. if (reGameStates.BuildType.Farmland == this._workPlace
  723. || reGameStates.BuildType.MiningPit == this._workPlace
  724. || reGameStates.BuildType.TimberYard == this._workPlace) {
  725. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onChangeFromAIWorker(this._workPlace, this._Strength);
  726. }
  727. //如果在农田工作
  728. if (reGameStates.BuildType.Farmland == this._workPlace) {
  729. //设置父节点的渲染值节点
  730. this.node.parent.zIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex + 1;
  731. }
  732. // this.TipLabel.string = Math.floor(this._Strength) + '';
  733. //小于此建筑消耗的值
  734. if (this._Strength <= 0) {
  735. //减去体力值,重置工作状态
  736. this.AIAttribute.physicalStrength = 0;
  737. this.onHideChildrenNode(true);
  738. this._isStartWorking = false;
  739. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  740. //如果在农田工作
  741. if (reGameStates.BuildType.Farmland == this._workPlace
  742. || reGameStates.BuildType.MiningPit == this._workPlace
  743. || reGameStates.BuildType.TimberYard == this._workPlace) {
  744. //设置农田的数量
  745. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 5;
  746. GlobalD.game.onUpdatePersonInventory(this._workPlace, 5);
  747. //更新原料数组
  748. // cc.log('更新原料地:', this.AIAttribute.targetBuildingsInfo);
  749. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  750. // cc.log('工作完毕,返回休息。');
  751. this._isEndWorking = true;
  752. // this.onSetPath(this.recordTheNearestPoint);
  753. if (this._workingOldPosition != null)
  754. this.node.setPosition(this._convertPos(this._workingOldPosition));
  755. this.onFindHouse();
  756. } else if (reGameStates.BuildType.Factory == this._workPlace) {
  757. //停止工厂烟雾
  758. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").stopSmokeAnim();
  759. if (this.transTarget) {
  760. let isHasHighWay = false;
  761. if (this.transTarget.buildInfo._targetBuildingsInfo)
  762. isHasHighWay = this.onSearchPathFromPositionArray(this.transTarget.buildInfo._targetBuildingsInfo.buildInfo._gotoPosistion);
  763. if (isHasHighWay) {
  764. // this.ItemLabel.string = '运送成品';
  765. //工作完之后运送成品去商店
  766. //设置工厂对应的商店
  767. //运送成品时候,目标点设置为 工厂最终的目标
  768. this.AIAttribute.targetBuildingsInfo = this.transTarget.buildInfo._targetBuildingsInfo;
  769. } else {
  770. //如果没有路,清空对象,
  771. // 如果商店存在
  772. if (this.transTarget.buildInfo._targetBuildingsInfo)
  773. // 清空商店的对应工厂信息
  774. this.transTarget.buildInfo._targetBuildingsInfo.onResetFromWorkersSuspend();
  775. //需要清空自身状态
  776. this.onResetSelfState();
  777. this.onFindHouse();
  778. }
  779. //不管是运送还是不运送,工厂状态提前清除
  780. //清除工厂对应的目标,商店等
  781. this.transTarget.onResetProductionRequest();
  782. } else {
  783. //需要清空自身状态
  784. this.onResetSelfState();
  785. this.onFindHouse();
  786. }
  787. }
  788. }
  789. }
  790. //根据路径移动
  791. this._MoveFromPath(dt);
  792. },
  793. _MoveFromPath(dt) {
  794. //寻路
  795. if (this._path.length != 0) {
  796. var herop = this.node.getPosition();
  797. //根据路径移动
  798. if (this.stepindex >= 1) {
  799. if (this.smallstepindex) {
  800. // cc.log(this._path);
  801. // 第一个点
  802. var startPosX = this._path[this.stepindex - 1].x;
  803. var startPosY = this._path[this.stepindex - 1].y;
  804. // 第二个点
  805. var nextPosX = this._path[this.stepindex].x;
  806. var nextPosY = this._path[this.stepindex].y;
  807. if (startPosX == nextPosX) {
  808. if (startPosY > nextPosY) {
  809. //往右移动
  810. this.vmove = 2;
  811. this.moveDirection = reGameStates.moveType.moveRight;
  812. } else if (startPosY < nextPosY) {
  813. this.vmove = 4;
  814. this.moveDirection = reGameStates.moveType.moveLeft;
  815. } else {
  816. // this.moveDirection = reGameStates.moveType.none;
  817. this.vmove = -1;
  818. }
  819. } else if (startPosY == nextPosY) {
  820. if (startPosX > nextPosX) {
  821. this.moveDirection = reGameStates.moveType.moveUp;
  822. this.vmove = 1;
  823. } else if (startPosX < nextPosX) {
  824. this.moveDirection = reGameStates.moveType.moveDown;
  825. this.vmove = 0;
  826. } else {
  827. this.moveDirection = reGameStates.moveType.none;
  828. this.vmove = -1;
  829. }
  830. } else {
  831. // this.moveDirection = reGameStates.moveType.none;
  832. this.vmove = -1;
  833. }
  834. this.AIAnimation.switchAnimation(this.moveDirection);
  835. herop = this._convertPos(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y);
  836. this.smallstepindex = false;
  837. //设置第一个点并且画线
  838. if (this.stepindex == 1) {
  839. // cc.log('this._targetRenderingIndex:', this._targetRenderingIndex);
  840. //设置旧的渲染值
  841. if (this._isLeaveFarmland && this._targetRenderingIndex != -1) {
  842. // if(this._targetRenderingIndex != -1)
  843. this.node.parent.zIndex = this._targetRenderingIndex;
  844. // this._isLeaveFarmland = false;
  845. }
  846. this.node.setPosition(herop);
  847. if (this.DrawNode && this.darwGraphicsPath)
  848. this.DrawNode.getComponent('Draw').DrawLineFromTarget();
  849. }
  850. }
  851. if (this.stepindex != -1) {
  852. this._moveEndTiledPos = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
  853. herop = this._convertPos(this._path[this.stepindex].x, this._path[this.stepindex].y);
  854. var oldPos = this.node.position;
  855. // get move direction
  856. var direction = herop.sub(oldPos).normalize();
  857. // multiply direction with distance to get new position
  858. var newPos = oldPos.add(direction.mul(this.AIAttribute.characterSpeed * dt));
  859. // set new position
  860. this.node.setPosition(newPos);
  861. }
  862. if (herop.sub(this.node.position).mag() <= 10) {
  863. this.smallstepindex = true;
  864. if (this.stepindex >= 1)
  865. this.onHideChildrenNode(true);
  866. // if (this._isLeaveFarmland && this.stepindex >= 1) {
  867. GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y));
  868. if (this.stepindex >= this._path.length - 1) {
  869. //记录最后行走的位置
  870. this._workingOldPosition = cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y);
  871. this.stepindex = -1;
  872. this.vmove = -1;
  873. // this.moveDirection = reGameStates.moveType.none;
  874. this.AIAnimation.switchAnimation(this.moveDirection);
  875. this._onCurrentAreaIs();
  876. } else {
  877. this.stepindex++;
  878. this.vmove = -1;
  879. }
  880. }
  881. }
  882. }
  883. },
  884. //判断到达的区域是什么地方
  885. _onCurrentAreaIs() {
  886. // cc.log('到达目标区域。_onCurrentAreaIs', this.AIAttribute.targetBuildingsInfo);
  887. //如果有对象
  888. if (this.AIAttribute.targetBuildingsInfo) {
  889. if (!this.AIAttribute.targetBuildingsInfo.node) {
  890. //目标已经为空(已经拆除?),
  891. this.onResetSelfState();
  892. this.onResetFactoryState();
  893. if (this._isColletion) {
  894. //重置工厂占用
  895. this._isColletion = false;
  896. }
  897. this.onFindHouse();
  898. return;
  899. }
  900. //判断是否到达目标,因为目标随时有可能会移动的
  901. let isReach = this.onReachTheTargetOrnot(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  902. if (!isReach) {
  903. if (this.AIAttribute.targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.Housing) {
  904. //如果没有到达,清空对象,走人
  905. cc.log('回到房子地方,房子不在了,给提示,定时查找道路');
  906. this.onFindHouse();
  907. } else {
  908. //如果没有到达,清空对象,走人
  909. cc.log('如果没有路,清空对象,离开!');
  910. if (this.transTarget) {
  911. // 清空商店的对应工厂信息
  912. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
  913. }
  914. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  915. this.AIAttribute.targetBuildingsInfo = null;
  916. this.onResetSelfState();
  917. this.onResetFactoryState();
  918. if (this._isColletion) {
  919. //重置工厂占用
  920. this._isColletion = false;
  921. }
  922. this.onFindHouse();
  923. }
  924. return;
  925. }
  926. } else {
  927. if (this.transTarget) {
  928. // 清空商店的对应工厂信息
  929. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersSuspend();
  930. }
  931. this.onResetFactoryState();
  932. this.onResetSelfState();
  933. if (this._isColletion) {
  934. this._isColletion = false;
  935. }
  936. //如果到达区域后,目标已经为空(已经拆除?),
  937. this.onFindHouse();
  938. return;
  939. }
  940. if (this._isEndWorking) {
  941. this._isEndWorking = false;
  942. this.onFindHouse();
  943. return;
  944. }
  945. //如果正在工作了就不用判断在什么区域了
  946. if (this._isStartWorking) {
  947. this.onCurrrentWorkerAnimation();
  948. return;
  949. }
  950. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  951. // cc.log('到达目的地', this.AIAttribute.targetBuildingsInfo);
  952. //需要减少的体力值
  953. this._Strength = this.AIAttribute.physicalStrength;
  954. switch (buildInfo.buildType) {
  955. //住房
  956. case reGameStates.BuildType.Housing:
  957. if (this._moveEndTiledPos && this._moveEndTiledPos.sub(this.AIAttribute.characterInfo.startTilePos).mag() !== 0) {
  958. //每次重新寻找房子,都更新一下人物信息位置信息
  959. this.AIAttribute.characterInfo.startTilePos = this._moveEndTiledPos;
  960. //当前建筑的id
  961. // cc.log('当前建筑的id', buildInfo.id);
  962. this.AIAttribute.characterInfo.occupantBuildingID = buildInfo.id;
  963. GlobalD.game.onUpdateWorkerCharacterInfoToArray(this.AIAttribute.characterInfo);
  964. }
  965. //隐藏
  966. this.onHideChildrenNode(false);
  967. //回复体力
  968. this.AIAttribute.isResting = true;
  969. setTimeout(() => {
  970. if (!this.AIAttribute.isThereAHouse) return;
  971. //初始化时候,
  972. this.AIAttribute.MyHouse.node.getComponent("buildingTips").playSleepAnim();
  973. }, 500);
  974. break;
  975. //如果是农田,采木场,矿坑,加工厂。
  976. case reGameStates.BuildType.Farmland:
  977. if (this._isColletion) {
  978. this._isLeaveFarmland = true;
  979. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  980. //运输.goodsMaterial.crops
  981. //this.materialAlreadyCollected.crops = this.transTarget.buildInfo._goods.goodsMaterial.crops;
  982. // if (!this.transTarget.buildInfo
  983. // && !this.transTarget.buildInfo._goods
  984. // && !this.transTarget.buildInfo._goods.goodsMaterial)
  985. if (this.transTarget == null) {
  986. //设置目标点的占用状态
  987. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  988. this._isColletion = false;
  989. this.onResetSelfState();
  990. this.onFindHouse();
  991. return;
  992. }
  993. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.crops);
  994. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  995. //目前商店都是定死一个值,5,原料场地也是 5
  996. this.materialAlreadyCollected.crops = purchasingNum;
  997. //设置动画
  998. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  999. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
  1000. //重置农田
  1001. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  1002. //设置目标点的占用状态
  1003. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1004. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1005. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1006. //设置目标点的销售状态
  1007. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1008. if (!this._onSearchMaterialTarget()) {
  1009. //如果原料都相等了。运送
  1010. let targetBuildInfo = this.transTarget.buildInfo;
  1011. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1012. if (isHasHighWay) {
  1013. // 设置目标为工厂
  1014. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1015. } else {
  1016. this._isColletion = false;
  1017. this.onResetFactoryState();
  1018. this.onResetSelfState();
  1019. this.onFindHouse();
  1020. }
  1021. }
  1022. ;
  1023. } else {
  1024. this._isStartWorking = true;
  1025. //在农田工作
  1026. this._workPlace = reGameStates.BuildType.Farmland;
  1027. this._workerIndex = 0;
  1028. this.onAnimationDire(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos);
  1029. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Rube);
  1030. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Poke);
  1031. this.onCurrrentWorkerAnimation();
  1032. this._setWorkerPosition();
  1033. }
  1034. break;
  1035. case reGameStates.BuildType.TimberYard:
  1036. if (this._isColletion) {
  1037. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  1038. //运输.goodsMaterial.
  1039. // this.materialAlreadyCollected.wood = this.transTarget.buildInfo._goods.goodsMaterial.wood;
  1040. if (this.transTarget == null) {
  1041. //设置目标点的占用状态
  1042. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1043. this._isColletion = false;
  1044. this.onResetSelfState();
  1045. this.onFindHouse();
  1046. return;
  1047. }
  1048. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.wood);
  1049. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  1050. //目前商店都是定死一个值,5,原料场地也是 5
  1051. this.materialAlreadyCollected.wood = purchasingNum;
  1052. //设置动画
  1053. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1054. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
  1055. //重置农田
  1056. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  1057. //设置目标点的占用状态
  1058. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1059. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1060. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1061. //设置目标点的销售状态
  1062. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1063. if (!this._onSearchMaterialTarget()) {
  1064. cc.log('运送矿物');
  1065. //如果原料都相等了。运送
  1066. // 设置目标为工厂
  1067. let targetBuildInfo = this.transTarget.buildInfo;
  1068. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1069. if (isHasHighWay) {
  1070. // 设置目标为工厂
  1071. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1072. } else {
  1073. this._isColletion = false;
  1074. this.onResetFactoryState();
  1075. this.onResetSelfState();
  1076. this.onFindHouse();
  1077. }
  1078. }
  1079. ;
  1080. } else {
  1081. this._isStartWorking = true;
  1082. this._workPlace = reGameStates.BuildType.TimberYard;
  1083. this._workerAniLeftOrRight = false;
  1084. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Lumberjack);
  1085. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Hatchet);
  1086. this.onCurrrentWorkerAnimation();
  1087. this._workerIndex = 0;
  1088. this._setWorkerPosition();
  1089. }
  1090. break;
  1091. case reGameStates.BuildType.MiningPit:
  1092. if (this._isColletion) {
  1093. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  1094. //运输.goodsMaterial.this.transTarget.buildInfo._goods.goodsMaterial.mineral;
  1095. if (this.transTarget == null) {
  1096. //设置目标点的占用状态
  1097. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1098. this._isColletion = false;
  1099. this.onResetSelfState();
  1100. this.onFindHouse();
  1101. return;
  1102. }
  1103. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.transTarget.buildInfo._goods.goodsMaterial.mineral);
  1104. //工人来收集材料的时候,根据工厂的目标来运送特定的数量
  1105. //目前商店都是定死一个值,5,原料场地也是 5
  1106. this.materialAlreadyCollected.mineral = purchasingNum;
  1107. //设置动画
  1108. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1109. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryRaw);
  1110. //重置农田
  1111. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  1112. //设置目标点的占用状态
  1113. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  1114. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  1115. GlobalD.game.onUpdatePersonInventory(this.AIAttribute.targetBuildingsInfo.buildInfo.buildType, -purchasingNum);
  1116. //设置目标点的销售状态
  1117. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo);
  1118. if (!this._onSearchMaterialTarget()) {
  1119. //如果原料都相等了。运送
  1120. // 设置目标为工厂
  1121. let targetBuildInfo = this.transTarget.buildInfo;
  1122. let isHasHighWay = this.onSearchPathFromPositionArray(targetBuildInfo._gotoPosistion);
  1123. if (isHasHighWay) {
  1124. // 设置目标为工厂
  1125. this.AIAttribute.targetBuildingsInfo = this.transTarget;
  1126. } else {
  1127. this._isColletion = false;
  1128. this.onResetFactoryState();
  1129. this.onResetSelfState();
  1130. this.onFindHouse();
  1131. }
  1132. }
  1133. ;
  1134. } else {
  1135. this._isStartWorking = true;
  1136. this._workPlace = reGameStates.BuildType.MiningPit;
  1137. this._workerIndex = 0;
  1138. //隐藏
  1139. this.onHideChildrenNode(false);
  1140. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Miner);
  1141. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Pickaxe);
  1142. }
  1143. break;
  1144. case reGameStates.BuildType.Factory:
  1145. //设置动画
  1146. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.CarryGay);
  1147. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.CargoCarryBox);
  1148. //到达工厂后,收集的物品清零
  1149. this.materialAlreadyCollected.crops = 0;
  1150. this.materialAlreadyCollected.wood = 0;
  1151. this.materialAlreadyCollected.mineral = 0;
  1152. //设置工作状态
  1153. this._isStartWorking = true;
  1154. this.AIAttribute.isWorking = true;
  1155. this._workPlace = reGameStates.BuildType.Factory;
  1156. //隐藏
  1157. this.onHideChildrenNode(false);
  1158. //播放工厂烟
  1159. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSmokeAnim();
  1160. break;
  1161. case reGameStates.BuildType.Shop:
  1162. //隐藏
  1163. this.onHideChildrenNode(false);
  1164. //到达商店后,放置成品,重置空闲状态,或者回家
  1165. //设置动画
  1166. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  1167. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Walk);
  1168. this.AIAttribute.isWorking = false;
  1169. this._workPlace = reGameStates.BuildType.Shop;
  1170. //商店设置
  1171. this.AIAttribute.targetBuildingsInfo.onResetFromWorkersFinished();
  1172. this.onFindHouse();
  1173. break;
  1174. }
  1175. },
  1176. //重置对工厂的状态
  1177. onResetFactoryState() {
  1178. //重置工厂占用
  1179. if (this.transTarget)
  1180. this.transTarget.buildInfo.isItOccupied = false;
  1181. // this.AIAttribute.isTransport = false;
  1182. //记录一下目标工厂
  1183. this.transTarget = null;
  1184. },
  1185. //重新设置自身状态
  1186. onResetSelfState() {
  1187. //收集的物品清零
  1188. this.materialAlreadyCollected.crops = 0;
  1189. this.materialAlreadyCollected.wood = 0;
  1190. this.materialAlreadyCollected.mineral = 0;
  1191. //设置动画
  1192. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  1193. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  1194. //工作状态重置
  1195. this.AIAttribute.isWorking = false;
  1196. },
  1197. //农场
  1198. //播放动画前判断方向
  1199. onAnimationDire(_targetPoint) {
  1200. //已2*2 为主,上面的点
  1201. this._workerAniLeftOrRight = false;
  1202. // let rightY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - this.AIAttribute.targetBuildingsInfo.buildInfo.occupyArea.y + 1;
  1203. let leftY = this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y;
  1204. if (_targetPoint.y == leftY) {
  1205. this._workerAniLeftOrRight = true;
  1206. }
  1207. return this._workerAniLeftOrRight;
  1208. },
  1209. //农场内行走的点,只能在同一条线上
  1210. onFarmlandMovePoint() {
  1211. let OriginPoint = this.recordTheNearestPoint;
  1212. let gotoPoints = this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion;
  1213. let length = gotoPoints.length;
  1214. for (let i = 0; i < length; i++) {
  1215. if (this.recordTheNearestPoint.y == gotoPoints[i].y || this.recordTheNearestPoint.x == gotoPoints[i].x) {
  1216. OriginPoint = gotoPoints[i];
  1217. break;
  1218. }
  1219. }
  1220. return OriginPoint;
  1221. },
  1222. onSwitchWokerStatusFromFarmland(consume) {
  1223. let index = 50 - consume;
  1224. if (index >= 10 && index <= 25) { //先播放左边动画
  1225. if (this._workerIndex == 0) {
  1226. this._workerIndex++;
  1227. this.AIAnimation.switchAnimation(this.moveDirection);
  1228. this.onSetPath(this.onFarmlandMovePoint());
  1229. }
  1230. } else if (index > 25 && index <= 40) {
  1231. if (this._workerIndex == 1) {
  1232. this._workerIndex++;
  1233. this.AIAnimation.switchAnimation(this.moveDirection);
  1234. this.onSetPath(this.recordTheNearestPoint);
  1235. }
  1236. }
  1237. },
  1238. // 伐木场
  1239. //播放动画前选中最近的点
  1240. onSelectNearestPoint() {
  1241. //已2*3 为主,上面的点
  1242. let tilePosArray = [];
  1243. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y));
  1244. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 1));
  1245. tilePosArray.push(cc.v2(this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.x - 1, this.AIAttribute.targetBuildingsInfo.buildInfo.startTilePos.y - 2));
  1246. let movePoint = tilePosArray[0];
  1247. this._workerAniLeftOrRight = true;
  1248. for (let i = 1; i < tilePosArray.length; i++) {
  1249. if (this.recordTheNearestPoint.sub(movePoint).mag() >
  1250. this.recordTheNearestPoint.sub(tilePosArray[i]).mag()) {
  1251. if (i == tilePosArray.length - 1) {
  1252. //如果是第三个点,动画是面向左边砍树
  1253. this._workerAniLeftOrRight = false;
  1254. }
  1255. movePoint = tilePosArray[i];
  1256. }
  1257. }
  1258. // cc.log('movePoint == ', movePoint);
  1259. return movePoint;
  1260. },
  1261. onHideChildrenNode(isActive) {
  1262. let length = this.HideArray.length;
  1263. for (let i = 0; i < length; i++) {
  1264. this.HideArray[i].active = isActive;
  1265. }
  1266. },
  1267. //如果正在工作的话,就切换动画咯
  1268. onCurrrentWorkerAnimation() {
  1269. if (this._workerIndex == 0) {
  1270. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1271. } else if (this._workerIndex == 1) { //先播放左边动画
  1272. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1273. } else if (this._workerIndex == 2) {
  1274. this.AIAnimation.playWorkerAnimation(this._workerAniLeftOrRight);
  1275. } else if (this._workerIndex == 3) {
  1276. this.AIAnimation.onSwitchWorker(reGameStates.AI_Worker_Type.Nothing);
  1277. this.AIAnimation.onSwitchWorkerState(reGameStates.AI_Animation_Type.Idle);
  1278. this.AIAnimation.switchAnimation(this.moveDirection);
  1279. }
  1280. },
  1281. });