Files
spellforge/scripts/autoloads/meta_progress.gd
T

56 lines
1.4 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 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))