FreeUpgradeMediator.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. PlatformManager.hideBanner();
  93. }
  94. OnHide():void{
  95. this.RemoveEvent();
  96. }
  97. private AddEvent():void{
  98. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  99. }
  100. private RemoveEvent():void{
  101. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  102. }
  103. private GameModelPropertyChanged(key: string): void {
  104. switch (key) {
  105. case GamePropertys.gold:
  106. this.CallNextFrame(this.RefreshGlod.bind(this));
  107. break;
  108. case GamePropertys.diamond:
  109. this.CallNextFrame(this.RefreshDiamond.bind(this));
  110. break;
  111. }
  112. }
  113. private RefreshGlod(): void {
  114. if (this.glodLabel != null) {
  115. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  116. }
  117. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  118. }
  119. private RefreshDiamond(): void {
  120. if (this.diamondLabel != null) {
  121. this.diamondLabel.string = GameModel.single.diamond.toString();
  122. }
  123. }
  124. AbandonButtonClickHandler():void{
  125. GameModel.single.SynthesisWeapon(this.a.cellId,this.b.cellId);
  126. this.closeView();
  127. }
  128. /**
  129. * 视频按钮点击
  130. */
  131. VideoButtonClickHandler():void{
  132. PlatformManager.showRewardedVideo(()=>{
  133. if(this.AddWeapon(this.nextWeaponId)){
  134. this.closeView();
  135. }
  136. },()=>{
  137. NoticeManager.ShowPrompt("看视频失败");
  138. });
  139. }
  140. private AddWeapon(weaponId:number):boolean{
  141. let cell:WeaponCell=GameModel.single.FindWeaponEmptyCell();
  142. if(cell==null){
  143. NoticeManager.ShowPrompt("武器格子不足!");
  144. return false;
  145. }
  146. let select:boolean=false;
  147. if(GameModel.single.currentWeaponCell==this.a||GameModel.single.currentWeaponCell==this.b){
  148. select=true;
  149. }
  150. //删除A
  151. GameModel.single.RemoveWeapon(this.a.cellId);
  152. //删除B
  153. GameModel.single.RemoveWeapon(this.b.cellId);
  154. //添加新武器
  155. GameModel.single.AddWeapon(cell.cellId,weaponId);
  156. if(select){
  157. GameModel.single.currentWeaponCellId=cell.cellId;
  158. GameModel.single.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  159. }
  160. let synthesisMaxWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
  161. let newWeaponConfig:any=GameConfigManager.GetWeaponConfig(cell.weaponId);
  162. //新记录
  163. if (newWeaponConfig.level > synthesisMaxWeaponConfig.level) {
  164. GameModel.single.synthesisMaxWeaponId = cell.weaponId;
  165. if(GameModel.single.currentWeaponCell.weaponConfig && newWeaponConfig.level > GameModel.single.currentWeaponCell.weaponConfig.level){//如果比手上的好
  166. GameModel.single.EquipWeapon(cell);
  167. return;
  168. }
  169. }
  170. return true;
  171. }
  172. private closeView():void{
  173. GUIManager.single.Show(UIConst.PREPARE_UI);
  174. this.HideSelf();
  175. }
  176. start () {
  177. // Your initialization goes here.
  178. }
  179. // update (deltaTime: number) {
  180. // // Your update function goes here.
  181. // }
  182. }