hump/index.html
2011-01-21 14:05:00 +01:00

1634 lines
58 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>hump - L&Ouml;VE Helper Utilities for More Productivity</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="highlight.css" />
<script type="text/javascript" src="highlight.pack.js"></script>
<script type="text/javascript">
window.onload = function() {
var examples = document.getElementsByTagName("code");
for (i = 0; i < examples.length; ++i) {
if (examples[i].className == "lua")
hljs.highlightBlock(examples[i], " ");
}
};
</script>
</style>
</head>
<body><a name="top"></a>
<a href="http://github.com/vrld/hump"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
<div id="header">
<h1>hump <span class="small">Helper Utilities for More Productivity</span></h1>
<h2>Documentation</h2>
</div>
<div id="nav">
<ul>
<li><a href="#gamestate">Gamestate</a></li>
<li><a href="#timer">Timer</a></li>
<li><a href="#vector">Vector</a></li>
<li><a href="#class">Class</a></li>
<li><a href="#camera">Camera</a></li>
<li><a href="#ringbuffer">Ringbuffer</a></li>
</ul>
</div>
<div class="module"><div class="name">hump</div>
<div class="preamble">
<p>hump is a collection of lightweight, yet powerful modules to speed up
game development with <a href="http://love2d.org/">L&Ouml;VE</a>. It offers
solutions to various small but annoyingly reccurent problems.</p>
</div>
<div class="overview">
<h3>Download</h3>
<p>You can download the latest packaged version of hump as <a href="http://github.com/vrld/hump/zipball/master">zip</a>-
or <a href="http://github.com/vrld/hump/tarball/master">tar</a>-archive directly from
<a href="http://github.com/">github</a>. You can also view and download the sourcecode
of individual modules <a href="http://github.com/vrld/hump">here</a>.</p>
<p>If you use the <a href="http://git-scm.com">Git</a> command line client, you can clone
the repository by running:</p>
<pre>git clone git://github.com/vrld/hump</pre>
<p>Once done, you can check for updates by running:</p>
<pre>git pull</pre>
<p>from inside the directory.</p>
</div>
<div class="overview">
<h3>License</h3>
<blockquote id="license">
<p>Copyright (c) 2010-2011 Matthias Richter</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.</p>
<p>Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.</p>
</blockquote>
</div>
</div>
<a name="gamestate"></a>
<div id="gamestate" class="module">
<div class="name">hump.gamestate<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.gamestate"</code></pre>
<p>A gamestate encapsulates independant data an behaviour into a single entity.</p>
<p>A typical game could consist of a <em>menu</em>-state, a <em>level</em>-state and a <em>game-over</em>-state.</p>
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#gamestate-new">new()</a></dt>
<dd>Create new gamestate.</dd>
<dt><a href="#gamestate-switch">switch()</a></dt>
<dd>Switch to gamestate.</dd>
<dt><a href="#gamestate-update">update()</a></dt>
<dd>Manually update current gamestate.</dd>
<dt><a href="#gamestate-draw">draw()</a></dt>
<dd>Manually draw the current gamestate.</dd>
<dt><a href="#gamestate-keypressed">keypressed()</a></dt>
<dd>Inform current gamestate of keypressed event.</dd>
<dt><a href="#gamestate-keyreleased">keyreleased()</a></dt>
<dd>Inform current gamestate of keyreleased event.</dd>
<dt><a href="#gamestate-mousepressed">mousepressed()</a></dt>
<dd>Inform current gamestate of mousepressed event.</dd>
<dt><a href="#gamestate-mousereleased">mousereleased()</a></dt>
<dd>Inform current gamestate of mousereleased event.</dd>
<dt><a href="#gamestate-joystickpressed">joystickpressed()</a></dt>
<dd>Inform current gamestate of joystickpressed event.</dd>
<dt><a href="#gamestate-joystickreleased">joystickreleased()</a></dt>
<dd>Inform current gamestate of a joystickreleased event.</dd>
<dt><a href="#gamestate-registerEvents">registerEvents()</a></dt>
<dd>Automatically do all of the above when needed.</dd>
</dl>
</div>
<a name="gamestate-callbacks"></a>
<div class="overview"><h3>Gamestate Callbacks</h3>
<p>A gamestate can define (nearly) all callbacks that L&Ouml;VE defines. In addition, there are callbacks
for entering and leaving a state.:</p>
<dl>
<dt>enter(previous, ...)</dt>
<dd>Called when entering the state. See <a href="#gamestate-switch">switch()</a>.</dd>
<dt>leave()</dt>
<dd>Called when leaving a state. See <a href="#gamestate-switch">switch()</a>.</dd>
<dt>update()</dt>
<dd>Update the game state. Called every frame.</dd>
<dt>draw()</dt>
<dd>Draw on the screen. Called every frame.</dd>
<dt>keypressed()</dt>
<dd>Triggered when a key is pressed.</dd>
<dt>keyreleased()</dt>
<dd>Triggered when a key is released.</dd>
<dt>mousepressed()</dt>
<dd>Triggered when a mouse button is pressed.</dd>
<dt>mousereleased()</dt>
<dd>Triggered when a mouse button is released.</dd>
<dt>joystickpressed()</dt>
<dd>Triggered when a joystick button is pressed.</dd>
<dt>joystickreleased()</dt>
<dd>Triggered when a joystick button is released.</dd>
</dl>
<p>When using <a href="#gamestate-registerEvents">registerEvents()</a>, all these
callbacks will receive the same arguments as the <a href="http://love2d.org/wiki/love">L&Ouml;VE callbacks</a> do.</p>
<div class="example">Example:
<pre><code class="lua">menu = Gamestate.new()
function menu:enter(previous, background_image)
self.background = background_image
Buttons.initialize()
end
function menu:leave()
Buttons.cleanup()
end
function menu:update(dt)
Buttons.update(dt)
end
function menu:draw()
love.graphics.draw(self.background, 0, 0)
Buttons.draw()
end
function menu:keyreleased(key)
if key == 'up' then
Buttons.selectPrevious()
elseif key == 'down' then
Buttons.selectNext()
elseif
Buttons.active:onClick()
end
end
function menu:mousereleased(x,y, mouse_btn)
local button = Buttons.hovered(x,y)
if button then
Button.select(button)
if mouse_btn == 'l' then
button:onClick()
end
end
end</code></pre>
</div>
</div>
<a name="gamestate-new"></a>
<div id="gamestate-new" class="function">
<div class="definition">function <span class="name">new</span><span class="arglist">()</span><a class="top" href="#gamestate">^ top</a></div>
<p>Declare a new gamestate. A gamestate can define several <a href="#gamestate-callbacks">callbacks</a>.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Gamestate</dt>
<dd>The new gamestate.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">menu = Gamestate.new()</code></pre>
</div>
</div>
<a name="gamestate-switch"></a>
<div id="gamestate-switch" class="function">
<div class="definition">function <span class="name">switch</span><span class="arglist">(to, ...)</span><a class="top" href="#gamestate">^ top</a></div>
<p>Switch to a gamestate, with any additional arguments passed to the new state.</p>
<p>Switching a gamestate will call the <a href="#gamestate-callbacks"><code>leave()</code></a> callback on
the current gamestate, replace the current gamestate with <code>to</code> and finally call
<a href="#gamestate-callbacks"><code>enter(old_state, ...)</code></a> on the new gamestate.</p>
<div class="arguments">Parameters:
<dl>
<dt>Gamestate <code>to</code></dt>
<dd>Target gamestate.</dd>
<dt>mixed <code>...</code></dt>
<dd>Additional arguments to pass to <code>to:enter()</code>.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>The result of <code>to:enter()</code>.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">Gamestate.switch(game, level_two)</code></pre>
</div>
</div>
<a name="gamestate-update"></a>
<a name="gamestate-draw"></a>
<a name="gamestate-keypressed"></a>
<a name="gamestate-keyreleased"></a>
<a name="gamestate-mousepressed"></a>
<a name="gamestate-mousereleased"></a>
<a name="gamestate-joystickpressed"></a>
<a name="gamestate-joystickreleased"></a>
<div id="gamestate-events" class="function">
<div class="definition">function <span class="name">update</span><span class="arglist">(...)</span><a class="top" href="#gamestate">^ top</a></div>
<div class="definition">function <span class="name">draw</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">keypressed</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">keyreleased</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">mousepressed</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">mousereleased</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">joystickpressed</span><span class="arglist">(...)</span></div>
<div class="definition">function <span class="name">joystickreleased</span><span class="arglist">(...)</span></div>
<p>Calls the corresponding function on the current gamestate (see <a href="#gamestate-callbacks">callbacks</a>).</p>
<p>Only needed when not using <a href="#gamestate-registerEvents"><code>registerEvents()</code></a>.</p>
<div class="arguments">Parameters:
<dl>
<dt>mixed <code>...</code></dt>
<dd>Arguments to pass to the corresponding <a href="#gamestate-callbacks">callback</a>.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>Result of the callback function.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">function love.update(dt)
Gamestate.update(dt)
end
function love.draw()
local mx,my = love.mouse.getPosition()
Gamestate.draw(mx, my)
end
function love.keypressed(key, code)
Gamestate.keypressed(key, code)
end</code></pre>
</div>
</div>
<a name="gamestate-registerEvents"></a>
<div id="gamestate-registerEvents" class="function">
<div class="definition">function <span class="name">registerEvents</span><span class="arglist">()</span><a class="top" href="#gamestate">^ top</a></div>
<p>Register all love callbacks to call <code>Gamestate.update()</code>, <code>Gamestate.draw()</code>, etc. automatically.</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">function love.load()
Gamestate.registerEvents()
Gamestate.switch(menu)
end</code></pre>
</div>
</div>
</div>
<a name="timer"></a>
<div id="timer" class="module">
<div class="name">hump.timer<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.timer"</code></pre>
<p>hump.timer provides a simple interface to use delayed functions, i.e.
functions that will only be executed after some amount time.</p>
<p>In addition, the module offers facilities to create functions that
interpolate or oscillate over time.</p>
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#timer-add">add()</a></dt>
<dd>Add a timed function.</dd>
<dt><a href="#timer-addPeriodic">addPeriodic()</a></dt>
<dd>Add a periodic function.</dd>
<dt><a href="#timer-clear">clear()</a></dt>
<dd>Clear all timed and periodic functions.</dd>
<dt><a href="#timer-update">update()</a></dt>
<dd>Update timers.</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-add"></a>
<div id="timer-add" class="function">
<div class="definition">function <span class="name">add</span><span class="arglist">(delay, func)</span><a class="top" href="#timer">^ top</a></div>
<p>Add a timed function. The function will be executed when <code>delay</code> seconds have elapsed.</p>
<p>Note that there is no guarantee that the delay will be exceeded. It is, however, guaranteed that the
function will <em>not be executed before</em> the delay has passed.</p>
<p>If the function is called, it will receive itself as only parameter. This may be useful to implement
the periodic behavior of <code>Timer.addPeriodic</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>delay</code></dt>
<dd>Time to pass before the function is called.</dd>
<dt>function <code>func</code></dt>
<dd>The function to be called.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">-- grant the player 5 seconds of immortality
player:setInvincible(true)
Timer.add(5, function() player:setInvincible(false) end)</code></pre>
<pre><code class="lua">-- print "foo" every second
Timer.add(1, function(func) print("foo") Timer.add(1, func) end)</code></pre>
</div>
</div>
<a name="timer-addPeriodic"></a>
<div id="timer-addPeriodic" class="function">
<div class="definition">function <span class="name">addPeriodic</span><span class="arglist">(delay, func, count)</span><a class="top" href="#timer">^ top</a></div>
<div class="definition">function <span class="name">addPeriodic</span><span class="arglist">(delay, func)</div>
<p>Add a periodic timed function, i.e. a function that will be called <code>count</code> times every <code>delay</code> seconds.</p>
<p>If <code>count</code> is omitted, the function loops until <a href="#timer-clear"><code>clear()</code></a> is called.</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>delay</code></dt>
<dd>Time to pass before the function is called.</dd>
<dt>function <code>func</code></dt>
<dd>The function to be called.</dd>
<dt>number <code>count</code></dt>
<dd>The number of times the function is called.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">Timer.addPeriodic(1, function() lamp:toggleLight() end)</code></pre>
<pre><code class="lua">Timer.addPeriodic(0.3, function() mothership:spawnFighter() end, 5)</code></pre>
</div>
</div>
<a name="timer-clear"></a>
<div id="timer-clear" class="function">
<div class="definition">function <span class="name">clear</span><span class="arglist">()</span><a class="top" href="#timer">^ top</a></div>
<p>Clears all timers.</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"></a>
<div id="timer-update" class="function">
<div class="definition">function <span class="name">update</span><span class="arglist">(dt)</span><a class="top" href="#timer">^ top</a></div>
<p>Update timers and execute functions if the deadline is reached. Use this in <code>love.update()</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>dt</code></dt>
<dd>Time that has passed since the last <code>update()</code>.</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()
Timer.update(dt)
end</code></pre>
</div>
</div>
<a name="timer-Interpolator"></a>
<div id="timer-Interpolator" class="function">
<div class="definition">function <span class="name">Interpolator</span><span class="arglist">(length, func)</span><a class="top" href="#timer">^ top</a></div>
<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>
<code>dt</code> is the time that has passed since the last call and <code>...</code> are arguments passed to the
interpolating function. It will return whatever the interpolating functions returns if the interpolation is not yet finished or
<code>nil</code> if the interpolation is done.</p>
<p>The prototype of the interpolating function is:
<pre><code class="lua">function inter(fraction, ...)</code></pre>
where <code>fraction</code> is a number between 0 and 1 and <code>...</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"></a>
<div id="timer-Oscillator" class="function">
<div class="definition">function <span class="name">Oscillator</span><span class="arglist">(length, func)</span><a class="top" href="#timer">^ top</a></div>
<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>Interpolator()</code>:
<pre><code class="lua">function wrapper(dt, ...)</code></pre>
<pre><code class="lua">function oscillator(fraction, ...)</code></pre>
<p>The wrapper function will return whatever <code>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"></a>
<div id="vector" class="module">
<div class="name">hump.vector<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.vector"</code></pre>
<p>A handy 2D vector class defining the most common things you do with vectors.</p>
<p>You can access the individual coordinates by using <code>vec.x</code> and <code>vec.y</code>.</p>
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#vector-new">new()</a></dt>
<dd>Create 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 lenght.</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>Get distance to 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:cross">vector:cross()</a></dt>
<dd>Cross product of two vectors.</dd>
</dl>
</div>
<div class="function">
<div class="definition">Arithmetic and relational operators</div>
<p>Vector arithmetic is implemented by using <code>__add</code>, <code>__mul</code> and other metamethods:</p>
<dl>
<dt><code>vector + vector = vector</code></dt><dd>Component wise sum.</dd>
<dt><code>vector - vector = vector</code></dt><dd>Component wise difference.</dd>
<dt><code>vector * vector = number</code></dt><dd>Dot product.</dd>
<dt><code>number * vector = vector</code></dt><dd>Vector scaling.</dd>
<dt><code>vector * number = vector</code></dt><dd>Vector scaling.</dd>
<dt><code>vector / number = vector</code></dt><dd>Vector scaling.</dd>
</dl>
<p>Relational operators are defined, too:</p>
<dl>
<dt><code>a == b</code></dt>
<dd><code>true</code>, if <code>a.x == b.x</code> and <code>a.y == b.y</code>.</dd>
<dt><code>a &lt;= b</code></dt>
<dd><code>true</code>, if <code>a.x &lt;= b.x</code> and <code>a.y &lt;= b.y</code>.</dd>
<dt><code>a &lt; b</code></dt>
<dd>Lexical sort: <code>true</code>, if <code>a.x &lt; b.x</code> or <code>a.x == b.x</code> and <code>a.y &lt; b.y</code>.</dd>
</dl>
<div class="example">Example:
<pre><code class="lua">acceleration = vector(0,-9)
player.velocity = player.velocity + acceleration * dt
player.position = player.position + player.velocity * dt</code></pre>
</div>
</div>
<a name="vector-new"></a>
<div id="vector-new" class="function">
<div class="definition">function <span class="name">new</span><span class="arglist">(x,y)</span><a class="top" href="#vector">^ top</a></div>
<p>Create new vector</p>
<div class="arguments">Parameters:
<dl>
<dt>numbers <code>x, y</code></dt>
<dd>Coordinates.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">a = vector.new(10,10)</code></pre>
<p>As a shortcut, you can call the module like a function, i.e.:</p>
<pre><code class="lua">vector = require 'hump.vector'
a = vector(10,10)</code></pre>
</div>
</div>
<a name="vector-isvector"></a>
<div id="vector-isvector" class="function">
<div class="definition">function <span class="name">isvector</span><span class="arglist">(v)</span><a class="top" href="#vector">^ top</a></div>
<p>Test whether a variable is a vector.</p>
<div class="arguments">Parameters:
<dl>
<dt>mixed <code>v</code></dt>
<dd>The variable to test.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>boolean</dt>
<dd><code>true</code> if <code>v</code> is a vector, <code>false</code> otherwise.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">if not vector.isvector(v) then
v = vector(v,0)
end</code></pre>
</div>
</div>
<a name="vector-vector:clone"></a>
<div id="vector-vector:clone" class="function">
<div class="definition">function <span class="name">vector:clone</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Copy a vector. Simply assigning a vector a vector to a variable will create
a reference, so when you modify the vector referenced by the new variable, will
also change the old one:</p>
<pre><code>a = vector(1,1) -- create vector
b = a -- b references a
c = a:clone() -- c is a copy of a
b.x = 0 -- changes a,b and c
print(a,b,c) -- prints '(1,0), (1,0), (1,1)'</code></pre>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The copied vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">copy = original:clone()</code></pre>
</div>
</div>
<a name="vector-vector:unpack"></a>
<div id="vector-vector:unpack" class="function">
<div class="definition">function <span class="name">vector:unpack</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Extract coordinates.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>numbers</dt>
<dd>The coordinates.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">x,y = pos:unpack()</code></pre>
<pre><code class="lua">love.graphics.draw(self.image, self.pos:unpack())</code></pre>
</div>
</div>
<a name="vector-vector.permul"></a>
<div id="vector-vector.permul" class="function">
<div class="definition">function <span class="name">vector:permul</span><span class="arglist">(other)</span><a class="top" href="#vector">^ top</a></div>
<p>Multiplies vectors coordinate wise, i.e. <code>result = vector(a.x * b.x, a.y * b.y)</code>.</p>
<p>This does not change either argument vectors, but creates a new one.</p>
<div class="arguments">Parameters:
<dl>
<dt>vector <code>other</code></dt>
<dd>The other vector.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>New vector as defined above.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">scaled = original:permul(vector(1,1.5))</code></pre>
</div>
</div>
<a name="vector-vector:len"></a>
<div id="vector-vector:len" class="function">
<div class="definition">function <span class="name">vector:len</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Get length of a vector, i.e. <code>math.sqrt(vec.x * vec.x + vec.y * vec.y)</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>number</dt>
<dd>Length of the vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">distance = (a - b):len()</code></pre>
</div>
</div>
<a name="vector-vector:len2"></a>
<div id="vector-vector:len2" class="function">
<div class="definition">function <span class="name">vector:len2</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Get squared length of a vector, i.e. <code>vec.x * vec.x + vec.y * vec.y</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>number</dt>
<dd>The squared length.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">-- get closest vertex to a given vector
closest, dsq = vertices[1], (pos - vertices[1]):len2()
for i = 2,#vertices do
local temp = (pos - vertices[i]):len2()
if temp &lt; dsq then
closest, dsq = vertices[i], temp
end
end</code></pre>
</div>
</div>
<a name="vector-vector.dist"></a>
<div id="vector-vector.dist" class="function">
<div class="definition">function <span class="name">vector:dist</span><span class="arglist">(other)</span><a class="top" href="#vector">^ top</a></div>
<p>Get distance of two vectors. The same as <code>(a - b):len()</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>vector <code>other</code></dt>
<dd>Other vector to measure the distance.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>number</dt>
<dd>The distance of both vectors.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">-- get closest vertex to a given vector
-- slightly slower than the example using len2()
closest, dist = vertices[1], pos:dist(vertices[1])
for i = 2,#vertices do
local temp = pos:dist(vertices[i])
if temp &lt; dist then
closest, dist = vertices[i], temp
end
end</code></pre>
</div>
</div>
<a name="vector-vector:normalized"></a>
<div id="vector-vector:normalized" class="function">
<div class="definition">function <span class="name">vector:normalized</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Get normalized vector, i.e. a vector with the same direction as the input vector,
but length 1.</p>
<p>This does not change the input vector, but creates a new vector.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>Vector with same direction as the input vector, but length 1.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">normal = edge:normalized()</code></pre>
</div>
</div>
<a name="vector-vector:normalize_inplace"></a>
<div id="vector-vector:normalize_inplace" class="function">
<div class="definition">function <span class="name">vector:normalize_inplace</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Normalize a vector, i.e. make the vector unit length.</p>
<p class="warning">This modifies the vector. If in doubt, use <code>vector:normalized()</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The normalized vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">normal = (b - a):perpendicular():normalize_inplace()</code></pre>
</div>
</div>
<a name="vector-vector:rotated"></a>
<div id="vector-vector:rotated" class="function">
<div class="definition">function <span class="name">vector:rotated</span><span class="arglist">(phi)</span><a class="top" href="#vector">^ top</a></div>
<p>Get a rotated vector.</p>
<p>This does not change the input vector, but creates a new vector.</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>phi</code></dt>
<dd>Rotation angle in radians.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The rotated vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">-- approximate a circle
circle = {}
for i = 1,30 do
local phi = 2 * math.pi * i / 30
circle[#circle+1] = vector(0,1):rotated(phi)
end</code></pre>
</div>
<div class="example">Sketch:
<p><img src="vector-rotated.png" alt="sketch of rotated vectors" width="260" height="171" /></p>
</div>
</div>
<a name="vector-vector:rotate_inplace"></a>
<div id="vector-vector:rotate_inplace" class="function">
<div class="definition">function <span class="name">vector:rotate_inplace</span><span class="arglist">(phi)</span><a class="top" href="#vector">^ top</a></div>
<p>Rotate a vector.</p>
<p class="warning">This modifies the vector. If in doubt, use <code>vector:rotate()</code>.</p>
</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>phi</code></dt>
<dd>Rotation angle in radians.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The rotated vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">-- ongoing rotation
spawner.direction:rotate_inplace(dt)</code></pre>
</div>
</div>
<a name="vector-vector:perpendicular"></a>
<div id="vector-vector:perpendicular" class="function">
<div class="definition">function <span class="name">vector:perpendicular</span><span class="arglist">()</span><a class="top" href="#vector">^ top</a></div>
<p>Quick rotation by 90&deg;. Creates a new vector. The same as (but faster):</p>
<pre><code class="lua">vec:rotate(math.pi/2)</code></pre>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>A vector perpendicular on the input vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">normal = (b - a):perpendicular():normalize_inplace()</code></pre>
</div>
<div class="example">Sketch:
<p><img src="vector-perpendicular.png" alt="sketch of perpendicular vectors" width="267" height="202" /></p>
</div>
</div>
<a name="vector-vector:projectOn"></a>
<div id="vector-vector:projectOn" class="function">
<div class="definition">function <span class="name">vector:projectOn</span><span class="arglist">(v)</span><a class="top" href="#vector">^ top</a></div>
<p>Project vector onto another vector.</p>
<div class="arguments">Parameters:
<dl>
<dt>vector <code>v</code></dt>
<dd>The vector to project on.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>vector</dt>
<dd>The projected vector.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">velocity_component = velocity:projectOn(axis)</code></pre>
</div>
<div class="example">Sketch:
<p><img src="vector-projectOn.png" alt="sketch of vector projection" width="605" height="178" /></p>
</div>
</div>
<a name="vector-vector:cross"></a>
<div id="vector-vector:cross" class="function">
<div class="definition">function <span class="name">vector:cross</span><span class="arglist">(other)</span><a class="top" href="#vector">^ top</a></div>
<p>Get cross product of both vectors.</p>
<p>For the math geeks:</p>
<p>The cross product not be defined for 2D vectors. To nonetheless get a result,
treat the vectors as being 3D vectors <em>(x,y,0)</em>. The cross product of both
vectors has just a <em>z</em>-component, and this is what this function returns.
It's also the determinant of both vectors: <em>d = det(a,b)</em>.</p>
<div class="arguments">Parameters:
<dl>
<dt>vector <code>other</code></dt>
<dd>Vector to compute the cross product with.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>number</dt>
<dd>The cross product of both vectors.</dd>
</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">parallelogram_area = a:cross(b)</code></pre>
</div>
</div>
</div>
<a name="class"></a>
<div id="class" class="module">
<div class="name">hump.class<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.class"</code></pre>
A small, handy class implementation with multiple inheritance.
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#class-new">new()</a></dt>
<dd>Create new class.</dd>
<dt><a href="#class-class.construct">class.construct()</a></dt>
<dd>Call class constructor.</dd>
<dt><a href="#class-inherit">inherit()</a></dt>
<dd>Class inheritance.</dd>
</dl>
</div>
<a name="class-new"></a>
<div id="class-new" class="function">
<div class="definition">function <span class="name">new</span><span class="arglist">(constructor)</span><a class="top" href="#class">^ top</a></div>
<div class="definition">function <span class="name">new</span><span class="arglist">{name = the_name, constructor}</span></div>
<p>Define a new class.</p>
<p>The constructor will receive the newly created object as first argument.</p>
<p>If you <code>require</code>d the module to a variable, you can use the variable
as a shortcut to <code>new()</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>function <code>constructor</code></dt>
<dd>Class constructor.</dd>
<dt>string <code>the_name</code></dt>
<dd>Class name to be returned when calling <code>tostring()</code> on the class.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Class</dt>
<dd>The class.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">Class = require 'hump.class'
-- define unnamed class
Feline = Class(function(self, size, weight)
self.size = size
self.weight = weight
end)
-- define class method
function Feline:stats()
return string.format("size: %.02f, weight %.02f", self.size, self.weight)
end
-- create two objects
garfield = Feline(.7, 45)
felix = Feline(.8, 12)
print("Garfield: " .. garfield:stats(), "Felix: " .. felix:stats())</code></pre>
<pre><code class="lua">Class = require 'hump.class'
-- define named class
Feline = Class{name = "Feline", function(self, size, weight)
self.size = size
self.weight = weight
end}
garfield = Feline(.7, 45)
print(Feline, garfield) -- prints 'Feline &lt;instance of Feline&gt;'</code></pre>
</div>
</div>
<a name="class-class.construct"></a>
<div id="class-new" class="function">
<div class="definition">function <span class="name">class.construct</span><span class="arglist">(object, ...)</span><a class="top" href="#class">^ top</a></div>
<div class="definition">function <span class="name">class.Construct</span><span class="arglist">(object, ...)</span></div>
<p>Calls class constructor of a class.</p>
<p>Needed in constructors of child classes to initialize parts of the object that the parent classes define.</p>
<div class="arguments">Parameters:
<dl>
<dt>Object <code>object</code></dt>
<dd>The object. This is usually <code>self</code>.</dd>
<dt>mixed <code>...</code></dt>
<dd>Arguments to pass to the constructor.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">Class = require 'hump.class'
Feline = Class{name = "Feline", function(self, size, weight)
self.size = size
self.weight = weight
end}
Cat = Class{name = "Cat", function(self, name, size, weight)
Feline.construct(self, size, weight)
self.name = name
end}
Cat:inherit(Feline)</code></pre></div>
</div>
<a name="class-inherit"></a>
<div id="class-inherit" class="function">
<div class="definition">function <span class="name">inherit</span><span class="arglist">(class, super, ...)</span><a class="top" href="#class">^ top</a></div>
<div class="definition">function <span class="name">class:inherit</span><span class="arglist">(super, ...)</span></div>
<div class="definition">function <span class="name">class:Inherit</span><span class="arglist">(super, ...)</span></div>
<p>Inherit functions (but not class variables).</p>
<p>If multiple super-classes are defined inherit from all of these. If two super-classes define
a method of the same name, inherit it from the one mentioned first.</p>
<div class="arguments">Parameters:
<dl>
<dt>Class <code>class</code></dt>
<dd>Child class.</dd>
<dt>Classes <code>super, ...</code></dt>
<dd>Parent classes to inherit from.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">Class = require 'hump.class'
Feline = Class{name = "Feline", function(self, size, weight)
self.size = size
self.weight = weight
end}
function Feline:stats()
return string.format("size: %.02f, weight %.02f", self.size, self.weight)
end
function Feline:speak() print("meow") end
Cat = Class{name = "Cat", function(self, name, size, weight)
Feline.construct(self, size, weight)
self.name = name
end}
Cat:inherit(Feline)
function Cat:stats()
return string.format("name: %s, %s", self.name, Feline.stats(self))
end
Tiger = Class{name = "Tiger", function(self, size, weight)
Feline.construct(self, size, weight)
end}
Tiger:Inherit(Feline)
function Tiger:speak() print("ROAR!") end
felix = Cat("Felix", .8, 12)
hobbes = Tiger(2.2, 68)
print(felix:stats()) -- "name: Felix, size: 0.80, weight 12.00"
print(hobbes:stats()) -- "size: 2.20, weight 68.00"
felix:speak() -- "meow"
hobbes:speak() -- "ROAR!"</code></pre>
</div>
</div>
<div class="caveats">
<h3>Caveats</h3>
<p>Be careful when using metamethods like <code>__add</code> or <code>__mul</code>:
When subclass inherits those methods from a superclass, but does not overwrite them,
the result of the operation may be of the type superclass. Consider the following:</p>
<pre><code class="lua">Class = require 'hump.class'
A = Class(function(self, x) self.x = x end)
function A:__add(other) return A(self.x + other.x) end
function A:show() print("A:", self.x) end
B = Class(function(self, x, y) A.construct(self, x) self.y = y end)
B:Inherit(A)
function B:show() print("B:", self.x, self.y) end
function B:foo() print("foo") end
one, two = B(1,2), B(3,4)
result = one + two
result:show() -- prints "A: 4"
result:foo() -- error: method does not exist</code></pre>
</div>
</div>
<a name="camera"></a>
<div id="camera" class="module">
<div class="name">hump.camera<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.camera"</code></pre>
<p class="warning">Depends on <a href="#vector">vector</a></p>
<p>Camera abstraction for L&Ouml;VE. A camera &quot;looks&quot; at a position and can be moved,
zoomed and rotated.</p>
<p>A camera defines it's own coordinate system, meaning that an object shown on
the screen likely has different coordinates in the game world than it has on the screen.</p>
<p>For example, the mouse position could be at pixel <code>400,400</code> on the screen
(= camera coordinates), but the camera looks at the point <code>100,100</code> and
is rotated by <code>90&deg;</code>. The world coordinates of the mouse cursor are <code>200,100</code>.</p>
<p>The camera class defines methods to convert between both coordinate systems.</p>
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#camera-new">new()</a></dt>
<dd>Create new camera object.</dd>
<dt><a href="#camera-camera:rotate">camera:rotate()</a></dt>
<dd>Rotate camera.</dd>
<dt><a href="#camera-camera:translate">camera:translate()</a></dt>
<dd>Move camera.</dd>
<dt><a href="#camera-camera:predraw">camera:predraw()</a></dt>
<dd>Apply camera transformation.</dd>
<dt><a href="#camera-camera:postdraw">camera:postdraw()</a></dt>
<dd>Revert camera transformation.</dd>
<dt><a href="#camera-camera:draw">camera:draw()</a></dt>
<dd>Apply camera transformations to a function.</dd>
<dt><a href="#camera-camera:toCameraCoords">camera:toCameraCoords()</a></dt>
<dd>Convert vector to camera coordinates.</dd>
<dt><a href="#camera-camera:toWorldCoords">camera:toWorldCoords()</a></dt>
<dd>Convert vector to world coordinates.</dd>
<dt><a href="#camera-camera:mousepos">camera:mousepos()</a></dt>
<dd>Get mouse position in world coordinates.</dd>
</dl>
</div>
<a name="camera-new"></a>
<div id="camera-new" class="function">
<div class="definition">function <span class="name">new</span><span class="arglist">(pos, zoom, rot)</span><a class="top" href="#camera">^ top</a></div>
<p>Create new camera object.</p>
<p>You can access and modify the camera parameters using <code>camera.pos</code>, <code>camera.zoom</code> and
<code>camera.rot</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt><a href="#vector">vector</a> <code>pos</code> (screen center)</dt>
<dd>Position the camera should look at.</dd>
<dt>number <code>zoom</code> (1)</dt>
<dd>Camera zoom.</dd>
<dt>number <code>rot</code> (0)</dt>
<dd>Camera rotation in radians.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>camera</dt>
<dd>Camera object.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">cam = hump.camera.new(vector(400,300), 2)</code></pre>
<p>If you assigned the module to a variable, you can call the module as a
shortcut to <code>hump.camera.new()</code>:</p>
<pre><code class="lua">camera = require 'hump.camera'
vector = require 'hump.vector'
cam = camera(vector(400,300), 2)</code></pre>
</div>
</div>
<a name="camera-camera:rotate"></a>
<div id="camera-camera:rotate" class="function">
<div class="definition">function <span class="name">camera:rotate</span><span class="arglist">(phi)</span><a class="top" href="#camera">^ top</a></div>
<p>Rotate the camera. Same as <code>cam.rot = cam.rot + phi</code>.</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>phi</code></dt>
<dd>Rotation angle in radians.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">cam:rotate(dt)</code></pre>
</div>
</div>
<a name="camera-camera:translate"></a>
<div id="camera-camera:translate" class="function">
<div class="definition">function <span class="name">camera:translate</span><span class="arglist">(direction)</span><a class="top" href="#camera">^ top</a></div>
<p>Move the camera. Same as <code>cam.pos = cam.pos + direction</code></p>
<div class="arguments">Parameters:
<dl>
<dt><a href="#vector">vector</a> <code>direction</code></dt>
<dd>Direction to move the camera.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">cam:translate(vector(100,0) * dt)</code></pre>
</div>
</div>
<a name="camera-camera:predraw"></a>
<div id="camera-camera:predraw" class="function">
<div class="definition">function <span class="name">camera:predraw</span><span class="arglist">()</span><a class="top" href="#camera">^ top</a></div>
<p>Apply camera transformations, i.e. move, scale and rotate everything so that you see
what you would see when looking through the camera.</p>
<p>Everything until the next <code>camera:postdraw()</code> will be transformed.</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">function love.draw()
cam:predraw()
draw_world()
cam:postdraw()
draw_hud()
end</code></pre>
</div>
</div>
<a name="camera-camera:postdraw"></a>
<div id="camera-camera:postdraw" class="function">
<div class="definition">function <span class="name">camera:postdraw</span><span class="arglist">()</span><a class="top" href="#camera">^ top</a></div>
<p>Revert camera transformations done by <code>camera:predraw()</code>.</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">function love.draw()
cam:predraw()
draw_world()
cam:postdraw()
draw_hud()
end</code></pre>
</div>
</div>
<a name="camera-camera:draw"></a>
<div id="camera-camera:draw" class="function">
<div class="definition">function <span class="name">camera:draw</span><span class="arglist">(func)</span><a class="top" href="#camera">^ top</a></div>
<p>Wrap a function between <code>predraw()</code> and <code>postdraw()</code>:</p>
<pre><code class="lua">cam:predraw()
func()
cam:postdraw()</code></pre>
<div class="arguments">Parameters:
<dl>
<dt>function <code>func</code></dt>
<dd>Drawing function to be wrapped.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">function love.draw()
cam:draw( draw_world )
draw_hud()
end</code></pre>
</div>
</div>
<a name="camera-camera:toCameraCoords"></a>
<div id="camera-camera:toCameraCoords" class="function">
<div class="definition">function <span class="name">camera:toCameraCoords</span><span class="arglist">(pos)</span><a class="top" href="#camera">^ top</a></div>
<p>Convert world coordinates to camera coordinates.</p>
<div class="arguments">Parameters:
<dl>
<dt><a href="#vector">vector</a> <code>pos</code></dt>
<dd>Position in world coordinates.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt><a href="#vector">vector</a></dt>
<dd>Position in camera coordinates.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">screen_pos = cam:toCameraCoords( player.pos )
love.graphics.line(love.mouse.getX(), love.mouse.getY(), screen_pos:unpack()</code></pre>
</div>
</div>
<a name="camera-camera:toWorldCoords"></a>
<div id="camera-camera:toWorldCoords" class="function">
<div class="definition">function <span class="name">camera:toWorldCoords</span><span class="arglist">(pos)</span><a class="top" href="#camera">^ top</a></div>
<p>Convert camera coordinates to world coordinates.</p>
<div class="arguments">Parameters:
<dl>
<dt><a href="#vector">vector</a> <code>pos</code></dt>
<dd>Position in camera coordinates.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt><a href="#vector">vector</a></dt>
<dd>Position in world coordinates.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">target = cam:toWorldCoords( vector(love.mouse.getPosition()) )
unit:plotPathTo(target)</code></pre>
</div>
</div>
<a name="camera-camera:mousepos"></a>
<div id="camera-camera:mousepos" class="function">
<div class="definition">function <span class="name">camera:mousepos</span><span class="arglist">()</span><a class="top" href="#camera">^ top</a></div>
<p>Get mouse position in world coordinates.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt><a href="#vector">vector</a></dt>
<dd>Mouse position in world coordinates.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">target = cam:mousepos()
unit:plotPathTo(target)</code></pre>
</div>
</div>
</div>
<a name="ringbuffer"></a>
<div id="ringbuffer" class="module">
<div class="name">hump.ringbuffer<a class="top" href="#top">^ top</a></div>
<div class="preamble">
<pre><code class="lua">require "hump.ringbuffer"</code></pre>
<p>A ring-buffer is a circular array. That means it does not have a first nor a
last, but only a <em>selected/current</em> element.</p>
<p>You can use this to implement <em>Tomb Raider</em> style inventories, looping
playlists, recurring dialogs (like a unit's answers when selecting it multiple times in <em>Warcraft</em>)
and generally everything that has a circular or looping structure.</p>
</div>
<div class="overview">
<h3>Module overview</h3>
<dl>
<dt><a href="#ringbuffer-new">new()</a></dt>
<dd>Create new ring-buffer.</dd>
<dt><a href="#ringbuffer-ringbuffer:insert">ringbuffer:insert()</a></dt>
<dd>Insert element.</dd>
<dt><a href="#ringbuffer-ringbuffer:remove">ringbuffer:remove()</a></dt>
<dd>Remove currently selected item.</dd>
<dt><a href="#ringbuffer-ringbuffer:removeAt">ringbuffer:removeAt()</a></dt>
<dd>Remove an item.</dd>
<dt><a href="#ringbuffer-ringbuffer:next">ringbuffer:next()</a></dt>
<dd>Select next item.</dd>
<dt><a href="#ringbuffer-ringbuffer:prev">ringbuffer:prev()</a></dt>
<dd>Select previous item.</dd>
<dt><a href="#ringbuffer-ringbuffer:get">ringbuffer:get()</a></dt>
<dd>Get currently selected item.</dd>
<dt><a href="#ringbuffer-ringbuffer:size">ringbuffer:size()</a></dt>
<dd>Get ringbuffer size.</dd>
</dl>
</div>
<a name="ringbuffer-new"></a>
<div id="ringbuffer-new" class="function">
<div class="definition">function <span class="name">new</span><span class="arglist">(...)</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Create new ringbuffer.</p>
<div class="arguments">Parameters:
<dl>
<dt>mixed <code>...</code></dt>
<dd>Initial elements.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Ringbuffer</dt>
<dd>Ringbuffer object.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = hump.ringbuffer.new(1,2,3)</code></pre>
<p>As with <a href="#vector">vector</a>, <a href="#camera">camera</a> and <a href="#class">class</a>,
you can call the module as shortcut to <code>new()</code>:</p>
<pre><code class="lua">ringbuffer = require 'hump.ringbuffer'
rb = ringbuffer(1,2,3)</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:insert"></a>
<div id="ringbuffer-ringbuffer:insert" class="function">
<div class="definition">function <span class="name">ringbuffer:insert</span><span class="arglist">(item, ...)</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Insert items behind current element.</p>
<div class="arguments">Parameters:
<dl>
<dt>mixed <code>item, ...</code></dt>
<dd>Items to insert.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>Nothing</dt>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,5,6) -- content: 1,5,6
rb:insert(2,3,4) -- content: 1,2,3,4,5,6</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:remove"></a>
<div id="ringbuffer-ringbuffer:remove" class="function">
<div class="definition">function <span class="name">ringbuffer:remove</span><span class="arglist">()</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Remove currently selected item and select next item.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>The item removed.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3,4) -- content: 1,2,3,4
rb:next() -- content: 2,3,4,1
rb:remove() -- content: 3,4,1</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:removeAt"></a>
<div id="ringbuffer-ringbuffer:removeAt" class="function">
<div class="definition">function <span class="name">ringbuffer:removeAt</span><span class="arglist">(pos)</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Remove item at position relative to current item.</p>
</p>
<div class="arguments">Parameters:
<dl>
<dt>number <code>pos</code></dt>
<dd>Position to remove.</dd>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>The item removed.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3,4,5) -- content: 1,2,3,4,5
rb:removeAt(2) -- content: 1,2,4,5
rb:removeAt(-1) -- content: 1,2,4</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:next"></a>
<div id="ringbuffer-ringbuffer:next" class="function">
<div class="definition">function <span class="name">ringbuffer:next</span><span class="arglist">()</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Select and return next item.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>The next item.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3)
print(rb:next()) -- prints '2'
print(rb:next()) -- prints '3'
print(rb:next()) -- prints '1'</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:prev"></a>
<div id="ringbuffer-ringbuffer:prev" class="function">
<div class="definition">function <span class="name">ringbuffer:prev</span><span class="arglist">()</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Select and return previous item.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>The previous item</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3)
print(rb:prev()) -- prints '3'
print(rb:prev()) -- prints '2'
print(rb:prev()) -- prints '1'</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:get"></a>
<div id="ringbuffer-ringbuffer:get" class="function">
<div class="definition">function <span class="name">ringbuffer:get</span><span class="arglist">()</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Get currently selected item.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>mixed</dt>
<dd>Currently selected item.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3)
rb:next()
print(rb:get()) -- prints '2'
</code></pre>
</div>
</div>
<a name="ringbuffer-ringbuffer:size"></a>
<div id="ringbuffer-ringbuffer:size" class="function">
<div class="definition">function <span class="name">ringbuffer:size</span><span class="arglist">()</span><a class="top" href="#ringbuffer">^ top</a></div>
<p>Get number of items in the buffer.</p>
<div class="arguments">Parameters:
<dl>
<dt>None</dt>
</dl>
</div>
<div class="returns">Returns:
<dl>
<dt>number</dt>
<dd>Number of items in the buffer.</dd>
</dl>
</div>
<div class="example">Example:
<pre><code class="lua">rb = ringbuffer(1,2,3)
print(rb:size()) -- prints '3'
rb:remove()
print(rb:size()) -- prints '2'</code></pre>
</div>
</div>
</div>
</div>
</body>