MegoZ_

Dive from Super Mario Odyssey

Jun 30th, 2022 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.40 KB | None | 0 0
  1. local dive = {}
  2. --v1.3
  3.  
  4. local textplus = require("textplus")
  5. local playeranim = require("playerAnim")
  6. local rng = require("rng")
  7. local twirl
  8.  
  9. function dive.onInitAPI()
  10.     registerEvent(dive, "onTick", "onTick")
  11.     registerEvent(dive, "onDraw", "onDraw")
  12. end
  13.  
  14. dive.showMeDebug = false
  15. local diveSfx = Misc.resolveFile("dive.ogg")
  16.  
  17. local isDiving = {false,false}
  18. local diveTimeline = {0,0}
  19. local diveCooldown = {0,0}
  20.  
  21. --The Dive™
  22. dive.impulseY =     4.27
  23. dive.diveCooldown =     0
  24. dive.allowEveryCharacter = false
  25.  
  26. --Animation
  27. local diveFrames = {41,41,41,2}
  28. local animationSpeed = 7
  29. local diveAnim = playeranim.Anim(diveFrames, animationSpeed)
  30.  
  31.  
  32.  
  33.  
  34. function dive.onTick() for k, p in ipairs(Player.get()) do
  35.  
  36.  
  37.  
  38.     if p:mem(0x12E,FIELD_BOOL) then
  39.         diveAnim:stop(p)
  40.     end
  41.  
  42.     local function resetDiving()
  43.         isDiving[k] = false
  44.         diveTimeline[k] = 0
  45.         diveCooldown[k] = 0
  46.     end
  47.  
  48.     -- Convenience Functions
  49.     local function isOnGround() return(
  50.         p:isOnGround()
  51.         or (p.mount == MOUNT_BOOT and p:mem(0x10C,FIELD_BOOL))      -- Hopping in boot
  52.         or p:mem(0x40,FIELD_WORD) > 0                               -- Climbing
  53.     )end
  54.     local function isOnMount() return(
  55.          
  56.         p.climbing
  57.     )end
  58.     local function isUnderwater() return(
  59.         p:mem(0x36,FIELD_BOOL)              -- In a liquid
  60.         or p:mem(0x06,FIELD_BOOL)       -- In quicksand
  61.     ) end
  62.     local function canDive() return (
  63.             not isDiving[k] and
  64.             not isOnGround() and
  65.             not p:mem(0x50,FIELD_BOOL) and -- Spinning
  66.             not isUnderwater() and
  67.             not isOnMount() and
  68.             not p:mem(0x44, FIELD_BOOL) and -- Riding a rainbow shell
  69.             not p:mem(0x13C, FIELD_BOOL) and -- Is Dead
  70.             not p.holdingNPC and
  71.             not p.isMega and
  72.             p.deathTimer == 0 and
  73.             Level.winState() == 0 and
  74.             p.forcedState == 0 and
  75.             ((p.character == 1 or p.character == 2) or dive.allowEveryCharacter)
  76.     ) end
  77.  
  78.  
  79.     if (package.loaded["twirl"] ~= nil) then
  80.         twirl = require("twirl")
  81.  
  82.         if diveTimeline[k] ~= 0 then
  83.             twirl.cancelTwirl(k,p)
  84.         end
  85.     end
  86.  
  87.  
  88.     dive.controls = p.keys.down and p.keys.altJump
  89.     if dive.controls and diveCooldown[k] > dive.diveCooldown then
  90.         isDiving[k] = true
  91.     elseif canDive() then
  92.         isDiving[k] = false
  93.         diveCooldown[k] = diveCooldown[k] + 1
  94.     else
  95.         diveCooldown[k] = 0
  96.     end
  97.  
  98.     if isDiving[k] then
  99.         diveTimeline[k] = diveTimeline[k] + 1
  100.  
  101.         if diveTimeline[k] < animationSpeed*#diveFrames then
  102.             player.keys.down = false
  103.         end
  104.     end
  105.  
  106.  
  107.     if diveTimeline[k] == 1 then
  108.         SFX.play(diveSfx)
  109.         diveAnim:play(p)
  110.  
  111.         if p.powerup ~= 1 then
  112.             local poof = Animation.spawn(10, p.x-10, p.y+p.height/3)
  113.             poof.speedX = 1*-p.direction + math.abs(p.speedX/8)*-p.direction
  114.             poof.speedY = 1
  115.         else
  116.             local poof = Animation.spawn(10, p.x-10, p.y)
  117.             poof.speedX = 1*-p.direction + math.abs(p.speedX/8)*-p.direction
  118.             poof.speedY = 1
  119.         end
  120.  
  121.         if math.abs(p.speedX) < 2 then
  122.             p.speedX = p.direction * (math.abs(p.speedX)+2.5) * 1.23
  123.         else
  124.             p.speedX = p.direction * (math.abs(p.speedX)+1) * 1.2
  125.         end
  126.         p.speedY = -dive.impulseY
  127.     end
  128.  
  129.     --Stop Animation
  130.     if diveTimeline[k] >= animationSpeed*#diveFrames or diveTimeline[k] == 0 or isOnMount() then
  131.         diveAnim:stop(p)
  132.     end
  133.  
  134.     --Reset Diving
  135.     for kn,n in ipairs(NPC.getIntersecting(p.x, p.y, p.x+p.width, p.y+p.height+11)) do
  136.         if n.isValid and (n.id == 26 or n.id == 457) then
  137.             resetDiving()
  138.         end
  139.     end
  140.     if isOnGround() or isOnMount() or isUnderwater() then              
  141.         resetDiving()
  142.     end
  143.  
  144.  
  145. --Debug: Prints Variables on Screen — Set ``dive.showMeDebug`` to true in order to activate. (Works with 2 players)
  146.     local function print(line, text, variable,color)
  147.         if dive.showMeDebug == false then return end
  148.         debugFont = textplus.loadFont("scripts/textplus/font/11.ini")
  149.    
  150.         textplus.print{font=debugFont,xscale=1.5,yscale=1.5,x=20^k*1.02,y=(6+line)*15,text=text..": "..tostring(variable),color=color}
  151.     end
  152.     print(1,    "p.speedX",                 p.speedX                        )
  153.     print(4,    "Is Diving",                isDiving[k]                     )
  154.     print(6,    "Can Dive?",                canDive()                       )
  155.     print(8,    "Timeline of Dive",         diveTimeline[k]                 )
  156.     print(9,    "Is Mounting Something",    isOnMount()                     )
  157.     print(13,   "0x12E",                    p:mem(0x12E, FIELD_BOOL)        )
  158.     print(14,   "0x06",                     p:mem(0x06,FIELD_BOOL)          )
  159.     print(15,   "Twirl is Loaded",          (package.loaded["twirl"] ~= nil )       )
  160.    
  161.     if diveCooldown[k] < dive.diveCooldown then
  162.         print(7,    "Cooldown",                 diveCooldown[k]     ,Color(1, 0.4, 0.4))
  163.     else
  164.         print(7,    "Cooldown",                 diveCooldown[k]     ,Color.green)
  165.     end
  166. end
  167. end
  168.  
  169.  
  170. --Déjà Vu!
  171.  
  172. return dive
Advertisement
Add Comment
Please, Sign In to add comment