StoreView.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. Seed: {
  5. default: null,
  6. type: cc.Node,
  7. },
  8. Exchange: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. SeedBtn: {
  13. default: null,
  14. type: cc.Node,
  15. },
  16. ExchangeBtn: {
  17. default: null,
  18. type: cc.Node,
  19. },
  20. SelectedFrame: cc.Node,
  21. //Building40: cc.Node,
  22. ManageUI: cc.Node,
  23. //四个按钮对应的sprite
  24. Normal_Seed_Sprite: cc.SpriteFrame,
  25. Selecteded_Seed_Sprite: cc.SpriteFrame,
  26. Normal_Exchange_Sprite: cc.SpriteFrame,
  27. Selecteded_Exchange_Sprite: cc.SpriteFrame,
  28. //需要切换节点顺序的父节点
  29. container: cc.Node,
  30. //商城的列表
  31. mySeedList: [],
  32. seedPrefab: cc.Prefab,
  33. seedSpriteFrame: {
  34. default: [],
  35. type: [cc.SpriteFrame]
  36. },
  37. seedNodeArry: {
  38. default: [],
  39. type: [cc.Node]
  40. },
  41. snbAmount: 0,
  42. cntAmount: 0,
  43. ExchangePopView: {
  44. default: null,
  45. type: cc.Node,
  46. },
  47. PayCNTPopView: {
  48. default: null,
  49. type: cc.Node,
  50. },
  51. PaySNBPopView: {
  52. default: null,
  53. type: cc.Node,
  54. },
  55. buySeedIndex: -1,
  56. snbAmountLabel: {
  57. default: [],
  58. type: [cc.Label]
  59. },
  60. //拿到仓库列表的节点
  61. buildingView: {
  62. default: null,
  63. type: cc.Node,
  64. },
  65. myWareHouseSeedList: null,
  66. //轮询种子列表状态
  67. AutoGetSeedsData: null,
  68. bottomLabel: {
  69. default: null,
  70. type: cc.Node,
  71. },
  72. },
  73. HiddenAll() {
  74. if (this.Seed.active)
  75. this.Seed.active = false;
  76. if (this.Exchange.active)
  77. this.Exchange.active = false;
  78. },
  79. start() {
  80. //获取到仓库列表的脚本
  81. this.buildingViewScript = this.buildingView.getComponent("BuildingView");
  82. //默认设置第一个节点为 最上面
  83. this.SeedBtn.setSiblingIndex(10);
  84. console.log('SNB=======', GlobalD.GameData.SNB)
  85. this.snbAmountLabel.string = GlobalD.GameData.SNB;
  86. //这里也初始化获取一个数据
  87. GlobalD.GameData.getWarehouseSeedAndFruit((res, vaule) => {
  88. this.myWareHouseSeedList = vaule.data.seed;
  89. });
  90. //获取种子和果实
  91. GlobalD.GameData.getMallSeed((res, vaule) => {
  92. console.log('vaule111111111=', vaule)
  93. this.mySeedList = vaule.data;
  94. for (let i = 0; i < this.mySeedList.length; i++) {
  95. //没有种子数量不显示 todo 看看后续需不需要处理删除
  96. if (this.mySeedList[i].amount < 1) continue;
  97. let _seed = cc.instantiate(this.seedPrefab);
  98. // _seed.getChildByName("buy_button").active = true;
  99. // _seed.getChildByName("Name").active = false;
  100. // _seed.getChildByName("Name").getChildByName("Num").getComponent(cc.Label).strng = '';//'x'+this.mySeedList[i].amount;
  101. this.seedNodeArry.push(_seed);
  102. _seed.parent = this.Seed;
  103. let _seedScript = _seed.getComponent("Content_Button");
  104. //设置一个生成点
  105. let _spawnScript = _seed.getComponent("Content_seed");
  106. _spawnScript.myIndex = i;
  107. _spawnScript.SpawnPoint = cc.find("Canvas/SpawnParent/SpawnPoint");
  108. _seedScript.NumLabel.string = this.mySeedList[i].amount;
  109. _seedScript.NameLabel.string = this.mySeedList[i].name;
  110. switch (this.mySeedList[i].picture) {
  111. case "Cabbage":
  112. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
  113. break;
  114. case "Potato":
  115. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
  116. break;
  117. case "Carrot":
  118. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
  119. break;
  120. case "Broccoli":
  121. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
  122. break;
  123. case "Tomato":
  124. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
  125. break;
  126. case "Squash":
  127. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
  128. break;
  129. case "Eggplant":
  130. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
  131. break;
  132. case "Pepper":
  133. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. });
  140. },
  141. //切换建筑的面板
  142. onSwitchBuildingContent(event, index) {
  143. this.SeedBtn.setScale(1);
  144. this.ExchangeBtn.setScale(1);
  145. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  146. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Exchange_Sprite;
  147. this.SeedBtn.setContentSize(100, 54);
  148. this.ExchangeBtn.setContentSize(100, 54);
  149. this.SeedBtn.y = -5;
  150. this.ExchangeBtn.y = -5;
  151. this.HiddenAll();
  152. //面板设置index
  153. //种子
  154. if ('0' == index) {
  155. this.SeedBtn.y = -20;
  156. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  157. this.Seed.active = true;
  158. if (this.seedNodeArry.length != 0) {
  159. this.SelectedFrame.parent = this.seedNodeArry[0];
  160. }
  161. //if (this.mySeedList.length > 0) {
  162. // let Synopsis = "成熟期:" + this.mySeedList[0].maturity;
  163. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, this.mySeedList[0].priceSnb, Synopsis);
  164. //}
  165. this.SeedBtn.setSiblingIndex(10);
  166. this.SeedBtn.setContentSize(86, 88);
  167. this.bottomLabel.active = false;
  168. }
  169. //兑换
  170. else if ('1' == index) {
  171. this.ExchangeBtn.y = -20;
  172. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Exchange_Sprite;
  173. this.Exchange.active = true;
  174. this.ExchangeBtn.setSiblingIndex(10);
  175. this.ExchangeBtn.setContentSize(86, 88);
  176. this.bottomLabel.active = true;
  177. }
  178. },
  179. switch: function () {
  180. var children = this.container.children;
  181. var length = children.length;
  182. if (length > 1) {
  183. var src = Math.floor(Math.random() * length);
  184. var node = children[src];
  185. var dst = src === length - 1 ? 0 : src + 1;
  186. node.setSiblingIndex(dst);
  187. }
  188. },
  189. SNBExchangeToCNT() {
  190. GlobalD.GameData.onSnbToCnt(this.snbAmount, (data) => {
  191. this.ExchangePopView.active = false;
  192. console.log('data=', data)
  193. // console.log('res=', res)
  194. });
  195. },
  196. onPayCNTPopView(myIndex) {
  197. this.buySeedIndex = myIndex;
  198. this.PayCNTPopView.active = true;
  199. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  200. this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  201. this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  202. this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  203. },
  204. onPaySNBPopView(myIndex) {
  205. this.buySeedIndex = myIndex;
  206. this.PaySNBPopView.active = true;
  207. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  208. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  209. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' SNB';
  210. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  211. },
  212. PayCNT() {
  213. console.log('this.cntAmount=', this.cntAmount)
  214. console.log('this.cntAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt))
  215. //购买种子也要使用轮询
  216. let currentAmount = 0;
  217. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  218. if (this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id) {
  219. currentAmount = this.myWareHouseSeedList[i].amount;
  220. }
  221. }
  222. //当前背包的数量
  223. console.log("currentAmount:", {
  224. seedId: this.mySeedList[this.buySeedIndex].id,
  225. currentAmount: currentAmount //把当前背包的数据传进去
  226. });
  227. GlobalD.GameData.payCNT(this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt), 4, this.mySeedList[this.buySeedIndex].id + "", (data) => {
  228. const [err, tx] = data;
  229. if (err === null) {
  230. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  231. console.log(tx) // 交易hash,唯一标识符
  232. //todo 种子
  233. if (this.AutoGetSeedsData) {
  234. this.unschedule(this.AutoGetSeedsData);
  235. this.AutoGetSeedsData = null;
  236. }
  237. this.AutoGetSeedsData = () => {
  238. // getSeedCount ++;
  239. GlobalD.GameData.onGetSeedState(
  240. {
  241. seedId: this.mySeedList[this.buySeedIndex].id,
  242. currentAmount: currentAmount //把当前背包的数据传进去
  243. },
  244. (res, value) => {
  245. console.log("轮询种子:" + JSON.stringify(value));
  246. if (0 === value.code) {
  247. this.unschedule(this.AutoGetSeedsData);
  248. console.log("轮询种子购买成功");
  249. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 5);
  250. this.PayCNTPopView.active = false;
  251. //直接重新初始化一下列表,
  252. this.buildingViewScript.onUpdateList();
  253. }
  254. });
  255. };
  256. this.schedule(this.AutoGetSeedsData, 5);
  257. } else {
  258. console.log(err)
  259. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 5);
  260. }
  261. });
  262. },
  263. PaySNB() {
  264. console.log('this.snbAmount=', this.cntAmount)
  265. console.log('priceSnb price=', this.mySeedList[this.buySeedIndex].priceSnb)
  266. console.log('this.snbAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb))
  267. //购买种子也要使用轮询
  268. let currentAmount = 0;
  269. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  270. if (this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id) {
  271. currentAmount = this.myWareHouseSeedList[i].amount;
  272. }
  273. }
  274. //当前背包的数量
  275. console.log("currentAmount:", {
  276. seedId: this.mySeedList[this.buySeedIndex].id,
  277. currentAmount: currentAmount //把当前背包的数据传进去
  278. });
  279. GlobalD.GameData.onBuySeedsWithSNB(this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb), this.mySeedList[this.buySeedIndex].id, (res, value) => {
  280. if (0 === value.code)
  281. {
  282. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  283. console.log('snb res =====',res) // 交易hash,唯一标识符
  284. console.log('snb value =====',value) // 交易hash,唯一标识符
  285. //todo 种子
  286. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 5);
  287. this.PaySNBPopView.active = false;
  288. //直接重新初始化一下列表,
  289. this.buildingViewScript.onUpdateList();
  290. }
  291. });
  292. },
  293. onTextChanged1: function (text, editbox, customEventData) {
  294. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  295. // 这里 editbox 是一个 cc.EditBox 对象
  296. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  297. this.cntAmount = parseInt(text);
  298. },
  299. // 假设这个回调是给 textChanged 事件的
  300. onTextChanged2: function (text, editbox, customEventData) {
  301. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  302. // 这里 editbox 是一个 cc.EditBox 对象
  303. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  304. this.snbAmount = parseInt(text);
  305. },
  306. });