BuildingView.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. Building40: cc.Node,
  58. Building50: cc.Node,
  59. ManageUI: cc.Node,
  60. //四个按钮对应的sprite
  61. Normal_Env_Sprite: cc.SpriteFrame,
  62. Selecteded_Env_Sprite: cc.SpriteFrame,
  63. Normal_Labour_Sprite: cc.SpriteFrame,
  64. Selecteded_Labour_Sprite: cc.SpriteFrame,
  65. Normal_Shop_Sprite: cc.SpriteFrame,
  66. Selecteded_Shop_Sprite: cc.SpriteFrame,
  67. Normal_Spe_Sprite: cc.SpriteFrame,
  68. Selecteded_Spe_Sprite: cc.SpriteFrame,
  69. Normal_Seed_Sprite: cc.SpriteFrame,
  70. Selecteded_Seed_Sprite: cc.SpriteFrame,
  71. Normal_Fruit_Sprite: cc.SpriteFrame,
  72. Selecteded_Fruit_Sprite: cc.SpriteFrame,
  73. //需要切换节点顺序的父节点
  74. container: cc.Node,
  75. //商城的列表
  76. mySeedList: [],
  77. myFruitList: [],
  78. seedPrefab: cc.Prefab,
  79. fruitPrefab: cc.Prefab,
  80. seedSpriteFrame: {
  81. default: [],
  82. type: [cc.SpriteFrame]
  83. },
  84. fruitSpriteFrame: {
  85. default: [],
  86. type: [cc.SpriteFrame]
  87. },
  88. },
  89. HiddenAll() {
  90. if (this.Env.active)
  91. this.Env.active = false;
  92. if (this.Labour.active)
  93. this.Labour.active = false;
  94. if (this.Shop.active)
  95. this.Shop.active = false;
  96. if (this.SpeBuilding.active)
  97. this.SpeBuilding.active = false;
  98. if (this.Seed.active)
  99. this.Seed.active = false;
  100. if (this.Fruit.active)
  101. this.Fruit.active = false;
  102. },
  103. start() {
  104. //默认设置第一个节点为 最上面
  105. this.EvnBtn.setSiblingIndex(10);
  106. //获取种子和果实
  107. GlobalD.GameData.getWarehouseSeedAndFruit((res, vaule) => {
  108. this.mySeedList = vaule.data.seed;
  109. this.myFruitList = vaule.data.fruit;
  110. console.log(this.mySeedList, this.myFruitList);
  111. for (let i = 0; i < this.mySeedList.length; i++) {
  112. //没有种子数量不显示 todo 看看后续需不需要处理删除
  113. if (this.mySeedList[i].amount < 1) continue;
  114. let _seed = cc.instantiate(this.seedPrefab);
  115. _seed.parent = this.Seed;
  116. let _seedScript = _seed.getComponent("Content_Button");
  117. //设置一个生成点
  118. let _spawnScript = _seed.getComponent("Content_seed");
  119. _spawnScript.SpawnPoint = cc.find("Canvas/SpawnParent/SpawnPoint");
  120. _spawnScript.goodsSeedInfo = this.mySeedList[i];//这里传递一个生成的种子信息过去
  121. _seedScript.NumLabel.string = this.mySeedList[i].amount;
  122. _seedScript.NameLabel.string = this.mySeedList[i].name;
  123. switch (this.mySeedList[i].picture) {
  124. case "Cabbage":
  125. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
  126. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[0];
  127. break;
  128. case "Potato":
  129. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
  130. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[1];
  131. break;
  132. case "Carrot":
  133. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
  134. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[2];
  135. break;
  136. case "Broccoli":
  137. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
  138. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[3];
  139. break;
  140. case "Tomato":
  141. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
  142. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[4];
  143. break;
  144. case "Squash":
  145. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
  146. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[5];
  147. break;
  148. case "Eggplant":
  149. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
  150. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[6];
  151. break;
  152. case "Pepper":
  153. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
  154. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[7];
  155. break;
  156. default:
  157. break;
  158. }
  159. //获取一下种子图片
  160. _spawnScript.seedSpriteFrame = _seedScript.bgSprite.spriteFrame;
  161. }
  162. for (let i = 0; i < this.myFruitList.length; i++) {
  163. let _fruit = cc.instantiate(this.fruitPrefab);
  164. _fruit.parent = this.Fruit;
  165. _fruit.zIndex = 999;
  166. let _fruitScript = _fruit.getComponent("Content_Button");
  167. _fruitScript.NumLabel.string = this.myFruitList[i].amount;
  168. _fruitScript.NameLabel.string = this.myFruitList[i].name;
  169. _fruitScript.Name = this.myFruitList[i].name;
  170. _fruitScript.SNB = this.myFruitList[i].priceSnb;
  171. _fruitScript.Synopsis = this.myFruitList[i].fruitDescribe;
  172. //设置果实信息
  173. let _contentFruit = _fruit.getComponent("Content_fruit");
  174. _contentFruit.fruitInfo = this.myFruitList[i];
  175. switch (this.myFruitList[i].picture) {
  176. case "Cabbage":
  177. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[0];
  178. break;
  179. case "Potato":
  180. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[1];
  181. break;
  182. case "Carrot":
  183. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[2];
  184. break;
  185. case "Broccoli":
  186. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[3];
  187. break;
  188. case "Tomato":
  189. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[4];
  190. break;
  191. case "Squash":
  192. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[5];
  193. break;
  194. case "Eggplant":
  195. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[6];
  196. break;
  197. case "Pepper":
  198. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[7];
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. });
  205. },
  206. //切换建筑的面板
  207. onSwitchBuildingContent(event, index) {
  208. this.EvnBtn.setScale(1);
  209. this.LabourBtn.setScale(1);
  210. this.ShopBtn.setScale(1);
  211. this.SpeBuildingBtn.setScale(1);
  212. this.SeedBtn.setScale(1);
  213. this.FruitBtn.setScale(1);
  214. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite;
  215. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Labour_Sprite;
  216. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite;
  217. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Spe_Sprite;
  218. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  219. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite;
  220. this.EvnBtn.setContentSize(100, 54);
  221. this.LabourBtn.setContentSize(100, 54);
  222. this.ShopBtn.setContentSize(100, 54);
  223. this.SpeBuildingBtn.setContentSize(100, 54);
  224. this.SeedBtn.setContentSize(100, 54);
  225. this.FruitBtn.setContentSize(100, 54);
  226. this.EvnBtn.y = -5;
  227. this.LabourBtn.y = -5;
  228. this.ShopBtn.y = -5;
  229. this.SpeBuildingBtn.y = -5;
  230. this.SeedBtn.y = -5;
  231. this.FruitBtn.y = -5;
  232. this.HiddenAll();
  233. //面板设置index
  234. //环境
  235. if ('0' == index) {
  236. this.EvnBtn.y = -20;
  237. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Env_Sprite;
  238. this.Env.active = true;
  239. this.SelectedFrame.parent = this.Building00;
  240. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building00.getComponent('Content_Button').Name, this.Building00.getComponent('Content_Button').Price, this.Building00.getComponent('Content_Button').Synopsis);
  241. //设置节点顺序
  242. this.EvnBtn.setSiblingIndex(10);
  243. this.EvnBtn.setContentSize(86, 88);
  244. }
  245. //劳动
  246. else if ('1' == index) {
  247. this.LabourBtn.y = -20;
  248. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Labour_Sprite;
  249. this.Labour.active = true;
  250. this.SelectedFrame.parent = this.Building10;
  251. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building10.getComponent('Content_Button').Name, this.Building10.getComponent('Content_Button').Price, this.Building10.getComponent('Content_Button').Synopsis);
  252. this.LabourBtn.setSiblingIndex(10);
  253. this.LabourBtn.setContentSize(86, 88);
  254. }
  255. //商铺
  256. else if ('2' == index) {
  257. this.ShopBtn.y = -20;
  258. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Shop_Sprite;
  259. this.Shop.active = true;
  260. this.SelectedFrame.parent = this.Building20;
  261. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building20.getComponent('Content_Button').Name, this.Building20.getComponent('Content_Button').Price, this.Building20.getComponent('Content_Button').Synopsis);
  262. this.ShopBtn.setSiblingIndex(10);
  263. this.ShopBtn.setContentSize(86, 88);
  264. }
  265. //特殊
  266. else if ('3' == index) {
  267. this.SpeBuildingBtn.y = -20;
  268. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Spe_Sprite;
  269. this.SpeBuilding.active = true;
  270. this.SelectedFrame.parent = this.Building30;
  271. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building30.getComponent('Content_Button').Name, this.Building30.getComponent('Content_Button').Price, this.Building30.getComponent('Content_Button').Synopsis);
  272. this.SpeBuildingBtn.setSiblingIndex(10);
  273. this.SpeBuildingBtn.setContentSize(86, 88);
  274. }
  275. //种子
  276. else if ('4' == index) {
  277. this.SeedBtn.y = -20;
  278. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  279. this.Seed.active = true;
  280. this.SelectedFrame.parent = this.Building40;
  281. if (this.mySeedList.length > 0) {
  282. let Synopsis = "成熟期:" + this.mySeedList[0].maturity;
  283. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, this.mySeedList[0].priceSnb, Synopsis);
  284. }
  285. this.SeedBtn.setSiblingIndex(10);
  286. this.SeedBtn.setContentSize(86, 88);
  287. }
  288. //果实
  289. else if ('5' == index) {
  290. this.FruitBtn.y = -20;
  291. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Fruit_Sprite;
  292. this.Fruit.active = true;
  293. this.SelectedFrame.parent = this.Building50;
  294. if (this.myFruitList.length > 0) {
  295. let Synopsis = "这是个" + this.myFruitList[0].name + "果实!";
  296. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.myFruitList[0].name, this.myFruitList[0].priceSnb, Synopsis);
  297. }
  298. this.FruitBtn.setSiblingIndex(10);
  299. this.FruitBtn.setContentSize(86, 88);
  300. }
  301. //切换节点顺序
  302. // this.switch();
  303. },
  304. switch: function () {
  305. var children = this.container.children;
  306. var length = children.length;
  307. if (length > 1) {
  308. var src = Math.floor(Math.random() * length);
  309. var node = children[src];
  310. var dst = src === length - 1 ? 0 : src + 1;
  311. node.setSiblingIndex(dst);
  312. }
  313. },
  314. });