LvData.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { _decorator, sys } from 'cc';
  2. import { IncrementData } from '../IncrementData';
  3. import { Hall } from '../../hall/Hall';
  4. import { data_type } from '../PlayerData';
  5. export enum LvDir {
  6. none = 0,
  7. left = 1,
  8. right = 2,
  9. up = 3,
  10. down = 4,
  11. xCenter = 5,
  12. xOut = 6,
  13. yCenter = 7,
  14. yOut = 8,
  15. }
  16. export class LvData {
  17. private static _instance: LvData = null;
  18. private _life: IncrementData = new IncrementData(); // 生命值
  19. private _is_win: boolean = false; // 连胜
  20. private _dir: LvDir = LvDir.none; // 关卡状态
  21. private static readonly STORAGE_KEY = 'game_data';
  22. private _rwpf: number = 1//人物皮肤
  23. private _fkpf: number = 1//方块皮肤
  24. private _stpf: number = 1//石头皮肤
  25. // 存储已解锁的皮肤ID数组
  26. private _unlockedRoleSkins: number[] = [1]; // 默认解锁第一个角色皮肤
  27. private _unlockedBlockSkins: number[] = [1]; // 默认解锁第一个方块皮肤
  28. private _unlockedStoneSkins: number[] = [1]; // 默认解锁第一个石头皮肤
  29. // 方块销毁统计
  30. private _destroyedBlocks: number = 0;
  31. private _totalBlocks: number = 0;
  32. // 单例模式
  33. public static get instance(): LvData {
  34. if (!this._instance) {
  35. this._instance = new LvData();
  36. this._instance.load(); // 加载保存的数据
  37. this._instance.updateDirByLevel(); // 初始化时设置关卡状态
  38. }
  39. return this._instance;
  40. }
  41. get lifeContor() {
  42. return this._life
  43. }
  44. private constructor() {
  45. }
  46. // 获取生命值
  47. public get life(): number {
  48. this._life.addLastTime(chsdk.date.now())
  49. return this._life.value;
  50. }
  51. // 设置生命值
  52. public set life(value: number) {
  53. this._life.value = value;
  54. this.save();
  55. }
  56. // 获取连胜状态
  57. public get is_win(): boolean {
  58. return this._is_win;
  59. }
  60. // 设置连胜状态
  61. public set is_win(value: boolean) {
  62. this._is_win = value;
  63. this.save();
  64. }
  65. // 获取关卡状态
  66. public get dir(): LvDir {
  67. return this._dir;
  68. }
  69. // 设置关卡状态
  70. public set dir(value: LvDir) {
  71. this._dir = value;
  72. this.save();
  73. }
  74. //人物皮肤
  75. public set rwpf(value: number) {
  76. this._rwpf = value;
  77. this.save();
  78. }
  79. public get rwpf(): number {
  80. return this._rwpf;
  81. }
  82. //方块皮肤
  83. public set fkpf(value: number) {
  84. this._fkpf = value;
  85. this.save();
  86. }
  87. public get fkpf(): number {
  88. return this._fkpf;
  89. }
  90. //石头皮肤
  91. public set stpf(value: number) {
  92. this._stpf = value;
  93. this.save();
  94. }
  95. public get stpf(): number {
  96. return this._stpf;
  97. }
  98. // 检查角色皮肤是否已解锁
  99. public isRoleSkinUnlocked(skinId: number): boolean {
  100. return this._unlockedRoleSkins.indexOf(skinId) !== -1;
  101. }
  102. // 解锁角色皮肤
  103. public unlockRoleSkin(skinId: number): void {
  104. if (this._unlockedRoleSkins.indexOf(skinId) === -1) { // 检查是否未解锁
  105. this._unlockedRoleSkins.push(skinId);
  106. this.save();
  107. }
  108. }
  109. // 检查方块皮肤是否已解锁
  110. public isBlockSkinUnlocked(skinId: number): boolean {
  111. return this._unlockedBlockSkins.indexOf(skinId) !== -1;
  112. }
  113. // 解锁方块皮肤
  114. public unlockBlockSkin(skinId: number): void {
  115. if (this._unlockedBlockSkins.indexOf(skinId) === -1) {
  116. this._unlockedBlockSkins.push(skinId);
  117. this.save();
  118. }
  119. }
  120. // 检查石头皮肤是否已解锁
  121. public isStoneSkinUnlocked(skinId: number): boolean {
  122. return this._unlockedStoneSkins.indexOf(skinId) !== -1;
  123. }
  124. // 解锁石头皮肤
  125. public unlockStoneSkin(skinId: number): void {
  126. if (this._unlockedStoneSkins.indexOf(skinId) === -1) {
  127. this._unlockedStoneSkins.push(skinId);
  128. this.save();
  129. }
  130. }
  131. // 初始化时设置总方块数
  132. public initBlockData(total: number) {
  133. this._totalBlocks = total;
  134. this._destroyedBlocks = 0;
  135. this.save();
  136. }
  137. // 增加销毁数量
  138. public addDestroyedBlocks(count: number) {
  139. this._destroyedBlocks += count;
  140. this._destroyedBlocks = Math.min(this._destroyedBlocks, this._totalBlocks);
  141. this.save();
  142. }
  143. // 获取进度
  144. public get blockProgress(): number {
  145. return this._totalBlocks > 0
  146. ? this._destroyedBlocks / this._totalBlocks
  147. : 0;
  148. }
  149. // 根据关卡自动设置对应的LvDir状态
  150. private updateDirByLevel() {
  151. // 计算有效关卡(10关后循环3-10)
  152. const lv = Hall.getInstance().player.data.get(data_type.max_floor)
  153. const cycleBase = lv > 10
  154. ? ((lv - 3) % 8) + 3 // 3-10的8个关卡循环
  155. : lv;
  156. switch (cycleBase) { // 改用计算后的有效关卡
  157. case 1:
  158. case 2:
  159. this._dir = LvDir.none;
  160. break;
  161. case 3:
  162. this._dir = LvDir.left;
  163. break;
  164. case 4:
  165. this._dir = LvDir.up;
  166. break;
  167. case 5:
  168. this._dir = LvDir.right;
  169. break;
  170. case 6:
  171. this._dir = LvDir.down;
  172. break;
  173. case 7:
  174. this._dir = LvDir.xOut;
  175. break;
  176. case 8:
  177. this._dir = LvDir.yOut;
  178. break;
  179. case 9:
  180. this._dir = LvDir.xCenter;
  181. break;
  182. case 10:
  183. this._dir = LvDir.yCenter;
  184. break;
  185. }
  186. }
  187. // 从本地存储加载数据
  188. private load() {
  189. const savedData = sys.localStorage.getItem(LvData.STORAGE_KEY);
  190. this._life.init(5 *60 * 1000,5,5);
  191. debugger
  192. if (savedData) {
  193. try {
  194. const data = JSON.parse(savedData);
  195. // this._lv = data.lv || 1;
  196. this._life.unserialize(data.life) ;
  197. this._is_win = data.is_win || false;
  198. this._dir = data.dir !== undefined ? data.dir : LvDir.none;
  199. this._rwpf = data.rwpf || 1;//人物皮肤
  200. this._fkpf = data.fkpf || 1;//方块皮肤
  201. this._stpf = data.stpf || 1;//石头皮肤
  202. // 加载已解锁的皮肤
  203. this._unlockedRoleSkins = data.unlockedRoleSkins || [1];
  204. this._unlockedBlockSkins = data.unlockedBlockSkins || [1];
  205. this._unlockedStoneSkins = data.unlockedStoneSkins || [1];
  206. } catch (error) {
  207. console.error('Failed to load saved data:', error);
  208. }
  209. }else{
  210. this._life.unserialize(null) ;
  211. }
  212. }
  213. // 保存数据到本地存储
  214. private save() {
  215. const data = {
  216. // lv: this._lv,
  217. life: this._life.serialize(),
  218. is_win: this._is_win,
  219. dir: this._dir,
  220. rwpf: this._rwpf,//人物皮肤
  221. fkpf: this._fkpf,//方块皮肤
  222. stpf: this._stpf,//石头皮肤
  223. // 保存已解锁的皮肤
  224. unlockedRoleSkins: this._unlockedRoleSkins,
  225. unlockedBlockSkins: this._unlockedBlockSkins,
  226. unlockedStoneSkins: this._unlockedStoneSkins
  227. };
  228. sys.localStorage.setItem(LvData.STORAGE_KEY, JSON.stringify(data));
  229. }
  230. // 重置游戏数据
  231. public reset() {
  232. // this._lv = 1;
  233. // this._life = 5;
  234. this._is_win = false;
  235. this._dir = LvDir.none;
  236. this._rwpf = 1;//人物皮肤
  237. this._fkpf = 1;//方块皮肤
  238. this._stpf = 1;//石头皮肤
  239. // 重置已解锁的皮肤
  240. this._unlockedRoleSkins = [1];
  241. this._unlockedBlockSkins = [1];
  242. this._unlockedStoneSkins = [1];
  243. this.save();
  244. }
  245. }