ShopMediator.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { _decorator, Component, Node, Prefab, LayoutComponent, instantiate, ScrollViewComponent, LabelComponent, ToggleComponent, JsonAsset } 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 { FenceItemRenderScript } from './FenceItemRenderScript';
  11. import { ShopItemRenderScript } from './ShopItemRenderScript';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('ShopMediator')
  14. export class ShopMediator extends GUIMediator {
  15. @property({
  16. type: LabelComponent
  17. })
  18. glodLabel: LabelComponent = null;
  19. @property({
  20. type: LabelComponent
  21. })
  22. glodLabel1: LabelComponent = null;
  23. @property({
  24. type: LabelComponent
  25. })
  26. diamondLabel: LabelComponent = null;
  27. @property({
  28. type:Prefab
  29. })
  30. WeaponListItemRenderPrefab:Prefab=null;
  31. @property({
  32. type:LayoutComponent
  33. })
  34. WeaponListContext:LayoutComponent=null;
  35. @property({
  36. type:ScrollViewComponent
  37. })
  38. WeaponList:ScrollViewComponent=null;
  39. @property({
  40. type:Prefab
  41. })
  42. FenceListItemRenderPrefab:Prefab=null;
  43. @property({
  44. type:LayoutComponent
  45. })
  46. FenceListContext:LayoutComponent=null;
  47. @property({
  48. type:ScrollViewComponent
  49. })
  50. FenceList:ScrollViewComponent=null;
  51. /**
  52. * 需要包含在内的武器
  53. */
  54. private include:number[]=[];
  55. private showType:number=0;
  56. OnShow(data?:any):void{
  57. super.OnShow(data);
  58. this.SwitchToggle(null,this.showType,true);
  59. this.RefreshGlod();
  60. this.RefreshDiamond();
  61. this.AddEvent();
  62. }
  63. OnHide():void{
  64. this.RemoveEvent();
  65. }
  66. /**
  67. * 返回按钮点击
  68. */
  69. public BackButtonClick():void{
  70. GUIManager.single.Show(UIConst.PREPARE_UI);
  71. this.HideSelf();
  72. }
  73. private AddEvent():void{
  74. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
  75. }
  76. private RemoveEvent():void{
  77. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
  78. }
  79. private GameModelPropertyChanged(key:string) {
  80. if(key==GamePropertys.WeaponCell){
  81. this.RefreshWeaponList(false);
  82. }else if(key==GamePropertys.gold){
  83. this.CallNextFrame(this.RefreshGlod.bind(this));
  84. }else if(key==GamePropertys.diamond){
  85. this.CallNextFrame(this.RefreshDiamond.bind(this));
  86. }else if(key==GamePropertys.currentFenceId){
  87. this.CallNextFrame(this.RefreshFenceList.bind(this));
  88. }
  89. }
  90. private RefreshGlod(): void {
  91. if (this.glodLabel != null) {
  92. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  93. }
  94. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  95. }
  96. private RefreshDiamond(): void {
  97. if (this.diamondLabel != null) {
  98. this.diamondLabel.string = GameModel.single.diamond.toString();
  99. }
  100. }
  101. private RefreshWeaponList(srollToZ:boolean):void{
  102. //根据当前枪生成包含列表
  103. this.buildInclude();
  104. let itemView:Node;
  105. let dataList:any[]=this.GetListData();
  106. if(dataList.length!=this.WeaponListContext.node.children.length){
  107. if(dataList.length<this.WeaponListContext.node.children.length){
  108. //删除多余的
  109. while(dataList.length<this.WeaponListContext.node.children.length){
  110. this.WeaponListContext.node.removeChild(this.WeaponListContext.node.children[0]);
  111. }
  112. }else{
  113. //添加缺少的
  114. while(dataList.length>this.WeaponListContext.node.children.length){
  115. itemView=instantiate(this.WeaponListItemRenderPrefab);
  116. this.WeaponListContext.node.addChild(itemView);
  117. }
  118. }
  119. }
  120. let itemScript:ShopItemRenderScript;
  121. let count:number=dataList.length;
  122. for (let index = 0; index < count; index++) {
  123. itemView=this.WeaponListContext.node.children[index];
  124. itemScript=itemView.getComponent(ShopItemRenderScript);
  125. if(itemScript==null){
  126. throw new Error("商城列表项未挂载ShopItemRenderScript脚本!");
  127. }
  128. itemScript.UpdateItemRender(dataList[index]);
  129. }
  130. if(srollToZ){
  131. this.WeaponList.scrollToTop(0.1);
  132. }
  133. }
  134. private buildInclude():void{
  135. this.include.length=0;
  136. let currentWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
  137. let weaponConfig:any;
  138. //低级6个
  139. for (let index = 1; index <=6; index++) {
  140. weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level-index);
  141. if(weaponConfig!=null){
  142. this.include.push(weaponConfig.id);
  143. }
  144. }
  145. //当前
  146. this.include.push(GameModel.single.synthesisMaxWeaponId);
  147. //高级3个
  148. for(let index=1;index<4;index++){
  149. weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level+index);
  150. if(weaponConfig!=null){
  151. this.include.push(weaponConfig.id);
  152. }
  153. }
  154. }
  155. /**
  156. * 根据当前合成过的最大枪来计算列表数据
  157. */
  158. private GetListData():any[]{
  159. let result:any[]=[];
  160. let config:any=GameConfigManager.GetShopConfig(GameModel.single.synthesisMaxWeaponId);
  161. let weaponConfig:any;
  162. //解锁线
  163. let unlockLevel:number;
  164. if(config.unsealedGunId!=undefined){
  165. weaponConfig=GameConfigManager.GetWeaponConfig(config.unsealedGunId);
  166. unlockLevel=weaponConfig.level;
  167. }else{
  168. unlockLevel=-1;
  169. }
  170. let shopList:any[]=GameConfigManager.ShopList;
  171. shopList.sort((a,b)=> a.index-b.index);
  172. let itemData:any;
  173. for (let index = 0; index < shopList.length; index++) {
  174. const element = shopList[index];
  175. if(this.include.indexOf(element.id)<0){
  176. continue;
  177. }
  178. weaponConfig=GameConfigManager.GetWeaponConfig(element.id);
  179. itemData={
  180. id:element.id,
  181. index:element.index
  182. }
  183. //buyType 购买方式 0金币购买 1宝石购买 2 视频购买
  184. if(config.diamondPurchaseId==undefined){
  185. itemData.buyType=0;
  186. }else if(config.diamondPurchaseId.indexOf(element.id)>=0){
  187. itemData.buyType=1;
  188. }else if(config.videoId==undefined){
  189. itemData.buyType=0
  190. }else if(config.videoId==element.id){
  191. itemData.buyType=2;
  192. }else{
  193. itemData.buyType=0;
  194. }
  195. //state 0 未解锁 1 已解锁
  196. if(unlockLevel<0||weaponConfig.level>unlockLevel){
  197. itemData.state=0;
  198. }else{
  199. itemData.state=1;
  200. }
  201. result.push(itemData);
  202. }
  203. return result;
  204. }
  205. SwitchToggle(target:ToggleComponent,type:number,srollToZ:boolean):void{
  206. this.showType=type;
  207. //枪械
  208. if(type==0){
  209. this.WeaponList.node.active=true;
  210. this.FenceList.node.active=false;
  211. this.RefreshWeaponList(srollToZ);
  212. }else{//栏杆
  213. this.WeaponList.node.active=false;
  214. this.FenceList.node.active=true;
  215. this.RefreshFenceList(srollToZ);
  216. }
  217. }
  218. /**
  219. * 刷新栅栏列表
  220. */
  221. private RefreshFenceList(srollToZ:boolean=false):void{
  222. let itemView:Node;
  223. let jsonAsset:JsonAsset=GameConfigManager.GetConfig("Fence");
  224. let jsonConfig:any=jsonAsset.json;
  225. let dataList:any[]=jsonConfig;
  226. if(dataList.length!=this.FenceListContext.node.children.length){
  227. if(dataList.length<this.FenceListContext.node.children.length){
  228. //删除多余的
  229. while(dataList.length<this.FenceListContext.node.children.length){
  230. this.FenceListContext.node.removeChild(this.FenceListContext.node.children[0]);
  231. }
  232. }else{
  233. //添加缺少的
  234. while(dataList.length>this.FenceListContext.node.children.length){
  235. itemView=instantiate(this.FenceListItemRenderPrefab);
  236. this.FenceListContext.node.addChild(itemView);
  237. }
  238. }
  239. }
  240. let itemScript:FenceItemRenderScript;
  241. let count:number=dataList.length;
  242. for (let index = 0; index < count; index++) {
  243. itemView=this.FenceListContext.node.children[index];
  244. itemScript=itemView.getComponent(FenceItemRenderScript);
  245. if(itemScript==null){
  246. throw new Error("商城列表项未挂载FenceItemRenderScript脚本!");
  247. }
  248. itemScript.UpdateItemRender(dataList[index]);
  249. }
  250. if(srollToZ){
  251. this.FenceList.scrollToTop(0.1);
  252. }
  253. }
  254. start () {
  255. // Your initialization goes here.
  256. }
  257. // update (deltaTime: number) {
  258. // // Your update function goes here.
  259. // }
  260. }