219 lines
6 KiB
GDScript
219 lines
6 KiB
GDScript
extends Node
|
|
|
|
var inventorySavepath = "user://inventory.save"
|
|
var savePath = "user://player.cfg"
|
|
var savePrefix = "player1"
|
|
|
|
var connected = false
|
|
|
|
var saveQueued = false
|
|
var saveDelay = 10
|
|
|
|
func _ready():
|
|
SaveConverter.update_save()
|
|
|
|
func set_save_paths(prefix:String):
|
|
savePrefix = prefix
|
|
inventorySavepath = "user://" + prefix + "inventory.save"
|
|
savePath = "user://" + prefix + ".cfg"
|
|
|
|
func _process(delta):
|
|
if saveDelay > 0:
|
|
saveDelay -= delta
|
|
else:
|
|
if saveQueued:
|
|
save_game(true)
|
|
saveDelay = 10
|
|
|
|
func save_game(forced:bool = false):
|
|
if forced:
|
|
save_inventory()
|
|
save_skills()
|
|
saveQueued = false
|
|
else:
|
|
saveQueued = true
|
|
|
|
func load_game():
|
|
load_inventory()
|
|
load_skills()
|
|
|
|
if !connected:
|
|
connected = true
|
|
InventoryManager.playerInventoryUpdated.connect(save_game)
|
|
|
|
func delete_save():
|
|
var saveGame = FileAccess.open(inventorySavepath, FileAccess.WRITE)
|
|
saveGame.store_string("")
|
|
saveGame.close()
|
|
|
|
var config = ConfigFile.new()
|
|
config.save(savePath)
|
|
|
|
func set_save_value(valueName:String, value):
|
|
var config = get_save_config()
|
|
config.set_value("variables", valueName, value)
|
|
config.save(savePath)
|
|
|
|
func get_save_value(valueName:String, defaultValue = null):
|
|
var config = get_save_config()
|
|
return config.get_value("variables", valueName, defaultValue)
|
|
|
|
func get_global_config():
|
|
var config = ConfigFile.new()
|
|
config.load("user://globalconfigs.cfg")
|
|
return config
|
|
|
|
func get_global_value(valueName:String, defaultValue = null):
|
|
var config = get_global_config()
|
|
return config.get_value("variables", valueName, defaultValue)
|
|
|
|
func set_global_value(valueName:String, value):
|
|
var config = get_global_config()
|
|
config.set_value("variables", valueName, value)
|
|
config.save("user://globalconfigs.cfg")
|
|
|
|
func save_to_section(sectionName:String, valueName:String, value):
|
|
var config = get_save_config()
|
|
config.set_value(sectionName, valueName, value)
|
|
config.save(savePath)
|
|
|
|
func get_value_from_section(sectionName:String, valueName:String, defaultValue):
|
|
var config = get_save_config()
|
|
return config.get_value(sectionName, valueName, defaultValue)
|
|
|
|
func get_save_config():
|
|
var config = ConfigFile.new()
|
|
config.load(savePath)
|
|
return config
|
|
|
|
func save_skills():
|
|
var config = get_save_config()
|
|
|
|
var skills = LevelManager.get_all_skills()
|
|
for skill in skills:
|
|
config.set_value(skill.skillName, "currentLevel", skill.currentLevel)
|
|
config.set_value(skill.skillName, "currentXP", skill.currentXP)
|
|
|
|
config.save(savePath)
|
|
|
|
func load_skills():
|
|
var config = get_save_config()
|
|
|
|
var skills = LevelManager.get_all_skills()
|
|
for skill in skills:
|
|
skill.currentLevel = config.get_value(skill.skillName, "currentLevel", 1)
|
|
skill.currentXP = config.get_value(skill.skillName, "currentXP", 0)
|
|
|
|
func save_inventory():
|
|
var saveGame = FileAccess.open(inventorySavepath, FileAccess.WRITE)
|
|
|
|
var inventories = InventoryManager.get_all_inventories()
|
|
for inventoryName in inventories.keys():
|
|
var currentInv = inventories[inventoryName]
|
|
var invInfo = {
|
|
"type": "inventory",
|
|
"items": [],
|
|
"name": inventoryName
|
|
}
|
|
for i in range(currentInv.items.size()):
|
|
var item = currentInv.items[i]
|
|
var itemInfo = item.save()
|
|
itemInfo.quantity = currentInv.quantities[i]
|
|
|
|
invInfo.items.append(itemInfo)
|
|
|
|
var json_string = JSON.stringify(invInfo)
|
|
saveGame.store_line(json_string)
|
|
|
|
save_plants(saveGame)
|
|
|
|
func save_plants(saveGame):
|
|
var plants = PlantManager.get_all_plant_details()
|
|
for plantName in plants.keys():
|
|
var currentPlant = plants[plantName]
|
|
var plantInfo = {
|
|
"type": "plant",
|
|
"name": plantName
|
|
}
|
|
|
|
var plantDetails = {}
|
|
plantDetails.state = currentPlant.state
|
|
plantDetails.plantLives = currentPlant.plantLives
|
|
plantDetails.timeBoosted = currentPlant.timeBoosted
|
|
plantDetails.queuedStates = []
|
|
|
|
for state in currentPlant.queuedStates:
|
|
var newQueuedState = {}
|
|
newQueuedState.state = state.state
|
|
newQueuedState.time = state.time
|
|
if state.item != null:
|
|
newQueuedState.item = state.item.save()
|
|
else:
|
|
newQueuedState.item = null
|
|
|
|
plantDetails.queuedStates.append(newQueuedState)
|
|
|
|
if currentPlant.item != null:
|
|
plantDetails.item = currentPlant.item.save()
|
|
else:
|
|
plantDetails.item = null
|
|
|
|
plantInfo.details = plantDetails
|
|
|
|
var json_string = JSON.stringify(plantInfo)
|
|
saveGame.store_line(json_string)
|
|
|
|
func load_inventory():
|
|
InventoryManager.clear_all_inventories()
|
|
PlantManager.clear_plant_details()
|
|
if not FileAccess.file_exists(inventorySavepath):
|
|
return
|
|
|
|
var saveGame = FileAccess.open(inventorySavepath, FileAccess.READ)
|
|
while saveGame.get_position() < saveGame.get_length():
|
|
var json_string = saveGame.get_line()
|
|
var json = JSON.new()
|
|
|
|
var parse_result = json.parse(json_string)
|
|
if not parse_result == OK:
|
|
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
|
|
continue
|
|
|
|
var lineInfo = json.get_data()
|
|
if lineInfo.type == "inventory":
|
|
for itemInfo in lineInfo.items:
|
|
var newItem = build_saved_item(itemInfo)
|
|
|
|
InventoryManager.add_item_to_inventory(newItem, itemInfo["quantity"], lineInfo.name, true)
|
|
elif lineInfo.type == "plant":
|
|
for state in lineInfo.details.queuedStates:
|
|
if state.item != null:
|
|
state.item = build_saved_item(state.item)
|
|
if lineInfo.details.item != null:
|
|
lineInfo.details.item = build_saved_item(lineInfo.details.item)
|
|
PlantManager.add_plant_details(lineInfo.name, lineInfo.details)
|
|
|
|
InventoryManager.playerInventoryUpdated.emit()
|
|
|
|
func build_saved_item(itemInfo):
|
|
var newItem = Item.new()
|
|
|
|
for i in itemInfo.keys():
|
|
var currentInfo = itemInfo[i]
|
|
if i == "itemSprite":
|
|
newItem.set_sprite_from_binary(itemInfo[i])
|
|
elif i == "additionalSprites":
|
|
newItem.set_additional_sprites_from_binary(itemInfo[i])
|
|
elif i == "itemTypes":
|
|
for itemType in itemInfo[i]:
|
|
newItem.itemTypes.append(int(itemType))
|
|
elif i == "itemModifications":
|
|
for itemModification in itemInfo[i]:
|
|
newItem.itemModifications.append(int(itemModification))
|
|
elif i != "quantity":
|
|
newItem.set(i, itemInfo[i])
|
|
|
|
return newItem
|
|
|
|
|