gymkhana/item_count (#16)
Item can now be countable objects, like money, bullets or even potatoes! Reviewed-on: gymkhana/gymkhana#16
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
# `item_count_add item_id value`
|
||||
#
|
||||
# Add value to the count in a item.
|
||||
# - item_id: string: id of the item to apply
|
||||
# - value: int: number to add
|
||||
#
|
||||
# @ESC
|
||||
extends ESCBaseCommand
|
||||
class_name ItemCountAddCommand
|
||||
|
||||
|
||||
var item_count_manager = ESCItemCountManager.new()
|
||||
|
||||
# Return the descriptor of the arguments of this command
|
||||
func configure() -> ESCCommandArgumentDescriptor:
|
||||
return ESCCommandArgumentDescriptor.new(
|
||||
1,
|
||||
[TYPE_STRING, TYPE_INT],
|
||||
[null, 1]
|
||||
)
|
||||
|
||||
|
||||
# Validate wether the given arguments match the command descriptor
|
||||
func validate(arguments: Array):
|
||||
if not escoria.object_manager.has(arguments[0]):
|
||||
escoria.logger.report_errors(
|
||||
"item_count_add: invalid object",
|
||||
["Object %s not registered" % arguments[0]]
|
||||
)
|
||||
return false
|
||||
|
||||
return .validate(arguments)
|
||||
|
||||
|
||||
# Run the command
|
||||
func run(command_params: Array) -> int:
|
||||
item_count_manager.add(command_params[0], command_params[1])
|
||||
|
||||
return ESCExecution.RC_OK
|
||||
@@ -0,0 +1,76 @@
|
||||
# A manager for inventory objects
|
||||
extends Resource
|
||||
class_name ESCItemCountManager
|
||||
|
||||
|
||||
func add(global_id: String, value:= 1) -> void:
|
||||
var item = get_item(global_id)
|
||||
item.count = item.count + value
|
||||
updateSprite(item)
|
||||
|
||||
|
||||
func remove(global_id: String, value:= 1) -> void:
|
||||
var item = get_item(global_id)
|
||||
item.count = item.count - value
|
||||
updateSprite(item)
|
||||
|
||||
|
||||
func set(global_id: String, value: int) -> void:
|
||||
var item = get_item(global_id)
|
||||
item.count = value
|
||||
updateSprite(item)
|
||||
|
||||
|
||||
func removeFromInventoryIfCountLessThan(global_id: String, value:= 1) -> void:
|
||||
var item = get_item(global_id)
|
||||
if item.count < value:
|
||||
escoria.inventory_manager.remove_item(global_id)
|
||||
|
||||
|
||||
func get_item(global_id: String) -> ESCItem:
|
||||
var node = escoria.object_manager.get_object(global_id).node
|
||||
if not node is ESCItem:
|
||||
escoria.logger.error(
|
||||
"item_count_add: invalid object",
|
||||
["Object is not an ESCItem"]
|
||||
)
|
||||
return null
|
||||
|
||||
return node
|
||||
|
||||
|
||||
func updateSprite(item: ESCItemWithTooltip) -> void:
|
||||
var child_node = item.get_node("Sprite") as Sprite
|
||||
if not child_node is Sprite:
|
||||
escoria.logger.report_errors(
|
||||
"item_count_add: invalid sprite",
|
||||
["No Sprite node found"]
|
||||
)
|
||||
|
||||
var texture_path = getCountTexturePath(item)
|
||||
var texture = load(texture_path)
|
||||
|
||||
# Update texture in scene
|
||||
var sprite = child_node as Sprite
|
||||
sprite.texture = texture
|
||||
|
||||
# Update texture in scene
|
||||
# TODO optional inventory_texture
|
||||
# TODO change inventory texture without removing and adding the item
|
||||
# https://github.com/godot-escoria/escoria-issues/issues/364
|
||||
# https://discord.com/channels/884336424780984330/1124614097917460584/1127151969614696548
|
||||
item.inventory_texture = texture
|
||||
if escoria.inventory_manager.inventory_has(item.global_id):
|
||||
escoria.inventory_manager.remove_item(item.global_id)
|
||||
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
|
||||
var i = 0
|
||||
|
||||
while (i < textures.size() - 1) and count >= textures[i + 1].start:
|
||||
i = i + 1
|
||||
|
||||
return textures[i].texture
|
||||
@@ -19,12 +19,26 @@ export(Dictionary) var tooltips = {}
|
||||
# Action 3 tooltip texts if item is target. Dictionary with tool's global id as key.
|
||||
export(Dictionary) var action3_target_texts = {}
|
||||
|
||||
# Action 4 tooltip texts if item is target. Dictionary with tool's global id as key.
|
||||
# Action 4 tooltip texts if item is target. Dictionary with tool's global id as key
|
||||
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
|
||||
|
||||
# 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 = []
|
||||
|
||||
var outline: ItemOutline
|
||||
|
||||
var isHighlighted: bool
|
||||
|
||||
Reference in New Issue
Block a user