ShopMediator.ts 7.0 KB

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