添加持久化逻辑

This commit is contained in:
2025-05-13 20:31:44 +08:00
parent dcb12db40d
commit cbee313bb5
17 changed files with 512 additions and 213 deletions

View File

@@ -0,0 +1,79 @@
import { config } from "../constants/config"
import { DOStage } from "../types/DOStage"
export class StorageManager {
private static instance: StorageManager
public static getInstance(): StorageManager {
if (!StorageManager.instance) {
StorageManager.instance = new StorageManager()
}
return StorageManager.instance
}
public static createDataBlock(index: number, show: number, score: number=0, notes: Array<number>=new Array()): any {//格子
var dataBlock = {
index: index,
show: show,
score: score,
notes: notes,
}
return dataBlock
}
public static createDataCandy(left: number): any {//可选数字
var dataCandy = {
left: left,
}
return dataCandy
}
public static createDataStage(mistake: number, difficulty: number, duration: number=0, blocks: Array<any>=new Array(), candys: Array<any>=new Array()): any {//关卡数据
var dataStage = {
mistake: mistake,
difficulty: difficulty,
duration: duration,
note_open: false,
blocks: blocks,
candys: candys,
}
return dataStage
}
private stageID: string = "";
private stageMap: Map<string, any> = new Map();
public init(): void {
}
public loadStage(stageID: string): DOStage {
this.stageID = stageID
var 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 = StorageManager.createDataStage(0, config.DIFFICULTY_TYPE.Easy)
this.stageMap.set(stageID, dataStage)
}
}
return new DOStage(dataStage)
}
public saveStage(): void {
if (this.stageMap.has(this.stageID)) {
var dataStage = this.stageMap.get(this.stageID)
var jsonStr = JSON.stringify(dataStage)
Laya.LocalStorage.setItem(this.stageID, jsonStr)
}
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "7503e6c9-bbbe-40ca-813c-660b4927d3a6"
}

View File

@@ -1,7 +1,8 @@
import { respath } from "../constants/respath"
import { Home } from "../views/Home"
import { StorageManager } from "./StorageManager"
import { Stage } from "../views/Stage"
const { regClass, property } = Laya
@regClass()
@@ -11,6 +12,9 @@ export class UIManager extends Laya.Script {
private static _instance: UIManager
onAwake(): void {
UIManager._instance = this
StorageManager.getInstance().init()
this.loadHome()
}
@@ -29,7 +33,7 @@ export class UIManager extends Laya.Script {
Laya.loader.load(respath.stage_ui_res).then((go)=>{
var prefab = go.create()
var stage = this.owner.addChild(prefab).getComponent(Stage)
stage.onLoadStage("aBCeIGFhdEDFcAHBGIhigBdfecAgHDiFBCaeBEAGHCIdffcIdEAHbgcABfGEdiHIFHaCDGEBDGEHbiaFC");
stage.onLoadStage(StorageManager.getInstance().loadStage("first"))
})
}