Book Image

Learning Game AI Programming with Lua

By : David Young
Book Image

Learning Game AI Programming with Lua

By: David Young

Overview of this book

Table of Contents (16 chapters)
Learning Game AI Programming with Lua
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Getting Started with AI Sandbox
Index

Playing with states


We can now start playing with animation states within our ASMs. In this case, we have two ASMs that need to be kept in sync, so we create a global weaponState variable that will check whether our weapon is in the sniper rifle pose or the SMG pose.

Next, we can bind each of the number pad keys to request specific states within our soldier and weapon ASMs. Special case handling is required for the transform states as well as the reload state:

Sandbox.lua:

local weaponState = "sniper";

local function IsNumKey(key, numKey)
    -- Match both numpad keys and numeric keys.
    return string.find(
        key, string.format("^[numpad_]*%d_key$", numKey));
end

function Sandbox_HandleEvent(sandbox, event)
    if (event.source == "keyboard" and event.pressed) then
        if (event.key == "f1_key") then
            Sandbox.SetCameraPosition(
                sandbox, Vector.new(0, 1, -3));
            Sandbox.SetCameraForward(
                sandbox, Vector.new(0, 0, -1));
    ...