UIGameHUD.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {
  2. _decorator,
  3. Button,
  4. Component,
  5. director,
  6. Label,
  7. Node,
  8. Sprite,
  9. } from "cc";
  10. const { ccclass, property, type } = _decorator;
  11. import { GameUILayers } from "../../scripts/GameUILayers";
  12. import { GameEvent, GameMgr } from "../../module_basic/scripts/GameMgr";
  13. import { ModuleDef } from "../../scripts/ModuleDef";
  14. import { RoomMgr } from "../../module_basic/scripts/RoomMgr";
  15. import { UIAlert } from "../../core_tgx/easy_ui_framework/alert/UIAlert";
  16. @ccclass("Layout_UIGameHUD")
  17. export class Layout_UIGameHUD extends Component {
  18. @property(Label) lblRoomId: Label;
  19. @property(Label) lblPos: Label;
  20. @property(Label) lblTimer: Label;
  21. @property(Label) lblScore: Label;
  22. @property(Button) btnExit: Button;
  23. }
  24. @tgx_class(ModuleDef.GAME)
  25. export class UIGameHUD extends tgx.UIController {
  26. public get layout(): Layout_UIGameHUD {
  27. return this._layout as Layout_UIGameHUD;
  28. }
  29. constructor() {
  30. super("ui_game_hud/ui_game_hud", GameUILayers.HUD, Layout_UIGameHUD);
  31. }
  32. protected onCreated(): void {
  33. this.layout.lblRoomId.string = RoomMgr.inst.data.displayId;
  34. this.onButtonEvent(this.layout.btnExit, () => {
  35. let op = UIAlert.show("Exit Game?", true);
  36. op.onClick((isOk) => {
  37. if (isOk) {
  38. director.emit(GameEvent.GAME_EXIT);
  39. }
  40. });
  41. });
  42. }
  43. fastFormatTimeStr(time: number) {
  44. if (time < 0) {
  45. time = 0;
  46. }
  47. let timeInSeconds = Math.floor(time / 1000);
  48. let s = (timeInSeconds % 60) as any;
  49. if (s < 10) {
  50. s = "0" + s;
  51. }
  52. let m = Math.floor(timeInSeconds / 60) as any;
  53. if (m < 10) {
  54. m = "0" + m;
  55. }
  56. return m + ":" + s;
  57. }
  58. refreshTimer() {
  59. let layout = this._layout as Layout_UIGameHUD;
  60. if (GameMgr.inst.gameData && GameMgr.inst.gameData.gameState == "playing") {
  61. layout.lblTimer.node.active = true;
  62. layout.lblTimer.string = this.fastFormatTimeStr(
  63. GameMgr.inst.gameStateEndTime - Date.now()
  64. );
  65. } else {
  66. layout.lblTimer.node.active = false;
  67. }
  68. }
  69. protected onUpdate(dt: number): void {
  70. let selfPlayer = GameMgr.inst.selfPlayer;
  71. if (selfPlayer == null) return;
  72. //this.layout.lblPos.string = 'x:' + ~~selfPlayer.transform[0] + ', y:' + ~~selfPlayer.transform[1];
  73. this.refreshTimer();
  74. this.layout.lblScore.string = "" + selfPlayer.weight;
  75. }
  76. }