This file defines a group of actions that are used as the basis of the Dog behaviors, used by the Dog.h file. There are actions for animation of the skeleton, translation and rotation of the scene graph node, as well as a basic action for waiting a certain amount of time.

Each action is defined as a node in the behavior tree, and a simple data-structure that stores parameters. The settings are defined using macros to help connect them automatically to the type-safe behavior tree builder.

#ifndef _ACTION_H_
#define _ACTION_H_

#include <Ogre.h>
#include <alive/Action.h>

Spatial Actions

This is a base class for rotation and translation actions, which stores a scene graph node and contains the necessary logic for initializing it.

struct SpatialAction : public alive::Action
{
    /**
     * Prepare execution for this action from a scene graph node.  This function
     * is called when the action is bound to a new actor.
     */
    void setup(Ogre::Node&);

    /**
     * Called after construction and just before execution.
     */
    void init();

    /// Scene graph leaf.
    Ogre::Node* m_pNode;

    /// This is a single virtual entry point for all customizations, implemented
    /// as a visitor pattern.
    DEFINE_VISITABLE_AS(SpatialAction);
};

Translation Settings

This is simple struct to store configuration parameters for the movement action.

DEFINE_SETTINGS(SettingsTranslate)
{
    DEFINE_PROPERTY(float, Speed);

    SettingsTranslate()
    :    m_Speed(1.0f)
    {
    }
};

Translation Action

This is the actual behavior tree node, which implements procedural movement. The action defines a single entry point for being updated every frame.

struct ActionTranslate : public SpatialAction
{
    /**
     * Update function called every frame to translate the node.
     */
    alive::Status execute();

    /// Bind this action to its settings, accessible to the tree builder.
    DECLARE_NODE_SETTINGS(SettingsTranslate);
    /// Let the tree builder this is a behavior tree node for execution.
    DEFINE_EXECUTION_NODE(ActionTranslate);
};

Rotation Settings

This is another configuration data-structure storing parameters for the turning action.

DEFINE_SETTINGS(SettingsRotate)
{
    DEFINE_PROPERTY(float, Speed);

    SettingsRotate()
    :    m_Speed(0.5f)
    {
    }
};

Rotation Action

This the behavior tree leaf used for procedural turning, also defining a single entry-point for updates every frame.

struct ActionRotate : public SpatialAction
{
    /**
     * Update function called every frame to rotate the node.
     */
    alive::Status execute();

    /// Bind this action to its settings, accessible to the tree builder.
    DECLARE_NODE_SETTINGS(SettingsRotate);
    /// Let the tree builder this is a behavior tree node for execution.
    DEFINE_EXECUTION_NODE(ActionRotate);
};

Waiting Settings

A simple configuration class for the action that waits a number of seconds. These parameters are available from the tree builder.

DEFINE_SETTINGS(SettingsWaitFor)
{
    /// Base number of seconds.
    DEFINE_PROPERTY(float, Time);
    /// Additional random range.
    DEFINE_PROPERTY(float, Random);

    SettingsWaitFor()
    :    m_Time(0.0f)
    ,    m_Random(0.0f)
    {}
};

Action for Waiting

This defines a behavior tree leaf that pauses for a certain amount of time, using the settings class above.

struct ActionWaitFor : public alive::Action
{
    /**
     * Advance the internal timer.
     */
    alive::Status execute();

    /**
     * Reset the inital timer to the desired (random) value.
     */
    void init();

    /// Internal seconds counter.
    float m_fTimer;

    /// Wire up the settings for the tree builder.
    DECLARE_NODE_SETTINGS(SettingsWaitFor);
    /// Specify this behavior tree node as able to execute.
    DEFINE_EXECUTION_NODE(ActionWaitFor);
};

Animation Settings

Another data-structure storing the configuration parameters for playing an animation.

DEFINE_SETTINGS(SettingsAnimate)
{
    DEFINE_PROPERTY(float, Speed);
    DEFINE_PROPERTY(bool, Loop)
    DEFINE_PROPERTY(std::string, Name);

    SettingsAnimate()
    :    m_Speed(1.0f)
    ,    m_Loop(true)
    {}
};

Animation Action

A behavior tree leaf for controlling playback of an animation on the current entity.

struct ActionAnimate : public alive::Action
{
    /**
     * Advance the current animation at the specified speed.
     */
    alive::Status execute();

    /**
     * Fade in the animation to full and enable it.
     */
    void init();

    /**
     * Blend out the animation and disable it.
     */
    void stop(alive::Status);

    Ogre::AnimationState*    m_pAnimState;

    /**
     * Finish constructing this action by binding it to this entity.
     */
    void setup(Ogre::Entity& entity);

    // Specify the settings used by this action.
    DECLARE_NODE_SETTINGS(SettingsAnimate);

    // Creates a node type for the behavior tree, using this class.
    DEFINE_EXECUTION_NODE(ActionAnimate);
};


#endif //_ACTION_H_