コード例 #1
0
Status BehaviorTreeStepper::stepThrough(VectorPtr<Task *> &taskVector) 
{
   if(taskVector.empty()) return INVALID;
         
   if(taskVector.back()->getStatus() == SUSPENDED)
      return SUSPENDED;

   Status status = INVALID;

   // loop through the tasks in the task list
   while(!taskVector.empty())
   {
      // get a task
      Task *currentTask = taskVector.back();

      // tick the task
      Task *nextTask = currentTask->tick();
      
      // if task returned no children, it has completed
      if(!nextTask)
      {
         // stop if it's RUNNING or SUSPENED
         status = currentTask->getStatus();
         if(status == RUNNING || status == SUSPENDED)
            break;
               
         // otherwise, remove it from the list
         taskVector.pop_back();
         if(!taskVector.empty())
            // and tell its parent that it completed
            taskVector.back()->onChildComplete(currentTask->getStatus());

         // complete the task
         currentTask->finish();
      }
      else
      {
         // add the child as a task
         nextTask->setup();
         taskVector.push_back(nextTask);
      }
   }

   return status;
}