70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import { config } from "../constants/config"
|
|
import { DataStage } from "../types/global"
|
|
import { DOStage } from "./DOStage"
|
|
|
|
|
|
export class StorageManager {
|
|
|
|
private static instance: StorageManager
|
|
public static getInstance(): StorageManager {
|
|
if (!StorageManager.instance) {
|
|
StorageManager.instance = new StorageManager()
|
|
}
|
|
return StorageManager.instance
|
|
}
|
|
|
|
private changed = false
|
|
|
|
private stageID: string = "";
|
|
private stageMap: Map<string, DataStage> = new Map();
|
|
|
|
public init(): void {
|
|
this.changed = false
|
|
}
|
|
|
|
public loadStage(stageID: string): DOStage {
|
|
this.stageID = stageID
|
|
var dataStage: DataStage
|
|
if (this.stageMap.has(stageID)) {
|
|
dataStage = this.stageMap.get(stageID)
|
|
}
|
|
else {
|
|
var jsonStr = Laya.LocalStorage.getItem(stageID)
|
|
if (jsonStr) {
|
|
dataStage = JSON.parse(jsonStr)
|
|
this.stageMap.set(stageID, dataStage)
|
|
}
|
|
else {
|
|
dataStage = {mistake: 0, difficulty: config.DIFFICULTY_TYPE.Easy, duration: 0, note_open: false, blocks: new Array(), candys: new Array()}
|
|
this.stageMap.set(stageID, dataStage)
|
|
}
|
|
}
|
|
var levelStr = "aBCeIGFhdEDFcAHBGIhigBdfecAgHDiFBCaeBEAGHCIdffcIdEAHbgcABfGEdiHIFHaCDGEBDGEHbiaFC"
|
|
return new DOStage(dataStage, levelStr)
|
|
}
|
|
|
|
public cleanStage(): void {
|
|
if (this.stageID && this.stageID.length > 0) {
|
|
if (this.stageMap.has(this.stageID)) {
|
|
this.stageMap.delete(this.stageID)
|
|
}
|
|
Laya.LocalStorage.removeItem(this.stageID)
|
|
this.stageID = ""
|
|
}
|
|
}
|
|
|
|
public saveStage(): void {
|
|
if (this.changed) {
|
|
this.changed = false
|
|
if (this.stageMap.has(this.stageID)) {
|
|
var dataStage = this.stageMap.get(this.stageID)
|
|
var jsonStr = JSON.stringify(dataStage)
|
|
Laya.LocalStorage.setItem(this.stageID, jsonStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
public onChanged(): void {
|
|
this.changed = true
|
|
}
|
|
} |