Document timer.do_for()

This commit is contained in:
Matthias Richter 2012-10-02 18:41:43 +02:00
parent 1cb943c70d
commit 1b470b08f2
2 changed files with 48 additions and 92 deletions

108
doc.lua
View File

@ -233,7 +233,7 @@ Module { name = "hump.timer",
{"function", "func", "The function to be delayed."}, {"function", "func", "The function to be delayed."},
}, },
returns = { returns = {
{"function", "The timer handle."} {"table", "The timer handle."}
}, },
example = { example = {
[===[ [===[
@ -261,7 +261,7 @@ Timer.add(1, function(func) print("foo") Timer.add(1, func) end)]===],
{"number", "count", "Number of times the function is to be called.", optional = true}, {"number", "count", "Number of times the function is to be called.", optional = true},
}, },
returns = { returns = {
{"function", "The timer handle."} {"table", "The timer handle."}
}, },
example = { example = {
"Timer.addPeriodic(1, function() lamp:toggleLight() end)", "Timer.addPeriodic(1, function() lamp:toggleLight() end)",
@ -274,6 +274,39 @@ end)]===],
}, },
}, },
Function { name = {"do_for", "instance:do_for"},
short = "Run a function for the next few seconds.",
long = [===[
Run a {#func(dt)} for the next {#delta} seconds. The function is called
every time {#update(dt)} is called. Optionally run {#after()} once
{#delta} seconds have passed.
{#after} will receive itself as only parameter.
The same constraints as with {#add()} apply.]===],
params = {
{"number", "delta", "Number of seconds the {#func} will be called."},
{"function", "func", "The function to be called upon {#update(dt)}."},
{"function", "after", "A function to be called after {#delta} seconds.", optional=true},
},
returns = {
{"table", "The timer handle."}
},
example = {
[===[Timer.do_for(3, function() screen:shake() end)]===],
[===[player.isInvincible = true
-- flash player for 3 seconds
local t = 0
player.timer:do_for(3, function(dt)
t = t + dt
player.visible = (t % .2) < .1
end, function()
player.visible = true -- make sure the player is visible after three seconds
player.isInvincible = false
end)]===]
},
},
Function { name = {"cancel", "instance:cancel"}, Function { name = {"cancel", "instance:cancel"},
short = "Cancel a scheduled function.", short = "Cancel a scheduled function.",
long = [===[ long = [===[
@ -284,7 +317,7 @@ end)]===],
{*Never} use this inside a scheduled function.]===], {*Never} use this inside a scheduled function.]===],
params = { params = {
{"function", "func", "The function to be canceled."}, {"table", "handle", "The function to be canceled."},
}, },
returns = {}, returns = {},
example = { example = {
@ -320,75 +353,6 @@ Timer.cancel(handle) -- NOT: Timer.cancel(tick)]===]
function love.update(dt) function love.update(dt)
do_stuff() do_stuff()
Timer.update(dt) Timer.update(dt)
end]===],
},
Function { name = "Interpolator",
short = "Create a new interpolating function.",
long = [===[
Create a wrapper for an interpolating function, i.e. a function that
acts depending on how much time has passed.
The wrapper will have the prototype:
[%function wrapper(dt, ...)]
where {#dt} is the time that has passed since the last call of the
wrapper and {#...} are arguments passed to the interpolating function.
It will return whatever the interpolating functions returns if the
interpolation is not yet finished or nil if the interpolation is done.
The prototype of the interpolating function is:
[%function interpolator(fraction, ...)]
where {#fraction} is a number between 0 and 1 depending on how much
time has passed and {#...} are additional arguments supplied to the
wrapper.]===],
params = {
{"number", "length", "Interpolation length in seconds."},
{"function", "func", "Interpolating function."},
},
returns = {
{"function", "The wrapper function."}
},
example = [===[
fader = Timer.Interpolator(5, function(frac, r,g,b)
love.graphics.setBackgroundColor(frac*r,frac*g,frac*b)
end)
function love.update(dt)
fader(dt, 255,255,255)
end]===],
},
Function { name = "Oscillator",
short = "Create a new oscillating function.",
long = [===[
Create a wrapper for an oscillating function, which is basically a
looping interpolating function.
The function prototypes are the same as with {#Interpolator()}:
[%function wrapper(dt, ...)]
[%function oscillator(fraction, ...)]
As with {#Interpolator}, the wrapper will return whatever
{#oscillator()} returns.]===],
params = {
{"number", "length", "Length of one interpolation period."},
{"function", "func", "Oscillating function."},
},
returns = {
{"function", "The wrapper function."}
},
example = [===[
mover = Timer.Oscillator(10, function(frac)
return 400 + 300 * math.sin(2*math.pi*frac)
end)
local xpos = 100
function love.update(dt)
xpos = mover(dt)
end
function love.draw()
love.graphics.circle('fill', xpos, 300, 80, 36)
end]===], end]===],
}, },
} }

View File

@ -56,14 +56,23 @@ end</code></pre></p><p><span class="warning">Note:</span> Only works when called
end</code></pre><pre><code class="lua">function love.load() end</code></pre><pre><code class="lua">function love.load()
Gamestate.registerEvents{'draw', 'update', 'quit'} Gamestate.registerEvents{'draw', 'update', 'quit'}
Gamestate.switch(menu) Gamestate.switch(menu)
end</code></pre></div></div></div><a name="Timer" id="Timer"></a><div class="outer-block"><h3>hump.timer<a class="top" href="#top">^ top</a></h3><div class="preamble"><pre><code class="lua">Timer = require "hump.timer"</code></pre><p>hump.timer provides a simple interface to use delayed functions, i.e. functions that will be executed after some amount time has passed. For example, you can use a timer to set the player invincible for a short amount of time.</p><p>In addition, the module offers facilities to create functions that interpolate or oscillate over time. An interpolator could fade the color or a text message, whereas an oscillator could be used for the movement of foes in a shmup.</p></div><div class="overview"><h4>Module overview</h4><dl><dt><a href="#Timer-new">new()</a></dt><dd>Create new timer instance.</dd><dt><a href="#Timer-add">add()</a></dt><dd>Schedule a function.</dd><dt><a href="#Timer-addPeriodic">addPeriodic()</a></dt><dd>Add a periodic function.</dd><dt><a href="#Timer-cancel">cancel()</a></dt><dd>Cancel a scheduled function.</dd><dt><a href="#Timer-clear">clear()</a></dt><dd>Remove all timed and periodic functions.</dd><dt><a href="#Timer-update">update()</a></dt><dd>Update scheduled functions.</dd><dt><a href="#Timer-Interpolator">Interpolator()</a></dt><dd>Create a new interpolating function.</dd><dt><a href="#Timer-Oscillator">Oscillator()</a></dt><dd>Create a new oscillating function.</dd></dl></div><a name="Timer-new" id="Timer-new"></a><div class="ref-block"><h4>function <span class="name">new</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><p><span class="warning">If you don't need multiple independent schedulers, you can use the global/default timer (see examples).</span></p><p>Creates a new timer instance that is independent of the global timer: It will manage it's own list of scheduled functions and does not in any way affect the the global timer. Likewise, the global timer does not affect the timer instance.</p><p><span class="warning">Note:</span> Timer instances use the colon-notation (e.g. <code class="lua">instance:update(dt)</code>), while the global timer uses the dot-notation (e.g. <code class="lua">Timer.update(dt)</code>).</p><div class="arguments">Parameters:<dl><dt>None</dt></dl></div><div class="returns">Returns:<dl><dt>Timer</dt><dd>A timer instance</dd></dl></div><div class="example">Example:<pre><code class="lua">menuTimer = Timer.new()</code></pre></div></div><a name="Timer-add" id="Timer-add"></a><a name="Timer-instance:add" id="Timer-instance:add"></a><div class="ref-block"><h4>function <span class="name">add</span><span class="arglist">(delay, func)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:add</span><span class="arglist">(delay, func)</span><a class="top" href="#Timer">^ top</a></h4><p>Schedule a function. The function will be executed after <code class="lua">delay</code> seconds have elapsed, given that <code class="lua">update(dt)</code> is called every frame.</p><p>Note that there is no guarantee that the delay will not be exceeded, it is only guaranteed that the function will not be executed <em>before</em> the delay has passed.</p><p>It is an error to schedule a function again if it is not yet finished or canceled.</p><p><code class="lua">func</code> will receive itself as only parameter. This is useful to implement periodic behavior (see the example).</p><div class="arguments">Parameters:<dl><dt>number <code>delay</code></dt><dd>Number of seconds the function will be delayed.</dd><dt>function <code>func</code></dt><dd>The function to be delayed.</dd></dl></div><div class="returns">Returns:<dl><dt>function</dt><dd>The timer handle.</dd></dl></div><div class="example">Example:<pre><code class="lua">-- grant the player 5 seconds of immortality end</code></pre></div></div></div><a name="Timer" id="Timer"></a><div class="outer-block"><h3>hump.timer<a class="top" href="#top">^ top</a></h3><div class="preamble"><pre><code class="lua">Timer = require "hump.timer"</code></pre><p>hump.timer provides a simple interface to use delayed functions, i.e. functions that will be executed after some amount time has passed. For example, you can use a timer to set the player invincible for a short amount of time.</p><p>In addition, the module offers facilities to create functions that interpolate or oscillate over time. An interpolator could fade the color or a text message, whereas an oscillator could be used for the movement of foes in a shmup.</p></div><div class="overview"><h4>Module overview</h4><dl><dt><a href="#Timer-new">new()</a></dt><dd>Create new timer instance.</dd><dt><a href="#Timer-add">add()</a></dt><dd>Schedule a function.</dd><dt><a href="#Timer-addPeriodic">addPeriodic()</a></dt><dd>Add a periodic function.</dd><dt><a href="#Timer-do_for">do_for()</a></dt><dd>Run a function for the next few seconds.</dd><dt><a href="#Timer-cancel">cancel()</a></dt><dd>Cancel a scheduled function.</dd><dt><a href="#Timer-clear">clear()</a></dt><dd>Remove all timed and periodic functions.</dd><dt><a href="#Timer-update">update()</a></dt><dd>Update scheduled functions.</dd></dl></div><a name="Timer-new" id="Timer-new"></a><div class="ref-block"><h4>function <span class="name">new</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><p><span class="warning">If you don't need multiple independent schedulers, you can use the global/default timer (see examples).</span></p><p>Creates a new timer instance that is independent of the global timer: It will manage it's own list of scheduled functions and does not in any way affect the the global timer. Likewise, the global timer does not affect the timer instance.</p><p><span class="warning">Note:</span> Timer instances use the colon-notation (e.g. <code class="lua">instance:update(dt)</code>), while the global timer uses the dot-notation (e.g. <code class="lua">Timer.update(dt)</code>).</p><div class="arguments">Parameters:<dl><dt>None</dt></dl></div><div class="returns">Returns:<dl><dt>Timer</dt><dd>A timer instance</dd></dl></div><div class="example">Example:<pre><code class="lua">menuTimer = Timer.new()</code></pre></div></div><a name="Timer-add" id="Timer-add"></a><a name="Timer-instance:add" id="Timer-instance:add"></a><div class="ref-block"><h4>function <span class="name">add</span><span class="arglist">(delay, func)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:add</span><span class="arglist">(delay, func)</span><a class="top" href="#Timer">^ top</a></h4><p>Schedule a function. The function will be executed after <code class="lua">delay</code> seconds have elapsed, given that <code class="lua">update(dt)</code> is called every frame.</p><p>Note that there is no guarantee that the delay will not be exceeded, it is only guaranteed that the function will not be executed <em>before</em> the delay has passed.</p><p>It is an error to schedule a function again if it is not yet finished or canceled.</p><p><code class="lua">func</code> will receive itself as only parameter. This is useful to implement periodic behavior (see the example).</p><div class="arguments">Parameters:<dl><dt>number <code>delay</code></dt><dd>Number of seconds the function will be delayed.</dd><dt>function <code>func</code></dt><dd>The function to be delayed.</dd></dl></div><div class="returns">Returns:<dl><dt>table</dt><dd>The timer handle.</dd></dl></div><div class="example">Example:<pre><code class="lua">-- grant the player 5 seconds of immortality
player.isInvincible = true player.isInvincible = true
Timer.add(5, function() player.isInvincible = false end)</code></pre><pre><code class="lua">-- print "foo" every second. See addPeriodic. Timer.add(5, function() player.isInvincible = false end)</code></pre><pre><code class="lua">-- print "foo" every second. See addPeriodic.
Timer.add(1, function(func) print("foo") Timer.add(1, func) end)</code></pre><pre><code class="lua">menuTimer:add(1, finishAnimation)</code></pre></div></div><a name="Timer-addPeriodic" id="Timer-addPeriodic"></a><a name="Timer-instance:addPeriodic" id="Timer-instance:addPeriodic"></a><div class="ref-block"><h4>function <span class="name">addPeriodic</span><span class="arglist">(delay, func, count)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:addPeriodic</span><span class="arglist">(delay, func, count)</span><a class="top" href="#Timer">^ top</a></h4><p>Add a function that will be called <code class="lua">count</code> times every <code class="lua">delay</code> seconds.</p><p>If <code class="lua">count</code> is omitted, the function will be called until it returns <code class="lua">false</code> or <code class="lua">clear()</code> is called.</p><div class="arguments">Parameters:<dl><dt>number <code>delay</code></dt><dd>Number of seconds between two consecutive function calls.</dd><dt>function <code>func</code></dt><dd>The function to be called periodically.</dd><dt>number <code>count</code> (optional)</dt><dd>Number of times the function is to be called.</dd></dl></div><div class="returns">Returns:<dl><dt>function</dt><dd>The timer handle.</dd></dl></div><div class="example">Example:<pre><code class="lua">Timer.addPeriodic(1, function() lamp:toggleLight() end)</code></pre><pre><code class="lua">mothership_timer:addPeriodic(0.3, function() self:spawnFighter() end, 5)</code></pre><pre><code class="lua">-- flicker player's image as long as he is invincible Timer.add(1, function(func) print("foo") Timer.add(1, func) end)</code></pre><pre><code class="lua">menuTimer:add(1, finishAnimation)</code></pre></div></div><a name="Timer-addPeriodic" id="Timer-addPeriodic"></a><a name="Timer-instance:addPeriodic" id="Timer-instance:addPeriodic"></a><div class="ref-block"><h4>function <span class="name">addPeriodic</span><span class="arglist">(delay, func, count)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:addPeriodic</span><span class="arglist">(delay, func, count)</span><a class="top" href="#Timer">^ top</a></h4><p>Add a function that will be called <code class="lua">count</code> times every <code class="lua">delay</code> seconds.</p><p>If <code class="lua">count</code> is omitted, the function will be called until it returns <code class="lua">false</code> or <code class="lua">clear()</code> is called.</p><div class="arguments">Parameters:<dl><dt>number <code>delay</code></dt><dd>Number of seconds between two consecutive function calls.</dd><dt>function <code>func</code></dt><dd>The function to be called periodically.</dd><dt>number <code>count</code> (optional)</dt><dd>Number of times the function is to be called.</dd></dl></div><div class="returns">Returns:<dl><dt>table</dt><dd>The timer handle.</dd></dl></div><div class="example">Example:<pre><code class="lua">Timer.addPeriodic(1, function() lamp:toggleLight() end)</code></pre><pre><code class="lua">mothership_timer:addPeriodic(0.3, function() self:spawnFighter() end, 5)</code></pre><pre><code class="lua">-- flicker player's image as long as he is invincible
Timer.addPeriodic(0.1, function() Timer.addPeriodic(0.1, function()
player:flipImage() player:flipImage()
return player.isInvincible return player.isInvincible
end)</code></pre></div></div><a name="Timer-cancel" id="Timer-cancel"></a><a name="Timer-instance:cancel" id="Timer-instance:cancel"></a><div class="ref-block"><h4>function <span class="name">cancel</span><span class="arglist">(func)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:cancel</span><span class="arglist">(func)</span><a class="top" href="#Timer">^ top</a></h4><p>Prevent a timer from being executed in the future.</p><p><em>Always</em> use the function handle returned by <code class="lua">add()</code>/<code class="lua">addPeriodic()</code> to cancel a timer.</p><p><em>Never</em> use this inside a scheduled function.</p><div class="arguments">Parameters:<dl><dt>function <code>func</code></dt><dd>The function to be canceled.</dd></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">function tick() end)</code></pre></div></div><a name="Timer-do_for" id="Timer-do_for"></a><a name="Timer-instance:do_for" id="Timer-instance:do_for"></a><div class="ref-block"><h4>function <span class="name">do_for</span><span class="arglist">(delta, func, after)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:do_for</span><span class="arglist">(delta, func, after)</span><a class="top" href="#Timer">^ top</a></h4><p>Run a <code class="lua">func(dt)</code> for the next <code class="lua">delta</code> seconds. The function is called every time <code class="lua">update(dt)</code> is called. Optionally run <code class="lua">after()</code> once <code class="lua">delta</code> seconds have passed.</p><p><code class="lua">after</code> will receive itself as only parameter.</p><p>The same constraints as with <code class="lua">add()</code> apply.</p><div class="arguments">Parameters:<dl><dt>number <code>delta</code></dt><dd>Number of seconds the <code class="lua">func</code> will be called.</dd><dt>function <code>func</code></dt><dd>The function to be called upon <code class="lua">update(dt)</code>.</dd><dt>function <code>after</code> (optional)</dt><dd>A function to be called after <code class="lua">delta</code> seconds.</dd></dl></div><div class="returns">Returns:<dl><dt>table</dt><dd>The timer handle.</dd></dl></div><div class="example">Example:<pre><code class="lua">Timer.do_for(3, function() screen:shake() end)</code></pre><pre><code class="lua">player.isInvincible = true
-- flash player for 3 seconds
local t = 0
player.timer:do_for(3, function(dt)
t = t + dt
player.visible = (t % .2) < .1
end, function()
player.visible = true -- make sure the player is visible after three seconds
player.isInvincible = false
end)</code></pre></div></div><a name="Timer-cancel" id="Timer-cancel"></a><a name="Timer-instance:cancel" id="Timer-instance:cancel"></a><div class="ref-block"><h4>function <span class="name">cancel</span><span class="arglist">(handle)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:cancel</span><span class="arglist">(handle)</span><a class="top" href="#Timer">^ top</a></h4><p>Prevent a timer from being executed in the future.</p><p><em>Always</em> use the function handle returned by <code class="lua">add()</code>/<code class="lua">addPeriodic()</code> to cancel a timer.</p><p><em>Never</em> use this inside a scheduled function.</p><div class="arguments">Parameters:<dl><dt>table <code>handle</code></dt><dd>The function to be canceled.</dd></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">function tick()
print('tick... tock...') print('tick... tock...')
end end
handle = Timer.addPeriodic(1, tick) handle = Timer.addPeriodic(1, tick)
@ -71,23 +80,6 @@ handle = Timer.addPeriodic(1, tick)
Timer.cancel(handle) -- NOT: Timer.cancel(tick)</code></pre></div></div><a name="Timer-clear" id="Timer-clear"></a><a name="Timer-instance:clear" id="Timer-instance:clear"></a><div class="ref-block"><h4>function <span class="name">clear</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:clear</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><p>Remove all timed and periodic functions. Functions that have not yet been executed will discarded. <em>Never</em> use this inside a scheduled function.</p><div class="arguments">Parameters:<dl><dt>None</dt></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">Timer.clear()</code></pre></div></div><a name="Timer-update" id="Timer-update"></a><a name="Timer-instance:update" id="Timer-instance:update"></a><div class="ref-block"><h4>function <span class="name">update</span><span class="arglist">(dt)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:update</span><span class="arglist">(dt)</span><a class="top" href="#Timer">^ top</a></h4><p>Update timers and execute functions if the deadline is reached. Use this in <code class="lua">love.update(dt)</code>.</p><div class="arguments">Parameters:<dl><dt>number <code>dt</code></dt><dd>Time that has passed since the last update().</dd></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">function love.update(dt) Timer.cancel(handle) -- NOT: Timer.cancel(tick)</code></pre></div></div><a name="Timer-clear" id="Timer-clear"></a><a name="Timer-instance:clear" id="Timer-instance:clear"></a><div class="ref-block"><h4>function <span class="name">clear</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:clear</span><span class="arglist">()</span><a class="top" href="#Timer">^ top</a></h4><p>Remove all timed and periodic functions. Functions that have not yet been executed will discarded. <em>Never</em> use this inside a scheduled function.</p><div class="arguments">Parameters:<dl><dt>None</dt></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">Timer.clear()</code></pre></div></div><a name="Timer-update" id="Timer-update"></a><a name="Timer-instance:update" id="Timer-instance:update"></a><div class="ref-block"><h4>function <span class="name">update</span><span class="arglist">(dt)</span><a class="top" href="#Timer">^ top</a></h4><h4>function <span class="name">instance:update</span><span class="arglist">(dt)</span><a class="top" href="#Timer">^ top</a></h4><p>Update timers and execute functions if the deadline is reached. Use this in <code class="lua">love.update(dt)</code>.</p><div class="arguments">Parameters:<dl><dt>number <code>dt</code></dt><dd>Time that has passed since the last update().</dd></dl></div><div class="returns">Returns:<dl><dt>Nothing</dt></dl></div><div class="example">Example:<pre><code class="lua">function love.update(dt)
do_stuff() do_stuff()
Timer.update(dt) Timer.update(dt)
end</code></pre></div></div><a name="Timer-Interpolator" id="Timer-Interpolator"></a><div class="ref-block"><h4>function <span class="name">Interpolator</span><span class="arglist">(length, func)</span><a class="top" href="#Timer">^ top</a></h4><p>Create a wrapper for an interpolating function, i.e. a function that acts depending on how much time has passed.</p><p>The wrapper will have the prototype: <pre><code class="lua">function wrapper(dt, ...)</code></pre> where <code class="lua">dt</code> is the time that has passed since the last call of the wrapper and <code class="lua">...</code> are arguments passed to the interpolating function. It will return whatever the interpolating functions returns if the interpolation is not yet finished or nil if the interpolation is done.</p><p>The prototype of the interpolating function is: <pre><code class="lua">function interpolator(fraction, ...)</code></pre> where <code class="lua">fraction</code> is a number between 0 and 1 depending on how much time has passed and <code class="lua">...</code> are additional arguments supplied to the wrapper.</p><div class="arguments">Parameters:<dl><dt>number <code>length</code></dt><dd>Interpolation length in seconds.</dd><dt>function <code>func</code></dt><dd>Interpolating function.</dd></dl></div><div class="returns">Returns:<dl><dt>function</dt><dd>The wrapper function.</dd></dl></div><div class="example">Example:<pre><code class="lua">fader = Timer.Interpolator(5, function(frac, r,g,b)
love.graphics.setBackgroundColor(frac*r,frac*g,frac*b)
end)
function love.update(dt)
fader(dt, 255,255,255)
end</code></pre></div></div><a name="Timer-Oscillator" id="Timer-Oscillator"></a><div class="ref-block"><h4>function <span class="name">Oscillator</span><span class="arglist">(length, func)</span><a class="top" href="#Timer">^ top</a></h4><p>Create a wrapper for an oscillating function, which is basically a looping interpolating function.</p><p>The function prototypes are the same as with <code class="lua">Interpolator()</code>: <pre><code class="lua">function wrapper(dt, ...)</code></pre> <pre><code class="lua">function oscillator(fraction, ...)</code></pre></p><p>As with <code class="lua">Interpolator</code>, the wrapper will return whatever <code class="lua">oscillator()</code> returns.</p><div class="arguments">Parameters:<dl><dt>number <code>length</code></dt><dd>Length of one interpolation period.</dd><dt>function <code>func</code></dt><dd>Oscillating function.</dd></dl></div><div class="returns">Returns:<dl><dt>function</dt><dd>The wrapper function.</dd></dl></div><div class="example">Example:<pre><code class="lua">mover = Timer.Oscillator(10, function(frac)
return 400 + 300 * math.sin(2*math.pi*frac)
end)
local xpos = 100
function love.update(dt)
xpos = mover(dt)
end
function love.draw()
love.graphics.circle('fill', xpos, 300, 80, 36)
end</code></pre></div></div></div><a name="vector" id="vector"></a><div class="outer-block"><h3>hump.vector<a class="top" href="#top">^ top</a></h3><div class="preamble"><pre><code class="lua">vector = require "hump.vector"</code></pre><p>A handy 2D vector class providing most of the things you do with vectors.</p><p>You can access the individual coordinates by using <code class="lua">vec.x</code> and <code class="lua">vec.y</code>.</p></div><div class="overview"><h4>Module overview</h4><dl><dt><a href="#vector-operators">operators</a></dt><dd>Arithmetics and relations</dd><dt><a href="#vector-new">new()</a></dt><dd>Create a new vector.</dd><dt><a href="#vector-isvector">isvector()</a></dt><dd>Test if value is a vector.</dd><dt><a href="#vector-vector:clone">vector:clone()</a></dt><dd>Copy a vector.</dd><dt><a href="#vector-vector:unpack">vector:unpack()</a></dt><dd>Extract coordinates.</dd><dt><a href="#vector-vector:permul">vector:permul()</a></dt><dd>Per element multiplication.</dd><dt><a href="#vector-vector:len">vector:len()</a></dt><dd>Get length.</dd><dt><a href="#vector-vector:len2">vector:len2()</a></dt><dd>Get squared length.</dd><dt><a href="#vector-vector:dist">vector:dist()</a></dt><dd>Distance to other vector.</dd><dt><a href="#vector-vector:normalized">vector:normalized()</a></dt><dd>Get normalized vector.</dd><dt><a href="#vector-vector:normalize_inplace">vector:normalize_inplace()</a></dt><dd>Normalize vector in-place.</dd><dt><a href="#vector-vector:rotated">vector:rotated()</a></dt><dd>Get rotated vector.</dd><dt><a href="#vector-vector:rotate_inplace">vector:rotate_inplace()</a></dt><dd>Rotate vector in-place.</dd><dt><a href="#vector-vector:perpendicular">vector:perpendicular()</a></dt><dd>Get perpendicular vector.</dd><dt><a href="#vector-vector:projectOn">vector:projectOn()</a></dt><dd>Get projection onto another vector.</dd><dt><a href="#vector-vector:mirrorOn">vector:mirrorOn()</a></dt><dd>Mirrors vector on other vector</dd><dt><a href="#vector-vector:cross">vector:cross()</a></dt><dd>Cross product of two vectors.</dd></dl></div><a name="vector-operators" id="vector-operators"></a><div class="section-block"><h4>Arithmetics and relations<a class="top" href="#vector">^ top</a></h4><p>Vector arithmetic is implemented by using <code class="lua">__add</code>, <code class="lua">__mul</code> and other metamethods:</p><p><dl><dt><code class="lua">vector + vector = vector</code></dt><dd>Component wise sum.</dd><dt><code class="lua">vector - vector = vector</code></dt><dd>Component wise difference.</dd><dt><code class="lua">vector * vector = number</code></dt><dd>Dot product.</dd><dt><code class="lua">number * vector = vector</code></dt><dd>Scalar multiplication (scaling).</dd><dt><code class="lua">vector * number = vector</code></dt><dd>Scalar multiplication.</dd><dt><code class="lua">vector / number = vector</code></dt><dd>Scalar multiplication.</dd></dl></p><p>Relational operators are defined, too:</p><p><dl><dt>a == b</dt><dd><code class="lua">true</code>, if <code class="lua">a.x == b.x</code> and <code class="lua">a.y == b.y</code>.</dd><dt>a <= b</dt><dd><code class="lua">true</code>, if <code class="lua">a.x <= b.x</code> and <code class="lua">a.y <= b.y</code>.</dd><dt>a < b</dt><dd>Lexical sort: <code class="lua">true</code>, if <code class="lua">a.x < b.x</code> or <code class="lua">a.x == b.x</code> and <code class="lua">a.y < b.y</code>.</dd></dl></p><div class="example">Example:<pre><code class="lua">-- acceleration, player.velocity and player.position are vectors end</code></pre></div></div></div><a name="vector" id="vector"></a><div class="outer-block"><h3>hump.vector<a class="top" href="#top">^ top</a></h3><div class="preamble"><pre><code class="lua">vector = require "hump.vector"</code></pre><p>A handy 2D vector class providing most of the things you do with vectors.</p><p>You can access the individual coordinates by using <code class="lua">vec.x</code> and <code class="lua">vec.y</code>.</p></div><div class="overview"><h4>Module overview</h4><dl><dt><a href="#vector-operators">operators</a></dt><dd>Arithmetics and relations</dd><dt><a href="#vector-new">new()</a></dt><dd>Create a new vector.</dd><dt><a href="#vector-isvector">isvector()</a></dt><dd>Test if value is a vector.</dd><dt><a href="#vector-vector:clone">vector:clone()</a></dt><dd>Copy a vector.</dd><dt><a href="#vector-vector:unpack">vector:unpack()</a></dt><dd>Extract coordinates.</dd><dt><a href="#vector-vector:permul">vector:permul()</a></dt><dd>Per element multiplication.</dd><dt><a href="#vector-vector:len">vector:len()</a></dt><dd>Get length.</dd><dt><a href="#vector-vector:len2">vector:len2()</a></dt><dd>Get squared length.</dd><dt><a href="#vector-vector:dist">vector:dist()</a></dt><dd>Distance to other vector.</dd><dt><a href="#vector-vector:normalized">vector:normalized()</a></dt><dd>Get normalized vector.</dd><dt><a href="#vector-vector:normalize_inplace">vector:normalize_inplace()</a></dt><dd>Normalize vector in-place.</dd><dt><a href="#vector-vector:rotated">vector:rotated()</a></dt><dd>Get rotated vector.</dd><dt><a href="#vector-vector:rotate_inplace">vector:rotate_inplace()</a></dt><dd>Rotate vector in-place.</dd><dt><a href="#vector-vector:perpendicular">vector:perpendicular()</a></dt><dd>Get perpendicular vector.</dd><dt><a href="#vector-vector:projectOn">vector:projectOn()</a></dt><dd>Get projection onto another vector.</dd><dt><a href="#vector-vector:mirrorOn">vector:mirrorOn()</a></dt><dd>Mirrors vector on other vector</dd><dt><a href="#vector-vector:cross">vector:cross()</a></dt><dd>Cross product of two vectors.</dd></dl></div><a name="vector-operators" id="vector-operators"></a><div class="section-block"><h4>Arithmetics and relations<a class="top" href="#vector">^ top</a></h4><p>Vector arithmetic is implemented by using <code class="lua">__add</code>, <code class="lua">__mul</code> and other metamethods:</p><p><dl><dt><code class="lua">vector + vector = vector</code></dt><dd>Component wise sum.</dd><dt><code class="lua">vector - vector = vector</code></dt><dd>Component wise difference.</dd><dt><code class="lua">vector * vector = number</code></dt><dd>Dot product.</dd><dt><code class="lua">number * vector = vector</code></dt><dd>Scalar multiplication (scaling).</dd><dt><code class="lua">vector * number = vector</code></dt><dd>Scalar multiplication.</dd><dt><code class="lua">vector / number = vector</code></dt><dd>Scalar multiplication.</dd></dl></p><p>Relational operators are defined, too:</p><p><dl><dt>a == b</dt><dd><code class="lua">true</code>, if <code class="lua">a.x == b.x</code> and <code class="lua">a.y == b.y</code>.</dd><dt>a <= b</dt><dd><code class="lua">true</code>, if <code class="lua">a.x <= b.x</code> and <code class="lua">a.y <= b.y</code>.</dd><dt>a < b</dt><dd>Lexical sort: <code class="lua">true</code>, if <code class="lua">a.x < b.x</code> or <code class="lua">a.x == b.x</code> and <code class="lua">a.y < b.y</code>.</dd></dl></p><div class="example">Example:<pre><code class="lua">-- acceleration, player.velocity and player.position are vectors
acceleration = vector(0,-9) acceleration = vector(0,-9)
player.velocity = player.velocity + acceleration * dt player.velocity = player.velocity + acceleration * dt