BuildingView.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. this.onUpdateList();
  107. },
  108. onUpdateList() {
  109. //先判断container下面是否存在Seed
  110. // this.Seed.destroyAllChildren;
  111. // console.log("this.Seed.children:", this.Seed.children);
  112. //清除种子的预制
  113. let _children = this.Seed.children;
  114. for (let i = _children.length - 1; i > 0; i--) {
  115. _children[i].destroy();
  116. }
  117. //清除仓库的预制
  118. let _fruitChildren = this.Fruit.children;
  119. for (let i = _fruitChildren.length - 1; i > 0; i--) {
  120. _fruitChildren[i].destroy();
  121. }
  122. // console.log("this.Seed.children2:", this.Seed.children);
  123. //获取种子和果实
  124. GlobalD.GameData.getWarehouseSeedAndFruit((res, vaule) => {
  125. this.mySeedList = vaule.data.seed;
  126. this.myFruitList = vaule.data.fruit;
  127. // console.log(this.mySeedList, this.myFruitList);
  128. for (let i = 0; i < this.mySeedList.length; i++) {
  129. //没有种子数量不显示 todo 看看后续需不需要处理删除
  130. if (this.mySeedList[i].amount < 1) continue;
  131. let _seed = cc.instantiate(this.seedPrefab);
  132. _seed.parent = this.Seed;
  133. // if(0 == i){
  134. // this.Building40 = _seed;
  135. // }
  136. let _seedScript = _seed.getComponent("Content_Button");
  137. _seedScript.Name = this.mySeedList[i].name;
  138. _seedScript.Price = '';
  139. _seedScript.SNB = this.mySeedList[i].priceSnb;
  140. _seedScript.Synopsis = "成熟期:" + this.mySeedList[i].maturity + "天";
  141. //设置一个生成点
  142. let _spawnScript = _seed.getComponent("Content_seed");
  143. _spawnScript.SpawnPoint = cc.find("Canvas/SpawnParent/SpawnPoint");
  144. _spawnScript.goodsSeedInfo = this.mySeedList[i];//这里传递一个生成的种子信息过去
  145. _seedScript.NumLabel.string = this.mySeedList[i].amount;
  146. _seedScript.NameLabel.string = this.mySeedList[i].name;
  147. switch (this.mySeedList[i].picture) {
  148. case "Cabbage":
  149. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
  150. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[0];
  151. break;
  152. case "Potato":
  153. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
  154. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[1];
  155. break;
  156. case "Carrot":
  157. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
  158. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[2];
  159. break;
  160. case "Broccoli":
  161. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
  162. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[3];
  163. break;
  164. case "Tomato":
  165. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
  166. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[4];
  167. break;
  168. case "Squash":
  169. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
  170. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[5];
  171. break;
  172. case "Eggplant":
  173. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
  174. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[6];
  175. break;
  176. case "Pepper":
  177. _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
  178. _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[7];
  179. break;
  180. default:
  181. break;
  182. }
  183. //获取一下种子图片
  184. _spawnScript.seedSpriteFrame = _seedScript.bgSprite.spriteFrame;
  185. }
  186. for (let i = 0; i < this.myFruitList.length; i++) {
  187. let _fruit = cc.instantiate(this.fruitPrefab);
  188. _fruit.parent = this.Fruit;
  189. _fruit.zIndex = 999;
  190. let _fruitScript = _fruit.getComponent("Content_Button");
  191. _fruitScript.NumLabel.string = this.myFruitList[i].amount;
  192. _fruitScript.NameLabel.string = this.myFruitList[i].name;
  193. _fruitScript.Name = this.myFruitList[i].name;
  194. _fruitScript.SNB = this.myFruitList[i].priceSnb;
  195. _fruitScript.Synopsis = this.myFruitList[i].fruitDescribe;
  196. //设置果实信息
  197. let _contentFruit = _fruit.getComponent("Content_fruit");
  198. _contentFruit.fruitInfo = this.myFruitList[i];
  199. switch (this.myFruitList[i].picture) {
  200. case "Cabbage":
  201. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[0];
  202. break;
  203. case "Potato":
  204. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[1];
  205. break;
  206. case "Carrot":
  207. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[2];
  208. break;
  209. case "Broccoli":
  210. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[3];
  211. break;
  212. case "Tomato":
  213. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[4];
  214. break;
  215. case "Squash":
  216. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[5];
  217. break;
  218. case "Eggplant":
  219. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[6];
  220. break;
  221. case "Pepper":
  222. _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[7];
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. });
  229. },
  230. //切换建筑的面板
  231. onSwitchBuildingContent(event, index) {
  232. this.EvnBtn.setScale(1);
  233. this.LabourBtn.setScale(1);
  234. this.ShopBtn.setScale(1);
  235. this.SpeBuildingBtn.setScale(1);
  236. this.SeedBtn.setScale(1);
  237. this.FruitBtn.setScale(1);
  238. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite;
  239. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Labour_Sprite;
  240. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite;
  241. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Spe_Sprite;
  242. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  243. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite;
  244. this.EvnBtn.setContentSize(100, 54);
  245. this.LabourBtn.setContentSize(100, 54);
  246. this.ShopBtn.setContentSize(100, 54);
  247. this.SpeBuildingBtn.setContentSize(100, 54);
  248. this.SeedBtn.setContentSize(100, 54);
  249. this.FruitBtn.setContentSize(100, 54);
  250. this.EvnBtn.y = -5;
  251. this.LabourBtn.y = -5;
  252. this.ShopBtn.y = -5;
  253. this.SpeBuildingBtn.y = -5;
  254. this.SeedBtn.y = -5;
  255. this.FruitBtn.y = -5;
  256. this.HiddenAll();
  257. //面板设置index
  258. //环境
  259. if ('0' == index) {
  260. this.EvnBtn.y = -20;
  261. this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Env_Sprite;
  262. this.Env.active = true;
  263. // this.SelectedFrame.parent = this.Building00;
  264. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building00.getComponent('Content_Button').Name, this.Building00.getComponent('Content_Button').Price, this.Building00.getComponent('Content_Button').Synopsis);
  265. //设置节点顺序
  266. this.EvnBtn.setSiblingIndex(10);
  267. this.EvnBtn.setContentSize(86, 88);
  268. }
  269. //劳动
  270. else if ('1' == index) {
  271. this.LabourBtn.y = -20;
  272. this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Labour_Sprite;
  273. this.Labour.active = true;
  274. // this.SelectedFrame.parent = this.Building10;
  275. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building10.getComponent('Content_Button').Name, this.Building10.getComponent('Content_Button').Price, this.Building10.getComponent('Content_Button').Synopsis);
  276. this.LabourBtn.setSiblingIndex(10);
  277. this.LabourBtn.setContentSize(86, 88);
  278. }
  279. //商铺
  280. else if ('2' == index) {
  281. this.ShopBtn.y = -20;
  282. this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Shop_Sprite;
  283. this.Shop.active = true;
  284. // this.SelectedFrame.parent = this.Building20;
  285. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building20.getComponent('Content_Button').Name, this.Building20.getComponent('Content_Button').Price, this.Building20.getComponent('Content_Button').Synopsis);
  286. this.ShopBtn.setSiblingIndex(10);
  287. this.ShopBtn.setContentSize(86, 88);
  288. }
  289. //特殊
  290. else if ('3' == index) {
  291. this.SpeBuildingBtn.y = -20;
  292. this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Spe_Sprite;
  293. this.SpeBuilding.active = true;
  294. // this.SelectedFrame.parent = this.Building30;
  295. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building30.getComponent('Content_Button').Name, this.Building30.getComponent('Content_Button').Price, this.Building30.getComponent('Content_Button').Synopsis);
  296. this.SpeBuildingBtn.setSiblingIndex(10);
  297. this.SpeBuildingBtn.setContentSize(86, 88);
  298. }
  299. //种子
  300. else if ('4' == index) {
  301. this.SeedBtn.y = -20;
  302. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  303. this.Seed.active = true;
  304. // this.SelectedFrame.parent = this.Building40;
  305. if (this.mySeedList.length > 0) {
  306. let Synopsis = "成熟期:" + this.mySeedList[0].maturity + "天";
  307. // this.mySeedList[0].priceSnb
  308. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, "", Synopsis);
  309. }
  310. this.SeedBtn.setSiblingIndex(10);
  311. this.SeedBtn.setContentSize(86, 88);
  312. }
  313. //果实
  314. else if ('5' == index) {
  315. this.FruitBtn.y = -20;
  316. this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Fruit_Sprite;
  317. this.Fruit.active = true;
  318. // this.SelectedFrame.parent = this.Building50;
  319. if (this.myFruitList.length > 0) {
  320. let Synopsis = "这是个" + this.myFruitList[0].name + "果实!";
  321. this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.myFruitList[0].name, this.myFruitList[0].priceSnb, Synopsis);
  322. }
  323. this.FruitBtn.setSiblingIndex(10);
  324. this.FruitBtn.setContentSize(86, 88);
  325. }
  326. //切换节点顺序
  327. // this.switch();
  328. },
  329. switch: function () {
  330. var children = this.container.children;
  331. var length = children.length;
  332. if (length > 1) {
  333. var src = Math.floor(Math.random() * length);
  334. var node = children[src];
  335. var dst = src === length - 1 ? 0 : src + 1;
  336. node.setSiblingIndex(dst);
  337. }
  338. },
  339. });