SettleUI.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { _decorator, Node, Label, Sprite, EventTouch, SpriteFrame} from 'cc';
  2. import { BaseExp } from '../core/base/BaseExp';
  3. import { autoBind } from '../extend/AutoBind';
  4. import { userIns } from '../data/UserData';
  5. import { Constants } from '../data/Constants';
  6. import { stateMgr } from '../core/manager/StateManager';
  7. import { uiMgr } from '../core/manager/UIManager';
  8. import { ITEM_TYPE, ResUtil } from '../utils/ResUtil';
  9. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  10. import { Utils } from '../utils/Utils';
  11. import { Game } from '../game/Game';
  12. import PlatformSystem from '../platform/PlatformSystem';
  13. import { audioMgr } from '../core/manager/AudioManager';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('SettleUI')
  16. export class SettleUI extends BaseExp {
  17. @property({ type: SpriteFrame, tooltip: "成功的图片" })
  18. public success_sf: SpriteFrame;
  19. @property({ type: SpriteFrame, tooltip: "失败的图片" })
  20. public fail_sf: SpriteFrame;
  21. @property({ type: SpriteFrame, tooltip: "枪默认图片" })
  22. public gun_def_sf: SpriteFrame;
  23. @autoBind({ type: Sprite, tooltip: "成功状态的图片" })
  24. public success_status_icon: Sprite;
  25. @autoBind({ type: Label, tooltip: "关卡等级" })
  26. public task_num_lable: Label;
  27. @autoBind({ type: Sprite, tooltip: "解锁的枪的图片" })
  28. public gun_icon: Sprite;
  29. @autoBind({ type: Label, tooltip: "解锁进度" })
  30. public progress_num_lable: Label;
  31. @autoBind({ type: Sprite, tooltip: "未解锁的枪的图片" })
  32. public gun_not_unlock_icon: Sprite;
  33. @autoBind({ type: Sprite, tooltip: "未解锁的枪的图片" })
  34. public gun_unlock_icon: Sprite;
  35. @autoBind({ type: Node, tooltip: "任务奖励按钮" })
  36. public task_reward_btn: Node;
  37. @autoBind({ type: Node, tooltip: "爆头奖励按钮" })
  38. public headshot_reward_btn: Node;
  39. @autoBind({ type: Node, tooltip: "下一关按钮" })
  40. public next_level_btn: Node;
  41. @autoBind({ type: Node, tooltip: "返回首页" })
  42. public back_home_btn: Node;
  43. @autoBind({ type: Node, tooltip: "重新开始" })
  44. public restart_btn: Node;
  45. @autoBind({ type: Node, tooltip: "看视频双倍奖励" })
  46. public video_btn: Node;
  47. @autoBind({ type: Label, tooltip: "金币文本" })
  48. public gold_lable: Label;
  49. @autoBind({ type: Label, tooltip: "钻石文本" })
  50. public diamond_lable: Label;
  51. //关卡数据
  52. private levelData:any = null;
  53. //传递过后的参数
  54. private param:any = null;
  55. /*const param:any = {
  56. isWin: false,//是否胜利
  57. headShotNum: Game.I.player.headShotNum,//爆头数量
  58. cb: ()=>{//回调函数
  59. }
  60. }*/
  61. start() {
  62. this.closeOnBlank = false;
  63. //注册动态变化值
  64. stateMgr.registerUI(Constants.gold, this.gold_lable);
  65. stateMgr.registerUI(Constants.diamond, this.diamond_lable);
  66. }
  67. public show(...args: any[]){
  68. this.hasAnim = true;
  69. this.param = args[0];
  70. //加载数据ui
  71. this.ui();
  72. }
  73. /**
  74. * 加载数据ui
  75. */
  76. public ui(){
  77. //先填充默认图片
  78. this.gun_not_unlock_icon.spriteFrame = this.gun_def_sf;
  79. this.gun_unlock_icon.spriteFrame = this.gun_def_sf;
  80. //成功失败的图片
  81. this.success_status_icon.spriteFrame = this.param.isWin? this.success_sf : this.fail_sf;
  82. const data: any = userIns.getCurLevelData();
  83. this.levelData = data;
  84. if(data){
  85. //关卡数
  86. this.task_num_lable.string = i18n("main.关卡任务 %{value}",{value: data.id});
  87. //枪的图片 解锁进度
  88. const unlock:string = data.unlock;
  89. if(!Utils.isNull(unlock)){
  90. let [gun_id,progress_str] = unlock.split('|');
  91. let [cur,max] = progress_str.split('/');
  92. let gunData:any = userIns.playerGunsTable.find(e=>e.id == gun_id);
  93. ResUtil.setSpriteFrame(gunData.gun_unlock_icon,this.gun_unlock_icon);
  94. ResUtil.setSpriteFrame(gunData.gun_not_unlock_icon,this.gun_not_unlock_icon);
  95. if(cur == max){//解锁该武器
  96. }
  97. //解锁进度
  98. this.progress_num_lable.string =`${cur}/${max}`;
  99. this.gun_not_unlock_icon.fillRange = 1 - Number(cur)/Number(max);
  100. }else{
  101. this.progress_num_lable.string = `looking forward to it`;
  102. }
  103. }
  104. //关卡的奖励
  105. const reward:string = this.param.isWin ? data.reward_1 : data.reward_2;
  106. let [g_id_1,num_1] = reward.split('_');
  107. //任务奖励
  108. let reward_icon_1: Node = this.task_reward_btn.getChildByName("reward_icon");
  109. const itemData:any = userIns.itemTable.find(e=>e.id == g_id_1);
  110. //任务奖励图片
  111. ResUtil.setSpriteFrame(itemData.icon,reward_icon_1);
  112. //任务奖励数量
  113. this.task_reward_btn.getChildByName("reward_num_lable").getComponent(Label).string = `x${num_1}`;
  114. this.video_btn.getChildByName("reward_num_lable").getComponent(Label).string = `x${num_1}`;
  115. //延迟0.5播放奖励动画
  116. this.scheduleOnce(() => {
  117. this.flyReward(itemData,Number(num_1),this.task_reward_btn);
  118. },0.5)
  119. //是否胜利 胜利的时候有爆头奖励
  120. const isShowHeadshot:boolean = this.param.isWin && this.param.headShotNum > 0;
  121. this.headshot_reward_btn.active = isShowHeadshot;
  122. if(isShowHeadshot){
  123. //爆头奖励
  124. let [g_id_2,num_2] = Game.I.player.pData.headshotReward.split('_');
  125. const t_num_2:number = Number(num_2) * Game.I.player.headShotNum;
  126. let reward_icon_2: Node = this.headshot_reward_btn.getChildByName("reward_icon");
  127. const hData:any = userIns.itemTable.find(e=>e.id == g_id_2);
  128. //爆头任务奖励图片
  129. ResUtil.setSpriteFrame(hData.icon,reward_icon_2);
  130. //爆头奖励数量
  131. this.headshot_reward_btn.getChildByName("reward_num_lable").getComponent(Label).string = `x${t_num_2}`;
  132. //延迟0.5播放奖励动画
  133. this.scheduleOnce(() => {
  134. this.flyReward(hData,Number(t_num_2),this.headshot_reward_btn);
  135. },0.5)
  136. }
  137. //下一关按钮
  138. this.next_level_btn.active = this.param.isWin;
  139. //重新开始按钮
  140. this.restart_btn.active = !this.param.isWin;
  141. }
  142. /**
  143. * 飞钻石和金币的奖励
  144. * @param data 奖励的数据
  145. * @param num 数量
  146. * @param clikTarget 飞动的节点
  147. */
  148. public flyReward(data:any,num:number,clikTarget: Node){
  149. if(num <= 0)return;
  150. audioMgr.playOneShot(Constants.audios.reward);
  151. if(data.type == 1){
  152. userIns.data.gold += num;
  153. ResUtil.flyAnim(ITEM_TYPE.Coin, clikTarget, this.gold_lable.node, 5, 50,(b) => {});
  154. }else if(data.type == 2){
  155. userIns.data.diamond += num;
  156. ResUtil.flyAnim(ITEM_TYPE.Diamond, clikTarget, this.diamond_lable.node, 5, 50,(b) => {});
  157. }
  158. }
  159. /**
  160. * 按钮点击事件
  161. * @param event 事件
  162. * @param param 参数
  163. */
  164. override onBtnClicked(event:EventTouch, param:any) {
  165. super.onBtnClicked(event, param);
  166. let btnName = event.target.name;
  167. if(btnName === 'restart_btn'){//重新开始
  168. uiMgr.hide(Constants.popUIs.settleUI,this.param.cb);
  169. Game.I.restart();
  170. }else if(btnName ==='back_home_btn'){//返回首页
  171. uiMgr.pop(Constants.mainUIs.main);
  172. }else if(btnName ==='next_level_btn'){//下一关
  173. //下一关判断是否触发广告
  174. let odds:number = Game.I.player.pData.adforceShow2;
  175. if(Utils.getRandomFloat(0,1) <= odds){
  176. PlatformSystem.platform.showRewardVideo((f) => {
  177. if(f) {//播放视频成功
  178. uiMgr.hide(Constants.popUIs.settleUI,this.param.cb);
  179. Game.I.nextLevel();
  180. }
  181. });
  182. }else{
  183. uiMgr.hide(Constants.popUIs.settleUI,this.param.cb);
  184. Game.I.nextLevel();
  185. }
  186. }else if(btnName ==='video_btn'){//看视频双倍奖励
  187. PlatformSystem.platform.showRewardVideo((f) => {
  188. if(f) {//播放视频成功
  189. this.video_btn.active = false;
  190. this.scheduleOnce(() => {
  191. //视频再奖励一次
  192. const reward:string = this.param.isWin ? this.levelData.reward_1 : this.levelData.reward_2;
  193. let [g_id_1,num_1] = reward.split('_');
  194. const itemData:any = userIns.itemTable.find(e=>e.id == g_id_1);
  195. this.flyReward(itemData,Number(num_1),this.video_btn);
  196. },0.3)
  197. }
  198. });
  199. }
  200. }
  201. }