Advertisement
MegoZ_

Twirl / NSMBW Mid-Air Spin

Jun 6th, 2022 (edited)
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 KB | None | 0 0
  1. local twirl = {}
  2. --v2.3
  3.  
  4. local textplus = require("textplus")
  5. local playeranim = require("playerAnim")
  6.  
  7. function twirl.onInitAPI()
  8.     registerEvent(twirl, "onTick", "onTick")
  9.     registerEvent(twirl, "onDraw", "onDraw")
  10. end
  11.  
  12. twirl.showMeDebug = false
  13. local twirlSfx = Misc.resolveFile("twirl.ogg")
  14.  
  15. local isTwirling = {false,false}
  16. local timeline = {0,0}
  17. local cooldown = {0,0}
  18. local unmounting = {false,false}
  19.  
  20. --The Twirl™
  21. twirl.descent =         1.5
  22. twirl.impulsePercent =  1.5
  23. twirl.extraImpulse =    1.3
  24. twirl.cooldown =        10
  25.  
  26. --Animation
  27. twirl.frames = {15, -2,-2, 13, 2,2}
  28. twirl.animSpeed = 2
  29. local twirlAnim = playeranim.Anim(twirl.frames, twirl.animSpeed)
  30.  
  31. --For compatibility with my other script ("dive.lua")
  32. twirl.enableDownKey = false
  33. function twirl.cancelTwirl(key,value)
  34.     isTwirling[key] = false
  35.     cooldown[key] = -1
  36.     timeline[key] = 0
  37.     twirlAnim:stop(value)
  38. end
  39.  
  40. function twirl.onTick() for k, p in ipairs(Player.get()) do
  41.  
  42.     local function resetTwirling()
  43.         isTwirling[k] = false
  44.         timeline[k] = 0
  45.         cooldown[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.         p.mount ~= 0 or
  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 canTwirl() return (
  63.             not isTwirling[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 unmounting[k] and
  69.             not p:mem(0x44, FIELD_BOOL) and -- Riding a rainbow shell
  70.             not p:mem(0x12E, FIELD_BOOL) and -- Ducking
  71.             not p:mem(0x13C, FIELD_BOOL) and
  72.             not p.holdingNPC and
  73.             not p.isMega and
  74.             p.deathTimer == 0 and
  75.             Level.winState() == 0 and
  76.             not (p.speedY > 0 and (p.powerup == 4 or p.powerup == 5)) and
  77.             p.forcedState == 0 and
  78.             (p.character == 1 or p.character == 2 or p.character == 4 or p.character == 7 or p.character == 15)
  79.     ) end
  80.  
  81.     if p.keys.altJump and cooldown[k] > twirl.cooldown then
  82.         isTwirling[k] = true
  83.     elseif canTwirl() then
  84.         isTwirling[k] = false
  85.         cooldown[k] = cooldown[k] + 1
  86.     else
  87.         cooldown[k] = 0
  88.     end
  89.  
  90.     if isTwirling[k] then
  91.         timeline[k] = timeline[k] + 1
  92.  
  93.         p.UnknownCTRLLock1 = 1 -- disables ducking without setting p.keys.down false... apparently.
  94.     else
  95.         p.UnknownCTRLLock1 = 0 -- re-enabling ducking. I might change this if this causes jank. for now it's very useful to me
  96.     end
  97.  
  98.     if timeline[k] == 1 then
  99.         twirlAnim:play(p)
  100.         SFX.play(twirlSfx)
  101.         if p.speedY > 2 then
  102.             p.speedY = twirl.descent
  103.         elseif p.speedY < -2 then
  104.             p.speedY = p.speedY - (p.speedY % twirl.impulsePercent)-twirl.extraImpulse
  105.         else
  106.             p.speedY = -twirl.extraImpulse*2
  107.         end
  108.     end
  109.  
  110.     --Stop Animation
  111.     if timeline[k] >= twirl.animSpeed*#twirl.frames or timeline[k] == 0 or isOnMount() then
  112.         twirlAnim:stop(p)
  113.     end
  114.  
  115.     --Unmounting
  116.     if isOnMount() then
  117.         unmounting[k] = true
  118.     end
  119.     if unmounting[k] and p.keys.altJump then
  120.         unmounting[k] = true
  121.     elseif not p.keys.altJump then
  122.         unmounting[k] = false
  123.     end
  124.  
  125.  
  126.     if timeline[k] > 20 then
  127.         resetTwirling()
  128.     end
  129.  
  130.  
  131. --Debug: Prints Variables on Screen — Set ``twirl.showMeDebug`` to true in order to activate. (Works with 2 players)
  132.     local function print(line, text, variable,color)
  133.         if twirl.showMeDebug == false then return end
  134.         debugFont = textplus.loadFont("scripts/textplus/font/11.ini")
  135.    
  136.         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}
  137.     end
  138.     print(1,    "Player's Y Velocity",      p.speedY                        )
  139.     print(4,    "Is Twirling",              isTwirling[k]                   )
  140.     print(6,    "Can Twirl?",               canTwirl()                      )
  141.     print(8,    "Timeline of Twirl",        timeline[k]                     )
  142.     print(9,    "Is Mounting Something",    isOnMount()                     )
  143.     print(13,   "0x12E",                    p:mem(0x12E, FIELD_BOOL)        )
  144.    
  145.     if cooldown[k] < twirl.cooldown then
  146.         print(7,    "Cooldown",                 cooldown[k]     ,Color(1, 0.4, 0.4))
  147.     else
  148.         print(7,    "Cooldown",                 cooldown[k]     ,Color.green)
  149.     end
  150.     if unmounting[k] then
  151.         print(10,   "Jumping off Mounting",     unmounting[k]   ,Color.lightblue)
  152.         print(7,    "Cooldown",                 cooldown[k]     ,Color.lightblue)
  153.     else
  154.         print(10,   "Jumping off Mounting",     unmounting[k])
  155.     end
  156.  
  157.     if canTwirl() then
  158.         if p.speedY > 2 then
  159.             print(12,   "Down","Cat!")
  160.         elseif p.speedY < -2 then
  161.             print(12,   "Up","Dog!")
  162.         else
  163.             print(12,   "Mid-air","Platypus!")
  164.         end
  165.     end
  166. end
  167. end
  168.  
  169. --Good job me, I'm proud.
  170.  
  171. return twirl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement