Add finite prebattle shop stock
This commit is contained in:
@@ -3,7 +3,7 @@ class_name CampaignState
|
||||
|
||||
const SAVE_PATH := "user://campaign_save.json"
|
||||
const MANUAL_SAVE_PATH := "user://campaign_manual_save.json"
|
||||
const SAVE_VERSION := 3
|
||||
const SAVE_VERSION := 4
|
||||
|
||||
var save_version := SAVE_VERSION
|
||||
var campaign_id := ""
|
||||
@@ -26,6 +26,7 @@ var flags := {}
|
||||
var joined_officers: Array[String] = []
|
||||
var applied_post_battle_choices := {}
|
||||
var pending_post_battle_choice_scenario_id := ""
|
||||
var shop_purchases := {}
|
||||
|
||||
|
||||
func load_campaign(path: String) -> bool:
|
||||
@@ -136,6 +137,7 @@ func start_new(scenario_id: String) -> void:
|
||||
joined_officers.clear()
|
||||
applied_post_battle_choices.clear()
|
||||
pending_post_battle_choice_scenario_id = ""
|
||||
shop_purchases.clear()
|
||||
for officer_id in initial_joined_officers:
|
||||
joined_officers.append(officer_id)
|
||||
|
||||
@@ -341,12 +343,31 @@ func get_inventory_snapshot() -> Dictionary:
|
||||
return inventory.duplicate(true)
|
||||
|
||||
|
||||
func try_buy_item(item_id: String, price: int) -> bool:
|
||||
func get_shop_purchase_count(scenario_id: String, item_id: String) -> int:
|
||||
var scenario_key := str(scenario_id)
|
||||
var item_key := str(item_id)
|
||||
if scenario_key.is_empty() or item_key.is_empty():
|
||||
return 0
|
||||
if not shop_purchases.has(scenario_key) or typeof(shop_purchases[scenario_key]) != TYPE_DICTIONARY:
|
||||
return 0
|
||||
return int(shop_purchases[scenario_key].get(item_key, 0))
|
||||
|
||||
|
||||
func try_buy_item(item_id: String, price: int, scenario_id: String = "", stock_limit: int = -1) -> bool:
|
||||
if item_id.is_empty() or price <= 0 or gold < price:
|
||||
return false
|
||||
var scenario_key := str(scenario_id)
|
||||
if stock_limit >= 0 and not scenario_key.is_empty() and get_shop_purchase_count(scenario_key, item_id) >= stock_limit:
|
||||
return false
|
||||
var previous_count := int(inventory.get(item_id, 0))
|
||||
var previous_shop_purchases: Dictionary = shop_purchases.duplicate(true)
|
||||
gold -= price
|
||||
inventory[item_id] = previous_count + 1
|
||||
if stock_limit >= 0 and not scenario_key.is_empty():
|
||||
if not shop_purchases.has(scenario_key) or typeof(shop_purchases[scenario_key]) != TYPE_DICTIONARY:
|
||||
shop_purchases[scenario_key] = {}
|
||||
var scenario_purchases: Dictionary = shop_purchases[scenario_key]
|
||||
scenario_purchases[item_id] = int(scenario_purchases.get(item_id, 0)) + 1
|
||||
if save_game():
|
||||
return true
|
||||
gold += price
|
||||
@@ -354,6 +375,7 @@ func try_buy_item(item_id: String, price: int) -> bool:
|
||||
inventory[item_id] = previous_count
|
||||
else:
|
||||
inventory.erase(item_id)
|
||||
shop_purchases = previous_shop_purchases
|
||||
return false
|
||||
|
||||
|
||||
@@ -795,7 +817,8 @@ func to_dict() -> Dictionary:
|
||||
"flags": flags.duplicate(true),
|
||||
"joined_officers": joined_officers.duplicate(),
|
||||
"applied_post_battle_choices": applied_post_battle_choices.duplicate(true),
|
||||
"pending_post_battle_choice_scenario_id": pending_post_battle_choice_scenario_id
|
||||
"pending_post_battle_choice_scenario_id": pending_post_battle_choice_scenario_id,
|
||||
"shop_purchases": shop_purchases.duplicate(true)
|
||||
}
|
||||
|
||||
|
||||
@@ -811,6 +834,7 @@ func _apply_save_data(parsed: Dictionary) -> void:
|
||||
flags = _copy_dictionary(parsed.get("flags", {}))
|
||||
applied_post_battle_choices = _copy_dictionary(parsed.get("applied_post_battle_choices", {}))
|
||||
pending_post_battle_choice_scenario_id = str(parsed.get("pending_post_battle_choice_scenario_id", ""))
|
||||
shop_purchases = _copy_dictionary(parsed.get("shop_purchases", {}))
|
||||
joined_officers.clear()
|
||||
var saved_joined = parsed.get("joined_officers", initial_joined_officers)
|
||||
for officer_id in saved_joined:
|
||||
|
||||
Reference in New Issue
Block a user