stdlib.easable: rewrite
This commit is contained in:
parent
6a0cb734db
commit
1bc3604f9f
|
@ -1,72 +1,42 @@
|
||||||
require('stdlib.util')
|
|
||||||
|
|
||||||
---@class easable
|
---@class easable
|
||||||
---@field public a number @the eased value
|
---@field eased number | any
|
||||||
---@field public toa number @the target, uneased value
|
---@field target number | any
|
||||||
local eas = {}
|
---@field speed number
|
||||||
|
local easable = {}
|
||||||
|
|
||||||
---@param new number @New value to ease to
|
--- move towards a new target
|
||||||
---@return void
|
function easable:set(n)
|
||||||
function eas:set(new)
|
self.target = n
|
||||||
self.toa = new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param new number @New value
|
--- move towards a new target additively
|
||||||
---@return void
|
function easable:add(n)
|
||||||
function eas:reset(new)
|
self.target = self.target + n
|
||||||
self.toa = new
|
|
||||||
self.a = new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param new number @How much to add to current value to ease to
|
--- set both the eased value and the target
|
||||||
---@return void
|
function easable:reset(n)
|
||||||
function eas:add(new)
|
self.target = n
|
||||||
self.toa = self.toa + new
|
self.eased = n
|
||||||
end
|
end
|
||||||
|
|
||||||
local easmeta = {}
|
---@param dt number
|
||||||
|
function easable:update(dt)
|
||||||
easmeta.__index = eas
|
self.eased = math.pow(self.speed, -dt) * (self.eased - self.target) + self.target
|
||||||
easmeta.__name = 'easable'
|
|
||||||
|
|
||||||
function easmeta.__add(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) + ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__sub(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) - ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__mul(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) * ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__div(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) / ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__mod(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) % ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__eq(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) == ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__lt(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) < ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
|
||||||
function easmeta.__le(a, b)
|
|
||||||
return ((type(a) == 'table' and a.a) and a.a or a) <= ((type(b) == 'table' and b.a) and b.a or b)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function easmeta:__call(dt)
|
function easable:__tostring()
|
||||||
self.a = mix(self.a, self.toa, dt)
|
return 'easable (' .. self.eased .. ' towards ' .. self.target .. ')'
|
||||||
end
|
|
||||||
function easmeta:__tostring()
|
|
||||||
return tostring(self.a)
|
|
||||||
end
|
|
||||||
function easmeta:__unm(self)
|
|
||||||
return -self.a
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param default number
|
easable.__index = easable
|
||||||
|
easable.__name = 'easable'
|
||||||
|
|
||||||
---@return easable
|
---@return easable
|
||||||
return function(default)
|
return function(default, speed)
|
||||||
default = default or 0
|
return setmetatable({
|
||||||
return setmetatable({a = default, toa = default}, easmeta)
|
eased = default,
|
||||||
|
target = default,
|
||||||
|
speed = speed and math.pow(2, speed) or 2
|
||||||
|
}, easable)
|
||||||
end
|
end
|
Loading…
Reference in New Issue