Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Made By Maticzpl
- --is it CGI?
- --"yeah idk it just reverses stacks
- -- I think it's fine" - LBPhacker
- GooAnim = {
- started = false,
- currentLife = 1,
- animSize = {['X'] = 0,['Y'] = 0},
- frameInterval = 5,
- offset = {['X'] = 5,['Y'] = 5}
- }
- function GooAnim.createAnimation(sizex,sizey)
- if GooAnim.started then
- local res = tpt.confirm("Start new animation?", "Do you want to abandom the current animation and start a new one?","Yes")
- if not res then return end
- end
- tpt.set_pause(1)
- tpt.decorations_enable(1)
- GooAnim.currentLife = GooAnim.frameInterval
- GooAnim.animSize.X = sizex
- GooAnim.animSize.Y = sizey
- GooAnim.started = true
- sim.createWallBox(GooAnim.offset.X, GooAnim.offset.Y, GooAnim.animSize.X + GooAnim.offset.X, GooAnim.animSize.Y + GooAnim.offset.Y, 12)
- GooAnim.nextFrame(true)
- end
- function GooAnim.nextFrame(clearArea,clone)
- clearArea = clearArea or false
- clone = clone or false
- sim.takeSnapshot()
- if not GooAnim.started then
- tpt.throw_error("Before creating the next frame\nBegin the animation with T")
- return
- end
- for x=1,GooAnim.animSize.X,1 do
- for y=1,GooAnim.animSize.Y,1 do
- local offX = x + GooAnim.offset.X
- local offY = y + GooAnim.offset.Y
- local oldColor = 0
- if sim.partID(offX, offY) == nil then
- clone = false
- else
- oldColor = sim.partProperty(sim.partID(offX, offY), "dcolour")
- end
- if clearArea then
- sim.partKill(offX, offY)
- end
- --create goo without checking for collisions (stacked)
- part = sim.partCreate(-3, offX, offY, 12)
- if clone then
- sim.partProperty(part, "dcolour", oldColor)
- end
- --set life to current frame you are working on
- sim.partProperty(part, "life", GooAnim.currentLife)
- end
- end
- GooAnim.currentLife = GooAnim.currentLife + GooAnim.frameInterval
- print("Frame",math.floor(GooAnim.currentLife / GooAnim.frameInterval) - 1)
- end
- local function optimizeFrames(stack,i)
- local j = 1;
- while true do
- if stack[i+j] ~= nil then
- if stack[i].deco == stack[i+j].deco then
- stack[i].life = stack[i].life + GooAnim.frameInterval;
- stack[i+j] = nil --remove while leaving the gap
- else
- return
- end
- else
- return
- end
- j = j + 1
- end
- end
- local function finalizeArea()
- local area = {} --initialize 3d array
- for xsize=1,GooAnim.animSize.X,1 do
- local yaxis = {}
- for ysize=1,GooAnim.animSize.Y,1 do
- yaxis[ysize + GooAnim.offset.Y ] = {}
- end
- area[xsize + GooAnim.offset.X ] = yaxis
- end
- --save and delete old parts
- for part in sim.parts() do
- local x = sim.partProperty(part, "x")
- local y = sim.partProperty(part, "y")
- --check for valid coordinates
- if x > tonumber(GooAnim.animSize.X + GooAnim.offset.X) or y > tonumber(GooAnim.animSize.Y + GooAnim.offset.Y) then
- sim.partKill(part)
- else
- local dcolour = sim.partProperty(part, "dcolour")
- local life = sim.partProperty(part, "life")
- local stackEntry = {}
- stackEntry.life = life
- stackEntry.deco = dcolour
- local stackTable = area[x][y]
- stackTable[math.floor(life / GooAnim.frameInterval)] = stackEntry
- sim.partKill(part)
- end
- end
- --restore old parts in correct order
- for x=1,GooAnim.animSize.X,1 do
- for y=1,GooAnim.animSize.Y,1 do
- local offX = x + GooAnim.offset.X
- local offY = y + GooAnim.offset.Y
- local stack = area[offX][offY]
- for i=1,#stack,1 do
- if stack[i] ~= nil then
- --slight part count optimization (dont keep particles that dont change)
- optimizeFrames(stack,i)
- goo = sim.partCreate(-3, offX, offY, 12)
- sim.partProperty(goo, "dcolour", stack[i].deco)
- sim.partProperty(goo, "life", stack[i].life)
- end
- end
- end
- end
- end
- function GooAnim.finishAnimation()
- local confirm = tpt.confirm("Finish animation?", "Do you want to finish the animation? You wont be able to add new frames.","Yes")
- if not confirm then
- return
- end
- finalizeArea()
- GooAnim.started = false
- end
- --Keyboard controlls
- function GooAnim.bindKeys()
- event.register(event.keypress, function(key,scan,rpt,shift,ctrl,alt)
- if rpt or shift or ctrl or alt then return end
- if key == 44 then -- < to next frame
- GooAnim.nextFrame()
- return
- end
- if key == 46 then -- > to clone frame
- GooAnim.nextFrame(false,true)
- return
- end
- if key == 116 then --T to Start / Stop animation
- if GooAnim.started then
- GooAnim.finishAnimation()
- else
- local x = tpt.input("Enter Width","Enter the target resolution for your animation",25)
- local y = tpt.input("Enter Height","Enter the target resolution for your animation",x)
- GooAnim.frameInterval = tpt.input("Enter Frame Interval","Enter the delay (in game frames) between an animation frame changes",5)
- if x == "" or y == "" then return end
- if tonumber(x) * tonumber(y) >= 40000 then
- tpt.message_box("Warning","Big resolution animations may exceed the particle limit")
- end
- GooAnim.createAnimation(x,y)
- end
- return
- end
- end)
- end
- GooAnim.bindKeys();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement