LocalDataMgr.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import { JsonAsset, sys } from "cc";
  2. // 定义今天的数据类
  3. export class LocalTodayData {
  4. private _todayData: TodayData;
  5. constructor() {
  6. let defaultData: TodayData = {
  7. lookVedionum: 0,
  8. isUnlockAll: false,
  9. UnLockAllTimes: 0
  10. }
  11. this._todayData = sys.localStorage.getItem("todayData") ? Object.assign(defaultData, JSON.parse(sys.localStorage.getItem("todayData"))) : defaultData
  12. }
  13. get todayData(): TodayData {
  14. return this._todayData;
  15. }
  16. set todayData(data: Partial<TodayData>) {
  17. Object.assign(this._todayData, data);
  18. this.saveTodayData();
  19. }
  20. // 获取用户数据
  21. getTodayData(): TodayData {
  22. return this._todayData;
  23. }
  24. // 设置用户数据
  25. setTodayData(data: Partial<TodayData>): void {
  26. Object.assign(this._todayData, data);
  27. this.saveTodayData();
  28. }
  29. // 保存用户数据到本地存储
  30. public saveTodayData(): void {
  31. sys.localStorage.setItem("todayData", JSON.stringify(this._todayData));
  32. }
  33. }
  34. export const Today = new LocalTodayData();
  35. // 定义用户数据管理类
  36. export class LocalDataMgr {
  37. private _userData: UserData;
  38. private _lixiantime: number;
  39. private _lixiantime2: string;
  40. constructor() {
  41. this.lixianTime2 = ""
  42. let defaultData: UserData = {
  43. unlockUnits: [],
  44. sound: true,
  45. music: true,
  46. openid: "demo",
  47. leveltime: 120
  48. }
  49. this._userData = sys.localStorage.getItem("userData") ? Object.assign(defaultData, JSON.parse(sys.localStorage.getItem("userData"))) : defaultData
  50. this.lixianTime = sys.localStorage.getItem("lixianTime") || 0
  51. }
  52. get lixianTime() {
  53. return this._lixiantime
  54. }
  55. set lixianTime(time: number) {
  56. this._lixiantime = time
  57. sys.localStorage.setItem("lixianTime", time)
  58. }
  59. get lixianTime2() {
  60. return this._lixiantime2
  61. }
  62. set lixianTime2(time: string) {
  63. this._lixiantime2 = time
  64. sys.localStorage.setItem("lixianTime2", time)
  65. }
  66. get userData(): UserData {
  67. return this._userData;
  68. }
  69. // 获取用户数据
  70. getUserData(): UserData {
  71. return this._userData;
  72. }
  73. // 设置用户数据
  74. setUserData(data: Partial<UserData>): void {
  75. Object.assign(this._userData, data)
  76. this.saveUserData();
  77. }
  78. // 保存用户数据到本地存储
  79. saveUserData(): void {
  80. sys.localStorage.setItem("userData", JSON.stringify(this._userData));
  81. }
  82. startgametime: number = 0;
  83. entergametime: number = 0
  84. }
  85. export const User = new LocalDataMgr();
  86. export interface usercards {
  87. id: number,
  88. level: number,
  89. num: number
  90. }
  91. // 用户数据接口
  92. export interface UserData {
  93. unlockUnits: Array<number>;
  94. sound: boolean;
  95. music: boolean;
  96. openid: string;
  97. leveltime: number;
  98. }
  99. export interface TodayData {
  100. lookVedionum: number;
  101. isUnlockAll: boolean;
  102. UnLockAllTimes: number;
  103. }
  104. export interface DifficutyData {
  105. difficutyCount: number;
  106. scales: Array<number>
  107. }
  108. export class UserDifficutyData {
  109. private _difficutyData: DifficutyData;
  110. createOrDeleteOtherScale() {
  111. const targetCount = this._difficutyData.difficutyCount;
  112. const currentScales = this._difficutyData.scales;
  113. const defaultScales = [1.18, 1.16, 1.14, 0.3976, 0.3690, 0.3029, 0.0667, 0.0766, 0.0531, 0.0716];
  114. if (currentScales.length > targetCount) {
  115. currentScales.splice(targetCount, currentScales.length - targetCount);
  116. }
  117. else if (currentScales.length < targetCount) {
  118. for (let i = currentScales.length; i < targetCount; i++) {
  119. let value: number;
  120. if (i < defaultScales.length) {
  121. value = defaultScales[i];
  122. } else {
  123. value = Math.random() * 0.035 + 0.04;
  124. }
  125. currentScales.push(value);
  126. }
  127. }
  128. for (let i = 0; i < targetCount; i++) {
  129. if (currentScales[i] === undefined || currentScales[i] === null) {
  130. if (i < defaultScales.length) {
  131. currentScales[i] = defaultScales[i];
  132. } else {
  133. currentScales[i] = Math.random() * 0.035 + 0.04;
  134. }
  135. }
  136. }
  137. // 保存修改后的数据
  138. this.saveDifficutyData();
  139. }
  140. constructor() {
  141. const saved = sys.localStorage.getItem("difficutyData");
  142. this._difficutyData = saved
  143. ? Object.assign({}, DEFAULT_DIFFICULTY_DATA, JSON.parse(saved))
  144. : {...DEFAULT_DIFFICULTY_DATA};
  145. }
  146. get difficutyData(): DifficutyData {
  147. return this._difficutyData;
  148. }
  149. set difficutyData(data: Partial<DifficutyData>) {
  150. Object.assign(this._difficutyData, data);
  151. this.saveDifficutyData();
  152. }
  153. // 获取难度数据
  154. getDifficutyData(): DifficutyData {
  155. return this._difficutyData;
  156. }
  157. // 设置难度数据
  158. setDifficutyData(data: Partial<DifficutyData>): void {
  159. Object.assign(this._difficutyData, data);
  160. this.saveDifficutyData();
  161. }
  162. // 保存用户数据到本地存储
  163. public saveDifficutyData(): void {
  164. sys.localStorage.setItem("difficutyData", JSON.stringify(this._difficutyData));
  165. }
  166. setDataToDefault() {
  167. this._difficutyData = {...DEFAULT_DIFFICULTY_DATA};
  168. this.createOrDeleteOtherScale();
  169. }
  170. }
  171. const DEFAULT_DIFFICULTY_DATA: DifficutyData = {
  172. difficutyCount: 10,
  173. scales: [1.18, 1.16, 1.14, 0.3976, 0.3690, 0.3029,
  174. 0.0667, 0.0766, 0.0531, 0.0716]
  175. };
  176. export const Difficulty = new UserDifficutyData();
  177. export interface SkinData {
  178. singleSelect:string
  179. multiSelect:string
  180. selectedSkinList:Array<number>
  181. }
  182. export class UserSkinData {
  183. private _skinData: SkinData;
  184. private _selectSkins:string[] = []
  185. constructor() {
  186. let defaultData: SkinData = {
  187. singleSelect: "true",
  188. multiSelect: "false",
  189. selectedSkinList: []
  190. }
  191. this._skinData = sys.localStorage.getItem("skinData") ? Object.assign(defaultData, JSON.parse(sys.localStorage.getItem("skinData"))) : defaultData
  192. }
  193. getSelectSkinPath():string[]
  194. {
  195. let selectSkinsKeys = this._skinData.selectedSkinList
  196. this._selectSkins = []
  197. for(let key of selectSkinsKeys)
  198. {
  199. let skin = allSkinList[key]
  200. this._selectSkins.push(skin)
  201. }
  202. this._selectSkins = this._selectSkins.length > 0?this._selectSkins:[defaultSkin]
  203. return this._selectSkins
  204. }
  205. setSelectStatu(singleSelect:boolean)
  206. {
  207. this._skinData.singleSelect = singleSelect.toString()
  208. this._skinData.multiSelect = (!singleSelect).toString()
  209. this.saveSkinData()
  210. }
  211. get skinData(): SkinData {
  212. return this._skinData;
  213. }
  214. set skinData(data: Partial<SkinData>) {
  215. Object.assign(this._skinData, data);
  216. this.saveSkinData();
  217. }
  218. // 获取皮肤数据
  219. getSkinData(): SkinData {
  220. return this._skinData;
  221. }
  222. // 设置皮肤数据
  223. setSkinData(data: Partial<SkinData>): void {
  224. Object.assign(this._skinData, data);
  225. this.saveSkinData();
  226. }
  227. // 保存用户数据到本地存储
  228. public saveSkinData(): void {
  229. sys.localStorage.setItem("skinData", JSON.stringify(this._skinData));
  230. }
  231. addSelectSkin(key:number)
  232. {
  233. let index = this._skinData.selectedSkinList.indexOf(key)
  234. if(index == -1)
  235. {
  236. this._skinData.selectedSkinList.push(key)
  237. }
  238. this.saveSkinData()
  239. }
  240. removeSelectSkin(key:number)
  241. {
  242. let index = this._skinData.selectedSkinList.indexOf(key)
  243. if(index != -1)
  244. {
  245. this._skinData.selectedSkinList.splice(index,1)
  246. }
  247. this.saveSkinData()
  248. }
  249. removeAllSelectSkin()
  250. {
  251. this._skinData.selectedSkinList = []
  252. this.saveSkinData()
  253. }
  254. addAllSelectSkin()
  255. {
  256. this._skinData.selectedSkinList = []
  257. let allSkinkeys = getAllSkinKeys()
  258. for(let i = 0;i < allSkinkeys.length;i++)
  259. {
  260. this._skinData.selectedSkinList.push(allSkinkeys[i])
  261. }
  262. this.saveSkinData()
  263. }
  264. clearData()
  265. {
  266. let defaultData: SkinData = {
  267. singleSelect: "true",
  268. multiSelect: "false",
  269. selectedSkinList: []
  270. }
  271. this._skinData = defaultData
  272. this.saveSkinData()
  273. }
  274. }
  275. export const Skin = new UserSkinData();
  276. export interface MapData {
  277. types:Array<string>
  278. selectedMaps:Array<number>
  279. }
  280. export class UserMapData {
  281. private _mapData: MapData;
  282. private _selectMaps:string[] = []
  283. constructor() {
  284. let defaultData: MapData = {
  285. types: ["全部","爆量"],
  286. selectedMaps: []
  287. }
  288. this._mapData = sys.localStorage.getItem("mapData") ? Object.assign(defaultData, JSON.parse(sys.localStorage.getItem("mapData"))) : defaultData
  289. }
  290. get mapData(): MapData {
  291. return this._mapData;
  292. }
  293. set mapData(data: Partial<MapData>) {
  294. Object.assign(this._mapData, data);
  295. this.saveMapData();
  296. }
  297. getSelectMapPath():string[]
  298. {
  299. let selectMapKeys = this._mapData.selectedMaps
  300. this._selectMaps = []
  301. for(let key of selectMapKeys)
  302. {
  303. let map = allMapList[key]
  304. this._selectMaps.push(map)
  305. }
  306. this._selectMaps = this._selectMaps.length > 0?this._selectMaps:[defaultMap]
  307. return this._selectMaps
  308. }
  309. // 获取地图数据
  310. getMapData(): MapData {
  311. return this._mapData;
  312. }
  313. // 设置地图数据
  314. setMapData(data: Partial<MapData>): void {
  315. Object.assign(this._mapData, data);
  316. this.saveMapData();
  317. }
  318. // 保存用户数据到本地存储
  319. public saveMapData(): void {
  320. sys.localStorage.setItem("mapData", JSON.stringify(this._mapData));
  321. }
  322. clearData()
  323. {
  324. let defaultData: MapData = {
  325. types: ["全部","爆量"],
  326. selectedMaps: []
  327. }
  328. this._mapData = defaultData
  329. this.saveMapData()
  330. }
  331. addSelectMap(key:number)
  332. {
  333. let index = this._mapData.selectedMaps.indexOf(key)
  334. if(index == -1)
  335. {
  336. this._mapData.selectedMaps.push(key)
  337. }
  338. this.saveMapData()
  339. }
  340. removeSelectMap(key:number)
  341. {
  342. let index = this._mapData.selectedMaps.indexOf(key)
  343. if(index != -1)
  344. {
  345. this._mapData.selectedMaps.splice(index,1)
  346. }
  347. this.saveMapData()
  348. }
  349. removeAllSelectMap()
  350. {
  351. this._mapData.selectedMaps = []
  352. this.saveMapData()
  353. }
  354. addAllSelectMap()
  355. {
  356. this._mapData.selectedMaps = []
  357. let allMapkeys = getAllMapKeys()
  358. for(let i = 0;i < allMapkeys.length;i++)
  359. {
  360. this._mapData.selectedMaps.push(allMapkeys[i])
  361. }
  362. this.saveMapData()
  363. }
  364. getLastLevelMapKey(currentBgKey:number):number
  365. {
  366. if(this._mapData.selectedMaps.length == 0)
  367. {
  368. let curkeyIndex = getAllMapKeys().indexOf(currentBgKey)
  369. let result = curkeyIndex > 0?getAllMapKeys()[curkeyIndex - 1]:getAllMapKeys()[getAllMapKeys().length - 1]
  370. return result;
  371. }
  372. let index = this._mapData.selectedMaps.indexOf(currentBgKey)
  373. if(index > 0)
  374. {
  375. return this._mapData.selectedMaps[index - 1]
  376. }
  377. return this._mapData.selectedMaps[this._mapData.selectedMaps.length - 1]
  378. }
  379. getNextLevelMapKey(currentBgKey:number):number
  380. {
  381. if (this._mapData.selectedMaps.length == 0) {
  382. let curkeyIndex = getAllMapKeys().indexOf(currentBgKey)
  383. let result = curkeyIndex < getAllMapKeys().length - 1?getAllMapKeys()[curkeyIndex + 1]:getAllMapKeys()[0]
  384. return result;
  385. }
  386. let index = this._mapData.selectedMaps.indexOf(currentBgKey)
  387. if(index < this._mapData.selectedMaps.length - 1)
  388. {
  389. return this._mapData.selectedMaps[index + 1]
  390. }
  391. return this._mapData.selectedMaps[0]
  392. }
  393. }
  394. export const UserMap = new UserMapData()
  395. export const defaultMap = "image/bg1"
  396. export interface SuperFindData {
  397. title:string
  398. levelDesc:string
  399. imgPath:string
  400. currentBgKey:number
  401. }
  402. export class UserSuperFindData {
  403. private _superFindData: SuperFindData;
  404. constructor() {
  405. let obj = Skin.getSkinData().selectedSkinList[0]
  406. let defaultData: SuperFindData = {
  407. title: `找到${Difficulty.getDifficutyData().difficutyCount}个跳舞的小熊`,
  408. levelDesc: `疯狂最强找茬-找小熊-第${1}关`,
  409. imgPath: `image/bg1`,
  410. currentBgKey:1
  411. }
  412. this._superFindData = sys.localStorage.getItem("superFindData") ? Object.assign(defaultData, JSON.parse(sys.localStorage.getItem("superFindData"))) : defaultData
  413. }
  414. get superFindData(): SuperFindData {
  415. return this._superFindData;
  416. }
  417. set superFindData(data: Partial<SuperFindData>) {
  418. Object.assign(this._superFindData, data);
  419. this.saveSuperFindData();
  420. }
  421. // 获取疯狂找茬数据
  422. getSuperFindData(): SuperFindData {
  423. return this._superFindData;
  424. }
  425. // 设置疯狂找茬数据
  426. setSuperFindData(data: Partial<SuperFindData>): void {
  427. Object.assign(this._superFindData, data);
  428. this.saveSuperFindData();
  429. }
  430. // 保存数据到本地存储
  431. public saveSuperFindData(): void {
  432. sys.localStorage.setItem("superFindData", JSON.stringify(this._superFindData));
  433. }
  434. clearData()
  435. {
  436. let defaultData: SuperFindData = {
  437. title: `找到${Difficulty.getDifficutyData().difficutyCount}个跳舞的小熊`,
  438. levelDesc: `疯狂最强找茬-找小熊-第${1}关`,
  439. imgPath: `image/bg1`,
  440. currentBgKey:1
  441. }
  442. this._superFindData = defaultData
  443. this.saveSuperFindData()
  444. }
  445. lastLevel()
  446. {
  447. let lastKey = UserMap.getLastLevelMapKey(this._superFindData.currentBgKey)
  448. this._superFindData.currentBgKey = lastKey
  449. this.setTitle()
  450. this._superFindData.levelDesc = `疯狂最强找茬-找小熊-第${lastKey}关`
  451. this._superFindData.imgPath = allMapList[lastKey]
  452. this.saveSuperFindData()
  453. }
  454. nextLevel()
  455. {
  456. let nextKey = UserMap.getNextLevelMapKey(this._superFindData.currentBgKey)
  457. this._superFindData.currentBgKey = nextKey
  458. this.setTitle()
  459. this._superFindData.levelDesc = `疯狂最强找茬-找小熊-第${nextKey}关`
  460. this._superFindData.imgPath = allMapList[nextKey]
  461. this.saveSuperFindData()
  462. }
  463. setTitle()
  464. {
  465. this._superFindData.title = `找到${Difficulty.getDifficutyData().difficutyCount}个跳舞的小熊`
  466. console.log(this._superFindData.title)
  467. this.saveSuperFindData()
  468. }
  469. }
  470. export const SuperFind = new UserSuperFindData();
  471. // export const canSelectedSkinList: { [key: number]: string } = {
  472. // //假设30个数据
  473. // 1: "spine/hecha",
  474. // 2: "spine/hecha",
  475. // 3: "spine/hecha",
  476. // 4: "spine/hecha",
  477. // 5: "spine/hecha",
  478. // 6: "spine/hecha",
  479. // 7: "spine/hecha",
  480. // 8: "spine/hecha",
  481. // 9: "spine/hecha",
  482. // 10: "spine/hecha",
  483. // 11: "spine/hecha",
  484. // 12: "spine/hecha",
  485. // 13: "spine/hecha",
  486. // 14: "spine/hecha",
  487. // 15: "spine/hecha",
  488. // 16: "spine/hecha",
  489. // 17: "spine/hecha",
  490. // 18: "spine/hecha",
  491. // 19: "spine/hecha",
  492. // 20: "spine/hecha",
  493. // 21: "spine/hecha",
  494. // 22: "spine/hecha",
  495. // 23: "spine/hecha",
  496. // 24: "spine/hecha",
  497. // 25: "spine/hecha",
  498. // 26: "spine/hecha",
  499. // 27: "spine/hecha",
  500. // 28: "spine/hecha",
  501. // 29: "spine/hecha",
  502. // 30: "spine/hecha",
  503. // };
  504. export const allSkinList: { [key: number]: string } = {
  505. //假设30个数据
  506. 1: "prefab/spine/spine01",
  507. 2: "prefab/spine/spine02",
  508. 3: "prefab/spine/spine03",
  509. 4: "prefab/spine/spine04",
  510. 5: "prefab/spine/spine05",
  511. 6: "prefab/spine/spine06",
  512. 7: "prefab/spine/spine07",
  513. 8: "prefab/spine/spine08",
  514. 9: "prefab/spine/spine09",
  515. 10: "prefab/spine/spine10",
  516. 11: "prefab/spine/spine11",
  517. 12: "prefab/spine/spine12",
  518. // 13: "prefab/spine/spine13",
  519. // 14: "prefab/spine/spine14",
  520. // 15: "prefab/spine/spine15",
  521. // 16: "prefab/spine/spine16",
  522. // 17: "prefab/spine/spine17",
  523. // 18: "prefab/spine/spine18",
  524. // 19: "prefab/spine/spine19",
  525. // 20: "prefab/spine/spine20",
  526. // 21: "prefab/spine/spine21",
  527. // 22: "prefab/spine/spine22",
  528. // 23: "prefab/spine/spine23",
  529. // 24: "prefab/spine/spine24",
  530. // 25: "prefab/spine/spine25",
  531. // 26: "prefab/spine/spine26",
  532. // 27: "prefab/spine/spine27",
  533. // 28: "prefab/spine/spine28",
  534. // 29: "prefab/spine/spine29",
  535. // 30: "prefab/spine/spine30",
  536. };
  537. export const defaultSkin = "prefab/spine/spine01"
  538. export const allMapList: { [key: number]: string } = {
  539. // 背景图片资源
  540. 1: "image/bg1",
  541. 2: "image/bg2",
  542. 3: "image/bg3",
  543. 4: "image/bg4",
  544. 5: "image/bg5",
  545. 6: "image/bg6",
  546. 7: "image/bg7",
  547. 8: "image/bg8",
  548. 9: "image/bg9",
  549. 10: "image/bg10",
  550. 11: "image/bg11",
  551. // 12: "image/bg12",
  552. // 13: "image/bg13",
  553. // 14: "image/bg14",
  554. // 15: "image/bg15",
  555. // 16: "image/bg16",
  556. // 17: "image/bg17",
  557. // 18: "image/bg18",
  558. // 19: "image/bg19",
  559. // 20: "image/bg20",
  560. // 21: "image/bg21",
  561. // 22: "image/bg22",
  562. // 23: "image/bg23",
  563. // 24: "image/bg24",
  564. // 25: "image/bg25",
  565. // 26: "image/bg26",
  566. // 27: "image/bg27",
  567. // 28: "image/bg28",
  568. // 29: "image/bg29",
  569. // 30: "image/bg30",
  570. };
  571. /**
  572. * 获取 所有皮肤 中所有的键
  573. * @returns 包含所有键的数字数组
  574. */
  575. export function getAllSkinKeys(): number[] {
  576. return Object.keys(allSkinList).map(Number);
  577. }
  578. /**
  579. * 获取 所有地图 中所有的键
  580. * @returns 包含所有键的数字数组
  581. */
  582. export function getAllMapKeys(): number[] {
  583. return Object.keys(allMapList).map(Number);
  584. }