UserMgr.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Node, Prefab, _decorator, assetManager, find, instantiate, sys } from 'cc';
  2. import { UserModel } from '../Model/UserModel';
  3. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  4. import { GameEvent } from '../Enum/GameEvent';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('UserManager')
  7. export class UserManager {
  8. private static _instance: UserManager | null = null;
  9. public static get instance(): UserManager {
  10. if (!this._instance) this._instance = new UserManager();
  11. return this._instance;
  12. }
  13. public userModel: UserModel = null;
  14. initilizeModel(): void {
  15. this.userModel = new UserModel();
  16. this.userModel.initialize();
  17. }
  18. /**
  19. * 增加体力
  20. * @param value 增加的数量
  21. * @returns 增加后的体力值
  22. */
  23. public addPower(value: number): number {
  24. this.userModel.powerCurrent = Math.min(this.userModel.powerCurrent + value, this.userModel.powerMax);
  25. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  26. return this.userModel.powerCurrent;
  27. }
  28. /**
  29. * 减少体力
  30. * @param value 减少的数量
  31. * @returns 是否成功减少
  32. */
  33. public reducePower(value: number): boolean {
  34. if (this.userModel.powerCurrent >= value) {
  35. this.userModel.powerCurrent -= value;
  36. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  37. return true;
  38. }
  39. return false;
  40. }
  41. /**
  42. * 增加雷达免费次数
  43. * @param value 增加的数量
  44. * @returns 增加后的次数
  45. */
  46. public addRadarFreeCount(value: number): number {
  47. this.userModel.radarFreeCount += value;
  48. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  49. return this.userModel.radarFreeCount;
  50. }
  51. /**
  52. * 减少雷达免费次数
  53. * @param value 减少的数量
  54. * @returns 是否成功减少
  55. */
  56. public reduceRadarFreeCount(value: number): boolean {
  57. if (this.userModel.radarFreeCount >= value) {
  58. this.userModel.radarFreeCount -= value;
  59. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * 增加侦探免费次数
  66. * @param value 增加的数量
  67. * @returns 增加后的次数
  68. */
  69. public addFreeScreenShotCount(value: number): number {
  70. this.userModel.freeScreenShotCount += value;
  71. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  72. return this.userModel.freeScreenShotCount;
  73. }
  74. /**
  75. * 减少侦探免费次数
  76. * @param value 减少的数量
  77. * @returns 是否成功减少
  78. */
  79. public reduceFreeScreenShotCount(value: number): boolean {
  80. if (this.userModel.freeScreenShotCount >= value) {
  81. this.userModel.freeScreenShotCount -= value;
  82. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PLAYER_INFO);
  83. return true;
  84. }
  85. return false;
  86. }
  87. reset(): void {
  88. this.userModel.freeScreenShotCount = 0;
  89. this.userModel.radarFreeCount = 0;
  90. }
  91. }