PlayerInventory.gd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. extends Node
  2. export var item_dict = {}
  3. var resInventorySlotNode = load("res://Scenes/UI/InventorySlot.tscn")
  4. var inventoryHUDNode = null
  5. export var activeInventorySlot = ""
  6. func _ready():
  7. reload()
  8. func reload():
  9. item_dict.clear()
  10. var savedata = get_node("/root/Save").get_node_data_by_name("PlayerInventory")
  11. if savedata != null:
  12. var savedata_values = savedata.values()[0].values()
  13. #print (savedata_values[0])
  14. item_dict = savedata_values[1]
  15. if savedata_values[0] != "":
  16. activeInventorySlot = savedata_values[0]
  17. else:
  18. activeInventorySlot = ""
  19. reloadInventoryHUD()
  20. func gem_collected(type):
  21. if item_dict.has(str(type)):
  22. item_dict[str(type)] += 1
  23. var inventorySlotNode = inventoryHUDNode.find_node("Slot" + str(type), true, false)
  24. if inventorySlotNode != null:
  25. updateInventoryCounter(type, inventorySlotNode)
  26. else:
  27. item_dict[str(type)] = 1
  28. addItemSlot(type)
  29. print ("gem collected: " + str(type))
  30. #debug use
  31. #var iSN = inventoryHUDNode.find_node("Slot" + str(type), true, false)
  32. #setActiveInventorySlot(iSN)
  33. func save():
  34. var save_dict = {
  35. item_dict = item_dict,
  36. activeInventorySlot = activeInventorySlot
  37. }
  38. return save_dict
  39. func reloadInventoryHUD():
  40. var currentWorld = get_node("/root/global").getCurrentWorld()
  41. inventoryHUDNode = currentWorld.find_node("HUDInventory", true, false)
  42. if inventoryHUDNode != null:
  43. print ("inventoryHUDNode found")
  44. #first delete all slots
  45. for node in inventoryHUDNode.get_children():
  46. node.name = node.name + "_" # rename nodes before deleting,
  47. node.queue_free()
  48. for item_id in item_dict:
  49. addItemSlot(item_id)
  50. #else:
  51. # print ("set active slot to null")
  52. # setActiveInventorySlot(null)
  53. func addItemSlot(item_id):
  54. if inventoryHUDNode != null:
  55. var newInventorySlotNode = resInventorySlotNode.instance()
  56. newInventorySlotNode.set_name("Slot" + str(item_id))
  57. if activeInventorySlot == newInventorySlotNode.name:
  58. setActiveInventorySlot(newInventorySlotNode)
  59. setInventoryImage(item_id, newInventorySlotNode)
  60. updateInventoryCounter(item_id, newInventorySlotNode)
  61. #add sortName to the node here
  62. var sortName = get_node("/root/GameItemList").get_item_value(item_id, "sortName")
  63. if sortName == null:
  64. print ("Warning: item has no sortName")
  65. return
  66. newInventorySlotNode.sort_name = str(sortName)
  67. if inventoryHUDNode.get_child_count() > 0:
  68. inventoryHUDNode.add_child(newInventorySlotNode)
  69. #### sort: todo: extract code into own function
  70. var current_pos = null
  71. var current_sort_index = newInventorySlotNode.get_index()
  72. for ch in inventoryHUDNode.get_children():
  73. if sortName.casecmp_to (ch.sort_name ) == -1 and current_sort_index > ch.get_index():
  74. current_pos = ch
  75. current_sort_index = ch.get_index()
  76. if current_pos != null:
  77. inventoryHUDNode.move_child ( newInventorySlotNode, current_pos.get_index() )
  78. ####
  79. else:
  80. inventoryHUDNode.add_child(newInventorySlotNode)
  81. if activeInventorySlot == "":
  82. setActiveInventorySlot(newInventorySlotNode)
  83. func setInventoryImage(item_id, inv_slot_node):
  84. var inventoryImageName = get_node("/root/GameItemList").get_item_value(item_id, "inventoryImage")
  85. if inventoryImageName == null:
  86. return
  87. var inventoryImagePath = "res://Textures/UI/" + str(inventoryImageName)
  88. var inventoryImage = load(inventoryImagePath)
  89. if inventoryImage == null:
  90. return
  91. var inventoryImageNode = inv_slot_node.find_node("InventoryImage", true, false)
  92. if inventoryImageNode != null:
  93. inventoryImageNode.texture = inventoryImage
  94. func updateInventoryCounter(item_id, inv_slot_node):
  95. var counterLabelNode = inv_slot_node.find_node("CounterLabel", true, false)
  96. if item_dict.has(str(item_id)) and counterLabelNode != null:
  97. counterLabelNode.text = str(item_dict[str(item_id)])
  98. func setActiveInventorySlot(inventorySlotNode):
  99. if inventorySlotNode == null:
  100. activeInventorySlot = ""
  101. print ("error404: inventorySlotNode is null")
  102. return
  103. print (activeInventorySlot)
  104. if activeInventorySlot != "":
  105. var oldInventorySlotNode = inventoryHUDNode.find_node(activeInventorySlot, true, false)
  106. if oldInventorySlotNode != null:
  107. oldInventorySlotNode.set_as_active_slot(false)
  108. activeInventorySlot = inventorySlotNode.name
  109. if activeInventorySlot != "":
  110. inventorySlotNode.set_as_active_slot(true)
  111. func _input(event):
  112. var inventorySlotNode = inventoryHUDNode.find_node(activeInventorySlot, true, false)
  113. var inventorySlotNodePos = -1
  114. if inventorySlotNode != null:
  115. inventorySlotNodePos = inventorySlotNode.get_position_in_parent()
  116. if event is InputEventMouseButton and event.pressed:
  117. match event.button_index:
  118. BUTTON_WHEEL_UP:
  119. var rightNeighborNode = inventoryHUDNode.get_child(inventorySlotNodePos + 1)
  120. if rightNeighborNode != null:
  121. print ("can change >>>")
  122. setActiveInventorySlot(rightNeighborNode)
  123. print ("mouse up: " + str(inventorySlotNodePos))
  124. BUTTON_WHEEL_DOWN:
  125. var leftNeighborNode = inventoryHUDNode.get_child(inventorySlotNodePos - 1)
  126. if leftNeighborNode != null:
  127. print ("can change <<<")
  128. setActiveInventorySlot(leftNeighborNode)
  129. print ("mouse down: " + str(inventorySlotNodePos))
  130. #save active slot (position ?, id ?) and load
  131. #add action