Advertisement
Guest User

Goo Animations

a guest
Jul 22nd, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1. --Made By Maticzpl
  2.  
  3. --is it CGI?
  4.  
  5. --"yeah idk it just reverses stacks
  6. -- I think it's fine" - LBPhacker
  7.  
  8. GooAnim = {
  9.     started = false,
  10.     currentLife = 1,
  11.     animSize = {['X'] = 0,['Y'] = 0},
  12.     frameInterval = 5,
  13.     offset = {['X'] = 5,['Y'] = 5}
  14. }
  15.  
  16.  
  17. function GooAnim.createAnimation(sizex,sizey)
  18.     if GooAnim.started then
  19.         local res = tpt.confirm("Start new animation?", "Do you want to abandom the current animation and start a new one?","Yes")
  20.         if not res then return end
  21.     end
  22.  
  23.     tpt.set_pause(1)
  24.     tpt.decorations_enable(1)
  25.  
  26.     GooAnim.currentLife = GooAnim.frameInterval
  27.     GooAnim.animSize.X = sizex
  28.     GooAnim.animSize.Y = sizey  
  29.     GooAnim.started = true
  30.    
  31.     sim.createWallBox(GooAnim.offset.X, GooAnim.offset.Y, GooAnim.animSize.X + GooAnim.offset.X, GooAnim.animSize.Y + GooAnim.offset.Y, 12)
  32.  
  33.    
  34.     GooAnim.nextFrame(true)
  35.    
  36. end
  37.  
  38. function GooAnim.nextFrame(clearArea,clone)
  39.     clearArea = clearArea or false
  40.     clone = clone or false
  41.  
  42.     sim.takeSnapshot()
  43.  
  44.     if not GooAnim.started then
  45.         tpt.throw_error("Before creating the next frame\nBegin the animation with T")
  46.         return
  47.     end
  48.  
  49.     for x=1,GooAnim.animSize.X,1 do        
  50.         for y=1,GooAnim.animSize.Y,1 do
  51.             local offX = x + GooAnim.offset.X
  52.             local offY = y + GooAnim.offset.Y
  53.            
  54.             local oldColor = 0
  55.  
  56.             if sim.partID(offX, offY) == nil then
  57.                 clone = false
  58.             else
  59.                 oldColor = sim.partProperty(sim.partID(offX, offY), "dcolour")
  60.             end
  61.  
  62.  
  63.             if clearArea then
  64.                 sim.partKill(offX, offY)
  65.             end
  66.             --create goo without checking for collisions (stacked)            
  67.             part = sim.partCreate(-3, offX, offY, 12)
  68.            
  69.             if clone then
  70.                 sim.partProperty(part, "dcolour", oldColor)
  71.             end
  72.  
  73.             --set life to current frame you are working on
  74.             sim.partProperty(part, "life", GooAnim.currentLife)
  75.  
  76.         end
  77.     end
  78.  
  79.  
  80.     GooAnim.currentLife = GooAnim.currentLife + GooAnim.frameInterval
  81.  
  82.     print("Frame",math.floor(GooAnim.currentLife / GooAnim.frameInterval) - 1)
  83. end
  84.  
  85. local function optimizeFrames(stack,i)
  86.     local j = 1;
  87.     while true do        
  88.  
  89.         if stack[i+j] ~= nil then
  90.             if stack[i].deco == stack[i+j].deco then
  91.  
  92.                 stack[i].life = stack[i].life + GooAnim.frameInterval;
  93.  
  94.                 stack[i+j] = nil --remove while leaving the gap
  95.             else
  96.                 return
  97.             end
  98.         else
  99.             return
  100.         end
  101.  
  102.         j = j + 1
  103.     end
  104. end
  105.  
  106.  
  107. local function finalizeArea()        
  108.     local area = {} --initialize 3d array
  109.     for xsize=1,GooAnim.animSize.X,1 do
  110.         local yaxis = {}
  111.         for ysize=1,GooAnim.animSize.Y,1 do
  112.             yaxis[ysize + GooAnim.offset.Y ] = {}          
  113.         end
  114.         area[xsize + GooAnim.offset.X ] = yaxis
  115.     end
  116.  
  117.     --save and delete old parts
  118.     for part in sim.parts() do                
  119.         local x = sim.partProperty(part, "x")
  120.         local y = sim.partProperty(part, "y")
  121.  
  122.         --check for valid coordinates
  123.         if x > tonumber(GooAnim.animSize.X + GooAnim.offset.X) or y > tonumber(GooAnim.animSize.Y + GooAnim.offset.Y) then
  124.             sim.partKill(part)        
  125.         else
  126.             local dcolour = sim.partProperty(part, "dcolour")
  127.             local life = sim.partProperty(part, "life")
  128.    
  129.    
  130.             local stackEntry = {}
  131.             stackEntry.life = life
  132.             stackEntry.deco = dcolour
  133.            
  134.             local stackTable = area[x][y]
  135.             stackTable[math.floor(life / GooAnim.frameInterval)] = stackEntry
  136.             sim.partKill(part)
  137.         end        
  138.     end
  139.  
  140.     --restore old parts in correct order
  141.     for x=1,GooAnim.animSize.X,1 do        
  142.         for y=1,GooAnim.animSize.Y,1 do
  143.             local offX = x + GooAnim.offset.X
  144.             local offY = y + GooAnim.offset.Y
  145.  
  146.             local stack = area[offX][offY]
  147.  
  148.             for i=1,#stack,1 do    
  149.  
  150.                 if stack[i] ~= nil then
  151.                    
  152.                     --slight part count optimization (dont keep particles that dont change)
  153.                     optimizeFrames(stack,i)
  154.                
  155.                     goo = sim.partCreate(-3, offX, offY, 12)
  156.  
  157.                     sim.partProperty(goo, "dcolour",    stack[i].deco)
  158.                     sim.partProperty(goo, "life",       stack[i].life)
  159.                 end
  160.                
  161.             end
  162.            
  163.         end
  164.     end
  165. end
  166.  
  167. function GooAnim.finishAnimation()
  168.     local confirm = tpt.confirm("Finish animation?", "Do you want to finish the animation? You wont be able to add new frames.","Yes")
  169.     if not confirm then
  170.         return
  171.     end
  172.  
  173.     finalizeArea()
  174.  
  175.     GooAnim.started = false
  176.    
  177. end
  178.  
  179.  
  180.  
  181. --Keyboard controlls
  182.  
  183. function GooAnim.bindKeys()
  184.     event.register(event.keypress, function(key,scan,rpt,shift,ctrl,alt)
  185.         if rpt or shift or ctrl or alt then return end
  186.        
  187.  
  188.  
  189.         if key == 44 then -- < to next frame
  190.             GooAnim.nextFrame()
  191.             return
  192.         end
  193.  
  194.         if key == 46 then -- > to clone frame
  195.             GooAnim.nextFrame(false,true)
  196.             return
  197.         end
  198.  
  199.         if key == 116 then --T to Start / Stop animation
  200.             if GooAnim.started then
  201.                 GooAnim.finishAnimation()
  202.             else
  203.                 local x = tpt.input("Enter Width","Enter the target resolution for your animation",25)
  204.                 local y = tpt.input("Enter Height","Enter the target resolution for your animation",x)
  205.                 GooAnim.frameInterval = tpt.input("Enter Frame Interval","Enter the delay (in game frames) between an animation frame changes",5)
  206.                
  207.                 if x == "" or y == "" then return end
  208.  
  209.                 if tonumber(x) * tonumber(y) >= 40000 then
  210.                     tpt.message_box("Warning","Big resolution animations may exceed the particle limit")
  211.                 end
  212.  
  213.                 GooAnim.createAnimation(x,y)
  214.             end
  215.             return
  216.         end
  217.  
  218.        
  219.        
  220.     end)    
  221. end
  222.  
  223. GooAnim.bindKeys();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement