bb5b15fb91a5f553bcdbe0c15ff3b4672c1e59b6
[supertux.git] / docs / scripting / scripting.xml
1 <?xml version='1.0' ?>
2 <!--
3 $Id$
4
5 SuperTux Documentation
6 Copyright (C) 2005 Ondra Hosek <ondra.hosek@gmail.com>
7
8 This document is free; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This document is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program (see the file named 'COPYING'); if not,
20 write to the Free Software Foundation, Inc., 59 Temple Place -
21 Suite 330, Boston, MA 02111-1307, USA.
22
23 -->
24 <!--<?xml-stylesheet type="text/xsl" href="/home/ondra/src/docbook-xsl-1.68.1/xhtml/docbook.xsl" ?>-->
25 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.3//EN"
26 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
27
28 <article xml:lang="en">
29
30 <articleinfo>
31 <title>SuperTux Scripting Documentation</title>
32 <author><firstname>Ondra</firstname><surname>Hosek</surname></author>
33 </articleinfo>
34 <para>Since May 2005, SuperTux sports a Squirrel scripting interface useful for level designers who want to add some interactive pep to their levels. This document poses as a reference article for those who want to explore the various objects of the SuperTux scripting model.</para>
35 <sect1><title>What is Squirrel?</title>
36 <para>One of your first questions might be, &quot;What does a rodent have to do
37 with a penguin?&quot; <ulink url="http://squirrel.sourceforge.net/">Squirrel</ulink> is a language with a syntax not much unlike other C-like languages (C, C++, Java, ...). In the current implementation, it is integrated as elements in the SuperTux level files.</para>
38 </sect1>
39 <sect1><title>Squirrel, Scheme and SuperTux</title>
40 <para>I have no clue if the developers simply chose Squirrel just because the name so nicely integrates into the series of words &quot;SuperTux&quot; and &quot;Scheme&quot;. Currently, the Squirrel code is integrated in string arguments of Scheme elements in SuperTux level files. (Whew.) This is an example code block inside a level:
41 <programlisting>(supertux-level
42   (version 2)
43   (name _("Go Blind"))
44   (author "Team")
45   (sector
46     (name "main")
47     (music "Annoying_penguin_gawking_sounds.mod")
48
49     ;; (Tilemaps, objects, whatever.)
50
51     (init-script "
52 DisplayEffect.fade_out(2.5);
53 ")
54   )
55 )</programlisting>
56 When this level loads, the screen fades out completely during two and a half seconds right after the level is loaded. (Mind you, this would be a frustrating experience for the player if you add a horde of badguys near the spawn point.)</para>
57 </sect1>
58
59 <sect1><title>Object reference</title>
60 <para>If you are interested in an object and what cans of worms you can open with it, this section is for you.</para>
61 <para>&quot;(NYI)&quot; after the function name symbolises functions that haven't been implemented yet. Calling them will result in a line being printed to standard output informing anybody who reads it that the script is using a function that actually doesn't exist. (Win32 users will happily ignore this, because they simply start the application by opening it with Explorer. Unix users are going to be more interested of what SuperTux can actually tell them, so it's better if you don't use non-existent functions in your scripts.)</para>
62
63 <sect2><title>DisplayEffect</title>
64 <para><code>DisplayEffect</code> is an interface for toying with the display.</para>
65 <sect3><title>fade_out</title>
66 <para>Usage: <code>DisplayEffect.fade_out(float fadetime)</code></para>
67 <para>Effect: Gradually fades out the screen to black for the next <code>fadetime</code> seconds.</para>
68 </sect3>
69
70 <sect3><title>fade_in</title>
71 <para>Usage: <code>DisplayEffect.fade_in(float fadetime)</code></para>
72 <para>Effect: Gradually fades in the screen from black for the next <code>fadetime</code> seconds.</para>
73 </sect3>
74
75 <sect3><title>set_black</title>
76 <para>Usage: <code>DisplayEffect.set_black(bool black)</code></para>
77 <para>Effect: Blackens or un-blackens the screen (depending on the value of <code>black</code>).</para>
78 </sect3>
79
80 <sect3><title>is_black</title>
81 <para>Usage: <code>DisplayEffect.is_black()</code></para>
82 <para>Returns: <code>bool</code></para>
83 <para>Effect: Returns true if the screen has been blackened by <code>set_black</code>. Calling <code>fade_in</code> or <code>fade_out</code> resets the return value to <code>false</code>.</para>
84 </sect3>
85
86 </sect2>
87
88 <sect2><title>Camera</title>
89 <para><code>Camera</code> is an interface to manipulate the camera.</para>
90
91 <sect3><title>shake (NYI)</title>
92 <para>Usage: <code>Camera.shake(float time, float x, float y)</code></para>
93 <para>Warning: This function has not yet been implemented.</para>
94 </sect3>
95
96 <sect3><title>set_pos (NYI)</title>
97 <para>Usage: <code>Camera.set_pos(float x, float y)</code></para>
98 <para>Warning: This function has not yet been implemented.</para>
99 </sect3>
100
101 <sect3><title>set_mode (NYI)</title>
102 <para>Usage: <code>Camera.set_mode(string modestring)</code></para>
103 <para>Warning: This function has not yet been implemented.</para>
104 </sect3>
105
106 </sect2>
107
108 <sect2><title>Level</title>
109 <para>The <code>Level</code> class provides pretty basic controlling functions for the current level.</para>
110
111 <sect3><title>finish</title>
112 <para>Usage: <code>Level.finish()</code></para>
113 <para>Effect: Ends the current level and marks it as completed if launched from a worldmap.</para>
114 <para>Tip: Very useful if you have created a frustrating level and want to, at some point, save the player from agony.</para>
115 </sect3>
116
117 <sect3><title>spawn</title>
118 <para>Usage: <code>Level.spawn(string sectorname, string spawnpointname)</code></para>
119 <para>Effect: Respawns Tux in the sector <code>sectorname</code> at the <code>spawnpointname</code> spawnpoint.</para>
120 <para>Exceptions: If <code>sectorname</code> or <code>spawnpointname</code> are empty or the specified sector does not exist, the function will bail out first chance it gets. If the specified spawnpoint doesn't exist, Tux will be spawned at the spawnpoint named <code>main</code>. If this spawnpoint doesn't exist either, Tux will simply end up at the origin (top-left 0, 0).</para>
121 </sect3>
122
123 </sect2>
124
125 <sect2><title>ScriptedObject</title>
126 <para>A <code>ScriptedObject</code> is basically a SuperTux object that can be scripted to move around and animate. This object will be used in the SuperTux cutscenes a whole lot.</para>
127
128 <sect3><title>Usage notes</title>
129 <para>Since a <code>ScriptedObject</code> is a reference object and not an active object, you will have to build it into your level file, where a <code>scriptedobject</code> is a child of <code>sector</code>. This is an example definition:
130 <programlisting>
131 (scripted-object
132   (name "WOOT")
133   (x 420)
134   (y 94)
135   (sprite "snowball")
136   (solid #t)
137   (physic-enabled #f)
138   (visible #t)
139 )
140 </programlisting>
141 </para>
142 <para>Now, the object can be accessed in code using the <code>WOOT</code> identifier like so:
143 <programlisting>
144 WOOT.set_animation("left");
145 </programlisting>
146 </para>
147 </sect3>
148
149 <sect3><title>set_animation</title>
150 <para>Usage: <code>&lt;scriptedobject&gt;.set_animation(string animation_name)</code></para>
151 <para>Effect: Activates the sprite's animation specified in <code>animation_name</code>.</para>
152 </sect3>
153
154 <sect3><title>get_animation</title>
155 <para>Usage: <code>&lt;scriptedobject&gt;.get_animation()</code></para>
156 <para>Returns: <code>string</code></para>
157 <para>Effect: Returns the name of the sprite's current animation.</para>
158 </sect3>
159
160 <sect3><title>move</title>
161 <para>Usage: <code>&lt;scriptedobject&gt;.move(float x, float y)</code></para>
162 <para>Effect: Moves the object by <code>x</code> units to the right and <code>y</code> down <emphasis>relative to its current position</emphasis>.</para>
163 </sect3>
164
165 <sect3><title>set_pos</title>
166 <para>Usage: <code>&lt;scriptedobject&gt;.set_pos(float x, float y)</code></para>
167 <para>Effect: Basically identical to <code>move</code>, except its <emphasis>relativity to the sector origin</emphasis>.</para>
168 </sect3>
169
170 <sect3><title>get_pos_x</title>
171 <para>Usage: <code>&lt;scriptedobject&gt;.get_pos_x()</code></para>
172 <para>Returns: <code>float</code></para>
173 <para>Effect: Returns the X coordinate of the object's position.</para>
174 </sect3>
175
176 <sect3><title>get_pos_y</title>
177 <para>Usage: <code>&lt;scriptedobject&gt;.get_pos_y()</code></para>
178 <para>Returns: <code>float</code></para>
179 <para>Effect: Totally identical to <code>get_pos_x</code> except for its obvious choice of the other (Y) axis.</para>
180 </sect3>
181
182 <sect3><title>set_velocity</title>
183 <para>Usage: <code>&lt;scriptedobject&gt;.set_velocity(float x, float y)</code></para>
184 <para>Effect: Makes the object move in a certain direction (with a certain speed) given by the <code>x</code> and <code>y</code> coordinates.</para>
185 </sect3>
186
187 <sect3><title>get_velocity_x</title>
188 <para>Usage: <code>&lt;scriptedobject&gt;.get_velocity_x()</code></para>
189 <para>Returns: <code>float</code></para>
190 <para>Effect: Returns the object's velocity in the direction of the X coordinate.</para>
191 </sect3>
192
193 <sect3><title>get_velocity_y</title>
194 <para>Usage: <code>&lt;scriptedobject&gt;.get_velocity_y()</code></para>
195 <para>Returns: <code>float</code></para>
196 <para>Effect: The difference between this function and <code>get_velocity_x</code> is just the same like between <code>get_pos_y</code> and <code>get_pos_x</code>: same thing, different coordinates.</para>
197 </sect3>
198
199 <sect3><title>set_visible</title>
200 <para>Usage: <code>&lt;scriptedobject&gt;.set_visible(bool visible)</code></para>
201 <para>Effect: Shows or hides the object according to the value of <code>visible</code>.</para>
202 </sect3>
203
204 <sect3><title>is_visible</title>
205 <para>Usage: <code>&lt;scriptedobject&gt;.is_visible()</code></para>
206 <para>Returns: <code>bool</code></para>
207 <para>Effect: Returns <code>true</code> if the object is visible. (You've seen this coming, haven't you?)</para>
208 </sect3>
209
210 <sect3><title>get_name</title>
211 <para>Usage: <code>&lt;scriptedobject&gt;.get_name()</code></para>
212 <para>Returns: <code>string</code></para>
213 <para>Effect: Simply gives you the name of the scripted object (as if you didn't have it already...)</para>
214 </sect3>
215
216 </sect2>
217
218 <sect2><title>Sound</title>
219 <para>This class provides a very simple interface to the audio subsystem.</para>
220
221 <sect3><title>play_music (NYI)</title>
222 <para>Usage: <code>Sound.play_music(string ???)</code></para>
223 <para>Warning: This function has not yet been implemented.</para>
224 </sect3>
225
226 <sect3><title>play_sound</title>
227 <para>Usage: <code>Sound.play_sound(string sound_name)</code></para>
228 <para>Effect: Plays the sound specified in <code>sound_name</code> (that is identical to the filename of the sound without the <code>.wav</code> extension).</para>
229 </sect3>
230
231 </sect2>
232
233 <sect2><title>Text</title>
234 <para>This module provides access to methods reponsible for displaying text on-screen.</para>
235
236 <sect3><title>set_text</title>
237 <para>Usage: <code>Text.set_text(string text)</code></para>
238 <para>Effect: Sets the text string to be displayed to <code>text</code>.</para>
239 </sect3>
240
241 <sect3><title>set_font</title>
242 <para>Usage: <code>Text.set_font(string font)</code></para>
243 <para>Effect: Sets the font of the text to be displayed to <code>text</code>. Currently valid values are <code>gold</code>, <code>white</code>, <code>blue</code>, <code>gray</code>, <code>big</code> and <code>small</code>.</para>
244 </sect3>
245
246 <sect3><title>fade_in</title>
247 <para>Usage: <code>Text.fade_in(float time)</code></para>
248 <para>Effect: Fades in the specified text for the next <code>time</code> seconds.</para>
249 </sect3>
250
251 <sect3><title>fade_out</title>
252 <para>Usage: <code>Text.fade_out(float time)</code></para>
253 <para>Effect: Just the opposite of <code>fade_out</code>.</para>
254 </sect3>
255
256 <sect3><title>set_visible</title>
257 <para>Usage: <code>Text.set_visible(bool visible)</code></para>
258 <para>Effect: Shows or hides the text abruptly (drastic counterpart to <code>fade_in</code> and <code>fade_out</code>).</para>
259 </sect3>
260
261 </sect2>
262
263 </sect1>
264
265 </article>