LocalData.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 $Map: number = 1;
  96. public setMap(Temp: number) {
  97. this.$Map = Temp
  98. this.save();
  99. }
  100. public getMap() {
  101. return this.$Map;
  102. }
  103. /////////////////////////////////////////////设置
  104. private $Music: boolean = true;
  105. private $Effects: boolean = true;
  106. public setMusic(Temp: boolean) {
  107. this.$Music = Temp
  108. this.save();
  109. }
  110. public getMusic() {
  111. return this.$Music;
  112. }
  113. public setEffects(Temp: boolean) {
  114. this.$Effects = Temp
  115. this.save();
  116. }
  117. public getEffects() {
  118. return this.$Effects;
  119. }
  120. /////////////////////////////////////////////签到
  121. private $Sign: { [key: number]: number }[] = []
  122. public setSign(Temp: { [key: number]: number }[]) {
  123. this.$Sign = Temp
  124. this.save();
  125. }
  126. public getSign() {
  127. return this.$Sign;
  128. }
  129. /////////////////////////////////////////////游戏内
  130. ///抛物线次数
  131. private $ParabolaCount: number = Global.FirstParabolaCount;//初始抛物线次数
  132. public setParabolaCount(num: number, Action: string) {
  133. this.$ParabolaCount = this.Action(this.$ParabolaCount, num, Action)
  134. this.save();
  135. cc.systemEvent.emit(EventName.changeParabolaCount);
  136. }
  137. public getParabolaCount() {
  138. return this.$ParabolaCount;
  139. }
  140. ///超级跳的次数
  141. private $Supjump: number = Global.FirstSupjumpCount;//初始超级跳次数
  142. public setSupjumpCount(num: number, Action: string) {
  143. this.$Supjump = this.Action(this.$Supjump, num, Action)
  144. this.save();
  145. cc.systemEvent.emit(EventName.changeSupjumpCount);
  146. }
  147. public getSupjumpCount() {
  148. return this.$Supjump;
  149. }
  150. ///飞的次数
  151. private $Fly: number = Global.FirstFlyCount;//初始超级跳次数
  152. public setFlyCount(num: number, Action: string) {
  153. this.$Fly = this.Action(this.$Fly, num, Action)
  154. this.save();
  155. cc.systemEvent.emit(EventName.changeFlyCount);
  156. }
  157. public getFlyCount() {
  158. return this.$Fly;
  159. }
  160. //保存玩家本地数据
  161. public save() {
  162. let data: any = {};
  163. for (const key in this) {
  164. if (Object.prototype.hasOwnProperty.call(this, key)) {
  165. if (key.charAt(0) == '$') {
  166. data[key] = this[key];
  167. }
  168. }
  169. }
  170. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  171. }
  172. //加载玩家本地数据
  173. private load() {
  174. let data = cc.sys.localStorage.getItem(this.$localkey);
  175. if (data) {
  176. data = JSON.parse(data);
  177. for (const key in data) {
  178. this[key] = data[key];
  179. }
  180. }
  181. }
  182. private Action(data: number, num: number, str: string) {
  183. switch (str) {
  184. case '+':
  185. return data += num
  186. case '-':
  187. return data -= num
  188. case '=':
  189. return data = num
  190. default:
  191. cc.error('出错了')
  192. return 0
  193. }
  194. }
  195. //#region 签到代码
  196. initSign() {
  197. if (this.getSign().length <= 0) {
  198. console.log('初始化了一次');
  199. let Total_temp: { [key: number]: number }[] = []
  200. for (let index = 0; index < 7; index++) {
  201. let temp: { [key: number]: number } = {}
  202. const today = new Date();
  203. const tomorrow = new Date(today);
  204. tomorrow.setDate(tomorrow.getDate() + index);
  205. tomorrow.setHours(0, 0, 0, 0);
  206. const todayTimestamp = tomorrow.getTime();
  207. temp[todayTimestamp] = 0
  208. Total_temp.push(temp)
  209. }
  210. this.setSign(Total_temp)
  211. }
  212. this.getSign().forEach((e, index) => {
  213. for (const key in e) {
  214. if (Object.prototype.hasOwnProperty.call(e, key)) {
  215. const element = e[key];
  216. console.log('数组排序的值', index);//数组排序的值
  217. console.log('当日零点时间戳值', key);//当日零点时间戳值
  218. console.log('签到次数', element);//签到次数
  219. if (index == 6) {
  220. const today = new Date();
  221. today.setHours(0, 0, 0, 0);
  222. const todayTimestamp = today.getTime();
  223. //如果以及是7天后了 那么就 重置一下
  224. if (todayTimestamp > parseInt(key)) {
  225. //清空
  226. this.setSign([])
  227. //重新赋值
  228. this.initSign()
  229. }
  230. }
  231. }
  232. }
  233. })
  234. }
  235. //返回今天签到次数
  236. getTodaySignCount(): [number, number] {
  237. const today = new Date();
  238. today.setHours(0, 0, 0, 0);
  239. const todayTimestamp = today.getTime();
  240. for (let i = 0; i < this.getSign().length; i++) {
  241. const e = this.getSign()[i];
  242. for (const key in e) {
  243. if (Object.prototype.hasOwnProperty.call(e, key)) {
  244. const element = e[key];
  245. if (todayTimestamp == parseInt(key)) {
  246. return [element, i]
  247. }
  248. }
  249. }
  250. }
  251. }
  252. //增加一次今天签到次数
  253. addTodaySignCount() {
  254. const today = new Date();
  255. today.setHours(0, 0, 0, 0);
  256. const todayTimestamp = today.getTime();
  257. for (let i = 0; i < this.getSign().length; i++) {
  258. const e = this.getSign()[i];
  259. for (const key in e) {
  260. if (Object.prototype.hasOwnProperty.call(e, key)) {
  261. if (todayTimestamp == parseInt(key)) {
  262. e[key]++
  263. }
  264. }
  265. }
  266. }
  267. }
  268. byIndex2Count(index: number): [number, number] {
  269. for (let i = 0; i < this.getSign().length; i++) {
  270. const e = this.getSign()[i];
  271. for (const key in e) {
  272. if (Object.prototype.hasOwnProperty.call(e, key)) {
  273. if (index == i) {
  274. return [parseInt(key), e[key]]
  275. }
  276. }
  277. }
  278. }
  279. }
  280. //#endregion
  281. }