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:
2023-09-22 00:10:02 +02:00
parent 92bfd3bbc2
commit 0b21813e77
14 changed files with 202 additions and 43 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -19,12 +19,26 @@ export(Dictionary) var tooltips = {}
# Action 3 tooltip texts if item is target. Dictionary with tool's global id as key. # Action 3 tooltip texts if item is target. Dictionary with tool's global id as key.
export(Dictionary) var action3_target_texts = {} 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 = {} export(Dictionary) var action4_target_texts = {}
# If action used by player is in this list, this is a valid target (second item in combination) # 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 = [] 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 outline: ItemOutline
var isHighlighted: bool var isHighlighted: bool

View File

@@ -1,3 +1,2 @@
:action3 :action3
say player "Es mi frontal de toda la vida, le tengo cariño" say player "Es mi frontal de toda la vida, le tengo cariño"

View File

@@ -1,6 +1,11 @@
:action1 :action1
say player "Que cosa tan curiosa" say player "Que patata más bonita, cómo se nota que es de la huerta."
:action2 :action2
say player "No lo quiero coger" say player "Bieen! una patata!"
set_active turno_cocina_patata false
item_count_add turno_cocina_patata
inventory_add turno_cocina_patata
:action3
say player "Que patata más bonita, cómo se nota que es de la huerta."

View File

@@ -15,12 +15,17 @@ esc_script = "res://gymkhana/items/inventory/turno_cocina_patata.esc"
inventory_texture = ExtResource( 2 ) inventory_texture = ExtResource( 2 )
dialog_color = Color( 1, 1, 1, 1 ) dialog_color = Color( 1, 1, 1, 1 )
tooltips = { tooltips = {
"action1": "¿Que es esto?", "action1": "Admirar la patata",
"action2": "Coger", "action2": "Cogerla",
"action3": "Mirar", "action3": "Admirar la patata",
"action4": "Usar"
} }
animations = null animations = null
count_textures = [
{ "start": 1, "texture": "res://gymkhana/items/inventory/assets/turno_cocina_patata.png" },
{ "start": 2, "texture": "res://gymkhana/items/inventory/assets/turno_cocina_dos_patatas.png" },
{ "start": 3, "texture": "res://gymkhana/items/inventory/assets/turno_cocina_tres_patatas.png" },
{ "start": 4, "texture": "res://gymkhana/items/inventory/assets/turno_cocina_cuatro_patatas.png" },
]
[node name="Sprite" type="Sprite" parent="."] [node name="Sprite" type="Sprite" parent="."]
position = Vector2( -2, 0 ) position = Vector2( -2, 0 )

View File

@@ -1,13 +1,13 @@
:action1 :action1
say player "Hay una patata!" say player "Solo veo pieles de cebollas."
:action2 :action2
say current_player "Bieeen!!! una patata!" say current_player "Bieeen!!! Había una patata!"
set_active turno_cocina_patata_grande false set_active turno_cocina_patata_grande false
inventory_add turno_cocina_patata_grande inventory_add turno_cocina_patata_grande
:action3 :action3
say current_player "Es bien grande" say current_player "Es la segunda patata más grande que he visto nunca."
:action4 :action4
say current_player "Así no me sirve de nada" say current_player "Creo que necesito patatas normales, no patatas gigantes..."

View File

@@ -11,10 +11,4 @@ esc_script = "res://gymkhana/items/inventory/turno_cocina_patata_grande.esc"
combine_when_selected_action_is_in = [ ] combine_when_selected_action_is_in = [ ]
inventory_texture = ExtResource( 2 ) inventory_texture = ExtResource( 2 )
dialog_color = Color( 1, 1, 1, 1 ) dialog_color = Color( 1, 1, 1, 1 )
tooltips = {
"action1": "¿Que es esto?",
"action2": "Coger",
"action3": "Mirar",
"action4": "Usar"
}
animations = null animations = null

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=2] [gd_scene load_steps=14 format=2]
[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_room.gd" type="Script" id=1] [ext_resource path="res://addons/escoria-core/game/core-scripts/esc_room.gd" type="Script" id=1]
[ext_resource path="res://gymkhana/characters/oier/oier.tscn" type="PackedScene" id=2] [ext_resource path="res://gymkhana/characters/oier/oier.tscn" type="PackedScene" id=2]
@@ -11,6 +11,7 @@
[ext_resource path="res://gymkhana/rooms/turno_cocina/cocina/assets/foreground-silla.png" type="Texture" id=9] [ext_resource path="res://gymkhana/rooms/turno_cocina/cocina/assets/foreground-silla.png" type="Texture" id=9]
[ext_resource path="res://gymkhana/items/inventory/turno_cocina_peso.tscn" type="PackedScene" id=10] [ext_resource path="res://gymkhana/items/inventory/turno_cocina_peso.tscn" type="PackedScene" id=10]
[ext_resource path="res://gymkhana/items/inventory/turno_cocina_bol.tscn" type="PackedScene" id=11] [ext_resource path="res://gymkhana/items/inventory/turno_cocina_bol.tscn" type="PackedScene" id=11]
[ext_resource path="res://gymkhana/items/inventory/turno_cocina_patata.tscn" type="PackedScene" id=12]
[sub_resource type="NavigationPolygon" id=3] [sub_resource type="NavigationPolygon" id=3]
vertices = PoolVector2Array( 2398, 485, 2442, 499, 2446, 617, 2409, 597, 2283, 493, 144, 568, 3, 570, -56, 524, 111, 445, 162, 471, 229, 445, 200, 465, 252, 478, 305, 524, 273, 570, 2287, 687, 2062, 635, 2113, 565, 696, 737, 512, 591, 703, 699, 1269, 739, 1241, 699, 1502, 646, 1409, 752, 1345, 667, 1489, 627, 1347, 563, 1480, 587, 1466, 514, 1345, 505, 1382, 439, 2109, 424, 2238, 386, 1788, 325, 2018, 419, 1787, 383, 1466, 412, 1493, 479, 1305.5, 467, 1337.5, 449, 1306.5, 498 ) vertices = PoolVector2Array( 2398, 485, 2442, 499, 2446, 617, 2409, 597, 2283, 493, 144, 568, 3, 570, -56, 524, 111, 445, 162, 471, 229, 445, 200, 465, 252, 478, 305, 524, 273, 570, 2287, 687, 2062, 635, 2113, 565, 696, 737, 512, 591, 703, 699, 1269, 739, 1241, 699, 1502, 646, 1409, 752, 1345, 667, 1489, 627, 1347, 563, 1480, 587, 1466, 514, 1345, 505, 1382, 439, 2109, 424, 2238, 386, 1788, 325, 2018, 419, 1787, 383, 1466, 412, 1493, 479, 1305.5, 467, 1337.5, 449, 1306.5, 498 )
@@ -39,7 +40,7 @@ player_doubleclick_speed_multiplier = 2.0
[node name="Foreground-mesa" type="Sprite" parent="ESCTerrain"] [node name="Foreground-mesa" type="Sprite" parent="ESCTerrain"]
position = Vector2( 1786, 424 ) position = Vector2( 1786, 424 )
z_index = 4096 z_index = 1000
texture = ExtResource( 8 ) texture = ExtResource( 8 )
[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="ESCTerrain"] [node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="ESCTerrain"]
@@ -155,7 +156,7 @@ esc_script = "res://gymkhana/rooms/turno_cocina/cocina/esc/fregadero_izq.esc"
combine_when_selected_action_is_in = [ ] combine_when_selected_action_is_in = [ ]
dialog_color = Color( 1, 1, 1, 1 ) dialog_color = Color( 1, 1, 1, 1 )
tooltips = { tooltips = {
"action1": "Mirar fregadero", "action1": "Mirar el fregadero",
"action2": "Usar" "action2": "Usar"
} }
action3_target_texts = { action3_target_texts = {
@@ -184,7 +185,7 @@ tooltips = {
"action2": "Meter la mano" "action2": "Meter la mano"
} }
action3_target_texts = { action3_target_texts = {
"turno_cocina_frontal": "Mirar denajo del sofa" "turno_cocina_frontal": "Alumbrar debajo del sofa"
} }
target_when_selected_action_is_in = [ "action3" ] target_when_selected_action_is_in = [ "action3" ]
animations = null animations = null
@@ -195,3 +196,19 @@ polygon = PoolVector2Array( 484, 406, 485, 421, 556, 423, 551, 415, 550, 409, 55
[node name="ESCLocation" type="Position2D" parent="debajo_sofa"] [node name="ESCLocation" type="Position2D" parent="debajo_sofa"]
position = Vector2( 420, 494 ) position = Vector2( 420, 494 )
script = ExtResource( 5 ) script = ExtResource( 5 )
[node name="turno_cocina_patata" parent="." instance=ExtResource( 12 )]
position = Vector2( 2202, 343 )
rotation = -0.270526
scale = Vector2( 0.35, 0.35 )
z_index = 1001
combine_when_selected_action_is_in = [ ]
[node name="turno_cocina_patata_collision" type="CollisionPolygon2D" parent="turno_cocina_patata"]
position = Vector2( 9.0867, -13.5233 )
z_index = 1001
polygon = PoolVector2Array( 6.50781, 0.0583801, 20.7368, 12.8992, 7.896, 27.1279, -19.937, 31.269, -42.4258, 16.1375, -26.0684, -0.0810547 )
[node name="turno_cocina_patata_location" type="Position2D" parent="turno_cocina_patata"]
position = Vector2( 93.1357, 65.3632 )
script = ExtResource( 5 )

View File

@@ -8,15 +8,10 @@
stop stop
say player "Bieen! una patata!" say player "Bieen! una patata!"
item_count_add turno_cocina_patata
inventory_add turno_cocina_patata inventory_add turno_cocina_patata
set_active cocina_debajo_sofa false set_active cocina_debajo_sofa false
:action3 turno_cocina_bol_lentejas
say player "Agua a ojo... en su justa medida."
inventory_remove turno_cocina_peso_bol
inventory_add turno_cocina_bol_lentejas
inventory_add turno_cocina_peso
:action3 turno_cocina_frontal :action3 turno_cocina_frontal
say player "Veo una patata!!" say player "Veo una patata!!"
set_global turno_cocina_frontal_debajo_sofa true set_global turno_cocina_frontal_debajo_sofa true

View File

@@ -141,7 +141,7 @@ pause_mode = 1
position = Vector2( 941, 385 ) position = Vector2( 941, 385 )
script = ExtResource( 6 ) script = ExtResource( 6 )
global_id = "turno_cocina_despensa_bidon_izq" global_id = "turno_cocina_despensa_bidon_izq"
esc_script = "res://gymkhana/rooms/turno_cocina/despensa/esc/bidon_der.esc" esc_script = "res://gymkhana/rooms/turno_cocina/despensa/esc/bidon_izq.esc"
combine_when_selected_action_is_in = [ ] combine_when_selected_action_is_in = [ ]
dialog_color = Color( 1, 1, 1, 1 ) dialog_color = Color( 1, 1, 1, 1 )
tooltips = { tooltips = {
@@ -163,7 +163,10 @@ script = ExtResource( 5 )
position = Vector2( 440, 161 ) position = Vector2( 440, 161 )
scale = Vector2( 0.880435, 0.88735 ) scale = Vector2( 0.880435, 0.88735 )
tooltips = { tooltips = {
"action1": "Mirar" "action1": "Mirar",
"action2": "Rascar por el fondo",
"action3": "Mirar",
"action4": "Usar"
} }
[node name="ESCLocation2" type="Position2D" parent="turno_cocina_patata_grande"] [node name="ESCLocation2" type="Position2D" parent="turno_cocina_patata_grande"]

View File

@@ -1,5 +1,5 @@
:action1 :action1
say player "Está lleno de algo que no se que es." say player "Está lleno de algo que no sé que es."
:action2 :action2
say player "Lo que hay dentro no lo necesito para nada." say player "Lo que hay dentro no lo necesito para nada."

View File

@@ -204,7 +204,7 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_options_chooser.gd" "path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_options_chooser.gd"
}, { }, {
"base": "Node", "base": "StateMachine",
"class": "ESCDialogPlayer", "class": "ESCDialogPlayer",
"language": "GDScript", "language": "GDScript",
"path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_player.gd" "path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_player.gd"
@@ -284,6 +284,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://addons/escoria-core/game/core-scripts/esc_item.gd" "path": "res://addons/escoria-core/game/core-scripts/esc_item.gd"
}, { }, {
"base": "Resource",
"class": "ESCItemCountManager",
"language": "GDScript",
"path": "res://gymkhana/addons/escoria-ui-return-monkey-island/esc/esc_item_count_manager.gd"
}, {
"base": "ESCItem", "base": "ESCItem",
"class": "ESCItemWithTooltip", "class": "ESCItemWithTooltip",
"language": "GDScript", "language": "GDScript",
@@ -494,6 +499,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://addons/escoria-core/game/core-scripts/esc/commands/inventory_remove.gd" "path": "res://addons/escoria-core/game/core-scripts/esc/commands/inventory_remove.gd"
}, { }, {
"base": "ESCBaseCommand",
"class": "ItemCountAddCommand",
"language": "GDScript",
"path": "res://gymkhana/addons/escoria-ui-return-monkey-island/esc/commands/item_count_add.gd"
}, {
"base": "Polygon2D", "base": "Polygon2D",
"class": "ItemOutline", "class": "ItemOutline",
"language": "GDScript", "language": "GDScript",
@@ -765,6 +775,7 @@ _global_script_class_icons={
"ESCInventoryItem": "", "ESCInventoryItem": "",
"ESCInventoryManager": "", "ESCInventoryManager": "",
"ESCItem": "res://addons/escoria-core/design/esc_item.svg", "ESCItem": "res://addons/escoria-core/design/esc_item.svg",
"ESCItemCountManager": "",
"ESCItemWithTooltip": "res://addons/escoria-core/design/esc_item.svg", "ESCItemWithTooltip": "res://addons/escoria-core/design/esc_item.svg",
"ESCLocation": "res://addons/escoria-core/design/esc_location.svg", "ESCLocation": "res://addons/escoria-core/design/esc_location.svg",
"ESCMain": "", "ESCMain": "",
@@ -807,6 +818,7 @@ _global_script_class_icons={
"IncGlobalCommand": "", "IncGlobalCommand": "",
"InventoryAddCommand": "", "InventoryAddCommand": "",
"InventoryRemoveCommand": "", "InventoryRemoveCommand": "",
"ItemCountAddCommand": "",
"ItemOutline": "res://addons/escoria-core/design/esc_item.svg", "ItemOutline": "res://addons/escoria-core/design/esc_item.svg",
"PlayLibSound": "", "PlayLibSound": "",
"PlaySndCommand": "", "PlaySndCommand": "",