forked from Ceisri/Reusable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventorySlot.gd
96 lines (76 loc) · 2.45 KB
/
InventorySlot.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extends TextureButton
var DRAG_PREVIEW = preload("res://Sprite.tscn")
onready var icon = $Icon
onready var quantity_label = $Quantity
var quantity = rand_range(0, 25)
var item = "null"
func _ready():
displayQuantity()
matchItemTypeToIcon()
func _physics_process(delta):
displayQuantity()
func displayQuantity():
if quantity != 0:
quantity_label.text = str(round(quantity))
elif quantity == 0:
quantity_label.text = ""
icon.texture = preload("res://UI/graphics/SkillIcons/empty.png")
print("zeroed out")
func get_drag_data(position: Vector2):
matchItemTypeToIcon()
var slot = get_parent().get_name()
var data = {
"origin_node": self,
"origin_slot": slot,
"origin_texture": icon.texture,
"origin_quantity": quantity,
"origin_item": item
}
var dragPreview = DRAG_PREVIEW.instance()
dragPreview.texture = icon.texture
add_child(dragPreview)
# Remove the icon from its original place
icon.texture = preload("res://UI/graphics/SkillIcons/empty.png")
quantity = 0
displayQuantity()
print("Item type:", item)
return data
func can_drop_data(position, data):
matchItemTypeToIcon()
displayQuantity()
var target_slot = get_parent().get_name()
data["target_texture"] = icon.texture
data["target_quantity"] = quantity
data["target_item"] = item
return true
func drop_data(position, data):
matchItemTypeToIcon()
displayQuantity()
var origin_texture = data["origin_texture"]
var target_texture = icon.texture
var origin_item = data["origin_item"]
var target_item = item
var origin_quantity = data["origin_quantity"]
var target_quantity = quantity
data["origin_node"].get_node("Icon").texture = target_texture
icon.texture = origin_texture
if origin_item == target_item:
# Only combine the quantities if target_texture and origin_texture are the same
quantity += data["origin_quantity"]
data["origin_node"].get_node("Icon").texture = preload("res://UI/graphics/SkillIcons/empty.png")
else:
# Swap the quantities if the textures are different
quantity = origin_quantity
data["origin_quantity"] = target_quantity
func matchItemTypeToIcon():
var texture_to_item = {
preload("res://UI/graphics/mushrooms/PNG/background/1.png"): "mushroom 1",
preload("res://UI/graphics/mushrooms/PNG/background/2.png"): "mushroom 2",
preload("res://UI/graphics/SkillIcons/empty.png"): "empty"
}
if icon.texture in texture_to_item:
item = texture_to_item[icon.texture]
elif icon.texture == null:
item = "empty"
else:
item = "unknown"