FreeUpgradeMediator.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { _decorator, Component, Node, LabelComponent, SpriteComponent, loader, SpriteFrame } 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 { NoticeManager } from '../../../engines/notices/NoticeManager';
  6. import StringUtils from '../../../engines/utils/StringUtils';
  7. import { PlatformManager } from '../../../Platform/PlatformManager';
  8. import GameConfigManager from '../../models/GameConfigManager';
  9. import { GameModel } from '../../models/GameModel';
  10. import { GamePropertys } from '../../models/GamePropertys';
  11. import { WeaponCell } from '../../models/weapons/WeaponCell';
  12. import { UIConst } from '../UIConst';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('FreeUpgradeMediator')
  15. export class FreeUpgradeMediator extends GUIMediator {
  16. @property({
  17. type: LabelComponent
  18. })
  19. glodLabel: LabelComponent = null;
  20. @property({
  21. type: LabelComponent
  22. })
  23. glodLabel1: LabelComponent = null;
  24. @property({
  25. type: LabelComponent
  26. })
  27. diamondLabel: LabelComponent = null;
  28. @property({
  29. type: SpriteComponent
  30. })
  31. weaponIcon:SpriteComponent=null;
  32. @property({
  33. type: LabelComponent
  34. })
  35. weaponLevel: LabelComponent = null;
  36. @property({
  37. type: LabelComponent
  38. })
  39. weaponName: LabelComponent = null;
  40. @property({
  41. type: SpriteComponent
  42. })
  43. targetWeaponIcon:SpriteComponent=null;
  44. @property({
  45. type: LabelComponent
  46. })
  47. targetWeaponLevel: LabelComponent = null;
  48. @property({
  49. type: LabelComponent
  50. })
  51. targetWeaponName: LabelComponent = null;
  52. private a:WeaponCell;
  53. private b:WeaponCell;
  54. private nextWeaponId:number;
  55. OnShow(data?:any):void{
  56. super.OnShow(data);
  57. this.a=data.a;
  58. this.b=data.b;
  59. let currentConfig:any=this.a.weaponConfig;
  60. let level:number=currentConfig.level+1;
  61. currentConfig=GameConfigManager.GetWeaponConfigByLevel(level);
  62. for (let index = 0; index < 2; index++) {
  63. const element:any=GameConfigManager.GetWeaponConfigByLevel(level);
  64. if(element!=null){
  65. if(GameConfigManager.WeaponIsMaxLevel(element.id)==false){
  66. level++;
  67. }
  68. }
  69. }
  70. let nextConfig:any=GameConfigManager.GetWeaponConfigByLevel(level);
  71. this.nextWeaponId=nextConfig.id;
  72. //当前
  73. loader.loadRes(currentConfig.icon+"/spriteFrame",SpriteFrame,(err,res:SpriteFrame)=>{
  74. if(err!=null){
  75. console.log("加载图标出错!");
  76. }
  77. this.weaponIcon.spriteFrame=res;
  78. });
  79. this.weaponName.string=currentConfig.name;
  80. this.weaponLevel.string=currentConfig.level;
  81. loader.loadRes(nextConfig.icon+"/spriteFrame",SpriteFrame,(err,res:SpriteFrame)=>{
  82. if(err!=null){
  83. console.log("加载图标出错!");
  84. }
  85. this.targetWeaponIcon.spriteFrame=res;
  86. });
  87. this.targetWeaponName.string=nextConfig.name;
  88. this.targetWeaponLevel.string=nextConfig.level;
  89. this.RefreshGlod();
  90. this.RefreshDiamond();
  91. this.AddEvent();
  92. GUIManager.single.Hide(UIConst.PREPARE_UI);
  93. PlatformManager.hideBanner();
  94. }
  95. OnHide():void{
  96. this.RemoveEvent();
  97. }
  98. private AddEvent():void{
  99. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  100. }
  101. private RemoveEvent():void{
  102. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  103. }
  104. private GameModelPropertyChanged(key: string): void {
  105. switch (key) {
  106. case GamePropertys.gold:
  107. this.CallNextFrame(this.RefreshGlod.bind(this));
  108. break;
  109. case GamePropertys.diamond:
  110. this.CallNextFrame(this.RefreshDiamond.bind(this));
  111. break;
  112. }
  113. }
  114. private RefreshGlod(): void {
  115. if (this.glodLabel != null) {
  116. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  117. }
  118. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  119. }
  120. private RefreshDiamond(): void {
  121. if (this.diamondLabel != null) {
  122. this.diamondLabel.string = GameModel.single.diamond.toString();
  123. }
  124. }
  125. AbandonButtonClickHandler():void{
  126. GameModel.single.SynthesisWeapon(this.a.cellId,this.b.cellId);
  127. this.closeView();
  128. }
  129. /**
  130. * 视频按钮点击
  131. */
  132. VideoButtonClickHandler():void{
  133. PlatformManager.showRewardedVideo(()=>{
  134. if(this.AddWeapon(this.nextWeaponId)){
  135. this.closeView();
  136. }
  137. },()=>{
  138. NoticeManager.ShowPrompt("看视频失败");
  139. });
  140. }
  141. private AddWeapon(weaponId:number):boolean{
  142. let cell:WeaponCell=GameModel.single.FindWeaponEmptyCell();
  143. if(cell==null){
  144. NoticeManager.ShowPrompt("武器格子不足!");
  145. return false;
  146. }
  147. let select:boolean=false;
  148. if(GameModel.single.currentWeaponCell==this.a||GameModel.single.currentWeaponCell==this.b){
  149. select=true;
  150. }
  151. //删除A
  152. GameModel.single.RemoveWeapon(this.a.cellId);
  153. //删除B
  154. GameModel.single.RemoveWeapon(this.b.cellId);
  155. //添加新武器
  156. GameModel.single.AddWeapon(cell.cellId,weaponId);
  157. if(select){
  158. GameModel.single.currentWeaponCellId=cell.cellId;
  159. GameModel.single.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  160. }
  161. let synthesisMaxWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
  162. let newWeaponConfig:any=GameConfigManager.GetWeaponConfig(cell.weaponId);
  163. //新记录
  164. if (newWeaponConfig.level > synthesisMaxWeaponConfig.level) {
  165. GameModel.single.synthesisMaxWeaponId = cell.weaponId;
  166. if(GameModel.single.currentWeaponCell.weaponConfig && newWeaponConfig.level > GameModel.single.currentWeaponCell.weaponConfig.level){//如果比手上的好
  167. GameModel.single.EquipWeapon(cell);
  168. return;
  169. }
  170. }
  171. return true;
  172. }
  173. private closeView():void{
  174. GUIManager.single.Show(UIConst.PREPARE_UI);
  175. this.HideSelf();
  176. }
  177. start () {
  178. // Your initialization goes here.
  179. }
  180. // update (deltaTime: number) {
  181. // // Your update function goes here.
  182. // }
  183. }