CellTile.gd 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. extends ColorRect
  2. class_name CellTile
  3. @export var draggable : bool = false
  4. @export var defaultIcon : CompressedTexture2D = null
  5. @onready var icon : TextureRect = $Icon
  6. @onready var countLabel : Label = $Count
  7. @onready var cooldownLabel : Label = $Cooldown
  8. var cell : BaseCell = null
  9. var selection : TextureRect = null
  10. var cooldownTimer : float = -INF
  11. var shineTimer : float = -INF
  12. var count : int = 0
  13. const modulateDisable : float = 0.5
  14. signal selected
  15. # Private
  16. func SetToolTip():
  17. var tooltip : String = ""
  18. if cell:
  19. tooltip = cell.name
  20. if cell is ItemCell and not cell.customfield.is_empty():
  21. tooltip += " (%s)" % cell.customfield
  22. if cell.description:
  23. tooltip += "\n%s" % cell.description
  24. if cell is SkillCell and Launcher.Player and Launcher.Player.progress:
  25. var skillLevel : int = Launcher.Player.progress.GetSkillLevel(cell)
  26. tooltip += "\nLevel: %d" % skillLevel
  27. if cell.weight != 0:
  28. tooltip += "\n\nWeight: %dg" % cell.weight
  29. set_tooltip_text(tooltip)
  30. func UpdateCountLabel():
  31. if countLabel:
  32. if not defaultIcon and ActorCommons.IsEquipped(cell):
  33. countLabel.text = "~"
  34. elif count >= 1000:
  35. countLabel.text = "999+"
  36. elif count <= 1:
  37. countLabel.text = ""
  38. else:
  39. countLabel.text = str(count)
  40. if icon:
  41. var modColor : Color = Color.BLACK if count <= 0 else Color.WHITE
  42. modColor.a = modulateDisable if count <= 0 else 1.0
  43. icon.material.set_shader_parameter("modulate", modColor)
  44. # Public
  45. func AssignData(newCell : BaseCell, newCount : int = 1):
  46. if cell != newCell:
  47. if cell:
  48. UnassignData(cell)
  49. if newCell:
  50. newCell.used.connect(Used)
  51. cell = newCell
  52. count = newCount
  53. if icon:
  54. icon.set_texture(cell.icon if cell else defaultIcon)
  55. if cell and cell is ItemCell and cell.shader != null:
  56. icon.set_material(cell.shader)
  57. UpdateCountLabel()
  58. SetToolTip()
  59. if not draggable and not defaultIcon:
  60. set_visible(count > 0 and cell != null)
  61. func UnassignData(sourceCell : BaseCell):
  62. if icon:
  63. icon.material.set_shader_parameter("progress", -INF)
  64. icon.material.set_shader_parameter("modulate", Color.WHITE)
  65. icon.set_texture(defaultIcon)
  66. if sourceCell:
  67. if sourceCell.used.is_connected(Used):
  68. sourceCell.used.disconnect(Used)
  69. func AddSelection():
  70. if not selection:
  71. selection = UICommons.CellSelectionPreset.instantiate()
  72. add_child.call_deferred(selection)
  73. func RemoveSelection():
  74. if selection:
  75. remove_child.call_deferred(selection)
  76. selection.queue_free()
  77. selection = null
  78. static func RefreshShortcuts(baseCell : BaseCell, newCount : int = -1):
  79. if baseCell == null or not Launcher.Player or not Launcher.Player.inventory:
  80. return
  81. if baseCell.type != CellCommons.Type.ITEM:
  82. newCount = 1
  83. elif newCount < 0:
  84. newCount = 0
  85. for item in Launcher.Player.inventory.items:
  86. if item and CellCommons.IsSameItem(baseCell, item):
  87. newCount = item.count
  88. break
  89. var tiles : Array[Node] = Launcher.GUI.get_tree().get_nodes_in_group("CellTile")
  90. for shortcutTile in tiles:
  91. if shortcutTile and shortcutTile.is_visible() and shortcutTile.draggable and shortcutTile.cell == baseCell:
  92. shortcutTile.count = newCount
  93. shortcutTile.UpdateCountLabel()
  94. func UseCell():
  95. if cell:
  96. cell.Use()
  97. func Used(cooldown : float = 0.0):
  98. cooldownTimer = cooldown
  99. set_process(true)
  100. func ClearCooldown():
  101. cooldownTimer = -INF
  102. cooldownLabel.text = ""
  103. UpdateCountLabel()
  104. if count > 0:
  105. shineTimer = 1.0
  106. # Drag
  107. func _get_drag_data(_position : Vector2):
  108. if cell and cell.usable:
  109. if icon:
  110. set_drag_preview(icon.duplicate())
  111. return cell
  112. return null
  113. func _can_drop_data(_at_position : Vector2, data):
  114. return draggable and data is BaseCell and data != cell and data.usable
  115. func _drop_data(_at_position : Vector2, data):
  116. AssignData(data)
  117. CellTile.RefreshShortcuts(data)
  118. # Default
  119. func _ready():
  120. if icon:
  121. icon.set_texture(cell.icon if cell else defaultIcon)
  122. set_process(false)
  123. func _gui_input(event):
  124. if event is InputEventMouseButton:
  125. if event.button_index == MOUSE_BUTTON_LEFT:
  126. if event.double_click:
  127. UseCell()
  128. elif event.pressed:
  129. selected.emit(self)
  130. func _process(delta : float):
  131. if cooldownTimer != -INF:
  132. cooldownTimer -= delta
  133. if cooldownTimer <= 0.0 or Launcher.Player == null:
  134. ClearCooldown()
  135. else:
  136. if cell.type == CellCommons.Type.SKILL:
  137. var cooldown : float = SkillCommons.GetCooldown(Launcher.Player, cell)
  138. var cooldownRatio : float = cooldownTimer / cooldown if cooldown > cooldownTimer else 1.0
  139. var modColor : Color = Color.GRAY.lerp(Color.BLACK, cooldownRatio)
  140. modColor.a = modulateDisable + (cooldownRatio * (1.0 - modulateDisable))
  141. icon.material.set_shader_parameter("modulate", modColor)
  142. cooldownLabel.text = ("%.1f" if cooldownTimer < 10 else "%.0f") % [cooldownTimer]
  143. elif shineTimer != -INF:
  144. shineTimer -= delta
  145. if shineTimer <= 0.0 or Launcher.Player == null:
  146. shineTimer = -INF
  147. icon.material.set_shader_parameter("progress", shineTimer)
  148. else:
  149. set_process(false)