166 lines
4.2 KiB
GDScript
166 lines
4.2 KiB
GDScript
extends Node
|
|
|
|
const SAVE_PATH = "user://loadouts.json"
|
|
|
|
var weapon_db = {
|
|
"none": {
|
|
"name": "None",
|
|
"category": "none",
|
|
"script": ""
|
|
},
|
|
"double_barrel_shotgun": {
|
|
"name": "Double Barrel Shotgun",
|
|
"category": "primary",
|
|
"script": "res://weapons/double_barrel_shotgun.gd"
|
|
},
|
|
"ak47": {
|
|
"name": "AK-47",
|
|
"category": "primary",
|
|
"script": "res://weapons/ak47.gd"
|
|
},
|
|
"m4": {
|
|
"name": "M4",
|
|
"category": "primary",
|
|
"script": "res://weapons/m4.gd"
|
|
},
|
|
"mp7": {
|
|
"name": "MP7",
|
|
"category": "primary",
|
|
"script": "res://weapons/mp7.gd"
|
|
},
|
|
"dmr": {
|
|
"name": "DMR",
|
|
"category": "primary",
|
|
"script": "res://weapons/dmr.gd"
|
|
},
|
|
"plasma_gun": {
|
|
"name": "Plasma Gun",
|
|
"category": "primary",
|
|
"script": "res://weapons/plasma_gun.gd"
|
|
},
|
|
"nail_gun": {
|
|
"name": "Nail Gun",
|
|
"category": "primary",
|
|
"script": "res://weapons/nail_gun.gd"
|
|
},
|
|
"awp": {
|
|
"name": "AWP",
|
|
"category": "special",
|
|
"script": "res://weapons/awp.gd"
|
|
},
|
|
"rocket_launcher": {
|
|
"name": "Rocket Launcher",
|
|
"category": "special",
|
|
"script": "res://weapons/rocket_launcher.gd"
|
|
},
|
|
"rocket_swarm": {
|
|
"name": "Rocket Swarm",
|
|
"category": "special",
|
|
"script": "res://weapons/rocket_swarm.gd"
|
|
},
|
|
"mortar": {
|
|
"name": "Mortar",
|
|
"category": "special",
|
|
"script": "res://weapons/mortar.gd"
|
|
},
|
|
"knife": {
|
|
"name": "Knife",
|
|
"category": "melee",
|
|
"script": "res://weapons/knife.gd"
|
|
}
|
|
}
|
|
|
|
var active_loadout_index: int = 0
|
|
|
|
var loadouts: Array = []
|
|
var _legacy_skin_indices: Array[int] = []
|
|
|
|
func _ready() -> void:
|
|
_ensure_default_loadouts()
|
|
load_loadouts()
|
|
|
|
func _ensure_default_loadouts() -> void:
|
|
loadouts.clear()
|
|
for i in range(5):
|
|
loadouts.append({
|
|
"name": "Loadout " + str(i + 1),
|
|
"skin": "default",
|
|
"primary_1": "double_barrel_shotgun",
|
|
"primary_2": "none",
|
|
"special": "none",
|
|
"melee": "knife"
|
|
})
|
|
|
|
func get_active_loadout() -> Dictionary:
|
|
if active_loadout_index >= 0 and active_loadout_index < loadouts.size():
|
|
return loadouts[active_loadout_index]
|
|
return loadouts[0]
|
|
|
|
|
|
func get_active_skin_id() -> String:
|
|
return str(get_active_loadout().get("skin", "default"))
|
|
|
|
|
|
func set_active_loadout(index: int) -> bool:
|
|
if index < 0 or index >= loadouts.size():
|
|
return false
|
|
active_loadout_index = index
|
|
save_loadouts()
|
|
return true
|
|
|
|
|
|
## Older saves stored character choice in SkinManager's separate config. Copy
|
|
## that choice into every legacy loadout once, then the loadout becomes the
|
|
## authoritative place for both equipment and character.
|
|
func migrate_legacy_skin(skin_id: String) -> void:
|
|
if _legacy_skin_indices.is_empty():
|
|
return
|
|
var migrated := skin_id if not skin_id.is_empty() else "default"
|
|
for index in _legacy_skin_indices:
|
|
if index >= 0 and index < loadouts.size():
|
|
loadouts[index]["skin"] = migrated
|
|
_legacy_skin_indices.clear()
|
|
save_loadouts()
|
|
|
|
func save_loadouts() -> void:
|
|
var save_data = {
|
|
"active_index": active_loadout_index,
|
|
"loadouts": loadouts
|
|
}
|
|
var json_string = JSON.stringify(save_data)
|
|
var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
|
|
if file:
|
|
file.store_string(json_string)
|
|
file.close()
|
|
|
|
func load_loadouts() -> void:
|
|
_legacy_skin_indices.clear()
|
|
if FileAccess.file_exists(SAVE_PATH):
|
|
var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
|
|
if file:
|
|
var json_string = file.get_as_text()
|
|
file.close()
|
|
|
|
var json = JSON.new()
|
|
var error = json.parse(json_string)
|
|
if error == OK:
|
|
var data = json.get_data()
|
|
if data is Dictionary:
|
|
if data.has("active_index"):
|
|
active_loadout_index = data["active_index"]
|
|
if data.has("loadouts") and data["loadouts"] is Array:
|
|
var saved_loadouts = data["loadouts"]
|
|
# Overwrite defaults, up to 5
|
|
for i in range(min(5, saved_loadouts.size())):
|
|
var sl = saved_loadouts[i]
|
|
if sl is Dictionary:
|
|
if sl.has("name"): loadouts[i]["name"] = sl["name"]
|
|
if sl.has("skin"):
|
|
loadouts[i]["skin"] = sl["skin"]
|
|
else:
|
|
_legacy_skin_indices.append(i)
|
|
if sl.has("primary_1"): loadouts[i]["primary_1"] = sl["primary_1"]
|
|
if sl.has("primary_2"): loadouts[i]["primary_2"] = sl["primary_2"]
|
|
if sl.has("special"): loadouts[i]["special"] = sl["special"]
|
|
if sl.has("melee"): loadouts[i]["melee"] = sl["melee"]
|