Update Animation.lua

This commit is contained in:
Robert Jelic
2022-07-06 19:11:41 +02:00
committed by GitHub
parent 36b2f08ad6
commit 0f77621548

View File

@@ -6,22 +6,30 @@ return function(name)
local animations = {}
local index = 1
local infinitePlay = false
local nextWaitTimer = 0
local lastFunc
local _OBJ
local function onPlay()
if (animations[index] ~= nil) then
animations[index].f(object, index)
end
index = index + 1
if (animations[index] ~= nil) then
if (animations[index].t > 0) then
timerObj = os.startTimer(animations[index].t)
if(animations[index]==nil)then
if(infinitePlay)then
index = 1
else
onPlay()
return
end
end
if (animations[index].t > 0) then
timerObj = os.startTimer(animations[index].t)
else
onPlay()
end
end
object = {
@@ -44,13 +52,142 @@ return function(name)
return self
end;
setObject = function(self, obj)
_OBJ = obj
return self
end;
move = function(self, x, y, time, frames, obj)
if(obj~=nil)then
_OBJ = obj
end
if(_OBJ.setPosition==nil)or(_OBJ.getPosition==nil)then return self end
local oX,oY = _OBJ:getPosition()
if(oX==x)and(oY==y)then return self end
local xAdd = oX<=x and (x-oX)/frames or (oX-x)/frames
local yAdd = oY<=y and (y-oY)/frames or (oY-y)/frames
local xInverted,yInverted = oX>x and true or false, oY>y and true or false
for n=1, math.floor(frames) do
local f
if(n==frames)then
f = function()
_OBJ:setPosition(x, y)
end
else
f = function()
_OBJ:setPosition(math.floor(xInverted and oX+(-xAdd*n) or oX+xAdd*n), math.floor(yInverted and oY+(-yAdd*n) or oY+yAdd*n))
end
end
table.insert(animations, { f = f, t = time/frames})
end
return self
end;
moveLerp = function(self, x, y, time, obj)
end,
offset = function(self, x, y, time, frames, obj)
if(obj~=nil)then
_OBJ = obj
end
if(_OBJ.setOffset==nil)or(_OBJ.getOffset==nil)then return self end
local oX,oY = _OBJ:getOffset()
oX = math.abs(oX)
oY = math.abs(oY)
if(oX==x)and(oY==y)then return self end
local xAdd = oX<=x and (x-oX)/frames or (oX-x)/frames
local yAdd = oY<=y and (y-oY)/frames or (oY-y)/frames
local xInverted,yInverted = oX>x and true or false, oY>y and true or false
for n=1, math.floor(frames) do
local f
if(n==frames)then
f = function()
_OBJ:setOffset(x, y)
end
else
f = function()
_OBJ:setOffset(math.floor(xInverted and oX+(-xAdd*n) or oX+xAdd*n), math.floor(yInverted and oY+(-yAdd*n) or oY+yAdd*n))
end
end
table.insert(animations, { f = f, t = time/frames})
end
return self
end;
textColoring = function(self, time, ...)
local colors = table.pack(...)
for n=1, #colors do
table.insert(animations, { f = function()
_OBJ:setForeground(colors[n])
end, t = time/#colors})
end
return self
end;
backgroundColoring = function(self, time, ...)
local colors = table.pack(...)
for n=1, #colors do
table.insert(animations, { f = function()
_OBJ:setBackground(colors[n])
end, t = time/#colors})
end
return self
end;
setText = function(self, time, text)
if(_OBJ.setText~=nil)then
for n=1, text:len() do
table.insert(animations, { f = function()
_OBJ:setText(text:sub(1,n))
end, t = time/text:len()})
end
end
return self
end;
changeText = function(self, time, ...)
if(_OBJ.setText~=nil)then
local text = table.pack(...)
for n=1, #text do
table.insert(animations, { f = function()
_OBJ:setText(text[n])
end, t = time/#text})
end
end
return self
end;
coloring = function(self, time, ...)
local colors = table.pack(...)
for n=1, #colors do
if(type(colors[n]=="table"))then
table.insert(animations, { f = function()
if(colors[n][1]~=nil)then
_OBJ:setBackground(colors[n][1])
end
if(colors[n][2]~=nil)then
_OBJ:setForeground(colors[n][2])
end
end, t = time/#colors})
end
end
return self
end;
wait = function(self, wait)
nextWaitTimer = wait
return self
end;
rep = function(self, reps)
for x = 1, reps do
for n = 1, reps do
table.insert(animations, { f = lastFunc, t = nextWaitTimer })
end
return self
@@ -61,10 +198,12 @@ return function(name)
lastFunc = nil
nextWaitTimer = 0
index = 1
infinitePlay = false
return self
end;
play = function(self)
play = function(self, infinite)
infinitePlay = infinite and true or false
index = 1
if (animations[index] ~= nil) then
if (animations[index].t > 0) then
@@ -78,6 +217,7 @@ return function(name)
cancel = function(self)
os.cancelTimer(timerObj)
infinitePlay = false
return self
end;
@@ -92,4 +232,4 @@ return function(name)
object.__index = object
return object
end
end