Guest User

smasnoturnback-v1.2.lua

a guest
Nov 17th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. --smasnoturnback.lua (v1.2)
  2. --By Spencer Everly
  3. --This script provides a remake of the noTurnBack option, but with additional things like going left but not turning back right, and other things!
  4.  
  5. --This is also compatible with customCamera, and supports zooming and area stretching with it. You don't have to use customCamera to use this library, but just a reminder that it works with it.
  6.  
  7. local smasnoturnback = {}
  8.  
  9. local customCamera
  10. pcall(function() customCamera = require("customCamera") end)
  11. local autoscroll = require("autoscroll")
  12.  
  13. smasnoturnback.enabled = false --Enable this to activate everything here, make sure this is enabled BEFORE you set the turn back option!
  14. smasnoturnback.overrideSection = false --Set to true to prevent the turn back on a certain area, useful for onLoadSection(number)
  15. smasnoturnback.turnBack = "left" --Set to 'right' for a no right turn back, or 'up' for a no top turn back, or even 'down' for a no bottom turn back. Anything else accidentally set will be automatically set to 'left'.
  16.  
  17. function smasnoturnback.setSectionBounds(section, left, top, bottom, right)
  18. local sectionObj = Section(section)
  19. local bounds = sectionObj.boundary
  20. bounds.left = left
  21. bounds.top = top
  22. bounds.bottom = bottom
  23. bounds.right = right
  24. sectionObj.boundary = bounds
  25. end
  26.  
  27. function smasnoturnback.onInitAPI()
  28. registerEvent(smasnoturnback,"onStart")
  29. registerEvent(smasnoturnback,"onCameraDraw")
  30. registerEvent(smasnoturnback,"onCameraUpdate")
  31. registerEvent(smasnoturnback,"onDraw")
  32. registerEvent(smasnoturnback,"onTick")
  33. end
  34.  
  35. smasnoturnback.originalBoundariesTop = {}
  36. smasnoturnback.originalBoundariesBottom = {}
  37. smasnoturnback.originalBoundariesLeft = {}
  38. smasnoturnback.originalBoundariesRight = {}
  39.  
  40. smasnoturnback.failsafeTable = {
  41. "left",
  42. "right",
  43. "up",
  44. "down",
  45. }
  46.  
  47. function smasnoturnback.onStart()
  48. for i = 0,20 do
  49. table.insert(smasnoturnback.originalBoundariesTop, Section(i).origBoundary.top)
  50. table.insert(smasnoturnback.originalBoundariesBottom, Section(i).origBoundary.bottom)
  51. table.insert(smasnoturnback.originalBoundariesLeft, Section(i).origBoundary.left)
  52. table.insert(smasnoturnback.originalBoundariesRight, Section(i).origBoundary.right)
  53. end
  54. end
  55.  
  56. function smasnoturnback.onCameraUpdate()
  57. if not SaveData.disableX2char then
  58. for k,v in ipairs(Section.getActiveIndices()) do
  59. if not autoscroll.isSectionScrolling(v) then
  60. if smasnoturnback.enabled and not smasnoturnback.overrideSection then
  61. for itwo = 1,4 do
  62. if smasnoturnback.turnBack ~= smasnoturnback.failsafeTable[itwo] then --Failsafe if the turnBack argument is anything else but the things in this script
  63. smasnoturnback.turnBack = "left"
  64. end
  65. end
  66. if smasnoturnback.turnBack == "left" then
  67. if customCamera then
  68. local fullX,fullY,fullWidth,fullHeight = customCamera.getFullCameraPos()
  69. if fullX >= player.sectionObj.boundary.left then
  70. local x1 = fullX
  71. smasnoturnback.setSectionBounds(player.section, x1, player.sectionObj.boundary.top, player.sectionObj.boundary.bottom, player.sectionObj.boundary.right)
  72. end
  73. else
  74. local fullX = camera.x
  75. if camera.x >= player.sectionObj.boundary.left then
  76. local x1 = fullX
  77. smasnoturnback.setSectionBounds(player.section, x1, player.sectionObj.boundary.top, player.sectionObj.boundary.bottom, player.sectionObj.boundary.right)
  78. end
  79. end
  80. elseif smasnoturnback.turnBack == "right" then
  81. if customCamera then
  82. local fullX,fullY,fullWidth,fullHeight = customCamera.getFullCameraPos()
  83. if fullX <= player.sectionObj.boundary.right then
  84. local x1 = fullX + 800
  85. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, player.sectionObj.boundary.top, player.sectionObj.boundary.bottom, x1)
  86. end
  87. else
  88. local fullX = camera.x
  89. if camera.x <= player.sectionObj.boundary.right then
  90. local x1 = fullX + 800
  91. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, player.sectionObj.boundary.top, player.sectionObj.boundary.bottom, x1)
  92. end
  93. end
  94. elseif smasnoturnback.turnBack == "up" then
  95. if customCamera then
  96. local fullX,fullY,fullWidth,fullHeight = customCamera.getFullCameraPos()
  97. if fullY >= player.sectionObj.boundary.top then
  98. local x1 = fullY
  99. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, x1, player.sectionObj.boundary.bottom, player.sectionObj.boundary.right)
  100. end
  101. else
  102. local fullY = camera.y
  103. if camera.y >= player.sectionObj.boundary.top then
  104. local x1 = fullY
  105. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, x1, player.sectionObj.boundary.bottom, player.sectionObj.boundary.right)
  106. end
  107. end
  108. elseif smasnoturnback.turnBack == "down" then
  109. if customCamera then
  110. local fullX,fullY,fullWidth,fullHeight = customCamera.getFullCameraPos()
  111. if fullY <= player.sectionObj.boundary.bottom then
  112. local x1 = fullY + 600
  113. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, player.sectionObj.boundary.top, x1, player.sectionObj.boundary.right)
  114. end
  115. else
  116. local fullY = camera.y
  117. if camera.y <= player.sectionObj.boundary.bottom then
  118. local x1 = fullY + 600
  119. smasnoturnback.setSectionBounds(player.section, player.sectionObj.boundary.left, player.sectionObj.boundary.top, x1, player.sectionObj.boundary.right)
  120. end
  121. end
  122. end
  123. end
  124. else
  125. smasnoturnback.enabled = false
  126. smasnoturnback.overrideSection = true
  127. end
  128. end
  129. end
  130. end
  131.  
  132. local levelTablesWithNoTurnbacks = {
  133. "levelsGoHere.lvlx",
  134. "youCanPutAnything.lvlx",
  135. "inThisTable.lvlx",
  136. "thatCanHaveANoTurnBack.lvlx",
  137. }
  138.  
  139. function smasnoturnback.onTick() --If you want a certain level or more, make a table with level filenames on it. A sample table is included above.
  140.  
  141. --This is a sample table used for applying no-turn-backs on levels.
  142. if table.icontains(levelTablesWithNoTurnbacks,Level.filename()) and not smasnoturnback.overrideSection then
  143. smasnoturnback.enabled = true
  144. end
  145.  
  146.  
  147.  
  148. if smasnoturnback.overrideSection then
  149. smasnoturnback.enabled = false
  150. end
  151. end
  152.  
  153. function smasnoturnback.sectionsWithNoPlayers() --Returns sections with no players in them.
  154. local nonPlayeredSections = {}
  155. local playeredSections = Section.getActiveIndices()
  156. for i = 0,20 do
  157. if playeredSections[i] ~= i then
  158. table.insert(nonPlayeredSections, i)
  159. end
  160. end
  161. return nonPlayeredSections
  162. end
  163.  
  164. function smasnoturnback.onDraw()
  165. for k,v in ipairs(smasnoturnback.sectionsWithNoPlayers()) do --When using the sections with no players code, revert any sections with no players in them to their original boundaries.
  166. if not autoscroll.isSectionScrolling(v) then
  167. if smasnoturnback.enabled and not smasnoturnback.overrideSection then
  168. for _,p in ipairs(Player.get()) do
  169. if p.sectionObj.idx ~= v then
  170. local sectionObj = Section(v)
  171. local bounds = sectionObj.boundary
  172. bounds.left = smasnoturnback.originalBoundariesLeft[v + 1]
  173. bounds.top = smasnoturnback.originalBoundariesTop[v + 1]
  174. bounds.bottom = smasnoturnback.originalBoundariesBottom[v + 1]
  175. bounds.right = smasnoturnback.originalBoundariesRight[v + 1]
  176. sectionObj.boundary = bounds
  177. end
  178. end
  179. end
  180. end
  181. end
  182. end
  183.  
  184. return smasnoturnback
Advertisement
Add Comment
Please, Sign In to add comment