AI_tourist_player.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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_tourist_Animation'
  15. },
  16. properties: {
  17. _path: {
  18. default: [],
  19. serializable: false,
  20. },
  21. moveDirection: {
  22. default: reGameStates.moveType.none,
  23. type: cc.Enum(reGameStates.moveType),
  24. serializable: false,
  25. },
  26. AIAnimation: {
  27. default: null,
  28. serializable: false,
  29. visible: false,
  30. },
  31. DrawNode: cc.Node,
  32. // //提示框
  33. // TipLabel: cc.Label,
  34. //信息弹窗
  35. TouristTips: cc.Node,
  36. followSpeed: {
  37. default: 100,
  38. serializable: false,
  39. visible: false,
  40. },
  41. //记录当前在哪里
  42. _workPlace: {
  43. default: null,
  44. type: cc.Enum(reGameStates.BuildType),
  45. serializable: false,
  46. visible: false,
  47. },
  48. //渲染的index
  49. _targetRenderingIndex: {
  50. default: 0,
  51. type: cc.Integer,
  52. visible: false,
  53. },
  54. //如果目标到达目的地
  55. //到达终点
  56. _walkEndTiledTile: {
  57. default: null,
  58. visible: false,
  59. },
  60. // //是否存在道路
  61. // _isThereAWay: {
  62. // default: false,
  63. // visible: false,
  64. // },
  65. //是否删除自己
  66. _isDestroySelf: {
  67. default: false,
  68. visible: false,
  69. },
  70. //绘制路径
  71. darwGraphicsPath: { default: false },
  72. //进入过的商店
  73. shopsHaveEntered: {
  74. default: [],
  75. type: cc.Integer,
  76. visible: false,
  77. },
  78. //是否去商店
  79. isGoToStore: { default: false, visible: false },
  80. //分配的目标商店
  81. distributionOfTargetStores: null,
  82. //每个商人最多寻找的次数,超过这个值,直接走人
  83. upperLimitOfSearchTimes: {
  84. default: 0,
  85. type: cc.Integer,
  86. visible: false,
  87. serializable: false,
  88. },
  89. //回退旧的位置
  90. _moveOldPos: {
  91. default: null,
  92. visible: false,
  93. serializable: false
  94. },
  95. //当前是否回到旧的位置
  96. _isGotoBack: false,
  97. // 需要隐藏的节点
  98. HideArray: [cc.Node],
  99. },
  100. // LIFE-CYCLE CALLBACKS:
  101. onLoad() {
  102. //动画脚本组件
  103. this.AIAnimation = this.getComponent('AI_tourist_Animation');
  104. //AI人物属性组件
  105. this.AIAttribute = this.getComponent('AI_tourist_Attribute');
  106. this.buytag = false;
  107. },
  108. start() {
  109. this.followSpeed = this.AIAttribute.characterSpeed;
  110. this.AIAnimation.switchAnimation(this.moveDirection);
  111. },
  112. //先寻找第一个商店
  113. _gotoFirstStore() {
  114. // cc.log('this.distributionOfTargetStores',this.distributionOfTargetStores.buildInfo.id);
  115. //去第一个默认商店
  116. if (this.distributionOfTargetStores && this.distributionOfTargetStores.buildInfo) {
  117. if (this.distributionOfTargetStores.buildInfo._inventory <= 0) {
  118. if (!this.distributionOfTargetStores.buildInfo.isItStopOperation) {
  119. this.distributionOfTargetStores.onSetTipSellOut();
  120. // cc.log('通知商店售罄!',this.distributionOfTargetStores.buildInfo.id);
  121. }
  122. //随机寻找下一个商店。
  123. this._GetStoreFromRandom();
  124. return;
  125. }
  126. //查看商店有没有道路
  127. let isHasHighWay = this.onSearchPathFromPositionArray(this.distributionOfTargetStores.buildInfo._gotoPosistion);
  128. //如果游客去的首个商店有道路并且没停运的话
  129. if (isHasHighWay && this.distributionOfTargetStores.buildInfo.isItSaleable) {
  130. this.AIAttribute.targetBuildingsInfo = this.distributionOfTargetStores;
  131. this._isDestroySelf = false;
  132. } else {
  133. //随机寻找下一个商店。
  134. this._GetStoreFromRandom();
  135. }
  136. } else {
  137. //如果第一商店为Null
  138. //随机寻找下一个商店。
  139. this._GetStoreFromRandom();
  140. }
  141. },
  142. _GetStoreFromRandom() {
  143. // cc.log('离开', this.upperLimitOfSearchTimes);
  144. //寻找的次数过多
  145. if (this.upperLimitOfSearchTimes >= 3) {
  146. //没有商店额话。直接离开
  147. this._onLeaveWalking();
  148. return;
  149. }
  150. this.upperLimitOfSearchTimes++;
  151. let shopBuildings = GlobalD.game.shopBuildingSalesArray;
  152. if (shopBuildings == null) {
  153. return;
  154. }
  155. let _length = shopBuildings.length;
  156. if (_length == 0) {
  157. //没有商店额话。直接离开
  158. this._onLeaveWalking();
  159. } else {
  160. //随机一个商店的下标
  161. let _randomIndex = Math.floor(Math.random() * _length);
  162. let isHasHighWay = this.onSearchPathFromPositionArray(shopBuildings[_randomIndex].buildInfo._gotoPosistion);
  163. //如果没有道路到达,是旧的对象,或者停运了。继续寻找商店
  164. if (
  165. !isHasHighWay
  166. || !shopBuildings[_randomIndex].buildInfo.isItSaleable
  167. || shopBuildings[_randomIndex].buildInfo._inventory <= 0
  168. || (this.AIAttribute.targetBuildingsInfo && this.AIAttribute.targetBuildingsInfo.buildInfo.id === shopBuildings[_randomIndex].buildInfo.id)
  169. ) {
  170. this._GetStoreFromRandom();//如果随机到是旧的商店,则重新寻找
  171. return;
  172. }
  173. this._isDestroySelf = false;
  174. //如果寻找到,设置为target目标
  175. this.AIAttribute.targetBuildingsInfo = shopBuildings[_randomIndex];
  176. //如果身上物品还没满
  177. if (this.AIAttribute.quantityOfGoodsPurchased < this.AIAttribute.totalQuantityOfGoodsPurchased) {
  178. //剩余的购买力
  179. if (this.AIAttribute.totalQuantityOfGoodsPurchased - this.AIAttribute.quantityOfGoodsPurchased < this.AIAttribute.purchasingPower)
  180. this.AIAttribute.purchasingPower = this.AIAttribute.totalQuantityOfGoodsPurchased - this.AIAttribute.quantityOfGoodsPurchased;
  181. } else {
  182. //超过购买量后
  183. this._onLeaveWalking();
  184. }
  185. }
  186. },
  187. //选中销售的地方
  188. onSearchWorkBuilding() {
  189. let shopBuildings = GlobalD.game.MaterialsArray;
  190. if (shopBuildings == null) {
  191. return;
  192. }
  193. let length = shopBuildings.length;
  194. if (length == 0) {
  195. // this.TipLabel.string = '没有原料地!';
  196. return false;
  197. } else {
  198. for (let i = 0; i < length; i++) {
  199. //在激活的状态下,只要有原料,并且可以销售,没有占用,游客就可以买
  200. if (shopBuildings[i].buildInfo.isItActive &&
  201. shopBuildings[i].buildInfo.isItSaleable &&
  202. !shopBuildings[i].buildInfo.isItOccupied &&
  203. 0 < shopBuildings[i].buildInfo._inventory) {
  204. //如果身上物品已满,就返回
  205. if (this.AIAttribute.quantityOfGoodsPurchased >= this.AIAttribute.totalQuantityOfGoodsPurchased) {
  206. cc.log('可购买的商品已满!totalQuantityOfGoodsPurchased', this.AIAttribute.totalQuantityOfGoodsPurchased);
  207. return false;
  208. }
  209. //剩余的购买力
  210. if (this.AIAttribute.totalQuantityOfGoodsPurchased - this.AIAttribute.quantityOfGoodsPurchased < this.AIAttribute.purchasingPower)
  211. this.AIAttribute.purchasingPower = this.AIAttribute.totalQuantityOfGoodsPurchased - this.AIAttribute.quantityOfGoodsPurchased;
  212. let isHasHighWay = this.onSearchPathFromPositionArray(shopBuildings[i].buildInfo._gotoPosistion);
  213. if (isHasHighWay) {
  214. //设置占用
  215. shopBuildings[i].buildInfo.isItOccupied = true;
  216. this.AIAttribute.targetBuildingsInfo = shopBuildings[i];
  217. return true;
  218. } else {
  219. //如果没有路,清空对象,
  220. // cc.log('如果没有路,离开!');
  221. this._onLeaveWalking();
  222. return false;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. },
  229. onUpdateGotoTarget() {
  230. //延迟更新目标
  231. setTimeout(() => {
  232. if (this.isGoToStore) {
  233. this._gotoFirstStore();
  234. } else {
  235. this._isDestroySelf = !this.onSearchWorkBuilding();
  236. }
  237. }, 1000);
  238. },
  239. //从生成点走到终点图块
  240. onGotoEndTiledTile(_startTilePos, _endTilePos) {
  241. // console.log(_startTilePos, _endTilePos);
  242. this.getPath(_startTilePos, _endTilePos);
  243. this.vmove = -1;
  244. this.stepindex = 1;
  245. this.smallstepindex = true;
  246. //记录终点值
  247. this._walkEndTiledTile = _endTilePos;
  248. this._isDestroySelf = true;
  249. this.onUpdateGotoTarget();
  250. },
  251. _onLeaveWalking() {
  252. this._isDestroySelf = true;
  253. this._path = [];
  254. if (this._walkEndTiledTile) {
  255. let isHas = this.onSearchPathFromPosition(this._walkEndTiledTile);
  256. if (!isHas) {
  257. cc.log('_onLeaveWalking离开时候没有道路,直接删除。', isHas);
  258. this.onDisappearsOnMaps();
  259. }
  260. }
  261. else {
  262. cc.log('this._walkEndTiledTile 为空!');
  263. }
  264. },
  265. //根据数组判断是否到达目标位置
  266. onReachTheTargetOrnot(positionArray) {
  267. let selfPos = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
  268. let length = positionArray.length;
  269. for (let i = 0; i < length; i++) {
  270. // cc.log("根据数组判断是否到达目标位置",selfPos,positionArray[i]);
  271. if (selfPos.sub(positionArray[i]).mag() == 0) {
  272. // cc.log("根据数组判断是否到达目标位置");
  273. return true;
  274. }
  275. }
  276. return false;
  277. },
  278. //根据数组寻找路径
  279. onSearchPathFromPositionArray(positionArray) {
  280. this._path = [];
  281. // this._isThereAWay = false;
  282. let isThereAWay = false;
  283. let length = positionArray.length;
  284. for (let i = 0; i < length; i++) {
  285. if (this.onSearchPathFromPosition(positionArray[i])) {
  286. // this._isThereAWay = true;
  287. isThereAWay = true;
  288. continue;
  289. }
  290. }
  291. return isThereAWay;
  292. },
  293. //根据位置寻找目标
  294. onSearchPathFromPosition(endPos) {
  295. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  296. if (startPos.sub(endPos).mag() == 0) {
  297. cc.warn('起始点和终点同一个图块!', startPos, endPos);
  298. // this._moveToOldPos();
  299. // this.onCurrentAreaIs();
  300. return false;
  301. }
  302. // cc.log('ai位置', startPos, endPos)
  303. let isHasPath = false;
  304. isHasPath = this.getPath(startPos, endPos);
  305. this.vmove = -1;
  306. this.stepindex = 1;
  307. this.smallstepindex = true;
  308. if (this._path.length >= 2)
  309. this._moveOldPos = this._path[this._path.length - 2];
  310. return isHasPath;
  311. },
  312. //往回走一段距离
  313. _moveToOldPos() {
  314. //一定要设置为空
  315. this._path = [];
  316. let _startTilePos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  317. let _endTilePos;
  318. if (this._moveOldPos) {
  319. _endTilePos = this._moveOldPos;
  320. } else {
  321. let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
  322. _endTilePos = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);
  323. }
  324. // cc.log('_endTilePos:', this.node.getPosition());
  325. let isHas = this.getPath(_startTilePos, _endTilePos);
  326. this.vmove = -1;
  327. this.stepindex = 1;
  328. this.smallstepindex = true;
  329. this._isGotoBack = true;
  330. this._isDestroySelf = false;
  331. if (!isHas) {
  332. cc.log('_moveToOldPos离开时候没有道路,直接删除。', isHas);
  333. this.onDisappearsOnMaps();
  334. }
  335. },
  336. getPath(start, end) {
  337. // AStar.setInitInfo(this.allowDiagonals, this.diagonalCost, this.dotTiebreaker, this.badSorting);
  338. AStar.setStart(start);
  339. let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
  340. // cc.log("getPath", getPath);
  341. //路径取反
  342. getPath.reverse();
  343. if (getPath.length == 0) {
  344. return false;
  345. } else {
  346. if (this._path.length == 0 || this._path.length > getPath.length) {
  347. this._path = getPath;
  348. // cc.log("this.recordTheNearestPoint2", this.recordTheNearestPoint, end);
  349. this.recordTheNearestPoint = end;
  350. }
  351. if (this.darwGraphicsPath) {
  352. var ctx = this.node.parent.getComponent(cc.Graphics);
  353. ctx.clear();
  354. ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
  355. for (let i = 0; i < this._path.length; i++) {
  356. let location = this.convertPos(this._path[i].x, this._path[i].y);
  357. if (i == 0) ctx.moveTo(location.x, location.y);
  358. else ctx.lineTo(location.x, location.y);
  359. }
  360. ctx.stroke();
  361. }
  362. return true;
  363. }
  364. },
  365. convertPos(tiledX, tiledY) {
  366. let location = GlobalD.TiledMap._locationFromtilePos(cc.v2(tiledX, tiledY));
  367. location = this.node.parent.convertToNodeSpaceAR(location);
  368. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  369. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  370. let CanvasPos = GlobalD.game.Canvas.position;
  371. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y - 48);
  372. return endLocation;
  373. },
  374. update(dt) {
  375. //根据路径移动
  376. this.MoveFromPath(dt);
  377. },
  378. MoveFromPath(dt) {
  379. //寻路
  380. if (this._path.length != 0) {
  381. var herop = this.node.getPosition();
  382. //根据路径移动
  383. if (this.stepindex >= 1) {
  384. if (this.smallstepindex) {
  385. // 第一个点
  386. var startPosX = this._path[this.stepindex - 1].x;
  387. var startPosY = this._path[this.stepindex - 1].y;
  388. // 第二个点
  389. var nextPosX = this._path[this.stepindex].x;
  390. var nextPosY = this._path[this.stepindex].y;
  391. if (startPosX == nextPosX) {
  392. if (startPosY > nextPosY) {
  393. //往右移动
  394. this.vmove = 2;
  395. this.moveDirection = reGameStates.moveType.moveRight;
  396. } else if (startPosY < nextPosY) {
  397. this.vmove = 4;
  398. this.moveDirection = reGameStates.moveType.moveLeft;
  399. } else {
  400. this.moveDirection = reGameStates.moveType.none;
  401. this.vmove = -1;
  402. }
  403. } else if (startPosY == nextPosY) {
  404. if (startPosX > nextPosX) {
  405. this.moveDirection = reGameStates.moveType.moveUp;
  406. this.vmove = 1;
  407. } else if (startPosX < nextPosX) {
  408. this.moveDirection = reGameStates.moveType.moveDown;
  409. this.vmove = 0;
  410. } else {
  411. this.moveDirection = reGameStates.moveType.none;
  412. this.vmove = -1;
  413. }
  414. } else {
  415. this.moveDirection = reGameStates.moveType.none;
  416. this.vmove = -1;
  417. }
  418. this.AIAnimation.switchAnimation(this.moveDirection);
  419. //cc.log('this._path vmove == ', this.vmove, this._path[this.stepindex], this._path[this.stepindex - 1]);
  420. herop = this.convertPos(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y);
  421. //设置深度
  422. // GlobalD.game.onUpdateVertexZFromSlibling(this.node.parent, cc.v2(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y),
  423. // (this.vmove == 1 || this.vmove == 0));
  424. // GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y));
  425. this.smallstepindex = false;
  426. //设置第一个点并且画线
  427. if (this.stepindex - 1 == 0) {
  428. // cc.log('设置第一个点', herop);
  429. //设置旧的渲染值
  430. this.node.parent.zIndex = this._targetRenderingIndex;
  431. this.node.setPosition(herop);
  432. if (this.DrawNode && this.darwGraphicsPath)
  433. this.DrawNode.getComponent('Draw').DrawLineFromTarget();
  434. }
  435. }
  436. if (this.stepindex != -1) {
  437. herop = this.convertPos(this._path[this.stepindex].x, this._path[this.stepindex].y);
  438. // if (!this.isMoving) return;
  439. var oldPos = this.node.position;
  440. // get move direction
  441. var direction = herop.sub(oldPos).normalize();
  442. // multiply direction with distance to get new position
  443. var newPos = oldPos.add(direction.mul(this.followSpeed * dt));
  444. // set new position
  445. this.node.setPosition(newPos);
  446. }
  447. let _distance = herop.sub(this.node.position).mag();
  448. if (_distance <= 10) {
  449. this.smallstepindex = true;
  450. if (this.stepindex >= 1)
  451. this.onHideChildrenNode(true);
  452. //设置渲染深度
  453. GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y));
  454. if (this.stepindex >= this._path.length - 1) {
  455. this.stepindex = -1;
  456. this.vmove = -1;
  457. this.moveDirection = reGameStates.moveType.none;
  458. this.AIAnimation.switchAnimation(this.moveDirection);
  459. this.onCurrentAreaIs();
  460. if (this.AIAttribute.targetBuildingsInfo && this.AIAttribute.targetBuildingsInfo.node) //设置深度
  461. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  462. } else {
  463. this.stepindex++;
  464. this.vmove = -1;
  465. }
  466. }
  467. }
  468. }
  469. },
  470. onDisappearsOnMaps() {
  471. // cc.log('删除游客!');
  472. this.node.parent.destroy();
  473. },
  474. //判断到达的区域是什么地方
  475. onCurrentAreaIs() {
  476. //如果走回到旧的位置,重新寻找商店
  477. if (this._isGotoBack) {
  478. this._isGotoBack = false;
  479. this._GetStoreFromRandom();
  480. return;
  481. }
  482. //走到终点
  483. if (this._isDestroySelf) {
  484. this.onDisappearsOnMaps();
  485. return;
  486. }
  487. // cc.log('this._isDestroySelf',this._isDestroySelf);
  488. //如果有对象
  489. if (this.AIAttribute.targetBuildingsInfo) {
  490. if (!this.AIAttribute.targetBuildingsInfo.node) {
  491. //目标已经为空(已经拆除?),
  492. this._onLeaveWalking();
  493. return;
  494. }
  495. //游客到了特定区域
  496. //判断是否到达目标,因为目标随时有可能会移动的
  497. let isReach = this.onReachTheTargetOrnot(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  498. if (!isReach) {
  499. //如果没有到达,清空对象,走人
  500. cc.log('如果没有路,清空对象,离开!');
  501. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  502. this.AIAttribute.targetBuildingsInfo = null;
  503. this._onLeaveWalking();
  504. return;
  505. }
  506. } else {
  507. //如果到达区域后,目标targetBuildingsInfo为空,
  508. this._onLeaveWalking();
  509. return;
  510. }
  511. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  512. // cc.log('到达目的地', this.AIAttribute.targetBuildingsInfo.buildInfo);
  513. switch (buildInfo.buildType) {
  514. //如果是农田,采木场,矿坑,加工厂。
  515. //购买原料
  516. case reGameStates.BuildType.Farmland:
  517. case reGameStates.BuildType.TimberYard:
  518. case reGameStates.BuildType.MiningPit:
  519. {
  520. this.taskCursor0();
  521. }
  522. break;
  523. case reGameStates.BuildType.Shop:
  524. //隐藏
  525. this.onHideChildrenNode(false);
  526. //如果没得销售,停运
  527. if (!this.AIAttribute.targetBuildingsInfo.buildInfo.isItSaleable) {
  528. this._moveToOldPos();
  529. return;
  530. }
  531. if (this.AIAttribute.targetBuildingsInfo.buildInfo._inventory == 0) {
  532. // 通知商店商品空缺
  533. // cc.log(this.AIAttribute.targetBuildingsInfo);
  534. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  535. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  536. //增加金钱
  537. // this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(99);
  538. this._moveToOldPos();
  539. } else {
  540. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.AIAttribute.purchasingPower);
  541. this.AIAttribute.quantityOfGoodsPurchased += purchasingNum;
  542. // this.TipLabel.string = '购买的商品数量' + this.AIAttribute.quantityOfGoodsPurchased;
  543. //如果不是对应购买的数量,就判断是没有购买完全,商品售罄。
  544. if (purchasingNum != this.AIAttribute.purchasingPower) {
  545. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  546. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  547. //增加金钱
  548. // this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(99);
  549. this._moveToOldPos();
  550. } else {
  551. let prise = this.AIAttribute.targetBuildingsInfo.buildInfo.goodsArray[0].goodsPrice + this.AIAttribute.targetBuildingsInfo.buildInfo.goodsPriceAddValue;
  552. let _buildingTips = this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips");
  553. if (!_buildingTips) {
  554. this.AIAttribute.targetBuildingsInfo.node.addComponent("buildingTips").playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  555. } else {
  556. _buildingTips.playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  557. }
  558. GlobalD.GameData.PlusGolden(prise * this.AIAttribute.quantityOfGoodsPurchased);
  559. this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect').addCount();
  560. this._moveToOldPos();
  561. //如果购买了商品后,商店没有商品了,通知售罄
  562. if (this.AIAttribute.targetBuildingsInfo.buildInfo._inventory <= 0) {
  563. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  564. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  565. }
  566. }
  567. }
  568. break;
  569. }
  570. },
  571. //欢迎光临
  572. taskCursor0: function () {
  573. //引导task.taskCursor == 2
  574. //isCanTasksbeTriggered 是否可以触发任务的游客,true是可以触发
  575. if (this.AIAttribute.isCanTasksbeTriggered && task.TaskIconCountClick == 13 && !task.taskGotoBuyOnly) {
  576. task.taskGotoBuyOnly = true;
  577. task.buyFarming(function () {
  578. this.gobuy();
  579. }.bind(this));
  580. } else {
  581. this.gobuy();
  582. }
  583. },
  584. gobuy: function () {
  585. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  586. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.AIAttribute.targetBuildingsInfo.buildInfo._inventory);
  587. this.AIAttribute.quantityOfGoodsPurchased += purchasingNum;
  588. //重置农田
  589. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  590. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  591. GlobalD.game.onUpdatePersonInventory(buildInfo.buildType, -purchasingNum);
  592. //设置目标点的占用状态
  593. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  594. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo, false);
  595. //增加金钱(商品加上环境影响的附加值)
  596. let prise = this.AIAttribute.targetBuildingsInfo.buildInfo.goodsArray[0].goodsPrice + this.AIAttribute.targetBuildingsInfo.buildInfo.goodsPriceAddValue;
  597. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  598. GlobalD.GameData.PlusGolden(prise * this.AIAttribute.quantityOfGoodsPurchased);
  599. if (this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect') != null) {
  600. this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect').addCount();
  601. }
  602. //购买完成,目标清空
  603. this.AIAttribute.targetBuildingsInfo = null;
  604. this._onLeaveWalking();
  605. },
  606. onHideChildrenNode(isActive) {
  607. let length = this.HideArray.length;
  608. for (let i = 0; i < length; i++) {
  609. this.HideArray[i].active = isActive;
  610. }
  611. },
  612. });