BuildingView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 = 1;
  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 = "出售SNB";
  172. switch (this.myFruitList[i].picture) {
  173. case "Cabbage":
  174. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[0];
  175. break;
  176. case "Potato":
  177. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[1];
  178. break;
  179. case "Carrot":
  180. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[2];
  181. break;
  182. case "Broccoli":
  183. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[3];
  184. break;
  185. case "Tomato":
  186. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[4];
  187. break;
  188. case "Squash":
  189. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[5];
  190. break;
  191. case "Eggplant":
  192. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[6];
  193. break;
  194. case "Pepper":
  195. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[7];
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. });
  202. },
  203. //切换建筑的面板
  204. onSwitchBuildingContent(event, index) {
  205. this.EvnBtn.setScale(1);
  206. this.LabourBtn.setScale(1);
  207. this.ShopBtn.setScale(1);
  208. this.SpeBuildingBtn.setScale(1);
  209. this.SeedBtn.setScale(1);
  210. this.FruitBtn.setScale(1);
  211. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite;
  212. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Labour_Sprite;
  213. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite;
  214. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Spe_Sprite;
  215. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  216. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite;
  217. this.EvnBtn.setContentSize(100, 54);
  218. this.LabourBtn.setContentSize(100, 54);
  219. this.ShopBtn.setContentSize(100, 54);
  220. this.SpeBuildingBtn.setContentSize(100, 54);
  221. this.SeedBtn.setContentSize(100, 54);
  222. this.FruitBtn.setContentSize(100, 54);
  223. this.EvnBtn.y = -5;
  224. this.LabourBtn.y = -5;
  225. this.ShopBtn.y = -5;
  226. this.SpeBuildingBtn.y = -5;
  227. this.SeedBtn.y = -5;
  228. this.FruitBtn.y = -5;
  229. this.HiddenAll();
  230. //面板设置index
  231. //环境
  232. if ('0' == index) {
  233. this.EvnBtn.y = -20;
  234. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Env_Sprite;
  235. this.Env.active = true;
  236. this.SelectedFrame.parent = this.Building00;
  237. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building00.getComponent('Content_Button').Name, this.Building00.getComponent('Content_Button').Price, this.Building00.getComponent('Content_Button').Synopsis);
  238. //设置节点顺序
  239. this.EvnBtn.setSiblingIndex(10);
  240. this.EvnBtn.setContentSize(86, 88);
  241. }
  242. //劳动
  243. else if ('1' == index) {
  244. this.LabourBtn.y = -20;
  245. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Labour_Sprite;
  246. this.Labour.active = true;
  247. this.SelectedFrame.parent = this.Building10;
  248. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building10.getComponent('Content_Button').Name, this.Building10.getComponent('Content_Button').Price, this.Building10.getComponent('Content_Button').Synopsis);
  249. this.LabourBtn.setSiblingIndex(10);
  250. this.LabourBtn.setContentSize(86, 88);
  251. }
  252. //商铺
  253. else if ('2' == index) {
  254. this.ShopBtn.y = -20;
  255. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Shop_Sprite;
  256. this.Shop.active = true;
  257. this.SelectedFrame.parent = this.Building20;
  258. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building20.getComponent('Content_Button').Name, this.Building20.getComponent('Content_Button').Price, this.Building20.getComponent('Content_Button').Synopsis);
  259. this.ShopBtn.setSiblingIndex(10);
  260. this.ShopBtn.setContentSize(86, 88);
  261. }
  262. //特殊
  263. else if ('3' == index) {
  264. this.SpeBuildingBtn.y = -20;
  265. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Spe_Sprite;
  266. this.SpeBuilding.active = true;
  267. this.SelectedFrame.parent = this.Building30;
  268. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building30.getComponent('Content_Button').Name, this.Building30.getComponent('Content_Button').Price, this.Building30.getComponent('Content_Button').Synopsis);
  269. this.SpeBuildingBtn.setSiblingIndex(10);
  270. this.SpeBuildingBtn.setContentSize(86, 88);
  271. }
  272. //种子
  273. else if ('4' == index) {
  274. this.SeedBtn.y = -20;
  275. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  276. this.Seed.active = true;
  277. this.SelectedFrame.parent = this.Building40;
  278. if (this.mySeedList.length > 0) {
  279. let Synopsis = "成熟期:" + this.mySeedList[0].maturity;
  280. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, this.mySeedList[0].priceSnb, Synopsis);
  281. }
  282. this.SeedBtn.setSiblingIndex(10);
  283. this.SeedBtn.setContentSize(86, 88);
  284. }
  285. //果实
  286. else if ('5' == index) {
  287. this.FruitBtn.y = -20;
  288. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Fruit_Sprite;
  289. this.Fruit.active = true;
  290. this.SelectedFrame.parent = this.Building50;
  291. if (this.myFruitList.length > 0) {
  292. let Synopsis = "这是个" + this.myFruitList[0].name + "果实!";
  293. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.myFruitList[0].name, this.myFruitList[0].priceSnb, Synopsis);
  294. }
  295. this.FruitBtn.setSiblingIndex(10);
  296. this.FruitBtn.setContentSize(86, 88);
  297. }
  298. //切换节点顺序
  299. // this.switch();
  300. },
  301. switch: function () {
  302. var children = this.container.children;
  303. var length = children.length;
  304. if (length > 1) {
  305. var src = Math.floor(Math.random() * length);
  306. var node = children[src];
  307. var dst = src === length - 1 ? 0 : src + 1;
  308. node.setSiblingIndex(dst);
  309. }
  310. },
  311. });