ShopMediator.ts 6.0 KB

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