BuildingView.js 14 KB

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