LUADOC - Farming Simulator 19

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
96function Tween:applyValue(newValue)
97 if self.functionTarget ~= nil then
98 self.setter(self.functionTarget, newValue)
99 else
100 self.setter(newValue)
101 end
102end

getDuration

Description
Get this tween's duration in milliseconds.
Definition
getDuration()
Code
43function Tween:getDuration()
44 return self.duration
45end

getFinished

Description
Check if this tween has finished.
Definition
getFinished()
Code
49function Tween:getFinished()
50 return self.isFinished
51end

new

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

reset

Description
Reset this tween to play it again.
Definition
reset()
Code
55function Tween:reset()
56 self.elapsedTime = 0
57 self.isFinished = self.duration == 0
58end

setCurve

Description
Set the curve function. Defaults to Tween.CURVE.LINEAR
Definition
setCurve()
Code
106function Tween:setCurve(func)
107 self.curveFunc = func or Tween.CURVE.LINEAR
108end

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
63function Tween:setTarget(target)
64 self.functionTarget = target
65end

tweenValue

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

update

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