author | Wuzzy <Wuzzy@disroot.org> |
Mon, 27 May 2024 20:07:14 +0200 | |
changeset 2275 | c7d4b5e110d7 |
parent 2252 | 70ab0d6cfa85 |
permissions | -rw-r--r-- |
1374 | 1 |
#summary Lua library documentation of Animate; for cinematics, cut-scenes and other nice animations |
1347 | 2 |
#labels !LuaLibrary |
2103 | 3 |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
4 |
= Lua library: `Animate` = |
2239 | 5 |
<wiki:toc max_depth="3" /> |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
6 |
This library provides functions that aid cinematic/cut-scene creation and functions for handling events. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
7 |
|
2237 | 8 |
== Structure of an Animate script == |
9 |
||
10 |
The Animate library is somewhat complex, so this section explains how scripts using the Animate library are supposed to look like and how the functions interact. |
|
2236 | 11 |
|
2237 | 12 |
=== Initial code === |
13 |
||
14 |
For every script using this library, the following lines need to be at the beginning of `onGameTick`: |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
15 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
16 |
<code language="lua">function onGameTick() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
17 |
AnimUnWait() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
18 |
if ShowAnimation() == false then |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
19 |
return |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
20 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
21 |
ExecuteAfterAnimations() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
22 |
CheckEvents() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
23 |
end</code> |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
24 |
|
2236 | 25 |
Also, `AnimInit()` needs to be called in `onGameInit()`: |
26 |
||
27 |
<code language="lua">function onGameInit() |
|
28 |
AnimInit() |
|
29 |
end</code> |
|
30 |
||
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
31 |
Each of these functions will be explained below. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
32 |
|
2238 | 33 |
Also, if your script calls `SetInputMask`, you *must* replace each call of it with `AnimSetInputMask` (see function description below). Failing to do so may lead to bugs. |
2237 | 34 |
|
2238 | 35 |
Also, you *must not* call `SetCinematicMode` ever in your script. |
2237 | 36 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
37 |
=== Data structures === |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
38 |
==== Animation step ==== |
2237 | 39 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
40 |
The animation step step is the primary data structure of this library. A step is a single action in an animation specified in table form. A step table must have the following fields: |
2237 | 41 |
|
2238 | 42 |
* `func` is the function to be executed. It can be any function that returns false when it needs to be called again in following game ticks (e.g. `AnimMove`). It could theoretically be any function, but usually you want to use one of the cinematic functions here. |
43 |
* `args` is a list containing the arguments that need to be passed to the function given, in the same order |
|
2244 | 44 |
* `swh` is an optional boolean value that defaults to `true` and denotes whether the current hedgehog should be switched to the hog given as argument 1 in `args`. You *must* set this to `false` if argument 1 is not a hedgehog. |
2237 | 45 |
|
46 |
Example 1: |
|
47 |
<code language="lua"> |
|
2252 | 48 |
-- This calls `AnimSay(myHog, "Snails!", SAY_SAY, 3000)`, |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
49 |
-- meaning the hedgehog gear `myHog` will say "Snails!" and set the animation delay to 3000 milliseconds |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
50 |
-- Because `swh` is not set, it is true, so `myHog` also becomes the current hedgehog |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
51 |
local step1 = {func = AnimSay, args = {myHog, "Snails!", SAY_SAY, 3000}} |
2237 | 52 |
</code> |
53 |
||
54 |
Example 2: |
|
55 |
<code language="lua"> |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
56 |
-- Moves myHog to the left |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
57 |
local step2 = {func = AnimMove, args = {myHog, "Left", 2000, 0}} |
2237 | 58 |
</code> |
59 |
||
60 |
Example 3: |
|
61 |
<code language="lua"> |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
62 |
-- Shows the caption "Hello, world!". Because argument 1 is not a hedgehog, `swh` is false |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
63 |
local step3 = {func = AddCaption, swh = false, args = {"Hello, world!"}} |
2237 | 64 |
</code> |
65 |
||
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
66 |
==== Animation ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
67 |
|
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
68 |
An animation is a data structure that is a sequence of animation steps. It is specified as a table of steps. The steps will be executed in the specified order. |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
69 |
|
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
70 |
==== Animation list ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
71 |
|
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
72 |
The animation list is a list of all animations that were added but not performed so far. This is an internal table, you can not access it directly. You add to the list with `AddAnim`. |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
73 |
|
2237 | 74 |
=== How animation works === |
75 |
||
76 |
The Animation library has two states: *Animating* and *Not Animating*. By default, the game starts in the *Not Animating* state, meaning the game behaves normally. In *Animating* state, the library looks at the list of registered animations and executes them one by one, in that order. |
|
77 |
||
78 |
You are expected to add to the list of animatinons with `AddAnim`. If the script is animating, this animation will be played after all the others have been played. If the script is not animating, the animation will be played immediately. (Assuming you have correctly added the boilerplate code mentioned at the beginning of this page.) |
|
79 |
||
80 |
Animations are basically cut scenes. During animation, Cinematic Mode is enabled (see `SetCinematicMode` which is part of the main Lua API) and most player input is blocked. Animations can cause various things to happen, like move hedgehogs, show speech bubbles, explode the landscape or just call (almost) any function. During an animation, the player can only watch or optionally skip (details later). The player can still move the camera. |
|
81 |
||
82 |
There is also an internal wait time. It starts at 0 and a few cinematic functions as well as `AnimWait` can increase this time. The wait time counts down to 0, during which the script waits to execute the next animation step. Adding to the wait time is important to time your animation correctly. |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
83 |
|
2242
031a5b86e533
LuaLibraryAnimate: Say what happens after animation ends
Wuzzy
parents:
2241
diff
changeset
|
84 |
Once all animations in the animation list have been played, the game returns to the *Not Animating* state and behaves normally again. |
031a5b86e533
LuaLibraryAnimate: Say what happens after animation ends
Wuzzy
parents:
2241
diff
changeset
|
85 |
|
2249 | 86 |
=== Skipping === |
87 |
||
88 |
You can optionally allow to skip animations so allows players can skip the currently running animation. Skipping is *not* enabled by default. By convention, pressing the Precise key in singleplayer missions should skip the current animation. We recommend to use the following code to enable skipping with the Precise key: |
|
89 |
||
2250
f4c68be644c2
LuaLibraryAnimate: Fix wrong example code: onPreciseLocal does not exist
Wuzzy
parents:
2249
diff
changeset
|
90 |
<code language="lua">function onPrecise() |
2249 | 91 |
if AnimInProgress() then |
92 |
SetAnimSkip(true) |
|
93 |
end |
|
94 |
end</code> |
|
95 |
||
96 |
When the current animation is skipped, it is actually immediately stopped, so the game might be in a state you do not want it to be in. For example, if hogs walk in your animation, the hogs will not be in the intended position if the animation was skipped. To fix this, you need to use `AddSkipFunction` (see below for an example). |
|
97 |
||
2251 | 98 |
=== Animation example === |
99 |
This is the complete source code for a mission script demonstrating a simple animation in which a single hedgeghog says Hello and walks to the left. It shows the basic structure of a script using animations. This is only an example, your own implementation may vary. |
|
100 |
||
101 |
<code language="lua"> |
|
102 |
HedgewarsScriptLoad("/Scripts/Animate.lua") |
|
103 |
||
104 |
-- Mandatory code for Animate library |
|
105 |
function onGameTick() |
|
106 |
AnimUnWait() |
|
107 |
if ShowAnimation() == false then |
|
108 |
return |
|
109 |
end |
|
110 |
ExecuteAfterAnimations() |
|
111 |
CheckEvents() |
|
112 |
end |
|
113 |
||
114 |
-- Store hegehog ID here |
|
115 |
local hog1 |
|
116 |
||
117 |
function onGameInit() |
|
118 |
-- Mandatory code for Animate library |
|
119 |
AnimInit() |
|
120 |
||
121 |
-- Configure mission |
|
122 |
Map = "Ruler" |
|
123 |
Theme = "Nature" |
|
124 |
Explosives = 0 |
|
125 |
MinesNum = 0 |
|
126 |
CaseFreq = 0 |
|
127 |
GameFlags = gfOneClanMode |
|
128 |
||
129 |
-- Add team and hog |
|
130 |
AddTeam("Test Team", -1, "egg", "Castle", "Default_qau", "cm_test") |
|
131 |
hog1 = AddHog("Hog 1", 0, 100, "NoHat") |
|
132 |
SetGearPosition(hog1, 1100, 770) |
|
133 |
end |
|
134 |
||
135 |
-- Skip animation when pressing [Precise] |
|
136 |
function onPrecise() |
|
137 |
if AnimInProgress() then |
|
138 |
SetAnimSkip(true) |
|
139 |
end |
|
140 |
end |
|
141 |
||
142 |
-- This function sets up the example animation |
|
143 |
local function addWalkAnim() |
|
144 |
-- X coordinate that hog walks to |
|
145 |
local goalX = 900 |
|
146 |
||
147 |
-- Animation example: Hog says hello and then walks to the left and then speaks again |
|
148 |
local animation = { |
|
149 |
-- List of animation steps |
|
150 |
{func = AnimSay, args = {hog1, "Hello. I will walk to the left.", SAY_SAY, 2000}}, |
|
151 |
{func = AnimMove, args = {hog1, "Left", goalX, 0}}, |
|
152 |
{func = AnimSay, args = {hog1, "I reached my goal!", SAY_SAY, 2000}}, |
|
153 |
} |
|
154 |
||
155 |
-- Add skip function if player presses the skip key |
|
156 |
local function afterAnimSkip(args) |
|
157 |
-- Teleport hog |
|
158 |
SetGearPosition(hog1, goalX, GetY(hog1)) |
|
159 |
-- Face left (because hog walked left) |
|
160 |
HogTurnLeft(hog1, true) |
|
161 |
-- Stop walking |
|
162 |
SetGearMessage(hog1, 0) |
|
163 |
end |
|
164 |
AddSkipFunction(animation, afterAnimSkip, {}) |
|
165 |
||
166 |
-- This will be execuded when the animation ended |
|
167 |
local function afterWalkAnim() |
|
168 |
AddCaption("Animation completed!") |
|
169 |
ShowMission("Animation completed!", "It's over!", "You can return to the menu now.", 0, 0) |
|
170 |
end |
|
171 |
AddFunction({func = afterWalkAnim, args = {}}) |
|
172 |
||
173 |
-- Add the animation to the animation list. |
|
174 |
-- In our case, it wiill be perfmored immediately. |
|
175 |
AddAnim(animation) |
|
176 |
end |
|
177 |
||
178 |
-- This makes sure the animation starts on the first turn |
|
179 |
local hasAnimationStarted = false |
|
180 |
function onNewTurn() |
|
181 |
if hasAnimationStarted then |
|
182 |
return |
|
183 |
end |
|
184 |
if CurrentHedgehog ~= hog1 then |
|
185 |
SwitchHog(hog1) |
|
186 |
end |
|
187 |
addWalkAnim() |
|
188 |
hasAnimationStarted = true |
|
189 |
end |
|
190 |
</code> |
|
191 |
||
2239 | 192 |
== Function reference == |
193 |
=== Cinematic handling === |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
194 |
|
2239 | 195 |
==== `AnimInit([startAnimating])` ==== |
1384 | 196 |
Initializes variables used by the other functions. Needs to be called in `onGameInit`. |
197 |
||
2077
514babfbad9e
Remove outdated legacy hints as clutter
Wuzzy <almikes@aol.com>
parents:
1985
diff
changeset
|
198 |
An optional convenience parameter `startAnimating` is available; if set to `true`, the game will start in “animation” mode which enables cinematic mode and disables all controls except precise for skipping. This is useful if you want to indicate that an animation will be played right at the start and the player must not be allowed to use any controls before the animation is over. If you set this parameter to `true`, you also *must* play at least one animation after this, otherwise the game will be stuck. |
1384 | 199 |
|
2239 | 200 |
==== `AnimInsertStepNext(step)` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
201 |
Inserts `step` after the current step (read: action) in the animation list. Useful when an action depends on variables (e.g. positions). It can be used mostly with `AnimCustomFunction`. Note: In case of multiple consecutive calls, steps need to be added in reverse order. |
1384 | 202 |
|
203 |
Example: |
|
2246 | 204 |
<code language="lua">-- Show explosion effects around hedgehog `liveHog`, then delete it |
205 |
function BlowHog(deadHog) |
|
206 |
AnimInsertStepNext({func = DeleteGear, args = {deadHog}, swh = false}) |
|
207 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog), GetY(deadHog), vgtBigExplosion, 0, true}, swh = false}) |
|
208 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 1200}}) |
|
209 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
|
210 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
|
211 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) + 10, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
|
212 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
|
213 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) - 10, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
|
214 |
AnimInsertStepNext({func = AnimWait, args = {deadHog, 100}}) |
|
215 |
AnimInsertStepNext({func = AnimVisualGear, args = {deadHog, GetX(deadHog) - 20, GetY(deadHog), vgtExplosion, 0, true}, swh = false}) |
|
1384 | 216 |
end |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
217 |
animation = {{func = AnimCustomFunction, args = {liveHog, BlowHog, {deadHog}}}} |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
218 |
AddAnim(animation)</code> |
1384 | 219 |
|
2239 | 220 |
==== `ShowAnimation()` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
221 |
Performs the first animation (if possible) in the animation list and removes it once it’s done. Returns `true` if the animation list was empty and no animation was played or `false` if an animation was played. |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
222 |
|
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
223 |
It needs to be used in `onGameTick`. Cut-scenes need to be added with `AddAnim(animation)`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
224 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
225 |
==== `Animate(animation)` ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
226 |
Performs the specified animation manually. Returns `true` if the animation was performed successfully, `false` otherwise. This function is internally used; its use in scripts is not recommended. |
1382 | 227 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
228 |
==== `AddAnim(animation)` ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
229 |
Adds `animation` to the animation list. `animation` is a list of animation steps; see data structures above for details. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
230 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
231 |
Example: |
2239 | 232 |
<code language="lua"> |
233 |
-- Makes myHog say "Snails! SNAILS!, wait 3 seconds, move it to the left until its X coordinate equals 2000, then show the caption "But he found no more snails ..." |
|
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
234 |
animation = { |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
235 |
{func = AnimSay, args = {myHog, "Snails! SNAILS!", SAY_SAY, 3000}}, |
2239 | 236 |
{func = AnimMove, args = {myHog, "Left", 2000, 0}}, |
237 |
{func = AddCaption, swh = false, args = {"But he found no more snails ..."}} |
|
238 |
} |
|
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
239 |
AddAnim(animation)</code> |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
240 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
241 |
==== `RemoveAnim(animation)` ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
242 |
Removes `animation` from the animation list. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
243 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
244 |
==== `AddSkipFunction(animation, func, args)` ==== |
2249 | 245 |
Adds `func` to the array of functions used to skip animations, associating it with `animation`. When the animation is skipped, the function is called with `args` as arguments. |
246 |
||
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
247 |
Example: |
2249 | 248 |
<code language="lua">-- Makes a hedgehog `myHog` walk to the left until X=1000 but if the animation was skipped, teleport the hog to this X position instead. |
249 |
local walkAnimation = { |
|
250 |
{func = AnimMove, args = {myHog, "Left", 1000, 0}}, |
|
251 |
} |
|
252 |
AddAnim(walkAnimation) |
|
253 |
local function afterWalkAnimationSkip(args) |
|
254 |
-- Teleport hog |
|
255 |
SetGearPosition(myHog, 1000, GetY(myHog)) |
|
256 |
-- Stop walking |
|
257 |
SetGearMessage(myHog, 0) |
|
258 |
end |
|
259 |
AddSkipFunction(walkAnimation, afterWalkAnimationSkip, {}) |
|
260 |
</code> |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
261 |
|
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
262 |
==== `RemoveSkipFunction(animation)` ==== |
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
263 |
Removes the skip function associated with `animation`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
264 |
|
2239 | 265 |
==== `SetAnimSkip(bool)` ==== |
2249 | 266 |
Sets the state of animation skipping to `true` or `false`. If `true`, animations will be skipped. It is useful in case the player is allowed to skip the animation. Note this library will automatically reset the state back to `false` from time to time. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
267 |
|
2250
f4c68be644c2
LuaLibraryAnimate: Fix wrong example code: onPreciseLocal does not exist
Wuzzy
parents:
2249
diff
changeset
|
268 |
By convention, a cut scene in a singleplayer mission should be skipped when the player presses the Precise key. In this case, use the `onPrecise` callback for this. |
1866 | 269 |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
270 |
Example: |
2250
f4c68be644c2
LuaLibraryAnimate: Fix wrong example code: onPreciseLocal does not exist
Wuzzy
parents:
2249
diff
changeset
|
271 |
<code language="lua">function onPrecise() |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
272 |
if AnimInProgress() then |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
273 |
SetAnimSkip(true) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
274 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
275 |
end</code> |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
276 |
|
2239 | 277 |
==== `AnimInProgress()` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
278 |
Returns `true` if a amimation is currently taking place, `false` otherwise. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
279 |
|
2239 | 280 |
==== `ExecuteAfterAnimations()` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
281 |
Calls the functions (added with `AddFunction`) that need to be executed after the animation. The best location for this function call is in `onGameTick`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
282 |
|
2239 | 283 |
==== `AddFunction(element)` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
284 |
Adds `element` to the functions array that are to be called after the animation. `element` is a table with the keys: `func`, `args`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
285 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
286 |
Example: |
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
287 |
<code language="lua">AddFunction({func = myAfterAnim, args = {2}})</code> |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
288 |
|
2239 | 289 |
==== `RemoveFunction()` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
290 |
Removes the first function from the aforementioned list. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
291 |
|
2239 | 292 |
==== `AnimUnWait()` ==== |
2247
fdd314eb697f
LuaLibraryAnimate: Explain Animate and more consistent wording
Wuzzy
parents:
2246
diff
changeset
|
293 |
Decreases the wait time used by animations. It is best called in `onGameTick`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
294 |
|
2239 | 295 |
==== `AnimSetInputMask(inputMask)` ==== |
1384 | 296 |
Call this function instead of `SetInputMask` if you want to set the input mask in a way that does not conflict with the Animate library. |
297 |
||
2235 | 298 |
Internally, the input mask you provide is simply AND-ed with the input mask used by the Animate library. Otherwise, this function works like `SetInputMask`. |
1384 | 299 |
|
2235 | 300 |
If you call `SetInputMask` directly, note that it might get quickly overwritten by the Animate library! |
1384 | 301 |
|
2239 | 302 |
=== Cinematic functions === |
2245 | 303 |
These are the functions you can specify in animation steps. Unless specified otherwise, the `gear` argument of each function switches the `CurrentHedgehog` to `gear` and follows it. This behavior can be disabled with `swh=false` (see definition of animation steps above). |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
304 |
|
2239 | 305 |
==== `AnimSwitchHog(gear)` ==== |
2243 | 306 |
Switches the `CurrentHedgehog` to `gear` and follows it. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
307 |
|
2244 | 308 |
==== `AnimFollowGear(gear)` ==== |
309 |
Follows `gear`. |
|
310 |
||
2239 | 311 |
==== `AnimGiveState(gear, state)` ==== |
1909 | 312 |
Sets the [States gear state] of `gear` to `state` (full bitmask). |
1382 | 313 |
|
2239 | 314 |
==== `AnimRemoveState(gear, state)` ==== |
1385 | 315 |
Removes the [States gear state] `state` from `gear`. |
1382 | 316 |
|
2239 | 317 |
==== `AnimWait(gear, time)` ==== |
2243 | 318 |
Increases the wait time by `time` (in milliseconds) without changing the `CurrentHedgehog`. The `gear` argument is ignored (it exists for compability with `ShowAnimation). |
319 |
||
320 |
==== `AnimGearWait(gear, time)` ==== |
|
321 |
Increases the wait time by `time` (in milliseconds) and changes the `CurrentHedgehog` to `gear`. |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
322 |
|
2239 | 323 |
==== `AnimSay(gear, text, manner, time)` ==== |
2235 | 324 |
Calls `HogSay` with the first three arguments and increases the wait time by `time`. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
325 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
326 |
Example: |
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
327 |
<code language="lua">animation = { |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
328 |
{func = AnimSay, args = {gear1, "You're so defensive!", SAY_SAY, 2500}}, |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
329 |
{func = AnimSay, args = {gear2, "No, I'm not!", SAY_SAY, 2000}} |
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
330 |
}</code> |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
331 |
|
2239 | 332 |
==== `AnimSound(gear, sound, time)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
333 |
Plays the sound `sound` and increases the wait time by `time`. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
334 |
|
2239 | 335 |
==== `AnimTurn(hog, dir)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
336 |
Makes `hog` face in direction `dir`, where `dir` equals either `"Right"` or `"Left"`. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
337 |
|
2241 | 338 |
==== `AnimMove(hog, dir, x, y, maxMoveTime)` ==== |
339 |
Makes `hog` walk in direction `dir` (`"Right"` or `"Left"`) until either its horizontal coordinate equals `x` or its vertical coordinate equals `y`. `maxMoveTime` is optional. If specified, the animation will automatically end if this number of milliseconds have passed. |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
340 |
|
2239 | 341 |
==== `AnimJump(hog, jumpType)` ==== |
2240 | 342 |
Makes `hog` perform a jump of type `jumpType`, where `jumpType` is one of: |
343 |
||
344 |
* `"long"`: Long jump |
|
345 |
* `"high"`: High jump |
|
346 |
* `"back"`: Backjump |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
347 |
|
2239 | 348 |
==== `AnimSetGearPosition(gear, x, y, fall)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
349 |
Sets the position of `gear` to (`x`, `y`). If the optional argument `fall` does not equal `false` then the gear is given a small falling velocity in order to get around exact positioning. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
350 |
|
2239 | 351 |
==== `AnimDisappear(gear, x, y)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
352 |
Teleports the gear to (`x`, `y`), adding some effects at the previous position and sounds. Doesn’t follow the gear. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
353 |
|
2239 | 354 |
==== `AnimOutOfNowhere(gear [, x, y])` ==== |
1985
39daa8abac0b
LuaLibraryAnimate: Optional args in AnimOutOfNowhere
Wuzzy
parents:
1909
diff
changeset
|
355 |
Teleports the gear to (`x`, `y`), adding effects and sounds at the final position. Follows gear. If `x` and `y` are not specified, gear will not teleport, only the animation is played. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
356 |
|
2239 | 357 |
==== `AnimTeleportGear(gear, x, y)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
358 |
Teleports the gear to (`x`, `y`), adding effects and sounds both at the starting position and the final position. Follows gear. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
359 |
|
2239 | 360 |
==== `AnimVisualGear(gear, x, y, vgType, state, critical)` ==== |
1517 | 361 |
Calls `AddVisualGear` with arguments second to sixth. `gear` is for compatibility only. |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
362 |
|
2239 | 363 |
==== `AnimCaption(gear, text, time)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
364 |
Adds `text` as caption and increases wait time by `time`. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
365 |
|
2239 | 366 |
==== `AnimCustomFunction(gear, func, args)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
367 |
Calls the function `func` with `args` as arguments. This function is useful, for instance, when the cinematic uses the position of a gear at the moment of execution. If `func` needs to be called in following game ticks then it should return false. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
368 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
369 |
Example: |
2239 | 370 |
<code language="lua"> |
371 |
-- Make hog1 and hog2 look at each other |
|
372 |
function NeedToTurn(hog1, hog2) |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
373 |
if GetX(hog1) < GetX(hog2) then |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
374 |
HogTurnLeft(hog1, false) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
375 |
HogTurnLeft(hog2, true) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
376 |
else |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
377 |
HogTurnLeft(hog2, false) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
378 |
HogTurnLeft(hog1, true) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
379 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
380 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
381 |
|
2248
de14b46c7c16
LuaLibraryAnimate: Rename cinem variable to animation
Wuzzy
parents:
2247
diff
changeset
|
382 |
animation = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hog2}}}}</code> |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
383 |
|
2239 | 384 |
=== Event handling === |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
385 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
386 |
Events are pairs of functions that are used to check for various conditions. One of them is for verification and, if it returns `true`, the second function is called. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
387 |
|
2239 | 388 |
==== `AddEvent(condFunc, condArgs, doFunc, doArgs, evType)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
389 |
Adds the functions `condFunc` and `doFunc` to the events list. They get called with the respective args (`condArgs` and `doArgs`). `condFunc` will get called in every game tick until it returns `true` or is removed. Once it returns `true`, `doFunc` is called and they are or are not removed from the list, depending on `evType` (`0` for removal, `1` for keeping). An `evType` of `1` is useful for repeating actions (e.g. every time a hog gets damaged, do something). |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
390 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
391 |
Example: |
2237 | 392 |
<code language="lua"> |
393 |
--[[ |
|
394 |
This code shows the mission text "Nice Work" if the hog `myHog` has an X coordinate above 1500, but only once. |
|
395 |
Also, it immediately gives `myHog` 5 grenades once it runs out of grenades. It does that every time this happens. |
|
396 |
]] |
|
397 |
function CheckPos() |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
398 |
return GetX(myHog) > 1500 |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
399 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
400 |
function CheckAmmo() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
401 |
return GetAmmoCount(myHog, amGrenade) == 0 |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
402 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
403 |
function DoPos() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
404 |
ShowMission("Scooter", "Mover", "Nice Work", 0, 0) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
405 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
406 |
function DoAmmo() |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
407 |
AddAmmo(myHog, amGrenade, 5) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
408 |
end |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
409 |
AddEvent(CheckPos, {}, DoPos, {}, 0) -- Add event that gets removed on completion |
2237 | 410 |
AddEvent(CheckAmmo, {}, DoAmmo, {}, 1) -- Add repeating event |
411 |
</code> |
|
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
412 |
|
2239 | 413 |
==== `AddNewEvent(condFunc, condArgs, doFunc, doArgs, evType)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
414 |
Does the same as `AddEvent`, but first checks if the event is already in the list so that it isn’t added twice. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
415 |
|
2239 | 416 |
==== `RemoveEventFunc(cFunc, cArgs)` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
417 |
Removes the event or events that have `cFunc` as the condition checking function. If `cArgs` does not equal `nil` then only those events get removed that have `cArgs` as arguments for `cFunc`, too. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
418 |
|
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
419 |
Example: |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
420 |
<code language="lua">AddEvent(condFunc1, condArgs1, doFunc, doArgs) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
421 |
AddEvent(condFunc1, condArgs2, doFunc, doArgs) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
422 |
AddEvent(condFunc1, condArgs2, doFunc, doArgs) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
423 |
AddEvent(condFunc2, condArgs1, doFunc, doArgs) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
424 |
AddEvent(condFunc2, condArgs2, doFunc, doArgs) |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
425 |
RemoveEventFunc(condFunc1) --Removes all three events that have condFunc1 |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
426 |
RemoveEventFunc(condFunc2, condArgs1) --Removes a single event</code> |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
427 |
|
2239 | 428 |
==== `CheckEvents()` ==== |
1329
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
429 |
Verifies all the condition functions in the events list and calls the respective ‘action’ functions if the conditions are met. If the `evType` of a completed event equals `0` then it is removed from the list. This function is best placed in `onGameTick`. |
bd781e19a52d
Split Lua libraries into several sub-pages
Wuzzy <almikes@aol.com>
parents:
diff
changeset
|
430 |
|
2239 | 431 |
=== Misc. === |
1382 | 432 |
|
2239 | 433 |
==== `StoppedGear(gear)` ==== |
1382 | 434 |
Returns `true` if the gear is considered to be resting (not moving). (dX and dY are very small) or if the gear does not exist. |