ShopMediator.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { _decorator, Component, Node, Prefab, LayoutComponent, instantiate, ScrollViewComponent } from 'cc';
  2. import { GUIManager } from '../../../engines/gui/GUIManager';
  3. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  4. import GameConfigManager from '../../models/GameConfigManager';
  5. import { GameModel } from '../../models/GameModel';
  6. import { UIConst } from '../UIConst';
  7. import { ShopItemRenderScript } from './ShopItemRenderScript';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('ShopMediator')
  10. export class ShopMediator extends GUIMediator {
  11. @property({
  12. type:Prefab
  13. })
  14. ListItemRenderPrefab:Prefab=null;
  15. @property({
  16. type:LayoutComponent
  17. })
  18. ListContext:LayoutComponent=null;
  19. @property({
  20. type:ScrollViewComponent
  21. })
  22. List:ScrollViewComponent=null;
  23. /**
  24. * 需要包含在内的武器
  25. */
  26. private include:number[]=[];
  27. OnShow(data?:any):void{
  28. super.OnShow(data);
  29. this.RefreshList(true);
  30. this.AddEvent();
  31. }
  32. OnHide():void{
  33. this.RemoveEvent();
  34. }
  35. /**
  36. * 返回按钮点击
  37. */
  38. public BackButtonClick():void{
  39. GUIManager.single.Show(UIConst.PREPARE_UI);
  40. this.HideSelf();
  41. }
  42. private AddEvent():void{
  43. }
  44. private RemoveEvent():void{
  45. }
  46. private RefreshList(srollToZ:boolean):void{
  47. //根据当前枪生成包含列表
  48. this.buildInclude();
  49. let itemView:Node;
  50. let dataList:any[]=this.GetListData();
  51. if(dataList.length!=this.ListContext.node.children.length){
  52. if(dataList.length<this.ListContext.node.children.length){
  53. //删除多余的
  54. while(dataList.length<this.ListContext.node.children.length){
  55. this.ListContext.node.removeChild(this.ListContext.node.children[0]);
  56. }
  57. }else{
  58. //添加缺少的
  59. while(dataList.length>this.ListContext.node.children.length){
  60. itemView=instantiate(this.ListItemRenderPrefab);
  61. this.ListContext.node.addChild(itemView);
  62. }
  63. }
  64. }
  65. let itemScript:ShopItemRenderScript;
  66. let count:number=dataList.length;
  67. for (let index = 0; index < count; index++) {
  68. itemView=this.ListContext.node.children[index];
  69. itemScript=itemView.getComponent(ShopItemRenderScript);
  70. if(itemScript==null){
  71. throw new Error("商城列表项未挂载ShopItemRenderScript脚本!");
  72. }
  73. itemScript.UpdateItemRender(dataList[index]);
  74. }
  75. if(srollToZ){
  76. this.List.scrollToTop(0.1);
  77. }
  78. }
  79. private buildInclude():void{
  80. this.include.length=0;
  81. let currentWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
  82. let weaponConfig:any;
  83. //低级6个
  84. for (let index = 1; index <=6; index++) {
  85. weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level-index);
  86. if(weaponConfig!=null){
  87. this.include.push(weaponConfig.id);
  88. }
  89. }
  90. //当前
  91. this.include.push(GameModel.single.synthesisMaxWeaponId);
  92. //高级3个
  93. for(let index=1;index<4;index++){
  94. weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level+index);
  95. if(weaponConfig!=null){
  96. this.include.push(weaponConfig.id);
  97. }
  98. }
  99. }
  100. /**
  101. * 根据当前合成过的最大枪来计算列表数据
  102. */
  103. private GetListData():any[]{
  104. let result:any[]=[];
  105. let config:any=GameConfigManager.GetShopConfig(GameModel.single.synthesisMaxWeaponId);
  106. let weaponConfig:any;
  107. //解锁线
  108. let unlockLevel:number;
  109. if(config.unsealedGunId!=undefined){
  110. weaponConfig=GameConfigManager.GetWeaponConfig(config.unsealedGunId);
  111. unlockLevel=weaponConfig.level;
  112. }else{
  113. unlockLevel=-1;
  114. }
  115. let shopList:any[]=GameConfigManager.ShopList;
  116. shopList.sort((a,b)=> a.index-b.index);
  117. let itemData:any;
  118. for (let index = 0; index < shopList.length; index++) {
  119. const element = shopList[index];
  120. if(this.include.indexOf(element.id)<0){
  121. continue;
  122. }
  123. weaponConfig=GameConfigManager.GetWeaponConfig(element.id);
  124. itemData={
  125. id:element.id,
  126. index:element.index
  127. }
  128. //buyType 购买方式 0金币购买 1宝石购买 2 视频购买
  129. if(config.diamondPurchaseId==undefined){
  130. itemData.buyType=0;
  131. }else if(config.diamondPurchaseId.indexOf(element.id)>=0){
  132. itemData.buyType=1;
  133. }else if(config.videoId==undefined){
  134. itemData.buyType=0
  135. }else if(config.videoId==element.id){
  136. itemData.buyType=2;
  137. }else{
  138. itemData.buyType=0;
  139. }
  140. //state 0 未解锁 1 已解锁
  141. if(unlockLevel<0||weaponConfig.level>unlockLevel){
  142. itemData.state=0;
  143. }else{
  144. itemData.state=1;
  145. }
  146. result.push(itemData);
  147. }
  148. return result;
  149. }
  150. start () {
  151. // Your initialization goes here.
  152. }
  153. // update (deltaTime: number) {
  154. // // Your update function goes here.
  155. // }
  156. }