BuildingView.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. Env: {
  5. default: null,
  6. type: cc.Node,
  7. },
  8. Labour: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. Shop: {
  13. default: null,
  14. type: cc.Node,
  15. },
  16. SpeBuilding: {
  17. default: null,
  18. type: cc.Node,
  19. },
  20. Seed: {
  21. default: null,
  22. type: cc.Node,
  23. },
  24. Fruit: {
  25. default: null,
  26. type: cc.Node,
  27. },
  28. warehouseScrollView: {
  29. default: null,
  30. type: cc.ScrollView
  31. },
  32. EvnBtn: {
  33. default: null,
  34. type: cc.Node,
  35. },
  36. LabourBtn: {
  37. default: null,
  38. type: cc.Node,
  39. },
  40. ShopBtn: {
  41. default: null,
  42. type: cc.Node,
  43. },
  44. SpeBuildingBtn: {
  45. default: null,
  46. type: cc.Node,
  47. },
  48. SeedBtn: {
  49. default: null,
  50. type: cc.Node,
  51. },
  52. FruitBtn: {
  53. default: null,
  54. type: cc.Node,
  55. },
  56. SelectedFrame: cc.Node,
  57. Building00: cc.Node,
  58. Building10: cc.Node,
  59. Building20: cc.Node,
  60. Building30: cc.Node,
  61. ManageUI: cc.Node,
  62. //四个按钮对应的sprite
  63. Normal_Env_Sprite: cc.SpriteFrame,
  64. Selecteded_Env_Sprite: cc.SpriteFrame,
  65. Normal_Labour_Sprite: cc.SpriteFrame,
  66. Selecteded_Labour_Sprite: cc.SpriteFrame,
  67. Normal_Shop_Sprite: cc.SpriteFrame,
  68. Selecteded_Shop_Sprite: cc.SpriteFrame,
  69. Normal_Spe_Sprite: cc.SpriteFrame,
  70. Selecteded_Spe_Sprite: cc.SpriteFrame,
  71. Normal_Seed_Sprite: cc.SpriteFrame,
  72. Selecteded_Seed_Sprite: cc.SpriteFrame,
  73. Normal_Fruit_Sprite: cc.SpriteFrame,
  74. Selecteded_Fruit_Sprite: cc.SpriteFrame,
  75. //需要切换节点顺序的父节点
  76. container: cc.Node,
  77. //商城的列表
  78. mySeedList: [],
  79. myFruitList: [],
  80. seedPrefab: cc.Prefab,
  81. fruitPrefab: cc.Prefab,
  82. seedSpriteFrame: {
  83. default: [],
  84. type: [cc.SpriteFrame],
  85. },
  86. seedIconSpriteFrame: {
  87. default: [],
  88. type: [cc.SpriteFrame],
  89. },
  90. fruitSpriteFrame: {
  91. default: [],
  92. type: [cc.SpriteFrame],
  93. },
  94. },
  95. HiddenAll() {
  96. // if (this.Env.active) this.Env.active = false
  97. // if (this.Labour.active) this.Labour.active = false
  98. // if (this.Shop.active) this.Shop.active = false
  99. // if (this.SpeBuilding.active) this.SpeBuilding.active = false
  100. if (this.Seed.active) this.Seed.active = false
  101. if (this.Fruit.active) this.Fruit.active = false
  102. if (this.warehouseScrollView) {
  103. this.warehouseScrollView.stopAutoScroll();
  104. this.warehouseScrollView.scrollToTop();
  105. }
  106. },
  107. start() {
  108. //默认设置第一个节点为 最上面
  109. // this.EvnBtn.setSiblingIndex(10)
  110. this.onUpdateList()
  111. },
  112. onUpdateList() {
  113. //先判断container下面是否存在Seed
  114. // this.Seed.destroyAllChildren;
  115. // console.log("this.Seed.children:", this.Seed.children);
  116. //清除种子的预制
  117. let _children = this.Seed.children
  118. for (let i = _children.length - 1; i >= 0; i--) {
  119. _children[i].destroy()
  120. }
  121. //清除仓库的预制
  122. let _fruitChildren = this.Fruit.children
  123. for (let i = _fruitChildren.length - 1; i >= 0; i--) {
  124. _fruitChildren[i].destroy()
  125. }
  126. // console.log("this.Seed.children2:", this.Seed.children);
  127. //获取种子和果实
  128. GlobalD.GameData.getWarehouseSeedAndFruit((vaule) => {
  129. this.mySeedList = vaule.data.seed
  130. this.myFruitList = vaule.data.fruit
  131. //console.log(this.mySeedList, this.myFruitList);
  132. for (let i = 0; i < this.mySeedList.length; i++) {
  133. //没有种子数量不显示
  134. if (this.mySeedList[i].amount < 1) continue
  135. let _seed = cc.instantiate(this.seedPrefab)
  136. _seed.parent = this.Seed
  137. let _seedScript = _seed.getComponent('Content_Button')
  138. _seedScript.Name = this.mySeedList[i].name
  139. _seedScript.Price = 0
  140. _seedScript.SNB = this.mySeedList[i].priceSnb
  141. _seedScript.Mature = '成熟期:' + this.mySeedList[i].maturity + '天,'
  142. _seedScript.Synopsis = this.mySeedList[i].seedDescribe
  143. //设置一个生成点
  144. let _spawnScript = _seed.getComponent('Content_seed')
  145. _spawnScript.SpawnPoint = cc.find('Canvas/SpawnParent/SpawnPoint')
  146. _spawnScript.goodsSeedInfo = this.mySeedList[i] //这里传递一个生成的种子信息过去
  147. _seedScript.NumLabel.string = this.mySeedList[i].amount
  148. _seedScript.NameLabel.string = this.mySeedList[i].name
  149. switch (this.mySeedList[i].picture) {
  150. case 'Cabbage':
  151. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[0]
  152. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[0]
  153. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[0]
  154. break
  155. case 'Potato':
  156. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[1]
  157. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[1]
  158. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[1]
  159. break
  160. case 'Carrot':
  161. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[2]
  162. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[2]
  163. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[2]
  164. break
  165. case 'Broccoli':
  166. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[3]
  167. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[3]
  168. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[3]
  169. break
  170. case 'Tomato':
  171. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[4]
  172. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[4]
  173. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[4]
  174. break
  175. case 'Squash':
  176. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[5]
  177. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[5]
  178. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[5]
  179. break
  180. case 'Eggplant':
  181. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[6]
  182. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[6]
  183. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[6]
  184. break
  185. case 'Pepper':
  186. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[7]
  187. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[7]
  188. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[7]
  189. break
  190. case 'Lentil':
  191. _seedScript.bgSprite.spriteFrame = this.seedIconSpriteFrame[8]
  192. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[8]
  193. _spawnScript.seedSpriteFrame = this.seedSpriteFrame[8]
  194. break
  195. default:
  196. break
  197. }
  198. }
  199. for (let i = 0; i < this.myFruitList.length; i++) {
  200. //没有果实数量不显示
  201. if (this.myFruitList[i].amount + this.myFruitList[i].amountPart <= 0) continue
  202. let _fruit = cc.instantiate(this.fruitPrefab)
  203. _fruit.parent = this.Fruit
  204. _fruit.zIndex = 999
  205. let _fruitScript = _fruit.getComponent('Content_Button')
  206. _fruitScript.NumLabel.string = Number(
  207. this.myFruitList[i].amount + this.myFruitList[i].amountPart
  208. ).toFixed(3)
  209. _fruitScript.NameLabel.string = this.myFruitList[i].name
  210. _fruitScript.Name = this.myFruitList[i].name
  211. _fruitScript.SNB = this.myFruitList[i].priceSnb
  212. _fruitScript.Synopsis = this.myFruitList[i].fruitDescribe
  213. //设置果实信息
  214. let _contentFruit = _fruit.getComponent('Content_fruit')
  215. _contentFruit.fruitInfo = this.myFruitList[i]
  216. switch (this.myFruitList[i].picture) {
  217. case 'Cabbage':
  218. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[0]
  219. break
  220. case 'Potato':
  221. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[1]
  222. break
  223. case 'Carrot':
  224. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[2]
  225. break
  226. case 'Broccoli':
  227. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[3]
  228. break
  229. case 'Tomato':
  230. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[4]
  231. break
  232. case 'Squash':
  233. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[5]
  234. break
  235. case 'Eggplant':
  236. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[6]
  237. break
  238. case 'Pepper':
  239. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[7]
  240. break
  241. case 'Lentil':
  242. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[8]
  243. break
  244. default:
  245. break
  246. }
  247. }
  248. })
  249. },
  250. //切换建筑的面板
  251. onSwitchBuildingContent(event, index) {
  252. // this.EvnBtn.setScale(1)
  253. // this.LabourBtn.setScale(1)
  254. // this.ShopBtn.setScale(1)
  255. // this.SpeBuildingBtn.setScale(1)
  256. this.SeedBtn.setScale(1)
  257. this.FruitBtn.setScale(1)
  258. // this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite
  259. // this.LabourBtn.getComponent(cc.Sprite).spriteFrame =
  260. // this.Normal_Labour_Sprite
  261. // this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite
  262. // this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame =
  263. // this.Normal_Spe_Sprite
  264. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite
  265. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite
  266. // this.EvnBtn.setContentSize(100, 54)
  267. // this.LabourBtn.setContentSize(100, 54)
  268. // this.ShopBtn.setContentSize(100, 54)
  269. // this.SpeBuildingBtn.setContentSize(100, 54)
  270. // this.SeedBtn.setContentSize(100, 54);
  271. // this.FruitBtn.setContentSize(100, 54);
  272. // this.EvnBtn.y = -5
  273. // this.LabourBtn.y = -5
  274. // this.ShopBtn.y = -5
  275. // this.SpeBuildingBtn.y = -5
  276. // this.SeedBtn.y = -5
  277. // this.FruitBtn.y = -5
  278. this.HiddenAll()
  279. //面板设置index
  280. //环境
  281. // if ('0' == index) {
  282. // this.EvnBtn.y = -20
  283. // this.EvnBtn.getComponent(cc.Sprite).spriteFrame =
  284. // this.Selecteded_Env_Sprite
  285. // this.Env.active = true
  286. // // this.SelectedFrame.parent = this.Building00;
  287. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  288. // this.Building00.getComponent('Content_Button').Name,
  289. // this.Building00.getComponent('Content_Button').Price,
  290. // 0,
  291. // this.Building00.getComponent('Content_Button').Synopsis
  292. // )
  293. // //设置节点顺序
  294. // this.EvnBtn.setSiblingIndex(10)
  295. // this.EvnBtn.setContentSize(110, 100)
  296. // }
  297. // //劳动
  298. // else if ('1' == index) {
  299. // this.LabourBtn.y = -20
  300. // this.LabourBtn.getComponent(cc.Sprite).spriteFrame =
  301. // this.Selecteded_Labour_Sprite
  302. // this.Labour.active = true
  303. // // this.SelectedFrame.parent = this.Building10;
  304. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  305. // this.Building10.getComponent('Content_Button').Name,
  306. // this.Building10.getComponent('Content_Button').Price,
  307. // 0,
  308. // this.Building10.getComponent('Content_Button').Synopsis
  309. // )
  310. // this.LabourBtn.setSiblingIndex(10)
  311. // // this.LabourBtn.setContentSize(86, 88);
  312. // this.LabourBtn.setContentSize(110, 100)
  313. // }
  314. // //商铺
  315. // else if ('2' == index) {
  316. // this.ShopBtn.y = -20
  317. // this.ShopBtn.getComponent(cc.Sprite).spriteFrame =
  318. // this.Selecteded_Shop_Sprite
  319. // this.Shop.active = true
  320. // // this.SelectedFrame.parent = this.Building20;
  321. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  322. // this.Building20.getComponent('Content_Button').Name,
  323. // this.Building20.getComponent('Content_Button').Price,
  324. // 0,
  325. // this.Building20.getComponent('Content_Button').Synopsis
  326. // )
  327. // this.ShopBtn.setSiblingIndex(10)
  328. // this.ShopBtn.setContentSize(110, 100)
  329. // }
  330. // //特殊
  331. // else if ('3' == index) {
  332. // this.SpeBuildingBtn.y = -20
  333. // this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame =
  334. // this.Selecteded_Spe_Sprite
  335. // this.SpeBuilding.active = true
  336. // // this.SelectedFrame.parent = this.Building30;
  337. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  338. // this.Building30.getComponent('Content_Button').Name,
  339. // this.Building30.getComponent('Content_Button').Price,
  340. // this.Building30.getComponent('Content_Button').Synopsis
  341. // )
  342. // this.SpeBuildingBtn.setSiblingIndex(10)
  343. // this.SpeBuildingBtn.setContentSize(110, 100)
  344. // }else
  345. //种子
  346. if ('4' == index) {
  347. // this.SeedBtn.y = -20
  348. this.SeedBtn.getComponent(cc.Sprite).spriteFrame =
  349. this.Selecteded_Seed_Sprite
  350. this.Seed.active = true
  351. if (this.mySeedList.length > 0) {
  352. let Synopsis = this.mySeedList[0].seedDescribe;
  353. let mature = '成熟期:' +
  354. this.mySeedList[0].maturity +
  355. '天';
  356. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  357. this.mySeedList[0].name,
  358. 0,
  359. mature,
  360. Synopsis
  361. )
  362. }
  363. this.SeedBtn.setSiblingIndex(10)
  364. // this.SeedBtn.setContentSize(110, 100)
  365. }
  366. //果实
  367. else if ('5' == index) {
  368. // this.FruitBtn.y = -20
  369. this.FruitBtn.getComponent(cc.Sprite).spriteFrame =
  370. this.Selecteded_Fruit_Sprite
  371. this.Fruit.active = true;
  372. if (this.myFruitList.length > 0) {
  373. let Synopsis = this.myFruitList[0].fruitDescribe;
  374. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(
  375. this.myFruitList[0].name,
  376. this.myFruitList[0].priceSnb,
  377. 0,
  378. Synopsis
  379. )
  380. }
  381. this.FruitBtn.setSiblingIndex(10)
  382. // this.FruitBtn.setContentSize(110, 100)
  383. }
  384. //切换节点顺序
  385. // this.switch();
  386. },
  387. switch: function () {
  388. var children = this.container.children
  389. var length = children.length
  390. if (length > 1) {
  391. var src = Math.floor(Math.random() * length)
  392. var node = children[src]
  393. var dst = src === length - 1 ? 0 : src + 1
  394. node.setSiblingIndex(dst)
  395. }
  396. },
  397. })