SettleUI.ts 9.7 KB

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