AI_tourist_player.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. this.getPath(_startTilePos, _endTilePos);
  242. this.vmove = -1;
  243. this.stepindex = 1;
  244. this.smallstepindex = true;
  245. //记录终点值
  246. this._walkEndTiledTile = _endTilePos;
  247. this._isDestroySelf = true;
  248. this.onUpdateGotoTarget();
  249. },
  250. _onLeaveWalking() {
  251. this._isDestroySelf = true;
  252. this._path = [];
  253. if (this._walkEndTiledTile) {
  254. let isHas = this.onSearchPathFromPosition(this._walkEndTiledTile);
  255. if (!isHas) {
  256. cc.log('_onLeaveWalking离开时候没有道路,直接删除。', isHas);
  257. this.onDisappearsOnMaps();
  258. }
  259. }
  260. else {
  261. cc.log('this._walkEndTiledTile 为空!');
  262. }
  263. },
  264. //根据数组判断是否到达目标位置
  265. onReachTheTargetOrnot(positionArray) {
  266. let selfPos = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
  267. let length = positionArray.length;
  268. for (let i = 0; i < length; i++) {
  269. // cc.log("根据数组判断是否到达目标位置",selfPos,positionArray[i]);
  270. if (selfPos.sub(positionArray[i]).mag() == 0) {
  271. // cc.log("根据数组判断是否到达目标位置");
  272. return true;
  273. }
  274. }
  275. return false;
  276. },
  277. //根据数组寻找路径
  278. onSearchPathFromPositionArray(positionArray) {
  279. this._path = [];
  280. // this._isThereAWay = false;
  281. let isThereAWay = false;
  282. let length = positionArray.length;
  283. for (let i = 0; i < length; i++) {
  284. if (this.onSearchPathFromPosition(positionArray[i])) {
  285. // this._isThereAWay = true;
  286. isThereAWay = true;
  287. continue;
  288. }
  289. }
  290. return isThereAWay;
  291. },
  292. //根据位置寻找目标
  293. onSearchPathFromPosition(endPos) {
  294. var startPos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  295. if (startPos.sub(endPos).mag() == 0) {
  296. cc.warn('起始点和终点同一个图块!', startPos, endPos);
  297. // this._moveToOldPos();
  298. // this.onCurrentAreaIs();
  299. return false;
  300. }
  301. // cc.log('ai位置', startPos, endPos)
  302. let isHasPath = false;
  303. isHasPath = this.getPath(startPos, endPos);
  304. this.vmove = -1;
  305. this.stepindex = 1;
  306. this.smallstepindex = true;
  307. if (this._path.length >= 2)
  308. this._moveOldPos = this._path[this._path.length - 2];
  309. return isHasPath;
  310. },
  311. //往回走一段距离
  312. _moveToOldPos() {
  313. //一定要设置为空
  314. this._path = [];
  315. let _startTilePos = GlobalD.TiledMap._tilePosFromLocation(this.node.getPosition());
  316. let _endTilePos;
  317. if (this._moveOldPos) {
  318. _endTilePos = this._moveOldPos;
  319. } else {
  320. let randomIndex = Math.floor(Math.random() * GlobalD.game.AllHighwayStylesAndIndex.length);
  321. _endTilePos = GlobalD.TiledMap.analyticalIndexData(GlobalD.game.AllHighwayStylesAndIndex[randomIndex].highwayInfoIndex);
  322. }
  323. // cc.log('_endTilePos:', this.node.getPosition());
  324. let isHas = this.getPath(_startTilePos, _endTilePos);
  325. this.vmove = -1;
  326. this.stepindex = 1;
  327. this.smallstepindex = true;
  328. this._isGotoBack = true;
  329. this._isDestroySelf = false;
  330. if (!isHas) {
  331. cc.log('_moveToOldPos离开时候没有道路,直接删除。', isHas);
  332. this.onDisappearsOnMaps();
  333. }
  334. },
  335. getPath(start, end) {
  336. // AStar.setInitInfo(this.allowDiagonals, this.diagonalCost, this.dotTiebreaker, this.badSorting);
  337. AStar.setStart(start);
  338. let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
  339. // cc.log("getPath", getPath);
  340. //路径取反
  341. getPath.reverse();
  342. if (getPath.length == 0) {
  343. return false;
  344. } else {
  345. if (this._path.length == 0 || this._path.length > getPath.length) {
  346. this._path = getPath;
  347. // cc.log("this.recordTheNearestPoint2", this.recordTheNearestPoint, end);
  348. this.recordTheNearestPoint = end;
  349. }
  350. if (this.darwGraphicsPath) {
  351. var ctx = this.node.parent.getComponent(cc.Graphics);
  352. ctx.clear();
  353. ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
  354. for (let i = 0; i < this._path.length; i++) {
  355. let location = this.convertPos(this._path[i].x, this._path[i].y);
  356. if (i == 0) ctx.moveTo(location.x, location.y);
  357. else ctx.lineTo(location.x, location.y);
  358. }
  359. ctx.stroke();
  360. }
  361. return true;
  362. }
  363. },
  364. convertPos(tiledX, tiledY) {
  365. let location = GlobalD.TiledMap._locationFromtilePos(cc.v2(tiledX, tiledY));
  366. location = this.node.parent.convertToNodeSpaceAR(location);
  367. //Canvas 的坐标是(360,540),图块的大小是(165,96)
  368. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  369. let CanvasPos = GlobalD.game.Canvas.position;
  370. let endLocation = cc.v2(location.x + CanvasPos.x, location.y + CanvasPos.y - 48);
  371. return endLocation;
  372. },
  373. update(dt) {
  374. //根据路径移动
  375. this.MoveFromPath(dt);
  376. },
  377. MoveFromPath(dt) {
  378. //寻路
  379. if (this._path.length != 0) {
  380. var herop = this.node.getPosition();
  381. //根据路径移动
  382. if (this.stepindex >= 1) {
  383. if (this.smallstepindex) {
  384. // 第一个点
  385. var startPosX = this._path[this.stepindex - 1].x;
  386. var startPosY = this._path[this.stepindex - 1].y;
  387. // 第二个点
  388. var nextPosX = this._path[this.stepindex].x;
  389. var nextPosY = this._path[this.stepindex].y;
  390. if (startPosX == nextPosX) {
  391. if (startPosY > nextPosY) {
  392. //往右移动
  393. this.vmove = 2;
  394. this.moveDirection = reGameStates.moveType.moveRight;
  395. } else if (startPosY < nextPosY) {
  396. this.vmove = 4;
  397. this.moveDirection = reGameStates.moveType.moveLeft;
  398. } else {
  399. this.moveDirection = reGameStates.moveType.none;
  400. this.vmove = -1;
  401. }
  402. } else if (startPosY == nextPosY) {
  403. if (startPosX > nextPosX) {
  404. this.moveDirection = reGameStates.moveType.moveUp;
  405. this.vmove = 1;
  406. } else if (startPosX < nextPosX) {
  407. this.moveDirection = reGameStates.moveType.moveDown;
  408. this.vmove = 0;
  409. } else {
  410. this.moveDirection = reGameStates.moveType.none;
  411. this.vmove = -1;
  412. }
  413. } else {
  414. this.moveDirection = reGameStates.moveType.none;
  415. this.vmove = -1;
  416. }
  417. this.AIAnimation.switchAnimation(this.moveDirection);
  418. //cc.log('this._path vmove == ', this.vmove, this._path[this.stepindex], this._path[this.stepindex - 1]);
  419. herop = this.convertPos(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y);
  420. //设置深度
  421. // GlobalD.game.onUpdateVertexZFromSlibling(this.node.parent, cc.v2(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y),
  422. // (this.vmove == 1 || this.vmove == 0));
  423. // GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex - 1].x, this._path[this.stepindex - 1].y));
  424. this.smallstepindex = false;
  425. //设置第一个点并且画线
  426. if (this.stepindex - 1 == 0) {
  427. // cc.log('设置第一个点', herop);
  428. //设置旧的渲染值
  429. this.node.parent.zIndex = this._targetRenderingIndex;
  430. this.node.setPosition(herop);
  431. if (this.DrawNode && this.darwGraphicsPath)
  432. this.DrawNode.getComponent('Draw').DrawLineFromTarget();
  433. }
  434. }
  435. if (this.stepindex != -1) {
  436. herop = this.convertPos(this._path[this.stepindex].x, this._path[this.stepindex].y);
  437. // if (!this.isMoving) return;
  438. var oldPos = this.node.position;
  439. // get move direction
  440. var direction = herop.sub(oldPos).normalize();
  441. // multiply direction with distance to get new position
  442. var newPos = oldPos.add(direction.mul(this.followSpeed * dt));
  443. // set new position
  444. this.node.setPosition(newPos);
  445. }
  446. let _distance = herop.sub(this.node.position).mag();
  447. if (_distance <= 10) {
  448. this.smallstepindex = true;
  449. if (this.stepindex >= 1)
  450. this.onHideChildrenNode(true);
  451. //设置渲染深度
  452. GlobalD.game.onUpdateVertexZFromZIndex(this.node.parent, cc.v2(this._path[this.stepindex].x, this._path[this.stepindex].y));
  453. if (this.stepindex >= this._path.length - 1) {
  454. this.stepindex = -1;
  455. this.vmove = -1;
  456. this.moveDirection = reGameStates.moveType.none;
  457. this.AIAnimation.switchAnimation(this.moveDirection);
  458. this.onCurrentAreaIs();
  459. if (this.AIAttribute.targetBuildingsInfo && this.AIAttribute.targetBuildingsInfo.node) //设置深度
  460. this._targetRenderingIndex = this.AIAttribute.targetBuildingsInfo.node.zIndex;
  461. } else {
  462. this.stepindex++;
  463. this.vmove = -1;
  464. }
  465. }
  466. }
  467. }
  468. },
  469. onDisappearsOnMaps() {
  470. // cc.log('删除游客!');
  471. this.node.parent.destroy();
  472. },
  473. //判断到达的区域是什么地方
  474. onCurrentAreaIs() {
  475. //如果走回到旧的位置,重新寻找商店
  476. if (this._isGotoBack) {
  477. this._isGotoBack = false;
  478. this._GetStoreFromRandom();
  479. return;
  480. }
  481. //走到终点
  482. if (this._isDestroySelf) {
  483. this.onDisappearsOnMaps();
  484. return;
  485. }
  486. // cc.log('this._isDestroySelf',this._isDestroySelf);
  487. //如果有对象
  488. if (this.AIAttribute.targetBuildingsInfo) {
  489. if (!this.AIAttribute.targetBuildingsInfo.node) {
  490. //目标已经为空(已经拆除?),
  491. this._onLeaveWalking();
  492. return;
  493. }
  494. //游客到了特定区域
  495. //判断是否到达目标,因为目标随时有可能会移动的
  496. let isReach = this.onReachTheTargetOrnot(this.AIAttribute.targetBuildingsInfo.buildInfo._gotoPosistion);
  497. if (!isReach) {
  498. //如果没有到达,清空对象,走人
  499. cc.log('如果没有路,清空对象,离开!');
  500. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  501. this.AIAttribute.targetBuildingsInfo = null;
  502. this._onLeaveWalking();
  503. return;
  504. }
  505. } else {
  506. //如果到达区域后,目标targetBuildingsInfo为空,
  507. this._onLeaveWalking();
  508. return;
  509. }
  510. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  511. // cc.log('到达目的地', this.AIAttribute.targetBuildingsInfo.buildInfo);
  512. switch (buildInfo.buildType) {
  513. //如果是农田,采木场,矿坑,加工厂。
  514. //购买原料
  515. case reGameStates.BuildType.Farmland:
  516. case reGameStates.BuildType.TimberYard:
  517. case reGameStates.BuildType.MiningPit:
  518. {
  519. this.taskCursor0();
  520. }
  521. break;
  522. case reGameStates.BuildType.Shop:
  523. //隐藏
  524. this.onHideChildrenNode(false);
  525. //如果没得销售,停运
  526. if (!this.AIAttribute.targetBuildingsInfo.buildInfo.isItSaleable) {
  527. this._moveToOldPos();
  528. return;
  529. }
  530. if (this.AIAttribute.targetBuildingsInfo.buildInfo._inventory == 0) {
  531. // 通知商店商品空缺
  532. // cc.log(this.AIAttribute.targetBuildingsInfo);
  533. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  534. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  535. //增加金钱
  536. // this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(99);
  537. this._moveToOldPos();
  538. } else {
  539. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.AIAttribute.purchasingPower);
  540. this.AIAttribute.quantityOfGoodsPurchased += purchasingNum;
  541. // this.TipLabel.string = '购买的商品数量' + this.AIAttribute.quantityOfGoodsPurchased;
  542. //如果不是对应购买的数量,就判断是没有购买完全,商品售罄。
  543. if (purchasingNum != this.AIAttribute.purchasingPower) {
  544. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  545. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  546. //增加金钱
  547. // this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(99);
  548. this._moveToOldPos();
  549. } else {
  550. let prise = this.AIAttribute.targetBuildingsInfo.buildInfo.goodsArray[0].goodsPrice + this.AIAttribute.targetBuildingsInfo.buildInfo.goodsPriceAddValue;
  551. let _buildingTips = this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips");
  552. if (!_buildingTips) {
  553. this.AIAttribute.targetBuildingsInfo.node.addComponent("buildingTips").playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  554. } else {
  555. _buildingTips.playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  556. }
  557. GlobalD.GameData.PlusGolden(prise * this.AIAttribute.quantityOfGoodsPurchased);
  558. this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect').addCount();
  559. this._moveToOldPos();
  560. //如果购买了商品后,商店没有商品了,通知售罄
  561. if (this.AIAttribute.targetBuildingsInfo.buildInfo._inventory <= 0) {
  562. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playSellOutAnim();
  563. this.AIAttribute.targetBuildingsInfo.onSetTipSellOut();
  564. }
  565. }
  566. }
  567. break;
  568. }
  569. },
  570. //欢迎光临
  571. taskCursor0: function () {
  572. //引导
  573. if (task.TaskIconCountClick == 13 && !task.taskGotoBuyOnly) {
  574. task.taskGotoBuyOnly = true;
  575. task.buyFarming(function () {
  576. this.gobuy();
  577. }.bind(this));
  578. } else {
  579. this.gobuy();
  580. }
  581. },
  582. go: function () {
  583. // var Canvas = cc.find("Canvas/UICamera");
  584. // task.removeTaskNode(task.taskCursorName[0]);
  585. // task.addTaskTips(Canvas, task.newTaskName[6], 0, 0, task.taskCursorName[0], function (nodePrefabs) {
  586. // //黑底
  587. // // this.nodePrefabsbg = nodePrefabs;
  588. // UtilsPrefabs.setOn(nodePrefabs.getChildByName("SureBtn"), function () {
  589. //
  590. // task.removeTaskNode(task.taskCursorName[0]);
  591. //
  592. // // this.sharegosharebtn = UtilsPrefabs.getNode("gosharebtn",this.shareview);
  593. //
  594. //
  595. // // this.taskCursor1();
  596. // //去购买
  597. // this.gobuy();
  598. //
  599. // // let BelowTheMask = cc.find('Canvas/UICamera/BelowMask');
  600. // // task.addSeneceTaskIcon(BelowTheMask);
  601. //
  602. // }.bind(this));
  603. // }.bind(this));
  604. },
  605. gobuy: function () {
  606. // //如果目标已经没有东西了。直接离开
  607. // if (this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied && this.AIAttribute.targetBuildingsInfo.buildInfo._inventory <= 0) {
  608. // cc.log('目标被占用:', this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied, '目标库存为:', this.AIAttribute.targetBuildingsInfo.buildInfo._inventory);
  609. // this.AIAttribute.targetBuildingsInfo = null;
  610. // this._onLeaveWalking();
  611. // return;
  612. // }
  613. let buildInfo = this.AIAttribute.targetBuildingsInfo.buildInfo;
  614. let purchasingNum = this.AIAttribute.targetBuildingsInfo.buildInfo.onSetCurrentInventory(-this.AIAttribute.targetBuildingsInfo.buildInfo._inventory);
  615. this.AIAttribute.quantityOfGoodsPurchased += purchasingNum;
  616. // this.TipLabel.string = '购买的商品数量' + this.AIAttribute.quantityOfGoodsPurchased;
  617. //重置农田
  618. this.AIAttribute.targetBuildingsInfo.node.getComponent('WorkingBuilding').onResetSprite(buildInfo.buildType);
  619. this.AIAttribute.targetBuildingsInfo.buildInfo._inventory = 0;
  620. GlobalD.game.onUpdatePersonInventory(buildInfo.buildType, -purchasingNum);
  621. //设置目标点的占用状态
  622. this.AIAttribute.targetBuildingsInfo.buildInfo.isItOccupied = false;
  623. GlobalD.game.onUpdateMaterialsArray(this.AIAttribute.targetBuildingsInfo, false);
  624. //增加金钱(商品加上环境影响的附加值)
  625. let prise = this.AIAttribute.targetBuildingsInfo.buildInfo.goodsArray[0].goodsPrice + this.AIAttribute.targetBuildingsInfo.buildInfo.goodsPriceAddValue;
  626. this.AIAttribute.targetBuildingsInfo.node.getComponent("buildingTips").playCoinAnim(prise * this.AIAttribute.quantityOfGoodsPurchased);
  627. GlobalD.GameData.PlusGolden(prise * this.AIAttribute.quantityOfGoodsPurchased);
  628. if (this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect') != null) {
  629. this.AIAttribute.targetBuildingsInfo.node.getComponent('Collect').addCount();
  630. }
  631. //购买完成,目标清空
  632. this.AIAttribute.targetBuildingsInfo = null;
  633. this._onLeaveWalking();
  634. },
  635. onHideChildrenNode(isActive) {
  636. let length = this.HideArray.length;
  637. for (let i = 0; i < length; i++) {
  638. this.HideArray[i].active = isActive;
  639. }
  640. },
  641. });