CellTile.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. extends ColorRect
  2. class_name CellTile
  3. @export var draggable : bool = false
  4. @onready var icon : TextureRect = $Icon
  5. @onready var countLabel : Label = $Count
  6. @onready var cooldownLabel : Label = $Cooldown
  7. var cell : BaseCell = null
  8. var cooldownTimer : float = -INF
  9. var shineTimer : float = -INF
  10. var count : int = 0
  11. const modulateDisable : float = 0.5
  12. # Private
  13. func SetToolTip():
  14. var tooltip : String = ""
  15. if cell:
  16. tooltip = cell.name
  17. if cell.description:
  18. tooltip += "\n" + cell.description
  19. if cell.weight == 0:
  20. tooltip += "\n\nWeight: " + str(cell.weight) + "g"
  21. set_tooltip_text(tooltip)
  22. func UpdateCountLabel():
  23. if countLabel:
  24. if count >= 1000:
  25. countLabel.text = "999+"
  26. elif count <= 1:
  27. countLabel.text = ""
  28. else:
  29. countLabel.text = str(count)
  30. if icon:
  31. var modColor : Color = Color.BLACK if count <= 0 else Color.WHITE
  32. modColor.a = modulateDisable if count <= 0 else 1.0
  33. icon.material.set_shader_parameter("modulate", modColor)
  34. # Public
  35. func AssignData(newCell : BaseCell, newCount : int = 1):
  36. if cell != newCell:
  37. if cell:
  38. UnassignData(cell)
  39. if newCell:
  40. newCell.used.connect(Used)
  41. cell = newCell
  42. count = newCount
  43. if icon:
  44. icon.set_texture(cell.icon if cell else null)
  45. UpdateCountLabel()
  46. SetToolTip()
  47. if not draggable:
  48. set_visible(count > 0 and cell != null)
  49. func UnassignData(sourceCell : BaseCell):
  50. icon.material.set_shader_parameter("progress", -INF)
  51. icon.material.set_shader_parameter("modulate", Color.WHITE)
  52. if sourceCell:
  53. if sourceCell.used.is_connected(Used):
  54. sourceCell.used.disconnect(Used)
  55. static func RefreshShortcuts(baseCell : BaseCell, newCount : int = -1):
  56. if baseCell == null or not Launcher.Player or not Launcher.Player.inventory:
  57. return
  58. if baseCell.type != CellCommons.Type.ITEM:
  59. newCount = 1
  60. elif newCount < 0:
  61. newCount = 0
  62. for item in Launcher.Player.inventory.items:
  63. if item and item.cell and item.cell == baseCell:
  64. newCount = item.count
  65. break
  66. var tiles : Array[Node] = Launcher.GUI.get_tree().get_nodes_in_group("CellTile")
  67. for shortcutTile in tiles:
  68. if shortcutTile and shortcutTile.is_visible() and shortcutTile.draggable and shortcutTile.cell == baseCell:
  69. shortcutTile.count = newCount
  70. shortcutTile.UpdateCountLabel()
  71. func UseCell():
  72. if cell:
  73. cell.Use()
  74. func Used(cooldown : float = 0.0):
  75. cooldownTimer = cooldown
  76. func ClearCooldown():
  77. cooldownTimer = -INF
  78. cooldownLabel.text = ""
  79. UpdateCountLabel()
  80. if count > 0:
  81. shineTimer = 1.0
  82. # Drag
  83. func _get_drag_data(_position : Vector2):
  84. if cell and cell.usable:
  85. if icon:
  86. set_drag_preview(icon.duplicate())
  87. return cell
  88. return null
  89. func _can_drop_data(_at_position : Vector2, data):
  90. return draggable and data is BaseCell and data != cell and data.usable
  91. func _drop_data(_at_position : Vector2, data):
  92. AssignData(data)
  93. CellTile.RefreshShortcuts(data)
  94. # Default
  95. func _gui_input(event):
  96. if event is InputEventMouseButton:
  97. if event.button_index == MOUSE_BUTTON_LEFT and event.double_click:
  98. UseCell()
  99. func _process(delta):
  100. if cooldownTimer != -INF:
  101. cooldownTimer -= delta
  102. if cooldownTimer <= 0.0 or Launcher.Player == null:
  103. ClearCooldown()
  104. else:
  105. if cell.type == CellCommons.Type.SKILL:
  106. var cooldown : float = SkillCommons.GetCooldown(Launcher.Player, cell)
  107. var cooldownRatio : float = cooldownTimer / cooldown if cooldown > cooldownTimer else 1.0
  108. var modColor : Color = Color.GRAY.lerp(Color.BLACK, cooldownRatio)
  109. modColor.a = modulateDisable + (cooldownRatio * (1.0 - modulateDisable))
  110. icon.material.set_shader_parameter("modulate", modColor)
  111. cooldownLabel.text = ("%.1f" if cooldownTimer < 10 else "%.0f") % [cooldownTimer]
  112. elif shineTimer != -INF:
  113. shineTimer -= delta
  114. if shineTimer <= 0.0 or Launcher.Player == null:
  115. shineTimer = -INF
  116. icon.material.set_shader_parameter("progress", shineTimer)