feat: save game system is working!

This commit is contained in:
2024-03-03 19:32:30 +01:00
parent 96ff7ea842
commit eed5e64a53
10 changed files with 650 additions and 138 deletions

View File

@@ -37,3 +37,9 @@ func run(command_params: Array) -> int:
item_count_manager.add(command_params[0], command_params[1])
return ESCExecution.RC_OK
# Function called when the command is interrupted.
func interrupt():
# Do nothing
pass

View File

@@ -3,26 +3,47 @@ extends Resource
class_name ESCItemCountManager
# If item is countable (E.g. money) marks which texture to use depending of count value.
# Each element is a Dictionary with start and texture keys:
# custom_data = {
# "count_textures": [
# { "start": 0, "texture": "res://gymkhana/items/inventory/assets/no_money.png"},
# { "start": 1, "texture": "res://gymkhana/items/inventory/assets/one_coin.png"},
# { "start": 2, "texture": "res://gymkhana/items/inventory/assets/two_coins.png"},
# { "start": 3, "texture": "res://gymkhana/items/inventory/assets/coins.png"},
# { "start": 10, "texture": "res://gymkhana/items/inventory/assets/bills.png"},
# ]
# }
func add(global_id: String, value:= 1) -> void:
var item = get_item(global_id)
set(global_id, item.count + value)
setCount(global_id, getCount(item) + value)
func remove(global_id: String, value:= 1) -> void:
var item = get_item(global_id)
set(global_id, item.count - value)
setCount(global_id, getCount(item) - value)
func set(global_id: String, value: int) -> void:
func getCount(item: ESCItem) -> int:
if "count" in item.custom_data:
return item.custom_data["count"]
else:
return 0
func setCount(global_id: String, value: int) -> void:
var item = get_item(global_id)
item.count = value
item.custom_data["count"] = value
escoria.globals_manager.set_global("_COUNT_%s" % global_id, value)
updateSprite(item)
func removeFromInventoryIfCountLessThan(global_id: String, value:= 1) -> void:
var item = get_item(global_id)
if item.count < value:
if item.custom_data["count"] < value:
escoria.inventory_manager.remove_item(global_id)
@@ -38,7 +59,7 @@ func get_item(global_id: String) -> ESCItem:
return node
func updateSprite(item: ESCItemWithTooltip) -> void:
func updateSprite(item: ESCItem) -> void:
var child_node = item.get_node("Sprite") as Sprite
if not child_node is Sprite:
escoria.logger.error(
@@ -64,9 +85,9 @@ func updateSprite(item: ESCItemWithTooltip) -> void:
escoria.inventory_manager.add_item(item.global_id)
func getCountTexturePath(item: ESCItemWithTooltip) -> String:
var count = item.count
var textures = item.count_textures # TODO sort dictionaries by start key
func getCountTexturePath(item: ESCItem) -> String:
var count = item.custom_data["count"]
var textures = item.custom_data["count_textures"] # TODO sort dictionaries by start key
var i = 0
while (i < textures.size() - 1) and count >= textures[i + 1].start:

View File

@@ -25,38 +25,28 @@ export(Dictionary) var action4_target_texts = {}
# If action used by player is in this list, this is a valid target (second item in combination)
export(Array) var target_when_selected_action_is_in = []
# If item is countable (E.g. money) marks the quantity
export(int) var count = 0
# ESCItemComponents children of this node
var components: Dictionary = {}
# If item is countable (E.g. money) marks which texture to use depending of count value.
# Each element is a Dictionary with start and texture keys:
# [
# { "start": 0, "texture": "res://gymkhana/items/inventory/assets/no_money.png"},
# { "start": 1, "texture": "res://gymkhana/items/inventory/assets/one_coin.png"},
# { "start": 2, "texture": "res://gymkhana/items/inventory/assets/two_coins.png"},
# { "start": 3, "texture": "res://gymkhana/items/inventory/assets/coins.png"},
# { "start": 10, "texture": "res://gymkhana/items/inventory/assets/bills.png"},
# ]
export(Array) var count_textures = []
func _ready():
register_components()
func set_tooltip(action: String, text: String):
tooltips[action] = text
func has_component(key: String)->bool:
return components.has(key)
func get_component(key: String):
if(has_component(key)):
return components[key]
return null
func register_components():
autoload_components()
for child in get_children():
@@ -65,6 +55,13 @@ func register_components():
components[child.get_component_type()] = child
child.register(custom_data)
func autoload_components():
add_child(ESCItemComponentOutline.new())
add_child(ESCItemComponentInventoryChecker.new())
func set_custom_data(data: Dictionary) -> void:
.set_custom_data(data)
if custom_data.has("count"):
ESCItemCountManager.new().updateSprite(self)