UITeam.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { _decorator, Button, Component, isValid, Label, Node, Sprite, Widget } from 'cc';
  2. import { ModuleDef } from '../../scripts/ModuleDef';
  3. import { GameUILayers } from '../../scripts/GameUILayers';
  4. import { UserMgr } from '../scripts/UserMgr';
  5. import { LobbyMgr } from '../scripts/LobbyMgr';
  6. import { GameSceneUtil } from '../scripts/GameSceneUtil';
  7. import { UIGameMatching } from '../ui_game_matching/UIGameMatching';
  8. import { UserLocalCache } from '../scripts/UserLocalCache';
  9. const { ccclass, property } = _decorator;
  10. /**
  11. * @en Layout component is used to mount on the root node of the UI corresponding to the Prefab, and declare a series of property references for tgxUIController to call.
  12. * @zh Layout 组件用于挂载到UI对应的Prefab根节点,并声明一系列 property 引用,供 tgxUIController 调用。
  13. */
  14. @ccclass('UITeam')
  15. export class Layout_UITeam extends Component {
  16. @property(Button) btnClose: Button;
  17. @property(Label) lblTeam: Label;
  18. @property(Sprite) sprIcon: Sprite;
  19. @property(Label) lblUserName: Label;
  20. @property(Label) roleName: Label;
  21. @property(Label) lblTips: Label;
  22. @property(Button) btnMatch: Button;
  23. @property(Button) btnRandomName: Button;
  24. @property([Button]) playerSlots: Button[] = [];
  25. }
  26. @tgx_class(ModuleDef.BASIC)
  27. export class UITeam extends tgx.UIController {
  28. constructor() {
  29. super('ui_team/ui_team', GameUILayers.POPUP, Layout_UITeam);
  30. }
  31. public get layout(): Layout_UITeam {
  32. return this._layout as Layout_UITeam;
  33. }
  34. protected async onCreated(args: any) {
  35. this.layout.lblTeam.string = UserMgr.inst.name + '的队伍';
  36. await UserMgr.inst.setUserIconAndName(UserMgr.inst.uid, this.layout.sprIcon, this.layout.lblUserName, ModuleDef.BASIC);
  37. this.layout.lblUserName.updateRenderData(true);
  38. this.layout.lblUserName.node.children.forEach(v=>{
  39. v.getComponent(Widget).updateAlignment();
  40. });
  41. for (let i = 0; i < this.layout.playerSlots.length; ++i) {
  42. this.onButtonEvent(this.layout.playerSlots[i], () => {
  43. tgx.UIAlert.show('功能暂未开放!');
  44. });
  45. }
  46. this.onButtonEvent(this.layout.btnClose,async ()=>{
  47. //@en if matching, need to cancel before closing
  48. //@zh 如果正在匹配中,则需要取消匹配
  49. if(this.isMatching){
  50. let ret = await LobbyMgr.inst.rpc_QuickPlayCancel();
  51. if(ret.isSucc){
  52. this.close();
  53. }
  54. }
  55. else{
  56. this.close();
  57. }
  58. });
  59. this.onButtonEvent(this.layout.btnRandomName,()=>{
  60. this.layout.roleName.string = UserMgr.inst.getRandomName(true);
  61. });
  62. this.onButtonEvent(this.layout.btnMatch,()=>{
  63. this._startMatchTime = Date.now();
  64. this.startMatch('normal');
  65. });
  66. this.layout.lblTips.node.active = false;
  67. this.layout.roleName.string = UserLocalCache.inst.getRoleName(UserMgr.inst.uid);
  68. }
  69. protected onUpdate(dt: number): void {
  70. if (this._startMatchTime > 0) {
  71. let elapsedTime = Math.floor((Date.now() - this._startMatchTime) / 1000);
  72. if (elapsedTime < 60) {
  73. this.layout.lblTips.string = '匹配中... ' + elapsedTime + 's';
  74. }
  75. else {
  76. let s = elapsedTime % 60;
  77. let m = Math.floor(elapsedTime / 60);
  78. this.layout.lblTips.string = '匹配中... ' + m + 'm' + s + 's';
  79. }
  80. }
  81. }
  82. private _startMatchTime = 0;
  83. private get isMatching(){
  84. return this._startMatchTime > 0;
  85. }
  86. async startMatch(type: string) {
  87. this.layout.btnMatch.node.active = false;
  88. this.layout.lblTips.node.active = true;
  89. let ret = await LobbyMgr.inst.rpc_QuickPlay(type);
  90. if(!isValid(this.node)){
  91. //@en if the node has been destroyed, it means the match has been canceled halfway
  92. //@zh 如果节点已被销毁,表示中途取消了匹配
  93. return;
  94. }
  95. if (!ret.isSucc) {
  96. tgx.UIAlert.show('未找到适合的对手');
  97. this.layout.btnMatch.node.active = true;
  98. this.layout.lblTips.node.active = false;
  99. this._startMatchTime = 0;
  100. }
  101. else {
  102. //@en inactive btnClose to forbidden users close this UI at the moment
  103. //@zh 禁用 btnClose,用户不能在此时关闭此界面
  104. this.layout.btnClose.node.active = false;
  105. //@en enter game and show the waiting UI.
  106. //@zh 进入游戏,并显示等待其他玩家界面
  107. let params = ret.res;
  108. tgx.UIMgr.inst.showUI(UIGameMatching,async (ui:UIGameMatching)=>{
  109. let ret = await GameSceneUtil.inst.enterGame(params, true);
  110. if(!ret.isSucc){
  111. //@en close matching ui, and restart to match
  112. //@zh 关闭面板,重新开始匹配
  113. ui.close();
  114. this.layout.btnClose.node.active = true;
  115. this.startMatch('normal');
  116. }
  117. else{
  118. this.close();
  119. }
  120. });
  121. }
  122. }
  123. }