BuildingView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. _seedScript.NumLabel.string = this.mySeedList[i].amount;
  121. _seedScript.NameLabel.string = this.mySeedList[i].name;
  122. switch (this.mySeedList[i].picture) {
  123. case "Cabbage":
  124. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
  125. break;
  126. case "Potato":
  127. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
  128. break;
  129. case "Carrot":
  130. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
  131. break;
  132. case "Broccoli":
  133. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
  134. break;
  135. case "Tomato":
  136. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
  137. break;
  138. case "Squash":
  139. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
  140. break;
  141. case "Eggplant":
  142. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
  143. break;
  144. case "Pepper":
  145. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. for (let i = 0; i < this.myFruitList.length; i++) {
  152. let _fruit = cc.instantiate(this.fruitPrefab);
  153. _fruit.parent = this.Fruit;
  154. _fruit.zIndex = 999;
  155. let _fruitScript = _fruit.getComponent("Content_Button");
  156. _fruitScript.NumLabel.string = 1;
  157. _fruitScript.NameLabel.string = this.myFruitList[i].name;
  158. _fruitScript.Name = this.myFruitList[i].name;
  159. _fruitScript.SNB = this.myFruitList[i].priceSnb;
  160. _fruitScript.Synopsis = "出售SNB";
  161. switch (this.myFruitList[i].picture) {
  162. case "Cabbage":
  163. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
  164. break;
  165. case "Potato":
  166. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
  167. break;
  168. case "Carrot":
  169. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
  170. break;
  171. case "Broccoli":
  172. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
  173. break;
  174. case "Tomato":
  175. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
  176. break;
  177. case "Squash":
  178. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
  179. break;
  180. case "Eggplant":
  181. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
  182. break;
  183. case "Pepper":
  184. _fruitScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. });
  191. },
  192. //切换建筑的面板
  193. onSwitchBuildingContent(event, index) {
  194. this.EvnBtn.setScale(1);
  195. this.LabourBtn.setScale(1);
  196. this.ShopBtn.setScale(1);
  197. this.SpeBuildingBtn.setScale(1);
  198. this.SeedBtn.setScale(1);
  199. this.FruitBtn.setScale(1);
  200. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite;
  201. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Labour_Sprite;
  202. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite;
  203. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Spe_Sprite;
  204. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  205. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite;
  206. this.EvnBtn.setContentSize(100, 54);
  207. this.LabourBtn.setContentSize(100, 54);
  208. this.ShopBtn.setContentSize(100, 54);
  209. this.SpeBuildingBtn.setContentSize(100, 54);
  210. this.SeedBtn.setContentSize(100, 54);
  211. this.FruitBtn.setContentSize(100, 54);
  212. this.EvnBtn.y = -5;
  213. this.LabourBtn.y = -5;
  214. this.ShopBtn.y = -5;
  215. this.SpeBuildingBtn.y = -5;
  216. this.SeedBtn.y = -5;
  217. this.FruitBtn.y = -5;
  218. this.HiddenAll();
  219. //面板设置index
  220. //环境
  221. if ('0' == index) {
  222. this.EvnBtn.y = -20;
  223. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Env_Sprite;
  224. this.Env.active = true;
  225. this.SelectedFrame.parent = this.Building00;
  226. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building00.getComponent('Content_Button').Name, this.Building00.getComponent('Content_Button').Price, this.Building00.getComponent('Content_Button').Synopsis);
  227. //设置节点顺序
  228. this.EvnBtn.setSiblingIndex(10);
  229. this.EvnBtn.setContentSize(86, 88);
  230. }
  231. //劳动
  232. else if ('1' == index) {
  233. this.LabourBtn.y = -20;
  234. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Labour_Sprite;
  235. this.Labour.active = true;
  236. this.SelectedFrame.parent = this.Building10;
  237. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building10.getComponent('Content_Button').Name, this.Building10.getComponent('Content_Button').Price, this.Building10.getComponent('Content_Button').Synopsis);
  238. this.LabourBtn.setSiblingIndex(10);
  239. this.LabourBtn.setContentSize(86, 88);
  240. }
  241. //商铺
  242. else if ('2' == index) {
  243. this.ShopBtn.y = -20;
  244. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Shop_Sprite;
  245. this.Shop.active = true;
  246. this.SelectedFrame.parent = this.Building20;
  247. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building20.getComponent('Content_Button').Name, this.Building20.getComponent('Content_Button').Price, this.Building20.getComponent('Content_Button').Synopsis);
  248. this.ShopBtn.setSiblingIndex(10);
  249. this.ShopBtn.setContentSize(86, 88);
  250. }
  251. //特殊
  252. else if ('3' == index) {
  253. this.SpeBuildingBtn.y = -20;
  254. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Spe_Sprite;
  255. this.SpeBuilding.active = true;
  256. this.SelectedFrame.parent = this.Building30;
  257. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building30.getComponent('Content_Button').Name, this.Building30.getComponent('Content_Button').Price, this.Building30.getComponent('Content_Button').Synopsis);
  258. this.SpeBuildingBtn.setSiblingIndex(10);
  259. this.SpeBuildingBtn.setContentSize(86, 88);
  260. }
  261. //种子
  262. else if ('4' == index) {
  263. this.SeedBtn.y = -20;
  264. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  265. this.Seed.active = true;
  266. this.SelectedFrame.parent = this.Building40;
  267. if (this.mySeedList.length > 0) {
  268. let Synopsis = "成熟期:" + this.mySeedList[0].maturity;
  269. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, this.mySeedList[0].priceSnb, Synopsis);
  270. }
  271. this.SeedBtn.setSiblingIndex(10);
  272. this.SeedBtn.setContentSize(86, 88);
  273. }
  274. //果实
  275. else if ('5' == index) {
  276. this.FruitBtn.y = -20;
  277. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Fruit_Sprite;
  278. this.Fruit.active = true;
  279. this.SelectedFrame.parent = this.Building50;
  280. if (this.myFruitList.length > 0) {
  281. let Synopsis = "这是个" + this.myFruitList[0].name + "果实!";
  282. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.myFruitList[0].name, this.myFruitList[0].priceSnb, Synopsis);
  283. }
  284. this.FruitBtn.setSiblingIndex(10);
  285. this.FruitBtn.setContentSize(86, 88);
  286. }
  287. //切换节点顺序
  288. // this.switch();
  289. },
  290. switch: function () {
  291. var children = this.container.children;
  292. var length = children.length;
  293. if (length > 1) {
  294. var src = Math.floor(Math.random() * length);
  295. var node = children[src];
  296. var dst = src === length - 1 ? 0 : src + 1;
  297. node.setSiblingIndex(dst);
  298. }
  299. },
  300. });