hump/index.html

1071 lines
52 KiB
HTML
Raw Normal View History

2010-08-12 13:08:09 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2010-08-12 15:43:47 +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>
<style type="text/css">
body {
margin-top: 1.0em;
background-color: #f7fcfc;
font-family: "Helvetica,Arial,FreeSans";
color: #000000;
}
#container {
margin: 0 auto;
width: 800px;
}
h1 { font-size: 3.8em; color: #080303; margin-bottom: 10px; border-bottom: 2px solid #080303; }
h1 .small { font-size: 0.4em; }
h1 a { text-decoration: none }
h2 { font-size: 1.5em; color: #080303; border-top: 1px solid #a0a0a0; }
h3 { color: #080303; margin-bottom: 1em; background: #eee; margin-top: 3em; }
h4 { color: #080303; margin-top: 2em; }
a { color: #080303; }
.description { font-size: 1.2em; margin-bottom: 30px; margin-top: 30px; font-style: italic;}
.download { float: right; }
pre { background: #000; color: #fff; padding: 15px;}
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
.footer { text-align:center; padding-top:30px; font-style: italic; }
ul.nav { display:inline; margin-left: 0; padding-left: 0; }
ul.nav li { display:inline; margin-right: 1em; }
a.top { font-size: 8pt; margin-left: 1em; float: right; }
a.source { border-radius: 5px; -moz-border-radius: 5px; font-weight:bold; float:right; border:1px solid #777; padding: 2px 4px 2px 4px; background: #e4e4e4; text-decoration:none; font-size: 9pt;}
a.source:hover { background: #eee; border-color: #aaa; color: #555;}
2010-08-13 10:02:47 +00:00
.doc { padding-left: 1em; margin-bottom: 1em; border-left: 1px dotted #aaa; }
.function { display:block; font-weight:bold; font-family: fixed; background: #f9f9f9; border:1px solid #ddd; margin-left: -1em; }
.functionlist { border-left: 1px dotted #aaa; margin: -.6em 0 0 2em; }
.functionlist a { text-decoration: none; font-weight:bold; color: #506060; }
.functionlist a:hover { color: black; }
2010-10-16 15:28:13 +00:00
.functionlist td em { font-size: .8em; font-weight:bold;width:7em; display:inline-block; }
2010-08-13 10:02:47 +00:00
2010-08-17 19:49:30 +00:00
td, th { vertical-align: top; }
2010-08-13 10:02:47 +00:00
.doc table { margin-top: 5px; }
2010-08-17 19:49:30 +00:00
td, th { border-top: 1px dashed #ddd; }
2010-08-13 10:02:47 +00:00
th { text-align: left; font-weight: normal; font-style: italic; text-decoration: underline; }
2010-08-17 19:49:30 +00:00
td { padding-left: 1em; margin: 0; }
td.p { text-align: right; }
2010-08-13 10:02:47 +00:00
.doc td em { font-size: .8em; font-weight:bold; font-style:normal; padding-right: .6em;}
.doc pre, code { color: black; padding: 0; font-family: fixed; border: 1px solid #ddd; background: #e7ecec; }
</style>
2010-08-12 13:08:09 +00:00
</head>
<body>
2010-08-13 10:02:47 +00:00
<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>
<a name="top"></a>
<div id="container">
<h1><a href="http://github.com/vrld/hump">hump</a>
<span class="small"> Helper Utilities for More Productivity</span></h1>
<ul class="nav">
<li><a href="#intro">Introduction</a></li>
<li><a href="#doc">Documenation</a></li>
<li><a href="#license">License</a></li>
<li><a href="#download">Download</a></li>
</ul>
<a name="intro"></a>
<h2>Introduction<a class="top" href="#top">^ top</a></h2>
<p><em>Helper Utilities for a Multitude of Problems</em> is a set of lightweight helpers
for the excellent <a href="http://love2d.org/">L&Ouml;VE</a> Engine.</p>
<p>It currently features
<ul>
2010-10-17 14:03:13 +00:00
<li><em>gamestate.lua</em>: a gamestate system.</li>
<li><em>timer.lua</em>: timed function calling and interpolating functions,</li>
2010-08-13 10:02:47 +00:00
<li><em>vector.lua</em>: a mature vector type,</li>
<li><em>class.lua</em>: a simple and easy class system with function inheritance,</li>
<li><em>camera.lua</em>: a translate-, zoom- and rotatable camera and</li>
2010-08-17 17:11:21 +00:00
<li><em>ringbuffer.lua</em>: a circular container.</li>
2010-08-17 19:49:30 +00:00
<li><em>sequence.lua</em>: utility to handle ingame cutscenes and such.</li>
2010-08-13 10:02:47 +00:00
</ul></p>
<p><em>hump</em> differs from other libraries in that every component is independent of the
2010-08-17 19:49:30 +00:00
remaining ones (apart from camera.lua, which depends on the vector type, and sequence.lua which requires ringbuffer.lua).
2010-08-13 10:02:47 +00:00
<em>hump</em>'s footprint is very small and thus should fit nicely into your projects.</p>
<a name="doc"></a>
<h2>Documentation<a class="top" href="#top">^ top</a></h2>
Below is the documentation of the various modules. You can directly jump to a
module by clicking these:
<ul>
2010-10-17 14:03:13 +00:00
<li><a href="#gamestate.lua">gamestate.lua</a></li>
<li><a href="#timer.lua">timer.lua</a></li>
2010-08-13 10:02:47 +00:00
<li><a href="#vector.lua">vector.lua</a></li>
<li><a href="#class.lua">class.lua</a></li>
<li><a href="#camera.lua">camera.lua</a></li>
2010-08-17 17:11:21 +00:00
<li><a href="#ringbuffer.lua">ringbuffer.lua</a></li>
2010-08-17 19:49:30 +00:00
<li><a href="#sequence.lua">sequence.lua</a></li>
2010-08-13 10:02:47 +00:00
</ul>
2010-10-17 14:03:13 +00:00
<div class="module"><a name="gamestate.lua"></a>
<h3>gamestate.lua<a class="top" href="#doc">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/gamestate.lua" class="source">view source</a>
<p>Useful to separate different states of your game (hence "gamestate") like title screens,
level loading, main game, etc. Each gamestate can have it's own <code>update()</code>, <code>draw()</code>,
<code>keyreleased()</code>, <code>keypressed()</code> and <code>mousereleased()</code> which correspond
to the ones defined in love. See the <a href="#gamestate.lua-callbackList">callback list</a> for more details.</p>
<p>Additionally, each gamestate can define a <code>enter(pre, ...)</code> and <code>leave()</code> function,
which are called when using <code>Gamestate.switch(to, ...)</code>. <em>Do not attempt to use these functions
yourself to initialize the first gamestate,</em> use the <code>switch</code> function instead.</p>
<p>The module defines the following functions:
<table class="functionlist">
<tr><td><a href="#gamestate.lua-new">Gamestate.new()</a></td><td>New gamestate</td></tr>
<tr><td><a href="#gamestate.lua-switch">Gamestate.switch(to, ...)</a></td><td>Switch gamestate</td></tr>
<tr><td><a href="#gamestate.lua-update">Gamestate.update(dt)</a></td><td>Call update callback</td></tr>
<tr><td><a href="#gamestate.lua-draw">Gamestate.draw()</a></td><td>Call draw callback</td></tr>
<tr><td><a href="#gamestate.lua-keypressed">Gamestate.keypressed(key, unicode)</a></td><td>Call keypressed callback</td></tr>
<tr><td><a href="#gamestate.lua-keyreleased">Gamestate.keyreleased(key)</a></td><td>Call keyreleased callback</td></tr>
<tr><td><a href="#gamestate.lua-mousereleased">Gamestate.mousereleased(x,y,btn)</a></td><td>Call mousereleased callback</td></tr>
<tr><td><a href="#gamestate.lua-registerEvents">Gamestate.registerEvents()</a></td><td>Register all callbacks</td></tr>
</table></p>
<a name="gamestate.lua-new"></a>
<div class="doc"><span class="function">function Gamestate.new()<a class="top" href="#gamestate.lua">^ top</a></span>
Create a new gamestate.
<table><tr><th>Returns:</th><td>The new (but empty) gamestate object.</td></tr></table>
</div>
<a name="gamestate.lua-switch"></a>
<div class="doc"><span class="function">function Gamestate.switch(to, ...)<a class="top" href="#gamestate.lua">^ top</a></span>
Switch the gamestate.
<p>Calls <code>leave()</code> on the currently active gamestate.</p>
<p>Calls <code>enter(current, ...)</code> on the target gamestate, where <code>current</code>
is the gamestate before the switch and <code>...</code> are the additionals arguments given
to <code>Gamestate.switch</code>.</p>
<table>
<tr><th>Parameters:</th><td class="p"><em>[gamestate]</em><code>to</code>:</td><td>target gamestate.</td></tr>
<tr><td> </td><td class="p"><code>...</code>:</td><td>additional arguments to pass</td></tr>
<tr><th>Returns:</th><td colspan="2">the result of <code>to:enter(current, ...)</code></td></tr>
</table>
</div>
<a name="gamestate.lua-update"></a>
<div class="doc"><span class="function">function Gamestate.update(dt)<a class="top" href="#gamestate.lua">^ top</a></span>
Calls <code>update(dt)</code> on current gamestate.
</div>
<a name="gamestate.lua-draw"></a>
<div class="doc"><span class="function">function Gamestate.draw()<a class="top" href="#gamestate.lua">^ top</a></span>
Calls <code>draw()</code> on current gamestate.
</div>
<a name="gamestate.lua-keypressed"></a>
<div class="doc"><span class="function">function Gamestate.keypressed(key, unicode)<a class="top" href="#gamestate.lua">^ top</a></span>
Calls <code>keypressed(key, unicode)</code> on current gamestate.
</div>
<a name="gamestate.lua-keyreleased"></a>
<div class="doc"><span class="function">function Gamestate.keyreleased(key)<a class="top" href="#gamestate.lua">^ top</a></span>
Calls <code>keyreleased(key)</code> on current gamestate.
</div>
<a name="gamestate.lua-mousereleased"></a>
<div class="doc"><span class="function">function Gamestate.mousereleased(x,y,btn)<a class="top" href="#gamestate.lua">^ top</a></span>
Calls <code>mousereleased(x,y,btn)</code> on the current gamestate.
</div>
<a name="gamestate.lua-registerEvents"></a>
<div class="doc"><span class="function">function Gamestate.registerEvents()<a class="top" href="#gamestate.lua">^ top</a></span>
Registers all above events so you don't need to call then in your <code>love.*</code> routines.
<p>It is an error to call this anywhere else than <code>love.load()</code>, since it overwrites the
callbacks. Don't worry though, your callbacks will still be executed.</p>
</div>
<a name="gamestate.lua-callbackList"></a>
<h4>List of callbacks<a class="top" href="#gamestate.lua">^ top</a></h4>
<table class="functionlist">
<tr><td><code>enter(previous, ...)</code></td>
<td>Gets called upon entering the state. <strong>Don't call this yourself, use
<code>Gamestate.switch</code> instead.</strong></td></tr>
<tr><td><code>leave()</code></td>
<td>Gets called upon leaving the state. The same warning as with <code>enter</code> applies.</td></tr>
<tr><td><code>update(dt)</code></td>
<td>Manually called by <code>Gamestate.update</code>, or automatically like <code>love.update</code>
when using <code>Gamestate.registerEvents()</code>.</td></tr>
<tr><td><code>draw()</code></td>
<td>Manually called by <code>Gamestate.draw</code>, or automatically like <code>love.draw</code></td></tr>
<tr><td><code>keypressed(key, unicode)</code></td>
<td>Manually called by <code>Gamestate.keypressed</code>, or automatically like <code>love.keypressed</code></td></tr>
<tr><td><code>keyreleased(key)</code></td>
<td>Manually called by <code>Gamestate.keyreleased</code>, or automatically like <code>love.keyreleased</code></td></tr>
<tr><td style="width:200px;"><code>mousereleased(x,y,btn)</code></td>
<td>Manually called by <code>Gamestate.mousereleased</code>, or automatically like <code>love.mousereleased</code></td></tr>
</table>
</div>
<div class="module"><a name="timer.lua"></a>
<h3>timer.lua<a class="top" href="#doc">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/timer.lua" class="source">view source</a>
<p>This module consists of two parts:<ul>
<li><code>Timer</code> to enable delayed function calls and</li>
<li><code>Interpolator</code> for easy abstraction of functions that interpolate something over time.</li>
</ul>All functions are <a href="http://en.wikipedia.org/wiki/Higher-order_function">higher order functions</a>,
which is a concept that may seem confusing at first. The <a href="#timer.lua-Example">example</a> shows how to
use this concept.
</p>
<p>Functions:
<table class="functionlist">
<tr><td><a href="#timer.lua-add">Timer.add(delay, func)</a></td><td>Add a delayed function</td></tr>
<tr><td><a href="#timer.lua-addPeriodic">Timer.addPeriodic(delay, func, count)</a></td><td>Add a periodic function</td></tr>
<tr><td><a href="#timer.lua-clear">Timer.clear()</a></td><td>Clear functions</td></tr>
<tr><td><a href="#timer.lua-update">Timer.update(dt)</a></td><td>Update timer</td></tr>
<tr><td><a href="#timer.lua-Interpolator">Interpolator(length, func)</a></td><td>Create interpolating function</td></tr>
</table></p>
<p>Note the <em>.</em> (dot) in the function names. It is an error to call <code>Timer.add</code> with a colon! If you
get weird errors, that might be the cause.</p>
<p>The <code>Timer</code> stores it's timed functions in the table <code>Timer.functions</code>.
You can manipulate it, but probably shouldn't.</p>
<a name="timer.lua-add"></a>
<div class="doc"><span class="function">function Timer.add(delay, func)<a href="#timer.lua" class="top">^ top</a></span>
Add a timed function. The function will be executed when <code>delay</code> seconds have elapsed.
<p>Note that there is <em>no guarantee</em> that the delay will be exceeded. It is, however, guaranteed that
the function will not be executed before 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> <a href="#timer.lua-Example">(see the example)</a>.
<table>
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>delay</code>:</td>
<td>Time that has to pass before the function is called</td></tr>
<tr><th></th><td class="p"><em>[function]</em><code>func</code>:</td><td>The function to be called</td></tr>
</table>
</div>
<a name="timer.lua-addPeriodic"></a>
<div class="doc"><span class="function">function Timer.addPeriodic(delay, func, count)<a href="#timer.lua" class="top">^ top</a></span>
Add a periodic timed function, i.e. a function that will be called every <code>delay</code> seconds.
<p>The same things as with <code>Timer.add</code> apply.</p>
<table>
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>delay</code>:</td>
<td>Time that has to pass before the function is called</td></tr>
<tr><th></th><td class="p"><em>[function]</em><code>func</code>:</td><td>The function to be called.</td></tr>
<tr><th></th><td class="p"><em>[optional number]</em><code>count</code>:</td>
<td>Number of times the function should be called. If omitted, the function loops indefinitely.</td></tr>
</table>
</div>
<a name="timer.lua-clear"></a>
<div class="doc"><span class="function">function Timer.clear()<a href="#timer.lua" class="top">^ top</a></span>
Clears all timers. That is, it resets the table <code>Timer.functions</code>.
</div>
<a name="timer.lua-update"></a>
<div class="doc"><span class="function">function Timer.update(dt)<a href="#timer.lua" class="top">^ top</a></span>
Update timers and execute functions if the deadline is reached. Use this function in <code>love.update(dt)</code>.
<table>
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>dt</code>:</td>
<td>Time that has passed since the last <code>Timer.update</code>.</td></tr>
</table>
</div>
<a name="timer.lua-Interpolator"></a>
<div class="doc"><span class="function">function Interpolator(length, func)<a href="#timer.lua" class="top">^ top</a></span>
Helper to create interpolating functions, i.e. functions that changes it's behavior depending
on how much time has passed.
<p>The interpolating function <code>func</code> will receive the fraction (in respect to
the supplied <code>length</code>) of time that has passed, but 1.0 at max.</p>
<p><code>Interpolator</code> creates a function that will have to be called in <code>love.update</code>, which
will itself execute <code>func</code> with the right parameters. It returns <code>true</code> as long as
the interpolation is not yet finished or <code>nil</code> if the interpolation stopped.</p>
<table>
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>length</code>:</td><td>Interpolation length.</td></tr>
<tr><th></th><td class="p"><em>[function]</em><code>func</code>:</td><td>Interpolating function.</td></tr>
<tr><th>Returns</th><td colspan="2">A function <code>inter(dt)</code> with argument <code>dt</code> that has
to be called in <code>love.update</code>.</td></tr>
</table>
</div>
<a name="timer.lua-Example"></a>
<h4>Example usage</h4>
<div class="doc"><span class="function">Timer example <a class="top" href="#timer.lua">^ top</a></span>
</div>
<div class="doc"><span class="function">Implementing <code>Timer.addPeriodic()</code> <a class="top" href="#timer.lua">^ top</a></span>
</div>
</div>
<div class="module"><a name="vector.lua"></a>
2010-08-13 10:02:47 +00:00
<h3>vector.lua<a class="top" href="#doc">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/vector.lua" class="source">view source</a>
2010-10-16 15:28:13 +00:00
<p>A vector class implementing nearly everything you want to do with vectors, and some more.</p>
<p>Accessors:
<table class="functionlist">
<tr><td><em>[number]</em> <code>vector.x</code></td><td><code>x</code>-coordinate of the vector</td></tr>
<tr><td><em>[number]</em> <code>vector.y</code></td><td><code>y</code>-coordinate of the vector</td></tr>
</table>
</p>
<p>Function overview (click item for more details):
2010-08-13 10:02:47 +00:00
<table class="functionlist">
<tr><td><a href="#vector.lua-vector">vector(x,y)</a></td><td>constructor</td></tr>
<tr><td><a href="#vector.lua-isvector">isvector(v)</a></td><td>type check</td></tr>
2010-08-17 12:35:29 +00:00
<tr><td><a href="#vector.lua-clone">vector:clone()</a></td><td>deep copy</td></tr>
<tr><td><a href="#vector.lua-unpack">vector:unpack()</a></td><td>element unfolding</td></tr>
2010-08-13 10:02:47 +00:00
<tr><td><a href="#vector.lua-operators">Operators</a></td><td>Arithmetics and ordering</td></tr>
<tr><td><a href="#vector.lua-permul">vector:permul(other)</a></td><td>per-element multiplication</td></tr>
<tr><td><a href="#vector.lua-len">vector:len()</a></td><td>vector length</td></tr>
<tr><td><a href="#vector.lua-len2">vector:len2()</a></td><td>squared vector length</td></tr>
<tr><td><a href="#vector.lua-dist">vector:dist(other)</a></td><td>distance to vector</td></tr>
<tr><td><a href="#vector.lua-normalized">vector:normalized()</a></td><td>normalize vector</td></tr>
<tr><td><a href="#vector.lua-normalize_inplace">vector:normalize_inplace()</a></td><td>normalize vector in-place</td></tr>
2010-08-17 12:35:29 +00:00
<tr><td><a href="#vector.lua-rotated">vector:rotated(phi)</a></td><td>rotate vector</td></tr>
<tr><td><a href="#vector.lua-rotate_inplace">vector:rotate_inplace(phi)</a></td><td>rotate vector in-place</td></tr>
2010-08-17 19:49:30 +00:00
<tr><td><a href="#vector.lua-perpendicular">vector:perpendicular()</a></td><td>quick rotation by 90&deg;</td></tr>
<tr><td><a href="#vector.lua-projectOn">Vecor:projectOn(v)</a></td><td>projection on other vector</td></tr>
2010-09-30 09:55:30 +00:00
<tr><td><a href="#vector.lua-cross">Vecor:cross(v)</a></td><td>cross product of two vectors</td></tr>
2010-08-13 10:02:47 +00:00
</table></p>
<a name="vector.lua-vector"></a>
<div class="doc"><span class="function">function vector(x,y)<a class="top" href="#vector.lua">^ top</a></span>
Creates a new vector. Element access with <code>v.x</code> and <code>v.y</code>.
<table>
2010-08-13 12:42:01 +00:00
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>x</code>:</td><td>x coordinate</td></tr>
<tr><td> </td><td class="p"><em>[number]</em><code>y</code>:</td><td>y coordinate</td></tr>
2010-08-13 10:02:47 +00:00
<tr><th>Returns:</th><td colspan="2">the vector</td></tr>
</table>
</div>
<a name="vector.lua-isvector"></a>
<div class="doc"><span class="function">function isvector(v)<a class="top" href="#vector.lua">^ top</a></span>
Tests for vector type.
<table>
2010-08-13 12:42:01 +00:00
<tr><th>Parameters:</th><td class="p"><code>v</code>:</td><td>variable to test</td></tr>
2010-08-13 10:02:47 +00:00
<tr><th>Returns:</th><td colspan="2"><code>true</code> if <code>v</code> is a vector</td></tr>
</table>
</div>
<a name="vector.lua-clone"></a>
<div class="doc"><span class="function">function vector:clone()<a class="top" href="#vector.lua">^ top</a></span>
Clones a vector. Use when you do not want to create references.
<table>
<tr><th>Returns:</th><td colspan="2">New vector with the same coordinates.</td></tr>
</table>
</div>
<a name="vector.lua-unpack"></a>
<div class="doc"><span class="function">function vector:unpack()<a class="top" href="#vector.lua">^ top</a></span>
Unpacks the vector.
<table>
<tr><th>Returns:</th><td colspan="2">the coordinate tuple <code>x, y</code></td></tr>
<tr><th colspan="3">Example:</th></tr>
<tr><td colspan="3"><pre>v = vector(1,2)
2010-08-12 15:43:47 +00:00
print(v:unpack()) -- prints "1 2"</pre></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
<a name="vector.lua-operators"></a>
<div class="doc"><span class="function">Operators<a class="top" href="#vector.lua">^ top</a></span>
<p>Arithmetic (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>) and
comparative operators (<code>==</code>, <code>&lt;=</code>, <code>&lt;</code>) are defined:</p>
<ul>
<li><code>+</code> and <code>-</code> <em>only</em> work on vectors. <code>-</code> is also
the unary minus (e.g. <code>print(-vector(1,2)) -- prints (-1,-2)</code>)</li>
<li><code>a * b</code> works on vectors and numbers:<ul>
<li>If <code>a</code> is a number and <code>b</code> is a vector (or vice versa),
the result the scalar multiplication.</li>
<li>If <code>a</code> and <code>b</code> both are vectors, then the result is the
<em>dot product</em>.</li></ul></li>
<li><code>a / b</code> is only defined for <code>a</code> being a vector and <code>b</code>
being a number. Same as <code>a * 1/b</code>.</li>
</ul>
<p><code>&lt;=</code> and <code>&lt;</code> sort lexically, i.e. <code>a &lt; b</code>
is true if it holds: <code>a.x &lt; b.x</code> or <code>a.y &lt; b.y</code> if
<code>a.x == b.x</code></p>
</div>
<a name="vector.lua-permul"></a>
<div class="doc"><span class="function">function vector:permul(other)<a class="top" href="#vector.lua">^ top</a></span>
2010-08-17 12:35:29 +00:00
Perform element-wise multiplication, e.g. <code>(2,3) x (3,4) = (6,12)</code>
2010-08-13 10:02:47 +00:00
</div>
<a name="vector.lua-len"></a>
<div class="doc"><span class="function">function vector:len()<a class="top" href="#vector.lua">^ top</a></span>
Get length of vector.
</div>
<a name="vector.lua-len2"></a>
<div class="doc"><span class="function">function vector:len2()<a class="top" href="#vector.lua">^ top</a></span>
Get squared length.
</div>
<a name="vector.lua-dist"></a>
<div class="doc"><span class="function">function vector:dist(other)<a class="top" href="#vector.lua">^ top</a></span>
Get distance to other vector.
<table><tr><th>Example:</th></tr>
<tr><td><pre>a,b = vector(0,1), vector(1,0)
2010-08-12 15:43:47 +00:00
print(a:dist(b)) -- prints 1.4142135623731</pre></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
<a name="vector.lua-normalized"></a>
<div class="doc"><span class="function">function vector:normalized()<a class="top" href="#vector.lua">^ top</a></span>
Get normalized vector. The original vector remains unchanged.
</div>
<a name="vector.lua-normalize_inplace"></a>
<div class="doc"><span class="function">function vector:normalize_inplace()<a class="top" href="#vector.lua">^ top</a></span>
Normalize vector and return it.
<p><strong>Warning:</strong> This will change the state of all references to this vector.</p>
</div>
<a name="vector.lua-rotated"></a>
<div class="doc"><span class="function">function vector:rotated(phi)<a class="top" href="#vector.lua">^ top</a></span>
Get rotated vector. The original vector remains unchanged.
<table>
2010-08-13 12:42:01 +00:00
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>phi</code>:</td><td>Rotation angle in radians.</td></tr>
2010-08-17 12:35:29 +00:00
<tr><th>Sketch:</th><td colspan="3"><img src="vector-rotated.png" alt="Rotated vector" /></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
<a name="vector.lua-rotate_inplace"></a>
<div class="doc"><span class="function">function vector:rotate_inplace(phi)<a class="top" href="#vector.lua">^ top</a></span>
Rotate the vector and return it.
<p><strong>Warning:</strong> This will change the state of all references to this vector.</p>
</div>
2010-08-17 12:35:29 +00:00
2010-08-17 17:11:21 +00:00
<a name="vector.lua-perpendicular"></a>
<div class="doc"><span class="function">function vector:perpendicular()<a href="#vector.lua" class="top">^ top</a></span>
Get vector rotated by 90&deg; clockwise, e.g. the vector <code>(0,1)</code> results in <code>(1,0)</code>.
<table>
<tr><th>Sketch:</th><td><img src="vector-perpendicular.png" alt="Perpendicular vector" /></td></tr>
</table>
</div>
2010-08-17 12:35:29 +00:00
2010-08-17 17:11:21 +00:00
<a name="vector.lua-projectOn"></a>
<div class="doc"><span class="function">function vector:projectOn(v)<a href="#vector.lua" class="top">^ top</a></span>
Project this vector onto another one, e.g. <code>(2,2)</code> projected onto <code>(1,0)</code> results in <code>(2,0)</code>.
<table>
<tr><th>Parameters:</th><td class="p"><em>[vector]</em><code>v</code></td><td>Vector to project onto</td></tr>
<tr><th>Returns:</th><td colspan="2">Vector with direction of <code>v</code> and length according to projection.</td></tr>
<tr><th>Sketch:</th><td colspan="2"><img src="vector-projectOn.png" alt="Projected vector" /></td></tr>
</table>
</div>
2010-09-30 09:55:30 +00:00
<a name="vector.lua-cross"></a>
<div class="doc"><span class="function">function vector:cross(v)<a href="#vector.lua" class="top">^ top</a></span>
Calculates cross product between two vectors, i.e.:
<p>Treats both vectors as vectors of the xy-plane <em>(z-coordinate =0)</em> and calculates a vector perpendicular
on them. Because the result is perpendicular on to the input vectors, it has the form <em>(0,0,z)</em>.
The z-coordinate is returned.</p>
<table>
<tr><th>Parameters:</th><td class="p"><em>[vector]</em><code>v</code></td><td>Vector to calculate the cross product with</td></tr>
<tr><th>Returns:</th><td colspan="2">Cross product as described above</td></tr>
</table>
</div>
2010-08-13 10:02:47 +00:00
</div>
2010-10-17 14:03:13 +00:00
<div class="module"><a name="class.lua"></a>
2010-08-13 10:02:47 +00:00
<h3>class.lua<a class="top" href="#doc">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/class.lua" class="source">view source</a>
2010-08-13 10:02:47 +00:00
<p>class.lua provides a simple and easy to use class system. Albeit it's
limitations, class.lua offers multiple inheritance and minimal
reflection capabilities. Note that only methods will be inherited
and runtime changes of a superclass won't affect subclasses.</p>
<p>The module only consists of three functions:
<table class="functionlist">
<tr><td><a href="#class.lua-Class">Class(constructor)</a></td><td>define class</td></tr>
<tr><td><a href="#class.lua-Class2">Class{name = name, constructor}</a></td><td>define named class</td></tr>
<tr><td><a href="#class.lua-Interface">Interface(name)</a></td><td>define interface</td></tr>
<tr><td><a href="#class.lua-Inherit">Inherit(class, super, ...)</a></td><td>subclassing</td></tr>
</table></p>
<p>For an example, <a href="#class.lua-Example">see below</a>. Also be sure to read the
<a href="#class.lua-Warning">warning</a> regarding metamethods.</p>
<a name="class.lua-Class"></a>
<div class="doc"><span class="function">function Class(constructor)<a class="top" href="#class.lua">^ top</a></span>
Creates a new unnamed class.
<table>
2010-08-13 12:42:01 +00:00
<tr><th>Parameters:</th><td class="p" style="width:225px;"><em>[optional function]</em><code>constructor</code></td>
2010-08-13 10:02:47 +00:00
<td>A function used to construct the class. The first parameter of this function
is the object, the others are parameters given upon construction.</td></tr>
<tr><th>Returns:</th><td colspan="2">the new class</td></tr>
<tr><th colspan="3">Example</th></tr>
<tr><td colspan="3"><pre>Feline = Class(function(self, size, weight)
self.size = size self.weight = weight
2010-08-12 15:43:47 +00:00
end)
function Feline:stats()
2010-08-13 10:02:47 +00:00
return string.format("size: %.02f, weight %.02f", self.size, self.weight)
2010-08-12 15:43:47 +00:00
end
garfield = Feline(.7, 45)
felix = Feline(.8, 12)
print("Garfield: " .. garfield:stats(), "Felix: " .. felix:stats())</pre></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
2010-08-12 15:43:47 +00:00
2010-08-13 10:02:47 +00:00
<a name="class.lua-Class2"></a>
<div class="doc"><span class="function">function Class{name = name, constructor}<a class="top" href="#class.lua">^ top</a></span>
Create a named class, i.e. define a <code>__tostring</code> metamethod. Parameters are the same as above.
Great for debugging. Both <code>name</code> and <code>constructor</code> can be omitted (but why would you want to?)
2010-08-12 15:43:47 +00:00
2010-08-13 10:02:47 +00:00
<table><tr><th>Example:</th></tr>
<tr><td><pre>Feline = Class{name = "Feline", function(self, size, weight)
self.size = size self.weight = weight
2010-08-12 15:43:47 +00:00
end}
print(Feline) -- prints 'Feline'</pre></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
<a name="class.lua-Interface"></a>
<div class="doc"><span class="function">function Interface(name)<a class="top" href="#class.lua">^ top</a></span>
Shortcut to <code>Class{name = name}</code>, i.e. a possibly named class without constructor.
</div>
<a name="class.lua-Inherit"></a>
<div class="doc"><span class="function">function Inherit(class, super, ...)<a class="top" href="#class.lua">^ top</a></span>
Add functions of <code>super</code> to <code>class</code>. Multiple interfaces can be defined.
<p><code>super</code>'s constructor can be accessed via super.construct(self). See example below.</p>
</div>
<a name="class.lua-Example"></a>
<h4>Example usage</h4>
<div class="doc"><span class="function">Everybody loves cats!<a class="top" href="#class.lua">^ top</a></span>
2010-08-12 15:43:47 +00:00
<pre>Feline = Class{name = "Feline", function(self, size, weight)
2010-08-13 10:02:47 +00:00
self.size = size self.weight = weight
2010-08-12 15:43:47 +00:00
end}
function Feline:stats()
2010-08-13 10:02:47 +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)
2010-08-13 10:02:47 +00:00
Feline.construct(self, size, weight)
self.name = name
2010-08-12 15:43:47 +00:00
end}
Inherit(Cat, Feline)
function Cat:stats()
2010-08-13 10:02:47 +00:00
return string.format("name: %s, %s", self.name, Feline.stats(self))
2010-08-12 15:43:47 +00:00
end
Tiger = Class{name = "tiger", function(self, size, weight)
2010-08-13 10:02:47 +00:00
Feline.construct(self, size, weight)
2010-08-12 15:43:47 +00:00
end}
Inherit(Tiger, Feline)
function Tiger:speak() print("ROAR!") end
felix = Cat("Felix", .8, 12)
hobbes = Tiger(2.2, 68)
print(felix:stats(), hobbes:stats())
felix:speak()
hobbes:speak()</pre>
2010-08-13 10:02:47 +00:00
</div>
<a name="class.lua-Warning"></a>
<h4>Warning</h4>
<div class="doc"><span class="function">Nothing is perfect<a class="top" href="#class.lua">^ top</a></span>
<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>
2010-08-12 15:43:47 +00:00
<pre>A = Class(function(self, x) self.x = x end)
function A:__add(other) return A(self.x + other.x) end
function A:print() print("A:", self.x) end
B = Class(function(self, x, y) A.construct(self, x) self.y = y end)
Inherit(B, A)
function B:print() 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:print() -- prints "A: 4"
result:foo() -- error: method does not exist</pre>
2010-08-13 10:02:47 +00:00
</div>
</div>
2010-10-17 14:03:13 +00:00
<div class="module"><a name="camera.lua"></a>
2010-08-13 10:02:47 +00:00
<h3>camera.lua<a class="top" href="#doc">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/camera.lua" class="source">view source</a>
2010-08-13 10:02:47 +00:00
<em>Depends on <a href="#vector.lua">vector.lua</a></em>
<p>Camera object to display only a partial region of the game world. The region
can be zoomed and rotated. You can transform camera coordinates to world coordinated
(e.g. get the location of the mouse in the game world). It is possible to have
more than one camera per game.</p>
2010-10-16 15:28:13 +00:00
<p>Accessors:
<table class="functionlist">
<tr><td><em>[vector]</em> <code>camera.pos</code></td><td>The camera's position. This is also the position it looks at.</td></tr>
<tr><td><em>[number]</em> <code>camera.zoom</code></td><td>The camera's zoom level.</td></tr>
<tr><td><em>[number]</em> <code>camera.rot</code></td><td>The camera's rotation.</td></tr>
</table>
</p>
2010-08-13 10:02:47 +00:00
<p>The module defined the following funtions:
<table class="functionlist">
<tr><td><a href="#camera.lua-Camera">Camera(pos, zoom, rotation)</a></td><td>create new camera</td></tr>
<tr><td><a href="#camera.lua-rotate">camera:rotate(phi)</a></td><td>rotate camera</td></tr>
<tr><td><a href="#camera.lua-translate">camera:translate(t)</a></td><td>move camera</td></tr>
<tr><td><a href="#camera.lua-draw">camera:draw(func)</a></td><td>apply camera transformations on function</td></tr>
2010-09-15 09:34:18 +00:00
<tr><td><a href="#camera.lua-predraw">camera:predraw()</a></td><td>apply camera transformations</td></tr>
<tr><td><a href="#camera.lua-postdraw">camera:postdraw()</a></td><td>revert camera transformations</td></tr>
<tr><td><a href="#camera.lua-toCameraCoords">camera:toCameraCoords(p)</a></td><td>get camera coordinates of a point</td></tr>
<tr><td><a href="#camera.lua-toWorldCoords">camera:toWorldCoords(p)</a></td><td>get world coordinates of a point</td></tr>
2010-08-13 10:02:47 +00:00
<tr><td><a href="#camera.lua-mousepos">camera:mousepos()</a></td><td>get world coordinates of mouse</td></tr>
</table></p>
<a name="camera.lua-Camera"></a>
<div class="doc"><span class="function">function Camera(pos, zoom, rotation)<a class="top" href="#camera.lua">^ top</a></span>
2010-08-13 12:42:01 +00:00
Create a new camera with position pos, zoom zoom and rotation rotation.
2010-08-13 10:02:47 +00:00
<table>
2010-08-13 12:42:01 +00:00
<tr><th>Parameters:</th><td class="p"><em>[optional vector]</em><code>pos</code>:</td>
2010-08-13 10:02:47 +00:00
<td>Initial position of the camera. Defaults to <code>(0,0)</code>.</td></tr>
2010-08-13 12:42:01 +00:00
<tr><td> </td><td class="p"><em>[optional number]</em><code>zoom</code>:</td>
2010-08-13 10:02:47 +00:00
<td>Initial zoom. Defaults to <code>1</code>.</td></tr>
2010-08-13 12:42:01 +00:00
<tr><td> </td><td class="p"><em>[optional number]</em><code>rotation</code>:</td>
2010-08-13 10:02:47 +00:00
<td>Initial rotation in radians. Defaults to <code>0</code>.</td></tr>
2010-08-13 12:42:01 +00:00
<tr><th>Returns:</th><td colspan="2">a new camera object</td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
<a name="camera.lua-rotate"></a>
<div class="doc"><span class="function">function camera:rotate(phi)<a class="top" href="#camera.lua">^ top</a></span>
Rotate camera by <code>phi</code> radians. Same as <code>camera.rot = camera.rot + phi.</code>
</div>
<a name="camera.lua-translate"></a>
<div class="doc"><span class="function">function camera:translate(t)<a class="top" href="#camera.lua">^ top</a></span>
Translate (move) camera by vector <code>t</code>. Same as <code>camera.pos = camera.pos + t</code>.
<table>
<tr><th>Parameters:</th><td class="p"><em>[vector]</em><code>t</code>:</td>
<td>Translation vector</td></tr>
</table>
2010-08-13 10:02:47 +00:00
</div>
<a name="camera.lua-draw"></a>
<div class="doc"><span class="function">function camera:draw(func)<a class="top" href="#camera.lua">^ top</a></span>
Apply camera transformation to drawings in function <code>func</code>.
Shortcut to <code>camera:apply()</code> and <code>camera:deapply()</code> (see below).
<table><tr><th>Example</th></tr>
<tr><td><pre>cam:draw(function() love.graphics.rectangle('fill', -100,-100, 200,200) end)</pre></td></tr>
</table>
</div>
2010-09-15 09:34:18 +00:00
<a name="camera.lua-predraw"></a>
<div class="doc"><span class="function">function camera:predraw()<a class="top" href="#camera.lua">^ top</a></span>
Apply camera transformations to every drawing operation until the next <code>camera:postdraw()</code>.
2010-08-13 10:02:47 +00:00
</div>
2010-09-15 09:34:18 +00:00
<a name="camera.lua-postdraw"></a>
<div class="doc"><span class="function">function camera:postdraw()<a class="top" href="#camera.lua">^ top</a></span>
2010-08-13 10:02:47 +00:00
Revert camera transformations for the rest of the drawing operations.
<table><tr><th>Example:</th><td>(equivalent to the <code>cam:draw()</code> example above)</td></tr>
2010-09-15 09:34:18 +00:00
<tr><td colspan="2"><pre>camera:predraw()
2010-08-13 10:02:47 +00:00
love.graphics.rectangle('fill', -100,-100, 200,200)
2010-09-15 09:34:18 +00:00
camera:postdraw()</pre></td></tr>
2010-08-13 10:02:47 +00:00
</table>
</div>
2010-09-15 09:34:18 +00:00
<a name="camera.lua-toCameraCoords"></a>
<div class="doc"><span class="function">function camera:toCameraCoords(p)<a class="top" href="#camera.lua">^ top</a></span>
Transform vector <code>p</code> from world coordinates to camera coordinates, i.e.
you have the position of an object in the world and want to know where it will be
drawn on the screen.
<table>
<tr><th>Parameters:</th><td class="p"><em>[vector]</em><code>p</code>:</td>
<td>Vector to transform to camera coordinates</td></tr>
<tr><th>Returns:</th><td colspan="2"><em>[vector]</em> transformed vector.</td></tr>
</table>
</div>
<a name="camera.lua-toWorldCoords"></a>
<div class="doc"><span class="function">function camera:toWorldCoords(p)<a class="top" href="#camera.lua">^ top</a></span>
Transform vector <code>p</code> from camera coordinates to world coordinates, i.e.
you have the coordinates of a point on the screen (e.g. the mouse), and wan't to
know where it's location is in the world.
<table>
<tr><th>Parameters:</th><td class="p"><em>[vector]</em><code>p</code>:</td>
<td>Vector to transform to world coordinates</td></tr>
<tr><th>Returns:</th><td colspan="2"><em>[vector]</em> transformed vector.</td></tr>
</table>
2010-08-13 10:02:47 +00:00
</div>
<a name="camera.lua-mousepos"></a>
<div class="doc"><span class="function">function camera:mousepos()<a class="top" href="#camera.lua">^ top</a></span>
Get mouse position in world coordinates, i.e. the position the users mouse is
currently pointing at when camera transformations are applied. Use this for any
mouse interaction with transformed objects in your game.
<table>
<tr><th>Returns:</th><td><em>[vector]</em> Mouse position in world coordinates.</td></tr>
</table>
2010-08-13 10:02:47 +00:00
</div>
</div>
2010-10-17 14:03:13 +00:00
<div class="module"><a name="ringbuffer.lua"></a>
2010-08-17 17:11:21 +00:00
<h3>ringbuffer.lua<a href="#doc" class="top">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/ringbuffer.lua" class="source">view source</a>
2010-08-17 17:11:21 +00:00
<p>A circular container that can hold arbitrary items. It can be used to create <em>Tomb Raider</em> style
inventories, a looping music playlist, recurring dialog sequences and much more.</p>
2010-10-16 15:28:13 +00:00
<p>Accessors:
<table class="functionlist">
<tr><td><em>[table]</em><code>buffer.items</code></td><td>Items table. It is not recommended to read or write this directly.</td></tr>
<tr><td><em>[number]</em><code>buffer.current</code></td><td>Index of current selection. Be careful with this.</td></tr>
</table></p>
2010-08-17 17:11:21 +00:00
<p>ringbuffer.lua defines the following functions:
<table class="functionlist">
<tr><td><a href="#ringbuffer.lua-Ringbuffer">Ringbuffer(...)</a></td><td>Create new ringbuffer</td></tr>
<tr><td><a href="#ringbuffer.lua-insert">ringbuffer:insert(item, ...)</a></td><td>Insert items</td></tr>
<tr><td><a href="#ringbuffer.lua-append">ringbuffer:append(item, ...)</a></td><td>Append items to the "end"</td></tr>
<tr><td><a href="#ringbuffer.lua-remove">ringbuffer:remove()</a></td><td>Remove item</td></tr>
<tr><td><a href="#ringbuffer.lua-removeAt">ringbuffer:removeAt(k)</a></td><td>Remove item at position</td></tr>
<tr><td><a href="#ringbuffer.lua-get">ringbuffer:get()</a></td><td>Get item</td></tr>
<tr><td><a href="#ringbuffer.lua-size">ringbuffer:size()</a></td><td>Number of items in buffer</td></tr>
<tr><td><a href="#ringbuffer.lua-next">ringbuffer:next()</a></td><td>Get next item</td></tr>
<tr><td><a href="#ringbuffer.lua-prev">ringbuffer:prev()</a></td><td>Get previous item</td></tr>
</table></p>
<a name="ringbuffer.lua-Ringbuffer"></a>
<div class="doc">
<span class="function">function Ringbuffer(...)<a href="#ringbuffer.lua" class="top">^ top</a></span>
Create a new ringbuffer.
<table>
<tr><th>Parameters</th><td class="p"><em>[optional]</em><code>...</code>:</td><td>Initial items of the buffer.</td></tr>
</table>
</div>
<a name="ringbuffer.lua-insert"></a>
<div class="doc">
<span class="function">function ringbuffer:insert(item, ...)<a href="#ringbuffer.lua" class="top">^ top</a></span>
Insert items behind current item.
<table>
<tr><th>Parameters:</th><td class="p"><code>item</code>:</td><td>Item to insert</td></tr>
<tr><th></th><td class="p"><em>[optional]</em><code>...</code>:</td><td>Additional items to insert</td></tr>
<tr><th colspan="3">Example</th></tr>
<tr><td colspan="3"><pre>rb = Ringbuffer(1,2,5,6) -- rb = {1 ,2,5,6}, rb:get() == 1
rb:next() -- rb = {1, 2 ,5,6}, rb:get() == 2
rb:insert(3,4) -- rb = {1, 2 ,3,4,5,6}, rb:get() == 2
rb:next() -- rb = {1,2, 3 ,4,5,6}, rb:get() == 3</pre></td></tr>
</table>
</div>
<a name="ringbuffer.lua-append"></a>
<div class="doc">
<span class="function">function ringbuffer:append(item, ...)<a href="#ringbuffer.lua" class="top">^ top</a></span>
Append items to ringbuffer.
<p>This is less intuitive than <code>ringbuffer:insert(item, ...)</code> since it appears the items are added
at a random location. Use is only recommended before using <code>ringbuffer:next()</code> or <code>ringbuffer:prev()</code>.</p>
<table>
<tr><th>Parameters:</th><td class="p"><code>item</code>:</td><td>Item to append</td></tr>
<tr><th></th><td class="p"><em>[optional]</em><code>...</code>:</td><td>Additional items to append</td></tr>
<tr><th colspan="3">Example</th></tr>
<tr><td colspan="3"><pre>rb = Ringbuffer(1,2,5,6) -- rb = {1 ,2,5,6}, rb:get() == 1
rb:next() -- rb = {1, 2 ,5,6}, rb:get() == 2
rb:append(3,4) -- rb = {1, 2 ,5,6,3,4}, rb:get() == 2
rb:next() -- rb = {1,2, 5 ,6,3,4}, rb:get() == 5</pre></td></tr>
</table>
</div>
<a name="ringbuffer.lua-remove"></a>
<div class="doc">
<span class="function">function ringbuffer:remove()<a href="#ringbuffer.lua" class="top">^ top</a></span>
Remove currently selected item. The next item will be selected.
<table>
<tr><th>Example</th></tr>
<tr><td><pre>rb = Ringbuffer(1,2,3) -- rb = {1 ,2,3}, rb:get() == 1
rb:next() -- rb = {1, 2 ,3}, rb:get() == 2
rb:remove() -- rb = {1, 3 }, rb:get() == 3</pre></td></tr>
</table>
</div>
<a name="ringbuffer.lua-removeAt"></a>
<div class="doc">
<span class="function">function ringbuffer:removeAt(k)<a href="#ringbuffer.lua" class="top">^ top</a></span>
Remove item at relative position <code>k</code>. <code>k</code> may be positive or negative and
even bigger than <code>ringbuffer:size()</code>.
<table>
<tr><th>Parameters:</th><td class="p"><em>[number]</em><code>k</code></td><td>Relative position of item to remove.</td></tr>
<tr><th colspan="3">Example:</th></tr>
<tr><td colspan="3"><pre>rb = Ringbuffer(1,2,3,4,5) -- rb = { 1 ,2,3,4,5}, rb:get() == 1
rb:removeAt(2) -- rb = { 1 ,2,4,5}, rb:get() == 1
rb:next() -- rb = {1, 2 ,4,5}, rb:get() == 2
rb:removeAt(-1) -- rb = { 2 ,4,5}, rb:get() == 2</pre></td></tr>
</table>
</div>
<a name="ringbuffer.lua-get"></a>
<div class="doc">
<span class="function">function ringbuffer:get()<a href="#ringbuffer.lua" class="top">^ top</a></span>
Get currently selected item.
<table> <tr> <th>Returns:</th> <td>The currently selected item.</td> </tr> </table>
</div>
<a name="ringbuffer.lua-size"></a>
<div class="doc">
<span class="function">function ringbuffer:size()<a href="#ringbuffer.lua" class="top">^ top</a></span>
<table> <tr> <th>Returns:</th> <td>Number of items in the buffer.</td> </tr> </table>
</div>
<a name="ringbuffer.lua-next"></a>
<div class="doc">
<span class="function">function ringbuffer:next()<a href="#ringbuffer.lua" class="top">^ top</a></span>
Select next item and return it.
<table> <tr> <th>Return:</th> <td>Selected item after operation.</td> </tr> </table>
</div>
<a name="ringbuffer.lua-prev"></a>
<div class="doc">
<span class="function">function ringbuffer:prev()<a href="#ringbuffer.lua" class="top">^ top</a></span>
Select previous item and return it.
<table> <tr> <th>Return:</th> <td>Selected item after operation.</td> </tr> </table>
</div>
</div>
2010-10-17 14:03:13 +00:00
<div class="module"><a name="sequence.lua"></a>
<h3>sequence.lua<a href="#doc" class="top">^ top</a></h3>
<a href="http://github.com/vrld/hump/blob/master/sequence.lua" class="source">view source</a>
2010-08-17 19:49:30 +00:00
<em>Depends on <a href="#ringbuffer.lua">ringbuffer.lua</a></em>
<p>sequence.lua offers basic support for automated switching of <em>Scenes</em>. You can use it to
add intros and cutscenes to your games.</p>
<p>sequence.lua defines two types: <code>Scene</code> and <code>Sequence</code>. A <code>Sequence</code>
2010-08-17 20:07:04 +00:00
holds several <code>Scene</code>s, but one <code>Scene</code> can belong to different <code>Sequence</code>s
(but due to side effects it shouldn't).</p>
2010-08-17 19:49:30 +00:00
<p>The module consists of the following:
<table class="functionlist">
<tr><td><a href="#sequence.lua-Sequence">Sequence(...)</a></td><td>Create new sequence</td></tr>
2010-08-17 20:07:04 +00:00
<tr><td><a href="#sequence.lua-add">sequence:add(...)</a></td><td>Add scenes</td></tr>
2010-08-17 19:49:30 +00:00
<tr><td><a href="#sequence.lua-select">sequence:select(k)</a></td><td>Select scene manually</td></tr>
<tr><td><a href="#sequence.lua-rewind">sequence:rewind()</a></td><td>Rewind sequence</td></tr>
<tr><td><a href="#sequence.lua-prevScene">sequence:prevScene</a></td><td>Manually select previous scene</td></tr>
<tr><td><a href="#sequence.lua-nextScene">sequence:nextScene</a></td><td>Manually select next scene</td></tr>
<tr><td><a href="#sequence.lua-draw">sequence:draw()</a></td><td>Draw current scene</td></tr>
<tr><td><a href="#sequence.lua-update">sequence:update(dt)</a></td><td>Update and switch current scene</td></tr>
<tr><td><a href="#sequence.lua-Scene">Scene(length)</a></td><td>New scene object</td></tr>
<tr><td><a href="#sequence.lua-isFinished">scene:isFinished()</a></td><td>Indicate end of scene</td></tr>
</table></p>
<p>A <code>Scene</code> defines three callback functions. <em>Do not</em> try to
2010-08-17 20:07:04 +00:00
access them yourself. Let <code>Sequence</code> do the job:
2010-08-17 19:49:30 +00:00
<table>
<tr><td><code>scene:draw()</code>:</td><td>Draw contents on screen <em>(mandatory)</em></td></tr>
<tr><td><code>scene:update(dt)</code>:</td><td>Update scene <em>(optional)</em></td></tr>
<tr><td><code>scene:enter()</code>:</td><td>Called when entering scene <em>(optional)</em></td></tr>
<tr><td><code>scene:leave()</code>:</td><td>Called when leaving scene <em>(optional)</em></td></tr>
</table></p>
<p>Additional to that, each <code>Scene</code> has a <code>time</code> variable corresponding to the time
spent in the scene.</p>
<p>The <a href="#sequence.lua-example">example</a> shows some of the concepts involved.</p>
<a name="sequence.lua-Sequence"></a>
<div class="doc">
<span class="function">function Sequence(...)<a href="#sequence.lua" class="top">^ top</a></span>
Create a new sequence with initial scenes.
<table>
<tr><th>Parameters:</th><td class="p"><em>[optional Scenes]</em><code>...</code></td>
<td>Initial scenes to add to sequence</td></tr>
<tr><th>Returns:</th><td colspan="2">Sequence object with initial scenes</td></tr>
</table>
</div>
<a name="sequence.lua-add"></a>
<div class="doc">
<span class="function">function sequence:add(...)<a href="#sequence.lua" class="top">^ top</a></span>
Append scenes to the end of the sequence.
<table>
<tr><th>Parameters:</th><td class="p"><em>[Scenes]</em><code>...</code></td>
<td>Scenes to add to sequence</td></tr>
</table>
</div>
<a name="sequence.lua-select"></a>
<div class="doc">
<span class="function">function sequence:select(k)<a href="#sequence.lua" class="top">^ top</a></span>
Manually selection of scene <code>k</code>. Usually this is not necessary but provided anyway.
<table>
<tr> <th>Parameters:</th> <td class="p"> <em>[number]</em> <code>k</code>: </td> <td>Number of the scene to select</td> </tr>
</table>
</div>
<a name="sequence.lua-rewind"></a>
<div class="doc">
<span class="function">function sequence:rewind()<a href="#sequence.lua" class="top">^ top</a></span>
Select first scene in sequence.
</div>
<a name="sequence.lua-prevScene"></a>
<div class="doc">
<span class="function">function sequence:prevScene()<a href="#sequence.lua" class="top">^ top</a></span>
Manually select previous scene. Usually not necessay but provided anyway.
<p><em>Warning:</em> May select the <em>last</em> scene when the current scene is the first scene in sequence.</p>
</div>
<a name="sequence.lua-nextScene"></a>
<div class="doc">
<span class="function">function sequence:nextScene()<a href="#sequence.lua" class="top">^ top</a></span>
Manually select next scene. Usually not necessay but provided anyway.
<p><em>Warning:</em> May select the <em>first</em> scene when the current scene is the last scene in sequence.</p>
</div>
<a name="sequence.lua-draw"></a>
<div class="doc">
<span class="function">function sequence:draw()<a href="#sequence.lua" class="top">^ top</a></span>
Draws current scene.
<p>Calls <code>scene:draw()</code> on currently active scene</p>
</div>
<a name="sequence.lua-update"></a>
<div class="doc">
<span class="function">function sequence:update(dt)<a href="#sequence.lua" class="top">^ top</a></span>
Update current scene and increase <code>scene.time</code>.
<p>Switches scene if current scene is finished (see <a href="#sequence.lua-isFinished"><code>scene:isFinished()</code></a>).</p>
</div>
<a name="sequence.lua-Scene"></a>
<div class="doc">
<span class="function">function Scene(length)<a href="#sequence.lua" class="top">^ top</a></span>
Create a new scene of length <code>length</code>
<table>
<tr> <th>Parameters:</th> <td class="p"><em>[optional]</em><code>length</code></td>
2010-08-17 19:49:30 +00:00
<td>Length of scene. If <code>< 0</code> scene will never finish. Defaults to <code>-1</code></td> </tr>
<tr> <th>Returns:</th> <td colspan="2">New scene object</td> </tr>
</table>
</div>
<a name="sequence.lua-isFinished"></a>
<div class="doc">
<span class="function"><a href="#sequence.lua" class="top">^ top</a></span>
Indicates that the scene is finished. Used by <code>sequence:update(dt)</code>.
<p>This can be overwritten, though it is not advised to do so.</p>
<table>
<tr> <th>Returns:</th> <td><code>true</code> if the scene is finished.</td> </tr>
</table>
</div>
<a name="sequence.lua-example"></a>
<h4>Example usage</h4>
<div class="doc">
<span class="function">Ending credits<a href="#sequence.lua" class="top">^ top</a></span>
<pre>local sc = {}
2010-10-17 14:03:13 +00:00
-- moving and fading looks cool
2010-08-17 19:49:30 +00:00
sc.idea = Scene(5)
function sc.idea:draw()
local frac = (self.time / self.length) ^ 3
love.graphics.setColor(255,255,255,160 - frac * 160)
love.graphics.print("Idea", 200 + frac * 30, 200)
love.graphics.setColor(255,255,255,255 - frac * 255)
love.graphics.print("Max Power", 300 - frac * 40, 250)
end
sc.code = Scene(5)
function sc.code:draw()
local frac = (self.time / self.length) ^ 3
love.graphics.setColor(255,255,255,160 - frac * 160)
love.graphics.print("Code", 200 + frac * 30, 200)
love.graphics.setColor(255,255,255,255 - frac * 255)
love.graphics.print("Max Power", 300 - frac * 40, 250)
end
sc.gfx = Scene(5)
function sc.gfx:draw()
local frac = (self.time / self.length) ^ 3
love.graphics.setColor(255,255,255,160 - frac * 160)
love.graphics.print("GFX", 200 + frac * 30, 200)
love.graphics.setColor(255,255,255,255 - frac * 255)
love.graphics.print("also Max Power", 300 - frac * 40, 250)
end
sc.endscreen = Scene(-1) -- last scene. no looping
function sc.endscreen:enter()
self.frac = 0
end
function sc.endscreen:update(dt)
self.frac = math.min(1, self.time / 3) -- 3 sec fade in
end
function sc.endscreen:draw()
love.graphics.setColor(255,255,255, self.frac * 255)
love.graphics.print("The most awesome game in history", 280, 280)
end
-- create credit sequence
credits = Sequence(sc.idea, sc.code, sc.gfx)
credits:add(sc.endscreen)
-- show and update sequence
function love.draw()
credits:draw()
end
function love.update(dt)
credits:update(dt)
end
</pre>
</div>
</div>
2010-08-13 10:02:47 +00:00
<a name="license"></a>
<h2>License<a class="top" href="#top">^ top</a></h2>
<p>Yay, <em>free software</em>:</p>
<blockquote><p>Copyright (c) 2010 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>
<a name="download"></a>
<h2>Download<a class="top" href="#top">^ top</a></h2>
<p>You can view and download the individual modules on github: <a href="http://github.com/vrld/hump">vrld/hump</a>.
You may also download the whole packed sourcecode either in
2010-08-13 10:02:47 +00:00
<a href="http://github.com/vrld/hump/zipball/master">zip</a> or
<a href="http://github.com/vrld/hump/tarball/master">tar</a> formats.</p>
<p>You can clone the project with <a href="http://git-scm.com">Git</a> by running:
<pre>git clone git://github.com/vrld/hump</pre>
Once done, tou can check for updates by running
<pre>git pull</pre></p>
2010-08-13 10:02:47 +00:00
</div>
2010-08-12 13:08:09 +00:00
</body>
</html>