LocalData.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //本地数据管理
  2. import EventName from "./EventName/EventName";
  3. import { Global } from "./Global";
  4. export default class LocalData {
  5. private static _instance: LocalData = null;
  6. public static getInstance() {
  7. if (!this._instance) {
  8. this._instance = new LocalData();
  9. this._instance.init();
  10. }
  11. return this._instance;
  12. }
  13. private readonly $localkey: string = "柑橘味";
  14. //玩家金币
  15. private $goldnum: number = Global.defaultGold;
  16. //玩家最高纪录
  17. private $MaxRecord: number = Global.defaultMaxRecord;
  18. //拥有的抛物线皮肤
  19. private $HasParabolaSkin: number[] = Global.defaultParabolaSkin;
  20. ///拥有的人物皮肤
  21. private $HasCharacterSkin: number[] = Global.defaultCharacterSkin;
  22. ///当前使用的人物皮肤
  23. private $useParabolaSkin: number = Global.useParabolaSkin;
  24. ///当前使用的人物皮肤
  25. private $useCharacterSkin: number = Global.useCharacterSkin;
  26. ///当前使用的人物皮肤
  27. public $Guide: string[] = []
  28. private $lastLoginTime: number = 0;
  29. //设置引导
  30. public setGuide(Action: string) {
  31. if (this.$Guide.includes(Action)) {
  32. return
  33. }
  34. this.$Guide.push(Action)
  35. this.save();
  36. }
  37. public getGuide(Action: string) {
  38. if (this.$Guide.includes(Action)) {
  39. return true
  40. }
  41. return false
  42. }
  43. public init() {
  44. this.load();
  45. let now = Date.now();
  46. this.$lastLoginTime = now;
  47. this.save();
  48. this.setCurrentParabolaSkin(1001)
  49. }
  50. public setGold(num: number, Action: string) {
  51. this.$goldnum = this.Action(this.$goldnum, num, Action)
  52. this.save();
  53. cc.systemEvent.emit(EventName.changeGold);
  54. }
  55. public getGold() {
  56. return this.$goldnum;
  57. }
  58. public setMaxRecord(num: number, Action: string) {
  59. this.$MaxRecord = this.Action(this.$MaxRecord, num, Action)
  60. this.save();
  61. cc.systemEvent.emit(EventName.changeMaxRecord);
  62. }
  63. public getMaxRecord() {
  64. return this.$MaxRecord;
  65. }
  66. public setHasCharacterSkin(num: number) {
  67. this.$HasCharacterSkin.push(num)
  68. this.save();
  69. }
  70. public getHasCharacterSkin() {
  71. return this.$HasCharacterSkin;
  72. }
  73. public setHasParabolaSkin(num: number) {
  74. this.$HasParabolaSkin.push(num)
  75. this.save();
  76. }
  77. public getHasParabolaSkin() {
  78. return this.$HasParabolaSkin;
  79. }
  80. public setCurrentCharacterSkin(num: number) {
  81. this.$useCharacterSkin = num
  82. this.save();
  83. }
  84. public getCurrentCharacterSkin() {
  85. return this.$useCharacterSkin;
  86. }
  87. public setCurrentParabolaSkin(num: number) {
  88. this.$useParabolaSkin = num
  89. this.save();
  90. }
  91. public getCurrentParabolaSkin() {
  92. return this.$useParabolaSkin;
  93. }
  94. ///////////////////////////////////////////////通关次数
  95. private $OverCount: number = 0;
  96. public setOverCount(Temp: number) {
  97. this.$OverCount = Temp
  98. this.save();
  99. }
  100. public getOverCount() {
  101. return this.$OverCount;
  102. }
  103. /////////////////////////////////////////////当前玩的关卡
  104. private $Map: number = 1;
  105. public setMap(Temp: number) {
  106. this.$Map = Temp
  107. this.save();
  108. }
  109. public getMap() {
  110. return this.$Map;
  111. }
  112. /////////////////////////////////////////////设置
  113. private $Music: boolean = true;
  114. private $Effects: boolean = true;
  115. public setMusic(Temp: boolean) {
  116. this.$Music = Temp
  117. this.save();
  118. }
  119. public getMusic() {
  120. return this.$Music;
  121. }
  122. public setEffects(Temp: boolean) {
  123. this.$Effects = Temp
  124. this.save();
  125. }
  126. public getEffects() {
  127. return this.$Effects;
  128. }
  129. /////////////////////////////////////////////签到
  130. private $Sign: { [key: number]: number }[] = []
  131. public setSign(Temp: { [key: number]: number }[]) {
  132. this.$Sign = Temp
  133. this.save();
  134. }
  135. public getSign() {
  136. return this.$Sign;
  137. }
  138. /////////////////////////////////////////////游戏内
  139. ///抛物线次数
  140. private $ParabolaCount: number = Global.FirstParabolaCount;//初始抛物线次数
  141. public setParabolaCount(num: number, Action: string) {
  142. this.$ParabolaCount = this.Action(this.$ParabolaCount, num, Action)
  143. this.save();
  144. cc.systemEvent.emit(EventName.changeParabolaCount);
  145. }
  146. public getParabolaCount() {
  147. return this.$ParabolaCount;
  148. }
  149. ///超级跳的次数
  150. private $Supjump: number = Global.FirstSupjumpCount;//初始超级跳次数
  151. public setSupjumpCount(num: number, Action: string) {
  152. this.$Supjump = this.Action(this.$Supjump, num, Action)
  153. this.save();
  154. cc.systemEvent.emit(EventName.changeSupjumpCount);
  155. }
  156. public getSupjumpCount() {
  157. return this.$Supjump;
  158. }
  159. ///飞的次数
  160. private $Fly: number = Global.FirstFlyCount;//初始超级跳次数
  161. public setFlyCount(num: number, Action: string) {
  162. this.$Fly = this.Action(this.$Fly, num, Action)
  163. this.save();
  164. cc.systemEvent.emit(EventName.changeFlyCount);
  165. }
  166. public getFlyCount() {
  167. return this.$Fly;
  168. }
  169. //保存玩家本地数据
  170. public save() {
  171. let data: any = {};
  172. for (const key in this) {
  173. if (Object.prototype.hasOwnProperty.call(this, key)) {
  174. if (key.charAt(0) == '$') {
  175. data[key] = this[key];
  176. }
  177. }
  178. }
  179. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  180. }
  181. //加载玩家本地数据
  182. private load() {
  183. let data = cc.sys.localStorage.getItem(this.$localkey);
  184. if (data) {
  185. data = JSON.parse(data);
  186. for (const key in data) {
  187. this[key] = data[key];
  188. }
  189. }
  190. }
  191. private Action(data: number, num: number, str: string) {
  192. switch (str) {
  193. case '+':
  194. return data += num
  195. case '-':
  196. return data -= num
  197. case '=':
  198. return data = num
  199. default:
  200. cc.error('出错了')
  201. return 0
  202. }
  203. }
  204. //#region 签到代码
  205. initSign() {
  206. if (this.getSign().length <= 0) {
  207. console.log('初始化了一次');
  208. let Total_temp: { [key: number]: number }[] = []
  209. for (let index = 0; index < 7; index++) {
  210. let temp: { [key: number]: number } = {}
  211. const today = new Date();
  212. const tomorrow = new Date(today);
  213. tomorrow.setDate(tomorrow.getDate() + index);
  214. tomorrow.setHours(0, 0, 0, 0);
  215. const todayTimestamp = tomorrow.getTime();
  216. temp[todayTimestamp] = 0
  217. Total_temp.push(temp)
  218. }
  219. this.setSign(Total_temp)
  220. }
  221. this.getSign().forEach((e, index) => {
  222. for (const key in e) {
  223. if (Object.prototype.hasOwnProperty.call(e, key)) {
  224. const element = e[key];
  225. console.log('数组排序的值', index);//数组排序的值
  226. console.log('当日零点时间戳值', key);//当日零点时间戳值
  227. console.log('签到次数', element);//签到次数
  228. if (index == 6) {
  229. const today = new Date();
  230. today.setHours(0, 0, 0, 0);
  231. const todayTimestamp = today.getTime();
  232. //如果以及是7天后了 那么就 重置一下
  233. if (todayTimestamp > parseInt(key)) {
  234. //清空
  235. this.setSign([])
  236. //重新赋值
  237. this.initSign()
  238. }
  239. }
  240. }
  241. }
  242. })
  243. }
  244. //返回今天签到次数
  245. getTodaySignCount(): [number, number] {
  246. const today = new Date();
  247. today.setHours(0, 0, 0, 0);
  248. const todayTimestamp = today.getTime();
  249. for (let i = 0; i < this.getSign().length; i++) {
  250. const e = this.getSign()[i];
  251. for (const key in e) {
  252. if (Object.prototype.hasOwnProperty.call(e, key)) {
  253. const element = e[key];
  254. if (todayTimestamp == parseInt(key)) {
  255. return [element, i]
  256. }
  257. }
  258. }
  259. }
  260. }
  261. //增加一次今天签到次数
  262. addTodaySignCount() {
  263. const today = new Date();
  264. today.setHours(0, 0, 0, 0);
  265. const todayTimestamp = today.getTime();
  266. for (let i = 0; i < this.getSign().length; i++) {
  267. const e = this.getSign()[i];
  268. for (const key in e) {
  269. if (Object.prototype.hasOwnProperty.call(e, key)) {
  270. if (todayTimestamp == parseInt(key)) {
  271. e[key]++
  272. }
  273. }
  274. }
  275. }
  276. }
  277. byIndex2Count(index: number): [number, number] {
  278. for (let i = 0; i < this.getSign().length; i++) {
  279. const e = this.getSign()[i];
  280. for (const key in e) {
  281. if (Object.prototype.hasOwnProperty.call(e, key)) {
  282. if (index == i) {
  283. return [parseInt(key), e[key]]
  284. }
  285. }
  286. }
  287. }
  288. }
  289. //#endregion
  290. }