Inventory.gd 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. extends Control
  2. @onready var hand = get_parent().get_parent().get_node("Hand")
  3. var hand_index = 0
  4. var hand_selected = false
  5. var item_count = 0
  6. func add_item(item):
  7. item_count = item_count + 1
  8. var instance = item.duplicate()
  9. var style = StyleBoxFlat.new()
  10. # set style
  11. style.bg_color = Color("#313131")
  12. instance.add_theme_stylebox_override('panel', style)
  13. # mobile button code
  14. if GameManager.is_running_on_mobile():
  15. var b = TouchScreenButton.new()
  16. b.position = Vector2(0, 0)
  17. b.texture_normal = preload("res://Textures/Controls/mobile_hotbar_fix.png")
  18. b.texture_pressed = preload("res://Textures/Controls/mobile_hotbar_fix.png")
  19. b.shape = RectangleShape2D.new()
  20. b.shape.size = Vector2(40, 40)
  21. b.set_script(load("res://Scripts/Misc/mobile_hotbar_button.gd"))
  22. b.shape_visible = true
  23. instance.add_child(b)
  24. self.add_child(instance)
  25. instance.visible = true
  26. func remove_id(id):
  27. for item in get_children():
  28. if item.has_meta("id") and item.get_meta("id") == id:
  29. item.queue_free()
  30. if hand.get_child(0) and hand.get_child(0).has_meta("id") and hand.get_child(0).get_meta("id") == id:
  31. item_count = item_count - 1
  32. hand.get_child(0).queue_free()
  33. return
  34. func remove_index(index):
  35. get_child(index).queue_free()
  36. item_count = item_count - 1
  37. func remove_item(item):
  38. item.queue_free()
  39. item_count = item_count - 1
  40. func select_item(index):
  41. if get_child_count() >= index and get_child(index):
  42. if hand_selected and hand_index == index:
  43. for child in hand.get_children():
  44. child.queue_free()
  45. for item_panel in get_children():
  46. unhighlight_panel(item_panel)
  47. hand_selected = false
  48. return
  49. hand_index = index # set the selected index to the current one
  50. var item = get_child(index) # get the index
  51. # unhighlight everything
  52. for item_panel in get_children():
  53. unhighlight_panel(item_panel)
  54. # highlight the current item
  55. highlight_panel(item)
  56. var mesh = item.get_child(0).get_child(0).get_child(0) # get the mesh
  57. # Clear everything in the hand
  58. for child in hand.get_children():
  59. child.queue_free()
  60. mesh = mesh.duplicate() # duplicate the mesh
  61. if item.has_meta("id"):
  62. mesh.set_meta("id", item.get_meta("id"))
  63. if item.has_meta("scale"):
  64. mesh.scale = item.get_meta("scale")
  65. if item.has_meta("rotation"):
  66. mesh.rotation_degrees = item.get_meta("rotation")
  67. else:
  68. mesh.rotation_degrees = Vector3.ZERO
  69. hand.add_child(mesh)
  70. hand_selected = true
  71. else:
  72. hand_selected = false
  73. for item_panel in get_children():
  74. unhighlight_panel(item_panel)
  75. for child in hand.get_children():
  76. child.queue_free()
  77. return
  78. func highlight_panel(panel):
  79. var style = panel.get_theme_stylebox('panel')
  80. style.bg_color = Color("#525252")
  81. func unhighlight_panel(panel):
  82. var style = panel.get_theme_stylebox('panel')
  83. style.bg_color = Color("#313131")
  84. func _input(event):
  85. if event.is_action_pressed("slot1"):
  86. select_item(0)
  87. elif event.is_action_pressed("slot2"):
  88. select_item(1)
  89. elif event.is_action_pressed("slot3"):
  90. select_item(2)
  91. elif event.is_action_pressed("slot4"):
  92. select_item(3)
  93. elif event.is_action_pressed("slot5"):
  94. select_item(4)