LuaLibraryAnimate.wiki
changeset 2237 dd92ebc28957
parent 2236 47493b26d4ee
child 2238 b559153c707a
equal deleted inserted replaced
2236:47493b26d4ee 2237:dd92ebc28957
     3 
     3 
     4 = Lua library: `Animate` =
     4 = Lua library: `Animate` =
     5 <wiki:toc max_depth="2" />
     5 <wiki:toc max_depth="2" />
     6 This library provides functions that aid cinematic/cut-scene creation and functions for handling events.
     6 This library provides functions that aid cinematic/cut-scene creation and functions for handling events.
     7 
     7 
     8 == Getting started ==
     8 == Structure of an Animate script ==
     9 
     9 
    10 In order to use its full potential, the following lines need to be at the beginning of `onGameTick`:
    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.
       
    11 
       
    12 === Initial code ===
       
    13 
       
    14 For every script using this library, the following lines need to be at the beginning of `onGameTick`:
    11 
    15 
    12 <code language="lua">function onGameTick()
    16 <code language="lua">function onGameTick()
    13     AnimUnWait()
    17     AnimUnWait()
    14     if ShowAnimation() == false then
    18     if ShowAnimation() == false then
    15         return
    19         return
    24     AnimInit()
    28     AnimInit()
    25 end</code>
    29 end</code>
    26 
    30 
    27 Each of these functions will be explained below.
    31 Each of these functions will be explained below.
    28 
    32 
    29 Note: The Animate library will directly overwrite the input mask with `SetInputMask`. If you already use this function in your script, it might lead to conflicts. Use `AnimSetInputMask` to set the input mask in a manner that is compatible with the Animate library.
    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.
       
    34 
       
    35 Also, you **must not** call `SetCinematicMode` ever in your script.
       
    36 
       
    37 === Steps ===
       
    38 
       
    39 The 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:
       
    40 
       
    41 * `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.
       
    42 * `args` is a list containing the arguments that need to be passed to the function given, in the same order
       
    43 * `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 hog or the argument list is empty.
       
    44 
       
    45 Example 1:
       
    46 <code language="lua">
       
    47     -- This calls `AnimSay(myHog, "Snails! SNAILS!", SAY_SAY, 3000)`,
       
    48     -- meaning the hedgehog gear `myHog` will say "Snails!" and set the animation delay to 3000 milliseconds
       
    49     -- Because `swh` is not set, it is true, so `myHog` also becomes the current hedgehog
       
    50     local step1 = {func = AnimSay, args = {myHog, "Snails!", SAY_SAY, 3000}}
       
    51 </code>
       
    52 
       
    53 Example 2:
       
    54 <code language="lua">
       
    55     -- Moves myHog to the left
       
    56     local step2 = {func = AnimMove, args = {myHog, "Left", 2000, 0}}
       
    57 </code>
       
    58 
       
    59 Example 3:
       
    60 <code language="lua">
       
    61     -- Shows the caption "Hello, world!". Because argument 1 is not a hedgehog, `swh` is false
       
    62     local step3 = {func = AddCaption, swh = false, args = {"Hello, world!"}}
       
    63 </code>
       
    64 
       
    65 === How animation works ===
       
    66 
       
    67 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.
       
    68 
       
    69 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.)
       
    70 
       
    71 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.
       
    72 
       
    73 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.
    30 
    74 
    31 == Cinematic handling ==
    75 == Cinematic handling ==
    32 
    76 
    33 === `AnimInit([startAnimating])` ===
    77 === `AnimInit([startAnimating])` ===
    34 Initializes variables used by the other functions. Needs to be called in `onGameInit`.
    78 Initializes variables used by the other functions. Needs to be called in `onGameInit`.
   122 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`.
   166 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`.
   123 
   167 
   124 If you call `SetInputMask` directly, note that it might get quickly overwritten by the Animate library!
   168 If you call `SetInputMask` directly, note that it might get quickly overwritten by the Animate library!
   125 
   169 
   126 == Cinematic functions ==
   170 == Cinematic functions ==
       
   171 These are the functions you can specify in animation steps.
   127 
   172 
   128 === `AnimSwitchHog(gear)` ===
   173 === `AnimSwitchHog(gear)` ===
   129 Switches to `gear` and follows it.
   174 Switches to `gear` and follows it.
   130 
   175 
   131 === `AnimGiveState(gear, state)` ===
   176 === `AnimGiveState(gear, state)` ===
   188       HogTurnLeft(hog2, false)
   233       HogTurnLeft(hog2, false)
   189       HogTurnLeft(hog1, true)
   234       HogTurnLeft(hog1, true)
   190    end
   235    end
   191 end
   236 end
   192 
   237 
   193 cinem = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hg2}}}}</code>
   238 cinem = {{func = AnimCustomFunction, args = {hog1, NeedToTurn, {hog1, hog2}}}}</code>
   194 
   239 
   195 == Event handling ==
   240 == Event handling ==
   196 
   241 
   197 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.
   242 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.
   198 
   243 
   199 === `AddEvent(condFunc, condArgs, doFunc, doArgs, evType)` ===
   244 === `AddEvent(condFunc, condArgs, doFunc, doArgs, evType)` ===
   200 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).
   245 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).
   201 
   246 
   202 Example:
   247 Example:
   203 <code language="lua">function CheckPos()
   248 <code language="lua">
       
   249 --[[
       
   250 This code shows the mission text "Nice Work" if the hog `myHog` has an X coordinate above 1500, but only once.
       
   251 Also, it immediately gives `myHog` 5 grenades once it runs out of grenades. It does that every time this happens.
       
   252 ]]
       
   253 function CheckPos()
   204    return GetX(myHog) > 1500
   254    return GetX(myHog) > 1500
   205 end
   255 end
   206 function CheckAmmo()
   256 function CheckAmmo()
   207    return GetAmmoCount(myHog, amGrenade) == 0
   257    return GetAmmoCount(myHog, amGrenade) == 0
   208 end
   258 end
   211 end
   261 end
   212 function DoAmmo()
   262 function DoAmmo()
   213    AddAmmo(myHog, amGrenade, 5)
   263    AddAmmo(myHog, amGrenade, 5)
   214 end
   264 end
   215 AddEvent(CheckPos, {}, DoPos, {}, 0) -- Add event that gets removed on completion
   265 AddEvent(CheckPos, {}, DoPos, {}, 0) -- Add event that gets removed on completion
   216 AddEvent(CheckAmmo, {}, DoAmmo, {}, 1) -- Add repeating event</code>
   266 AddEvent(CheckAmmo, {}, DoAmmo, {}, 1) -- Add repeating event
       
   267 </code>
   217 
   268 
   218 === `AddNewEvent(condFunc, condArgs, doFunc, doArgs, evType)` ===
   269 === `AddNewEvent(condFunc, condArgs, doFunc, doArgs, evType)` ===
   219 Does the same as `AddEvent`, but first checks if the event is already in the list so that it isn’t added twice.
   270 Does the same as `AddEvent`, but first checks if the event is already in the list so that it isn’t added twice.
   220 
   271 
   221 === `RemoveEventFunc(cFunc, cArgs)` ===
   272 === `RemoveEventFunc(cFunc, cArgs)` ===