Advertisement
Guest User

Upgrade Card TTS code from Slay the Spire

a guest
Dec 17th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | Gaming | 0 0
  1. function onLoad(saved_game_data)
  2. if saved_game_data ~= "" and saved_game_data ~= nil then
  3. local loaded_data = JSON.decode(saved_game_data)
  4. --local left_zone_guid = loaded_data[1]
  5. end
  6.  
  7. setupSnapPoints()
  8. createButtons()
  9. end
  10.  
  11. function onSave()
  12. local data_to_save = {
  13. }
  14.  
  15. saved_game_data = JSON.encode(data_to_save)
  16.  
  17. return saved_game_data
  18. end
  19.  
  20. function createButtons()
  21. self.createButton({
  22. click_function = "swapUpgrade",
  23. function_owner = self,
  24. label = "Upgrade",
  25. position = {0, 0.75, -7},
  26. rotation = {0, 0, 0},
  27. --scale = {1/self.getScale()[1], 1/self.getScale()[2], 1/self.getScale()[3]},
  28. width = 4500,
  29. height = 700,
  30. tooltip = "",
  31. font_color = {1, 1, 1},
  32. font_size = 500,
  33. color = "Grey"
  34. })
  35. end
  36.  
  37. function swapUpgrade(obj, color, alt_click)
  38. if alt_click then
  39. return
  40. end
  41.  
  42. getObjectsAsync(swapWithUpgrade, {color})
  43. end
  44.  
  45. function swapWithUpgrade(objectsOnToolInfo, args)
  46. local requestingPlayerColor = args[1]
  47.  
  48. local object_list = objectsOnToolInfo
  49. local top_source_obj_guid = getObjectGUIDOnTop(object_list)
  50. local target_pos = getSlotPosition()
  51. for _, source_obj in ipairs(object_list) do
  52. if source_obj.name == "Card" or source_obj.name == "CardCustom" or source_obj.name == "Deck" or source_obj.name == "DeckCustom" then
  53. local source_obj_guid = source_obj.getGUID()
  54. target_pos = source_obj.getPosition()
  55. target_pos[2] = target_pos[2] + 0.5
  56.  
  57. if source_obj.name == "Deck" or source_obj.name == "DeckCustom" then
  58. -- Go through the deck and do the evaluation for each card
  59. for _, cur_card in ipairs(source_obj.getObjects()) do
  60. -- Upgrade cards should return to the upgrade deck. Base cards should go to a global discard pile
  61. if Global.call('IsUpgradeCard', cur_card.guid) then
  62. local reward_deck_info = Global.getTable('PLAYER_REWARD_DECKS')
  63. if reward_deck_info[requestingPlayerColor] ~= nil and reward_deck_info[requestingPlayerColor]["Upgrade"] ~= nil then
  64. local targetPos = reward_deck_info[requestingPlayerColor]["Upgrade"]
  65. targetPos[2] = targetPos[2] + 1
  66. local targetRot = {0, 180, 180}
  67.  
  68. -- Need to always retest if the deck still exists
  69. if source_obj and getObjectFromGUID(source_obj_guid) and source_obj.remainder == nil then
  70. source_obj.takeObject({guid=cur_card.guid, position=targetPos, rotation=targetRot})
  71. else
  72. local cur_card_obj = getObjectFromGUID(cur_card.guid)
  73. cur_card_obj.setPositionSmooth(targetPos)
  74. cur_card_obj.setRotationSmooth(targetRot)
  75. end
  76. else
  77. broadcastToColor("WARNING: Unable to find upgrade deck to return upgrade card.", requestingPlayerColor)
  78. end
  79. else
  80. -- Base card should be discarded
  81. local targetPos = getDiscardLocation()
  82. local targetRot = {0, 180, 0}
  83.  
  84. -- Need to always retest if the deck still exists
  85. if source_obj and getObjectFromGUID(source_obj_guid) and source_obj.remainder == nil then
  86. source_obj.takeObject({guid=cur_card.guid, position=targetPos, rotation=targetRot})
  87. else
  88. local cur_card_obj = getObjectFromGUID(cur_card.guid)
  89. cur_card_obj.setPositionSmooth(targetPos)
  90. cur_card_obj.setRotationSmooth(targetRot)
  91. end
  92. end
  93. end
  94. else
  95. -- Upgrade cards should return to the upgrade deck. Base cards should go to a global discard pile
  96. if Global.call('IsUpgradeCard', source_obj.getGUID()) then
  97. -- Upgrade card. Put all the upgraded cards back in their original decks
  98. local reward_deck_info = Global.getTable('PLAYER_REWARD_DECKS')
  99. if reward_deck_info[requestingPlayerColor] ~= nil and reward_deck_info[requestingPlayerColor]["Upgrade"] ~= nil then
  100. local targetPos = reward_deck_info[requestingPlayerColor]["Upgrade"]
  101. targetPos[2] = targetPos[2] + 1
  102. local targetRot = {0, 180, 180}
  103.  
  104. source_obj.setPositionSmooth(targetPos)
  105. source_obj.setRotationSmooth(targetRot)
  106. else
  107. broadcastToColor("WARNING: Unable to find upgrade deck to return upgrade card.", requestingPlayerColor)
  108. end
  109. else
  110. --Base card
  111. local targetPos = getDiscardLocation()
  112. local targetRot = {0, 180, 0}
  113.  
  114. source_obj.setPositionSmooth(targetPos)
  115. source_obj.setRotationSmooth(targetRot)
  116. end
  117. end
  118. end
  119. end
  120.  
  121. -- Get the associated card for the object that was on top
  122. local associated_target_obj_guid = Global.call('GetAssociatedUpgradeCardGUID', top_source_obj_guid)
  123. Global.call('FindAndDeployObjectExternal', {associated_target_obj_guid, target_pos, self.getRotation()})
  124. end
  125.  
  126. function getDiscardLocation()
  127. local toolPos = self.getPosition()
  128. return Global.call("getPlayerDiscardLocation", toolPos)
  129. end
  130.  
  131. function getSlotPosition()
  132. local X_OFFSET = 0
  133. local Z_OFFSET = 0.135
  134. local object_scale = self.getScale()
  135. local expected_scale = {0.18, 0.18, 0.18}
  136. local base_scale = {2.46, 2, 3.38}
  137. local scale_factor = {object_scale[1] / expected_scale[1], object_scale[2] / expected_scale[2], object_scale[3] / expected_scale[3]}
  138. local target_scale = {scale_factor[1] * base_scale[1], scale_factor[2] * base_scale[2], scale_factor[3] * base_scale[3]}
  139.  
  140. local base_pos = self.getPosition()
  141.  
  142. local local_x_offset = (X_OFFSET * scale_factor[1]) * math.cos(math.rad(self.getRotation()[2])) + (Z_OFFSET * scale_factor[3]) * math.sin(math.rad(self.getRotation()[2]))
  143. local local_z_offset = (Z_OFFSET * scale_factor[3]) * math.cos(math.rad(self.getRotation()[2])) - (X_OFFSET * scale_factor[1]) * math.sin(math.rad(self.getRotation()[2]))
  144. local pos = {base_pos.x + local_x_offset, base_pos.y, base_pos.z + local_z_offset}
  145. return pos
  146. end
  147.  
  148. -- Takes a list of objects and returns the one on top. Optional if you require that the object is face up
  149. function getObjectGUIDOnTop(objList, requireFaceup)
  150. local topObj = nil
  151. for _, obj in ipairs(objList) do
  152. if topObj == nil or obj.getPosition().y > topObj.getPosition().y then
  153. if not requireFaceup or (obj.getRotation().z > 135 or obj.getRotation().z < -135) then
  154. topObj = obj
  155. end
  156. end
  157. end
  158.  
  159. if topObj.name == "Deck" or topObj.name == "DeckCustom" then
  160. if topObj.getRotation().z > 135 or topObj.getRotation().z < -135 then
  161. return topObj.getObjects()[1].guid
  162. else
  163. return topObj.getObjects()[#topObj.getObjects()].guid
  164. end
  165. end
  166.  
  167. return topObj.getGUID()
  168. end
  169.  
  170. -- Retrieves objects on the tool and calls the associated callback with the results. Does this by spawning a temporary scripting zone on each area and getting objects
  171. function getObjectsAsync(callback_function, callback_func_args)
  172. local hitList = Physics.cast({
  173. origin = getSlotPosition(),
  174. direction = {0,1,0},
  175. type = 3,
  176. size = {3.21, 2, 4.30},
  177. max_distance = 0,
  178. debug = false,
  179. })
  180.  
  181. local cardList = {}
  182. for i, hitInfo in ipairs(hitList) do
  183. -- Gather only cards and decks
  184. if hitInfo.hit_object.name == "Card" or hitInfo.hit_object.name == "CardCustom" or hitInfo.hit_object.name == "Deck" or hitInfo.hit_object.name == "DeckCustom" then
  185. table.insert(cardList, hitInfo.hit_object)
  186. end
  187. end
  188.  
  189. callback_function(cardList, callback_func_args)
  190. end
  191.  
  192. function setupSnapPoints()
  193. -- Add Snap points for the card area
  194. local mat_scale = self.getScale()
  195. local snap_points = {}
  196.  
  197. local X_OFFSET = 0
  198. local Z_OFFSET = 0.135
  199.  
  200. local pos = {X_OFFSET, 0, Z_OFFSET}
  201.  
  202. table.insert(snap_points, {position = {pos[1] / mat_scale[1], 0.0, pos[3] / mat_scale[3]}, rotation_snap = true})
  203. self.setSnapPoints(snap_points)
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement