feat(meta): MetaProgress autoload(碎片+已解锁+持久化 user://meta.json)

This commit is contained in:
2026-07-21 11:42:18 +08:00
parent 14c0709d74
commit e8d0c06440
2 changed files with 56 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
## MetaProgress — 全局元进展(Autoload: MetaProgress
## 以太碎片 + 已解锁法术,持久化 user://meta.json(死亡不重置)。
extends Node
const PATH: String = "user://meta.json"
var aether: int = 0
var unlocked: Array = [] # 已解锁法术 id (String)
func _ready() -> void:
load_meta()
func add_aether(n: int) -> void:
if n <= 0:
return
aether += n
save_meta()
func is_unlocked(id: String) -> bool:
return id in unlocked
## 已解锁 或 碎片不足 → false(不扣费);否则扣费+记录+存档 → true
func unlock(id: String, cost: int) -> bool:
if is_unlocked(id) or aether < cost:
return false
aether -= cost
unlocked.append(id)
save_meta()
return true
func clear() -> void:
aether = 0
unlocked = []
if FileAccess.file_exists(PATH):
DirAccess.remove_absolute(ProjectSettings.globalize_path(PATH))
func save_meta() -> void:
var f := FileAccess.open(PATH, FileAccess.WRITE)
if f:
f.store_string(JSON.stringify({"aether": aether, "unlocked": unlocked}))
f.close()
func load_meta() -> void:
aether = 0
unlocked = []
if not FileAccess.file_exists(PATH):
return
var parsed = JSON.parse_string(FileAccess.get_file_as_string(PATH))
if not (parsed is Dictionary):
return
aether = int(parsed.get("aether", 0))
var u = parsed.get("unlocked", [])
if u is Array:
for id in u:
unlocked.append(String(id))