Player.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { _decorator } from 'cc';
  2. const { ccclass } = _decorator;
  3. @ccclass('Player')
  4. export class Player {
  5. private static _instance: Player;
  6. /** 玩家ID(可从服务端返回赋值) */
  7. public playerId: string = "";
  8. /** 玩家昵称 */
  9. public nickname: string = "Guest";
  10. /** 玩家等级 */
  11. public level: number = 1;
  12. /** 玩家当前分数 */
  13. public score: number = 0;
  14. /** 玩家货币类型(比如 "USD", "CNY", "GOLD") */
  15. public currencyType: string = "GOLD";
  16. public betConfig: number[] = [];
  17. /** 玩家登录状态 */
  18. public isLoggedIn: boolean = false;
  19. private constructor() {}
  20. /**
  21. * 获取单例实例
  22. */
  23. public static getInstance(): Player {
  24. if (!this._instance) {
  25. this._instance = new Player();
  26. }
  27. return this._instance;
  28. }
  29. /**
  30. * 初始化或从服务端同步玩家数据
  31. */
  32. public init(data: any) {
  33. if (!data) return;
  34. this.playerId = data.playerId || this.playerId;
  35. this.nickname = data.nickname || this.nickname;
  36. this.level = data.level || this.level;
  37. this.score = data.score || this.score;
  38. this.currencyType = data.currencyType || this.currencyType;
  39. this.betConfig = data.betConfig || this.betConfig;
  40. this.isLoggedIn = true;
  41. }
  42. /**
  43. * 增加分数
  44. */
  45. public addScore(amount: number) {
  46. this.score += amount;
  47. if (this.score < 0) this.score = 0;
  48. }
  49. /**
  50. * 增加金币或钻石
  51. */
  52. public addCurrency(type: 'coins' | 'gems', amount: number) {
  53. }
  54. /**
  55. * 消耗货币
  56. */
  57. public spendCurrency(type: 'coins' | 'gems', amount: number): boolean {
  58. return false;
  59. }
  60. /**
  61. * 重置玩家数据(例如登出时)
  62. */
  63. public reset() {
  64. this.playerId = "";
  65. this.nickname = "Guest";
  66. this.level = 1;
  67. this.score = 0;
  68. this.currencyType = "GOLD";
  69. this.isLoggedIn = false;
  70. }
  71. /**
  72. * 打印调试信息
  73. */
  74. public debugInfo() {
  75. console.log("=== Player Info ===");
  76. console.log(`ID: ${this.playerId}`);
  77. console.log(`Name: ${this.nickname}`);
  78. console.log(`Level: ${this.level}`);
  79. console.log(`Score: ${this.score}`);
  80. console.log(`Currency: ${this.currencyType}`);
  81. console.log("====================");
  82. }
  83. }