feat: Rewrote inventory handling basing on ESCItems (#417)

Co-authored-by: Dennis Ploeger <develop@dieploegers.de>
This commit is contained in:
Dennis Ploeger
2021-10-21 21:56:25 +02:00
committed by GitHub
parent 0271046421
commit af26521d3d
35 changed files with 245 additions and 336 deletions

View File

@@ -1,95 +1,15 @@
# The inventory representation of an ESC item if pickable
extends TextureButton
class_name ESCInventoryItem, \
"res://addons/escoria-core/design/esc_inventory_item.svg"
# Signal emitted when the item was left clicked
#
# #### Parameters
#
# - item_id: Global ID of the clicked item
signal mouse_left_inventory_item(item_id)
# Signal emitted when the item was right clicked
#
# #### Parameters
#
# - item_id: Global ID of the clicked item
signal mouse_right_inventory_item(item_id)
# Signal emitted when the item was double clicked
#
# #### Parameters
#
# - item_id: Global ID of the clicked item
signal mouse_double_left_inventory_item(item_id)
# Signal emitted when the item was focused
#
# #### Parameters
#
# - item_id: Global ID of the clicked item
signal inventory_item_focused(item_id)
# Signal emitted when the item is not focused anymore
signal inventory_item_unfocused()
# Basic information about an inventory item
class_name ESCInventoryItem
# Global ID of the ESCItem that uses this ESCInventoryItem
# Will be set by ESCItem automatically
var global_id
var global_id: String = ""
# The texture for the item
var texture: Texture = null
# Connect input handlers
func _ready():
connect("gui_input", self, "_on_inventory_item_gui_input")
connect("mouse_entered", self, "_on_inventory_item_mouse_enter")
connect("mouse_exited", self, "_on_inventory_item_mouse_exit")
func _init(p_item: ESCItem) -> void:
global_id = p_item.global_id
texture = p_item._get_inventory_texture()
# Handle the gui input and emit the respective signals
#
# #### Parameters
#
# - event: The event received
func _on_inventory_item_gui_input(event: InputEvent):
if InputMap.has_action("switch_action_verb") \
and event.is_action_pressed("switch_action_verb"):
if event.button_index == BUTTON_WHEEL_UP:
escoria.inputs_manager._on_mousewheel_action(-1)
elif event.button_index == BUTTON_WHEEL_DOWN:
escoria.inputs_manager._on_mousewheel_action(1)
if event is InputEventMouseButton:
# var p = get_global_mouse_position()
if event.doubleclick:
if event.button_index == BUTTON_LEFT:
emit_signal(
"mouse_double_left_inventory_item",
global_id,
event
)
else:
if event.is_pressed():
if event.button_index == BUTTON_LEFT:
emit_signal(
"mouse_left_inventory_item",
global_id,
event
)
if event.button_index == BUTTON_RIGHT:
emit_signal(
"mouse_right_inventory_item",
global_id,
event
)
# Handle mouse entering the item and send the respecitve signal
func _on_inventory_item_mouse_enter():
emit_signal("inventory_item_focused", global_id)
# Handle mouse leaving the item and send the respecitve signal
func _on_inventory_item_mouse_exit():
emit_signal("inventory_item_unfocused")

View File

@@ -103,9 +103,9 @@ export(bool) var combine_is_one_way = false
# A false value is useful for items in the background, such as buttons.
export(bool) var use_from_inventory_only = false
# Scene based on ESCInventoryItem used in inventory for the object if it is
# picked up, that displays and handles the item
export(PackedScene) var inventory_item_scene_file: PackedScene
# The visual representation for this item when its in the inventory
export(Texture) var inventory_texture: Texture = null \
setget ,_get_inventory_texture
# Color used for dialogs
export(Color) var dialog_color = ColorN("white")
@@ -124,25 +124,22 @@ export(float) var v_speed_damp: float = 1.0
export(NodePath) var animation_player_node: NodePath = "" \
setget _set_animation_player_node
# ESCAnimationsResource (for walking, idling...)
var animations: ESCAnimationResource
# Reference to the animation node (null if none was found)
var animation_sprite = null
# Reference to the sprite node
var _sprite_node: Node = null
# Reference to the current terrain
var terrain: ESCTerrain
# Reference to this items collision shape node
var collision: Node
# The representation of this item in the scene. Will
# be loaded, if inventory_item_scene_file is set.
var inventory_item: ESCInventoryItem = null setget ,_get_inventory_item
# Reference to the sprite node
var _sprite_node: Node = null
# The movable subnode
var _movable: ESCMovable = null
@@ -464,15 +461,6 @@ func _update_terrain(rc: int, event_name: String) -> void:
_movable.update_terrain(event_name)
# Get inventory item from the inventory item scene
# **Returns** The inventory item of this ESCitem
func _get_inventory_item() -> ESCInventoryItem:
if not inventory_item and inventory_item_scene_file:
inventory_item = inventory_item_scene_file.instance()
inventory_item.global_id = self.global_id
return inventory_item
func _get_property_list():
var properties = []
properties.append({
@@ -505,3 +493,15 @@ func _set_animation_player_node(node_path: NodePath):
)
animation_player_node = node_path
# Returns either the set inventory texture or the texture of a TextureRect
# found as a child if it is null
func _get_inventory_texture() -> Texture:
if inventory_texture == null:
for c in get_children():
if c is TextureRect or c is Sprite:
return c.texture
return null
else:
return inventory_texture