SettleUI.ts 9.3 KB

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