hump/index.html

1653 lines
59 KiB
HTML
Raw Normal View History

2010-08-12 13:08:09 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2011-01-20 19:32:18 +00:00
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2010-08-12 13:08:09 +00:00
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
2010-08-13 10:02:47 +00:00
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>hump - L&Ouml;VE Helper Utilities for More Productivity</title>
2011-01-20 19:32:18 +00:00
<link rel="stylesheet" type="text/css" href="style.css" />
2010-11-13 15:48:05 +00:00
<link rel="stylesheet" type="text/css" href="highlight.css" />
<script type="text/javascript" src="highlight.pack.js"></script>
2010-11-13 15:48:05 +00:00
<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>
2011-01-20 19:32:18 +00:00
</style>
2010-08-12 13:08:09 +00:00
</head>
2011-01-20 19:32:18 +00:00
<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>
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
<div id="header">
<h1>hump <span class="small">Helper Utilities for More Productivity</span></h1>
<h2>Documentation</h2>
2010-10-17 14:03:13 +00:00
</div>
2011-01-20 19:32:18 +00:00
<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>
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
<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>
2010-10-17 14:03:13 +00:00
</div>
2011-01-20 19:32:18 +00:00
</div>
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<dt><a href="#gamestate-focus">focus()</a></dt>
<dd>Inform current gamestate of a focus event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-keypressed">keypressed()</a></dt>
2011-01-23 18:47:29 +00:00
<dd>Inform current gamestate of a keypressed event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-keyreleased">keyreleased()</a></dt>
2011-01-23 18:47:29 +00:00
<dd>Inform current gamestate of a keyreleased event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-mousepressed">mousepressed()</a></dt>
2011-01-23 18:47:29 +00:00
<dd>Inform current gamestate of a mousepressed event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-mousereleased">mousereleased()</a></dt>
2011-01-23 18:47:29 +00:00
<dd>Inform current gamestate of a mousereleased event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-joystickpressed">joystickpressed()</a></dt>
2011-01-23 18:47:29 +00:00
<dd>Inform current gamestate of a joystickpressed event.</dd>
2011-01-20 19:32:18 +00:00
<dt><a href="#gamestate-joystickreleased">joystickreleased()</a></dt>
<dd>Inform current gamestate of a joystickreleased event.</dd>
2011-01-23 18:47:29 +00:00
<dt><a href="#gamestate-quit">quit()</a></dt>
<dd>Inform current gamestate of a quit event.</dd>
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<dt>focus()</dt>
<dd>Called if the window gets or looses focus.</dd>
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<dt>quit()</dt>
<dd>Called on quitting the game. Only called on the active gamestate.</dd>
2011-01-20 19:32:18 +00:00
</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
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
function menu:leave()
Buttons.cleanup()
end
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
function menu:update(dt)
Buttons.update(dt)
end
2010-10-18 11:02:45 +00:00
2011-01-20 19:32:18 +00:00
function menu:draw()
love.graphics.draw(self.background, 0, 0)
Buttons.draw()
end
2010-10-17 14:03:13 +00:00
2011-01-20 19:32:18 +00:00
function menu:keyreleased(key)
if key == 'up' then
Buttons.selectPrevious()
elseif key == 'down' then
Buttons.selectNext()
elseif
Buttons.active:onClick()
end
2010-10-17 14:24:27 +00:00
end
2011-01-20 19:32:18 +00:00
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>
2011-01-23 18:47:29 +00:00
<a name="gamestate-focus"></a>
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<a name="gamestate-quit"></a>
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<div class="definition">function <span class="name">focus</span><span class="arglist">(...)</span></div>
2011-01-20 19:32:18 +00:00
<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>
2011-01-23 18:47:29 +00:00
<div class="definition">function <span class="name">quit</span><span class="arglist">(...)</span></div>
2011-01-20 19:32:18 +00:00
<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)
2010-10-17 14:24:27 +00:00
end
function love.draw()
2011-01-20 19:32:18 +00:00
local mx,my = love.mouse.getPosition()
Gamestate.draw(mx, my)
end
2010-10-17 14:24:27 +00:00
2011-01-20 19:32:18 +00:00
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>
<p>This is achieved by overwriting the love callbacks, e.g.:</p>
<pre><code class="lua">local _update = love.update
function love.update(dt)
_update(dt)
Gamestate.current:update(dt)
end</code></pre>
2011-01-20 19:32:18 +00:00
<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>
2010-10-17 14:24:27 +00:00
</div>
2011-01-20 19:32:18 +00:00
</div>
2010-10-17 14:24:27 +00:00
2011-01-20 19:32:18 +00:00
<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:
2011-01-21 11:17:54 +00:00
<pre><code class="lua">fader = Timer.Interpolator(5, function(frac, r,g,b)
2011-01-20 19:32:18 +00:00
love.graphics.setBackgroundColor(frac*r,frac*g,frac*b)
2010-11-13 15:48:05 +00:00
end)
2011-01-20 19:32:18 +00:00
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:
2011-01-21 11:17:54 +00:00
<pre><code class="lua">mover = Timer.Oscillator(10, function(frac)
2011-01-20 19:32:18 +00:00
return 400 + 300 * math.sin(2*math.pi*frac)
2010-11-13 15:48:05 +00:00
end)
2011-01-20 19:32:18 +00:00
local xpos = 100
2010-10-17 14:24:27 +00:00
function love.update(dt)
2011-01-20 19:32:18 +00:00
xpos = mover(dt)
2010-10-17 14:24:27 +00:00
end
function love.draw()
2011-01-20 19:32:18 +00:00
love.graphics.circle('fill', xpos, 300, 80, 36)
2010-11-13 15:48:05 +00:00
end</code></pre>
2011-01-20 19:32:18 +00:00
</div>
2010-08-13 10:02:47 +00:00
</div>
2011-01-20 19:32:18 +00:00
</div>
2010-08-17 12:35:29 +00:00
2011-01-20 19:32:18 +00:00
<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>
2010-08-17 17:11:21 +00:00
</div>
2010-08-17 12:35:29 +00:00
2011-01-20 19:32:18 +00:00
<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>
2011-01-20 19:32:18 +00:00
</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:
2011-01-21 13:05:00 +00:00
<pre><code class="lua">parallelogram_area = a:cross(b)</code></pre>
2011-01-20 19:32:18 +00:00
</div>
2010-08-17 17:11:21 +00:00
</div>
2010-09-30 09:55:30 +00:00
2010-08-13 10:02:47 +00:00
</div>
2011-01-20 19:32:18 +00:00
<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
2010-08-12 15:43:47 +00:00
end)
2011-01-20 19:32:18 +00:00
-- define class method
2010-08-12 15:43:47 +00:00
function Feline:stats()
2011-01-20 19:32:18 +00:00
return string.format("size: %.02f, weight %.02f", self.size, self.weight)
2010-08-12 15:43:47 +00:00
end
2011-01-20 19:32:18 +00:00
-- create two objects
2010-08-12 15:43:47 +00:00
garfield = Feline(.7, 45)
felix = Feline(.8, 12)
2011-01-20 19:32:18 +00:00
print("Garfield: " .. garfield:stats(), "Felix: " .. felix:stats())</code></pre>
<pre><code class="lua">Class = require 'hump.class'
2010-08-12 15:43:47 +00:00
2011-01-20 19:32:18 +00:00
-- define named class
Feline = Class{name = "Feline", function(self, size, weight)
self.size = size
self.weight = weight
2010-08-12 15:43:47 +00:00
end}
2011-01-20 19:32:18 +00:00
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}
2010-08-13 10:02:47 +00:00
2011-01-20 19:32:18 +00:00
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
2010-08-12 15:43:47 +00:00
end}
function Feline:stats()
2011-01-20 19:32:18 +00:00
return string.format("size: %.02f, weight %.02f", self.size, self.weight)
2010-08-12 15:43:47 +00:00
end
function Feline:speak() print("meow") end
Cat = Class{name = "Cat", function(self, name, size, weight)
2011-01-20 19:32:18 +00:00
Feline.construct(self, size, weight)
self.name = name
2010-08-12 15:43:47 +00:00
end}
2011-01-20 19:32:18 +00:00
Cat:inherit(Feline)
2010-08-12 15:43:47 +00:00
function Cat:stats()
2011-01-20 19:32:18 +00:00
return string.format("name: %s, %s", self.name, Feline.stats(self))
2010-08-12 15:43:47 +00:00
end
2011-01-20 19:32:18 +00:00
Tiger = Class{name = "Tiger", function(self, size, weight)
Feline.construct(self, size, weight)
2010-08-12 15:43:47 +00:00
end}
2010-11-13 16:04:38 +00:00
Tiger:Inherit(Feline)
2010-08-12 15:43:47 +00:00
function Tiger:speak() print("ROAR!") end
felix = Cat("Felix", .8, 12)
hobbes = Tiger(2.2, 68)
2010-11-13 16:04:38 +00:00
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>
2011-01-20 19:32:18 +00:00
</div>
2010-08-13 10:02:47 +00:00
</div>
2011-01-20 19:32:18 +00:00
<div class="caveats">
<h3>Caveats</h3>
2010-08-13 10:02:47 +00:00
<p>Be careful when using metamethods like <code>__add</code> or <code>__mul</code>:
2011-01-20 19:32:18 +00:00
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)
2010-08-12 15:43:47 +00:00
function A:__add(other) return A(self.x + other.x) end
2010-11-13 15:48:05 +00:00
function A:show() print("A:", self.x) end
2010-08-12 15:43:47 +00:00
B = Class(function(self, x, y) A.construct(self, x) self.y = y end)
2010-11-13 16:04:38 +00:00
B:Inherit(A)
2010-11-13 15:48:05 +00:00
function B:show() print("B:", self.x, self.y) end
2010-08-12 15:43:47 +00:00
function B:foo() print("foo") end
one, two = B(1,2), B(3,4)
result = one + two
2010-11-13 15:48:05 +00:00
result:show() -- prints "A: 4"
result:foo() -- error: method does not exist</code></pre>
2010-08-13 10:02:47 +00:00
</div>
</div>
2011-01-20 19:32:18 +00:00
<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>
2011-01-21 13:05:00 +00:00
<p>Camera abstraction for L&Ouml;VE. A camera &quot;looks&quot; at a position and can be moved,
2011-01-20 19:32:18 +00:00
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>
2011-01-21 13:05:00 +00:00
<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>
2011-01-20 19:32:18 +00:00
<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:
2011-01-21 13:05:00 +00:00
<pre><code class="lua">screen_pos = cam:toCameraCoords( player.pos )
love.graphics.line(love.mouse.getX(), love.mouse.getY(), screen_pos:unpack()</code></pre>
2011-01-20 19:32:18 +00:00
</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>
2010-08-17 17:11:21 +00:00
</div>
</div>
2011-01-20 19:32:18 +00:00
<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
2011-01-21 13:05:00 +00:00
last, but only a <em>selected/current</em> element.</p>
2011-01-20 19:32:18 +00:00
<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>
2010-08-17 19:49:30 +00:00
</div>
</div>
2010-08-13 10:02:47 +00:00
</div>
2010-08-12 13:08:09 +00:00
</body>