In a previous blog entry I showed a code sample of the behaviour tree. In the code below is the code that is fired for each behaviour.
The return status mean: -
RUNNING - Keep on running the behaviour
FAILED - Exit this behaviour with error (has different effect depending on sequeance/selector/parallel node.
COMPLETE - Exit this behaviour with complete (has different effect depending on sequeance/selector/parallel node.
#include "actor/include/action.h"
#include <alive/TreeBuilder.h>
#include <alive/engine/ConstantNode.h>
#include <alive/tree/Parallel.h>
#include <alive/tree/Repeat.h>
#include <alive/tree/ErrorHandler.h>
void SpatialAction::setup(Pedestrian& ped, Vision& vis, ActorGun& rifle)
{
m_pedestrian = &ped;
m_vision = &vis;
m_rifle = &rifle;
}
void SpatialAction::init()
{
m_pedestrian = NULL;
m_Observer.BIND(SpatialAction, this, stop);
}
// Moves the actor to the nearest enemy
alive::Status ActionMoveToEnemy::execute()
{
if (m_pedestrian == 0)
{
return FAILED;
}
if (m_vision == 0)
{
return FAILED;
}
m_pedestrian->currentAction += "ActionMoveToEnemy,";
if (m_pedestrian->moveToPosition( m_vision->m_lastSeenPos ))
return RUNNING;
else
return FAILED;
}
void ActionMoveToEnemy::stop(alive::Status)
{
m_pedestrian->stopMove();
}
alive::Status ActionStopMove::execute()
{
m_pedestrian->stop();
if(m_pedestrian->moveStatus == MOVE_IDLE)
return COMPLETED;
return RUNNING;
}
alive::Status hasGun::execute()
{
if( (m_rifle != NULL) == m_Settings.getEquals())
{
m_pedestrian->currentAction += "hasGun,";
return COMPLETED;
}
return FAILED;
}
alive::Status WithinFiringRange::execute()
{
if(!m_rifle)
{
// If no rifle I'm not in firing range
if (!m_Settings.getEquals())
return RUNNING;
else
return RUNNING;
}
if( m_rifle->withinRange( m_vision->distanceToEnemy() ) == m_Settings.getEquals())
{
m_pedestrian->currentAction += "WithinFiringRange,";
return RUNNING;
}
return FAILED;
}
// Moves the actor to the next waypoint
alive::Status ActionMoveToWaypoint::execute()
{
if (m_pedestrian == 0)
{
return FAILED;
}
m_pedestrian->currentAction += "ActionMoveToWaypoint,";
if (m_pedestrian->moveToNextWaypoint())
return RUNNING;
else
return COMPLETED;
}
void ActionMoveToWaypoint::stop(alive::Status)
{
m_pedestrian->stopMove();
}
alive::Status ActionCurrentMode::execute()
{
m_pedestrian->currentMode = m_Settings.getmode();
return COMPLETED;
}
void AimAtEnemy::init()
{
}
alive::Status LookForward::execute()
{
m_pedestrian->currentAction += "LookForward,";
if (m_pedestrian->anim->m_bodyControl->needToReset( BONE_SPINE ) )
{
m_pedestrian->anim->m_bodyControl->reset( BONE_SPINE );
return RUNNING;
}
if(m_Settings.getParallel())
return RUNNING;
else
return COMPLETED;
}
This isn't the complete code but it might help people get a feel.