LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

Tween

Description
Tween class which linearly interpolates a quantity from a start value to an end value over a given duration.
Functions

applyValue

Description
Apply a value via the setter function.
Definition
applyValue()
Code
92function Tween:applyValue(newValue)
93 if self.functionTarget ~= nil then
94 self.setter(self.functionTarget, newValue)
95 else
96 self.setter(newValue)
97 end
98end

getDuration

Description
Get this tween's duration in milliseconds.
Definition
getDuration()
Code
39function Tween:getDuration()
40 return self.duration
41end

getFinished

Description
Check if this tween has finished.
Definition
getFinished()
Code
45function Tween:getFinished()
46 return self.isFinished
47end

new

Description
Create a new Tween.
Definition
new(function setterFunction, float startValue, float endValue, float duration, table customMt)
Arguments
functionsetterFunctionValue setter function. Signature: callback(value) or callback(target, value).
floatstartValueOriginal value
floatendValueTarget value
floatdurationDuration of tween in milliseconds
tablecustomMtSubclass metatable for inheritance
Code
20function Tween.new(setterFunction, startValue, endValue, duration, customMt)
21 local self = setmetatable({}, customMt or Tween_mt)
22
23 self.setter = setterFunction
24 self.startValue = startValue
25 self.endValue = endValue
26 self.duration = duration
27 self.elapsedTime = 0
28
29 self.isFinished = duration == 0
30 self.functionTarget = nil
31
32 self.curveFunc = Tween.CURVE.LINEAR
33
34 return self
35end

reset

Description
Reset this tween to play it again.
Definition
reset()
Code
51function Tween:reset()
52 self.elapsedTime = 0
53 self.isFinished = self.duration == 0
54end

setCurve

Description
Set the curve function. Defaults to Tween.CURVE.LINEAR
Definition
setCurve()
Code
102function Tween:setCurve(func)
103 self.curveFunc = func or Tween.CURVE.LINEAR
104end

setTarget

Description
Set a callback target for this tween. If a target has been set, the setter function must support receiving the target as its first argument.
Definition
setTarget()
Code
59function Tween:setTarget(target)
60 self.functionTarget = target
61end

tweenValue

Description
Get the current tween value.
Definition
tweenValue()
Code
86function Tween:tweenValue(t)
87 return MathUtil.lerp(self.startValue, self.endValue, self.curveFunc(t))
88end

update

Description
Update the tween's state.
Definition
update()
Code
65function Tween:update(dt)
66 if self.isFinished then
67 return
68 end
69
70 self.elapsedTime = self.elapsedTime + dt
71
72 local newValue
73 if self.elapsedTime >= self.duration then
74 self.isFinished = true
75 newValue = self:tweenValue(1)
76 else
77 local t = self.elapsedTime / self.duration
78 newValue = self:tweenValue(t)
79 end
80
81 self:applyValue(newValue)
82end