uiSwitchBtnView.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import AccountModel from "../../../data/Account/AccountModel";
  2. import UIBase from "../../../framework/ui/UIBase";
  3. import UIHelp from "../../../framework/ui/UIHelp";
  4. import gameData, { PlayerData } from "../../../gameLogic/utrl/gameData";
  5. import { ATTACK_MODE, EVENT_TYPE } from "../../../gameLogic/utrl/gameEnum";
  6. import gameEventManager from "../../../gameLogic/utrl/gameEventManager";
  7. import bundleManager from "../../../manager/bundleManager";
  8. const { ccclass, property } = cc._decorator;
  9. /**武器切换按钮UI */
  10. @ccclass
  11. export default class UiSwitchBtnView extends UIBase {
  12. count: number = 0;
  13. onEnable() {
  14. gameEventManager.on(EVENT_TYPE.INIT_SWITCH_BTN, this.initView, this)
  15. gameEventManager.on(EVENT_TYPE.getGun_guide, this.getGunGuide, this)
  16. gameEventManager.on(EVENT_TYPE.attack_mode, this.changeFrames, this)
  17. }
  18. onDisable() {
  19. gameEventManager.off(EVENT_TYPE.INIT_SWITCH_BTN, this.initView, this)
  20. gameEventManager.off(EVENT_TYPE.attack_mode, this.changeFrames, this)
  21. gameEventManager.off(EVENT_TYPE.getGun_guide, this.getGunGuide, this)
  22. }
  23. start() {
  24. this.initSwitchState();
  25. }
  26. initSwitchState() {
  27. this.isNewUser();
  28. if (AccountModel.getInstance().is_new == 0 || !this.node.$guide2Lb || !this.node.$hand) {
  29. this.node.$switch_btn.active = true;
  30. } else {
  31. this.node.$switch_btn.active = false;
  32. }
  33. }
  34. isNewUser() {
  35. let playerDt = AccountModel.getInstance().playerDtList;
  36. if (playerDt.length > 0) {
  37. AccountModel.getInstance().is_new = 0;
  38. }
  39. }
  40. initView() {
  41. this.changeFrames();
  42. this.initSwitchState();
  43. if (this.node.$guide2Lb && this.node.$hand) {
  44. this.node.$guide2Lb.active = false;;
  45. this.node.$hand.active = false;
  46. }
  47. }
  48. changeFrames() {
  49. let is_close = gameData.cur_attack_mode == ATTACK_MODE.close;
  50. let switchBtnFrames = is_close ? 'joyStick/yuan1' : 'joyStick/jin1';
  51. let curModeFrames = is_close ? 'joyStick/jin2' : 'joyStick/yuan2';
  52. bundleManager.setBundleFrame('UI', switchBtnFrames, this.node.$switch_btn);
  53. bundleManager.setBundleFrame('UI', curModeFrames, this.node.$cur_modeSp);
  54. }
  55. getGunGuide() {
  56. if (this.node.$guide2Lb && this.node.$hand && this.node.$switch_btn) {
  57. this.node.$guide2Lb.active = true;
  58. this.node.$hand.active = true;
  59. this.node.$switch_btn.active = true;
  60. if (this.node.$hand) {
  61. cc.tween(this.node.$hand)
  62. .repeatForever(cc.tween().to(0.5, { angle: 0 }).to(0.5, { angle: 45 }))
  63. .start()
  64. }
  65. }
  66. }
  67. clickSwitchBtn() {
  68. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  69. if (this.isCanSwitch() == false) {
  70. return;
  71. }
  72. if(gameData.curBulletNum<=0){
  73. UIHelp.ShowTips('Out of ammo, switched to melee.');//当前子弹为0,无法切换成远程模式...
  74. return;
  75. }
  76. gameData.cur_attack_mode = ATTACK_MODE.remote;
  77. this.removeGetGunGuide();
  78. } else {
  79. gameData.cur_attack_mode = ATTACK_MODE.close;
  80. }
  81. gameEventManager.emit(EVENT_TYPE.attack_mode);
  82. this.changeFrames();
  83. cc.tween(this.node.$switch_btn.$switch_sp)
  84. .by(0.2, { angle: 180 })
  85. .start();
  86. }
  87. isCanSwitch() {
  88. let playerDt = AccountModel.getInstance().playerDtList;
  89. if (playerDt.length > 0) {
  90. for (let i = 0; i < playerDt.length; ++i) {
  91. if (playerDt[i].remote_weapon) {
  92. return true;
  93. }
  94. }
  95. }
  96. UIHelp.ShowTips('No ranged weapons...');//暂无远程武器...
  97. return false;
  98. }
  99. removeGetGunGuide() {
  100. let hand = this.node.$hand;
  101. let lb = this.node.$guide2Lb;
  102. if (hand && lb) {
  103. if (this.node.$hand.active && this.node.$guide2Lb.active) {
  104. this.node.$hand.destroy();
  105. this.node.$guide2Lb.destroy();
  106. }
  107. }
  108. }
  109. }