AccelerateMediator.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { _decorator, Component, Node, ProgressBarComponent, LabelComponent } from 'cc';
  2. import BufferManager from '../../../engines/buffers/BufferManager';
  3. import IBuffer from '../../../engines/buffers/IBuffer';
  4. import { GUIManager } from '../../../engines/gui/GUIManager';
  5. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  6. import { DataModelEventType } from '../../../engines/models/DataModelEventType';
  7. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  8. import StringUtils from '../../../engines/utils/StringUtils';
  9. import { PlatformManager } from '../../../Platform/PlatformManager';
  10. import AccelerateBuffer from '../../buffers/AccelerateBuffer';
  11. import GameConfigManager from '../../models/GameConfigManager';
  12. import { GameModel } from '../../models/GameModel';
  13. import { GamePropertys } from '../../models/GamePropertys';
  14. import { UIConst } from '../UIConst';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('AccelerateMediator')
  17. export class AccelerateMediator extends GUIMediator {
  18. @property({
  19. type:ProgressBarComponent
  20. })
  21. progressBar:ProgressBarComponent=null;
  22. @property({
  23. type:LabelComponent
  24. })
  25. timeLabel:LabelComponent=null;
  26. @property({
  27. type:LabelComponent
  28. })
  29. diamondCLabel:LabelComponent=null;
  30. @property({
  31. type: LabelComponent
  32. })
  33. glodLabel: LabelComponent = null;
  34. @property({
  35. type: LabelComponent
  36. })
  37. glodLabel1: LabelComponent = null;
  38. @property({
  39. type: LabelComponent
  40. })
  41. diamondLabel: LabelComponent = null;
  42. /**
  43. * 最大时间
  44. */
  45. private max:number=150*1000*10;
  46. private time:number=150*1000;
  47. private consume:number=20;
  48. start():void{
  49. this.time=GameConfigManager.getGlobalValue("accelerateTime")*1000;
  50. this.max=GameConfigManager.getGlobalValue("accelerateMaxTime")*1000;
  51. this.consume=GameConfigManager.getGlobalValue("accelerateDiamondConsume");
  52. }
  53. OnShow(data?:any):void{
  54. super.OnShow(data);
  55. this.RefreshGlod();
  56. this.RefreshDiamond();
  57. //消耗
  58. this.diamondCLabel.string=this.consume.toString();
  59. this.AddEvent();
  60. }
  61. OnHide():void{
  62. this.RemoveEvent();
  63. }
  64. private AddEvent():void{
  65. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  66. }
  67. private RemoveEvent():void{
  68. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  69. }
  70. private GameModelPropertyChanged(key: string): void {
  71. switch (key) {
  72. case GamePropertys.gold:
  73. this.CallNextFrame(this.RefreshGlod.bind(this));
  74. break;
  75. case GamePropertys.diamond:
  76. this.CallNextFrame(this.RefreshDiamond.bind(this));
  77. break;
  78. }
  79. }
  80. private RefreshGlod(): void {
  81. if (this.glodLabel != null) {
  82. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  83. }
  84. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  85. }
  86. private RefreshDiamond(): void {
  87. if (this.diamondLabel != null) {
  88. this.diamondLabel.string = GameModel.single.diamond.toString();
  89. }
  90. }
  91. DiamondButtonClickHandler():void{
  92. if(GameModel.single.diamond>this.consume){
  93. if(this.CurrentBufferTime>=this.max){
  94. NoticeManager.ShowPrompt("加速时间已满!");
  95. return;
  96. }
  97. if(this.AddBuffer()){
  98. //扣钱
  99. GameModel.single.diamond-=this.consume;
  100. this.CloseButtonClickHandler();
  101. }
  102. }else{
  103. NoticeManager.ShowPrompt("钻石不足");
  104. }
  105. }
  106. /**
  107. * 视频按钮点击
  108. */
  109. VideoButtonClickHandler():void{
  110. if(this.CurrentBufferTime>=this.max){
  111. NoticeManager.ShowPrompt("加速时间已满!");
  112. return;
  113. }
  114. PlatformManager.showRewardedVideo(()=>{
  115. this.AddBuffer();
  116. },()=>{
  117. NoticeManager.ShowPrompt("看视频失败");
  118. });
  119. }
  120. /**
  121. * 关闭按钮点击
  122. */
  123. CloseButtonClickHandler():void{
  124. GUIManager.single.Show(UIConst.PREPARE_UI);
  125. this.HideSelf();
  126. }
  127. private AddBuffer():boolean{
  128. let buffer:AccelerateBuffer=new AccelerateBuffer("Accelerate",this.time);
  129. BufferManager.RunBuffer(buffer);
  130. return true;
  131. }
  132. private get CurrentBufferTime():number{
  133. let buffers:IBuffer[]=BufferManager.GetBufferGroup("Accelerate");
  134. if(buffers==null||buffers.length==0){
  135. return 0;
  136. }
  137. return buffers[0].GetTimeRemaining();
  138. }
  139. update(dt:number):void{
  140. this.progressBar.progress=this.CurrentBufferTime/this.max;
  141. this.timeLabel.string=StringUtils.TimeFormatting(this.CurrentBufferTime,":",":",":",":",":","");
  142. super.update(dt);
  143. }
  144. }